]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/joystick.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/joystick.cpp
3 // Purpose: wxJoystick class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/joystick.h"
24 #include "wx/string.h"
27 #include "wx/window.h"
28 #include "wx/msw/private.h"
30 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
34 // Why doesn't BC++ have joyGetPosEx?
35 #if !defined(__WIN32__) || defined(__BORLANDC__)
36 #define NO_JOYGETPOSEX
39 #include "wx/window.h"
40 #include "wx/msw/registry.h"
44 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
47 ////////////////////////////////////////////////////////////////////////////
50 johan@linkdata.se 2002-08-20:
51 Now returns only valid, functioning
52 joysticks, counting from the first
53 available and upwards.
55 wxJoystick::wxJoystick(int joystick
)
60 maxsticks
= joyGetNumDevs();
61 for( i
=0; i
<maxsticks
; i
++ )
63 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
67 /* Found the one we want, store actual OS id and return */
75 /* No such joystick, return ID 0 */
80 wxPoint
wxJoystick::GetPosition() const
83 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
84 if (res
== JOYERR_NOERROR
)
85 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
90 int wxJoystick::GetZPosition() const
93 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
94 if (res
== JOYERR_NOERROR
)
101 johan@linkdata.se 2002-08-20:
102 Return a bitmap with all button states in it,
103 like the GTK version does and Win32 does.
105 int wxJoystick::GetButtonState() const
108 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
109 if (res
== JOYERR_NOERROR
)
111 return joyInfo
.wButtons
;
115 if (joyInfo
.wButtons
& JOY_BUTTON1
)
116 buttons
|= wxJOY_BUTTON1
;
117 if (joyInfo
.wButtons
& JOY_BUTTON2
)
118 buttons
|= wxJOY_BUTTON2
;
119 if (joyInfo
.wButtons
& JOY_BUTTON3
)
120 buttons
|= wxJOY_BUTTON3
;
121 if (joyInfo
.wButtons
& JOY_BUTTON4
)
122 buttons
|= wxJOY_BUTTON4
;
133 Returns -1 to signify error.
135 int wxJoystick::GetPOVPosition() const
137 #ifndef NO_JOYGETPOSEX
139 joyInfo
.dwFlags
= JOY_RETURNPOV
;
140 joyInfo
.dwSize
= sizeof(joyInfo
);
141 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
142 if (res
== JOYERR_NOERROR
)
144 return joyInfo
.dwPOV
;
154 johan@linkdata.se 2002-08-20:
155 Returns -1 to signify error.
157 int wxJoystick::GetPOVCTSPosition() const
159 #ifndef NO_JOYGETPOSEX
161 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
162 joyInfo
.dwSize
= sizeof(joyInfo
);
163 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
164 if (res
== JOYERR_NOERROR
)
166 return joyInfo
.dwPOV
;
175 int wxJoystick::GetRudderPosition() const
177 #ifndef NO_JOYGETPOSEX
179 joyInfo
.dwFlags
= JOY_RETURNR
;
180 joyInfo
.dwSize
= sizeof(joyInfo
);
181 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
182 if (res
== JOYERR_NOERROR
)
184 return joyInfo
.dwRpos
;
193 int wxJoystick::GetUPosition() const
195 #ifndef NO_JOYGETPOSEX
197 joyInfo
.dwFlags
= JOY_RETURNU
;
198 joyInfo
.dwSize
= sizeof(joyInfo
);
199 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
200 if (res
== JOYERR_NOERROR
)
202 return joyInfo
.dwUpos
;
211 int wxJoystick::GetVPosition() const
213 #ifndef NO_JOYGETPOSEX
215 joyInfo
.dwFlags
= JOY_RETURNV
;
216 joyInfo
.dwSize
= sizeof(joyInfo
);
217 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
218 if (res
== JOYERR_NOERROR
)
220 return joyInfo
.dwVpos
;
229 int wxJoystick::GetMovementThreshold() const
232 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
233 if (res
== JOYERR_NOERROR
)
241 void wxJoystick::SetMovementThreshold(int threshold
)
243 UINT thresh
= threshold
;
244 joySetThreshold(m_joystick
, thresh
);
248 ////////////////////////////////////////////////////////////////////////////
251 johan@linkdata.se 2002-08-20:
252 Now returns the number of connected, functioning
253 joysticks, as intended.
255 int wxJoystick::GetNumberJoysticks()
258 int i
, maxsticks
, actualsticks
;
259 maxsticks
= joyGetNumDevs();
261 for( i
=0; i
<maxsticks
; i
++ )
263 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
272 johan@linkdata.se 2002-08-20:
273 The old code returned true if there were any
274 joystick capable drivers loaded (=always).
276 bool wxJoystick::IsOk() const
279 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
282 int wxJoystick::GetManufacturerId() const
285 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
291 int wxJoystick::GetProductId() const
294 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
300 wxString
wxJoystick::GetProductName() const
305 if (joyGetDevCaps(m_joystick
, &joyCaps
, sizeof(joyCaps
)) != JOYERR_NOERROR
)
306 return wxEmptyString
;
308 wxRegKey
key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
309 REGSTR_PATH_JOYCONFIG
, joyCaps
.szRegKey
, REGSTR_KEY_JOYCURR
));
311 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
312 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
315 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
316 REGSTR_PATH_JOYOEM
, str
.c_str()));
317 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
322 int wxJoystick::GetXMin() const
325 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
328 return joyCaps
.wXmin
;
331 int wxJoystick::GetYMin() const
334 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
337 return joyCaps
.wYmin
;
340 int wxJoystick::GetZMin() const
343 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
346 return joyCaps
.wZmin
;
349 int wxJoystick::GetXMax() const
352 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
355 return joyCaps
.wXmax
;
358 int wxJoystick::GetYMax() const
361 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
364 return joyCaps
.wYmax
;
367 int wxJoystick::GetZMax() const
370 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
373 return joyCaps
.wZmax
;
376 int wxJoystick::GetNumberButtons() const
379 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
382 return joyCaps
.wNumButtons
;
385 int wxJoystick::GetNumberAxes() const
387 #if defined(__WIN32__)
389 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
392 return joyCaps
.wNumAxes
;
398 int wxJoystick::GetMaxButtons() const
400 #if defined(__WIN32__)
402 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
405 return joyCaps
.wMaxButtons
;
411 int wxJoystick::GetMaxAxes() const
413 #if defined(__WIN32__)
415 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
418 return joyCaps
.wMaxAxes
;
424 int wxJoystick::GetPollingMin() const
427 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
430 return joyCaps
.wPeriodMin
;
433 int wxJoystick::GetPollingMax() const
436 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
439 return joyCaps
.wPeriodMax
;
442 int wxJoystick::GetRudderMin() const
444 #if defined(__WIN32__)
446 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
449 return joyCaps
.wRmin
;
455 int wxJoystick::GetRudderMax() const
457 #if defined(__WIN32__)
459 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
462 return joyCaps
.wRmax
;
468 int wxJoystick::GetUMin() const
470 #if defined(__WIN32__)
472 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
475 return joyCaps
.wUmin
;
481 int wxJoystick::GetUMax() const
483 #if defined(__WIN32__)
485 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
488 return joyCaps
.wUmax
;
494 int wxJoystick::GetVMin() const
496 #if defined(__WIN32__)
498 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
501 return joyCaps
.wVmin
;
507 int wxJoystick::GetVMax() const
509 #if defined(__WIN32__)
511 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
514 return joyCaps
.wVmax
;
521 bool wxJoystick::HasRudder() const
523 #if defined(__WIN32__)
525 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
528 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
534 bool wxJoystick::HasZ() const
536 #if defined(__WIN32__)
538 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
541 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
547 bool wxJoystick::HasU() const
549 #if defined(__WIN32__)
551 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
554 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
560 bool wxJoystick::HasV() const
562 #if defined(__WIN32__)
564 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
567 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
573 bool wxJoystick::HasPOV() const
575 #if defined(__WIN32__)
577 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
580 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
586 bool wxJoystick::HasPOV4Dir() const
588 #if defined(__WIN32__)
590 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
593 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
599 bool wxJoystick::HasPOVCTS() const
601 #if defined(__WIN32__)
603 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
606 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
613 ////////////////////////////////////////////////////////////////////////////
615 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
617 BOOL changed
= (pollingFreq
== 0);
618 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
619 return (res
== JOYERR_NOERROR
);
622 bool wxJoystick::ReleaseCapture()
624 MMRESULT res
= joyReleaseCapture(m_joystick
);
625 return (res
== JOYERR_NOERROR
);
628 #endif // wxUSE_JOYSTICK