]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/joystick.cpp
1 /////////////////////////////////////////////////////////////////////////////
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"
19 #include "wx/string.h"
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
23 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
27 // Why doesn't BC++ have joyGetPosEx?
28 #if !defined(__WIN32__) || defined(__BORLANDC__)
29 #define NO_JOYGETPOSEX
32 #include "wx/window.h"
33 #include "wx/msw/registry.h"
34 #include "wx/msw/joystick.h"
38 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
41 ////////////////////////////////////////////////////////////////////////////
44 johan@linkdata.se 2002-08-20:
45 Now returns only valid, functioning
46 joysticks, counting from the first
47 available and upwards.
49 wxJoystick::wxJoystick(int joystick
)
54 maxsticks
= joyGetNumDevs();
55 for( i
=0; i
<maxsticks
; i
++ )
57 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
61 /* Found the one we want, store actual OS id and return */
69 /* No such joystick, return ID 0 */
74 wxPoint
wxJoystick::GetPosition() const
77 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
78 if (res
== JOYERR_NOERROR
)
79 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
84 int wxJoystick::GetZPosition() const
87 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
88 if (res
== JOYERR_NOERROR
)
95 johan@linkdata.se 2002-08-20:
96 Return a bitmap with all button states in it,
97 like the GTK version does and Win32 does.
99 int wxJoystick::GetButtonState() const
102 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
103 if (res
== JOYERR_NOERROR
)
105 return joyInfo
.wButtons
;
109 if (joyInfo
.wButtons
& JOY_BUTTON1
)
110 buttons
|= wxJOY_BUTTON1
;
111 if (joyInfo
.wButtons
& JOY_BUTTON2
)
112 buttons
|= wxJOY_BUTTON2
;
113 if (joyInfo
.wButtons
& JOY_BUTTON3
)
114 buttons
|= wxJOY_BUTTON3
;
115 if (joyInfo
.wButtons
& JOY_BUTTON4
)
116 buttons
|= wxJOY_BUTTON4
;
127 Returns -1 to signify error.
129 int wxJoystick::GetPOVPosition() const
131 #ifndef NO_JOYGETPOSEX
133 joyInfo
.dwFlags
= JOY_RETURNPOV
;
134 joyInfo
.dwSize
= sizeof(joyInfo
);
135 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
136 if (res
== JOYERR_NOERROR
)
138 return joyInfo
.dwPOV
;
148 johan@linkdata.se 2002-08-20:
149 Returns -1 to signify error.
151 int wxJoystick::GetPOVCTSPosition() const
153 #ifndef NO_JOYGETPOSEX
155 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
156 joyInfo
.dwSize
= sizeof(joyInfo
);
157 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
158 if (res
== JOYERR_NOERROR
)
160 return joyInfo
.dwPOV
;
169 int wxJoystick::GetRudderPosition() const
171 #ifndef NO_JOYGETPOSEX
173 joyInfo
.dwFlags
= JOY_RETURNR
;
174 joyInfo
.dwSize
= sizeof(joyInfo
);
175 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
176 if (res
== JOYERR_NOERROR
)
178 return joyInfo
.dwRpos
;
187 int wxJoystick::GetUPosition() const
189 #ifndef NO_JOYGETPOSEX
191 joyInfo
.dwFlags
= JOY_RETURNU
;
192 joyInfo
.dwSize
= sizeof(joyInfo
);
193 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
194 if (res
== JOYERR_NOERROR
)
196 return joyInfo
.dwUpos
;
205 int wxJoystick::GetVPosition() const
207 #ifndef NO_JOYGETPOSEX
209 joyInfo
.dwFlags
= JOY_RETURNV
;
210 joyInfo
.dwSize
= sizeof(joyInfo
);
211 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
212 if (res
== JOYERR_NOERROR
)
214 return joyInfo
.dwVpos
;
223 int wxJoystick::GetMovementThreshold() const
226 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
227 if (res
== JOYERR_NOERROR
)
235 void wxJoystick::SetMovementThreshold(int threshold
)
237 UINT thresh
= threshold
;
238 joySetThreshold(m_joystick
, thresh
);
242 ////////////////////////////////////////////////////////////////////////////
245 johan@linkdata.se 2002-08-20:
246 Now returns the number of connected, functioning
247 joysticks, as intended.
249 int wxJoystick::GetNumberJoysticks()
252 int i
, maxsticks
, actualsticks
;
253 maxsticks
= joyGetNumDevs();
255 for( i
=0; i
<maxsticks
; i
++ )
257 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
266 johan@linkdata.se 2002-08-20:
267 The old code returned true if there were any
268 joystick capable drivers loaded (=always).
270 bool wxJoystick::IsOk() const
273 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
276 int wxJoystick::GetManufacturerId() const
279 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
285 int wxJoystick::GetProductId() const
288 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
294 wxString
wxJoystick::GetProductName() const
297 if (joyGetDevCaps(m_joystick
, &joyCaps
, sizeof(joyCaps
)) != JOYERR_NOERROR
)
298 return wxEmptyString
;
300 wxRegKey
key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
301 REGSTR_PATH_JOYCONFIG
, joyCaps
.szRegKey
, REGSTR_KEY_JOYCURR
));
304 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
305 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
308 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
309 REGSTR_PATH_JOYOEM
, str
.c_str()));
310 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
315 int wxJoystick::GetXMin() const
318 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
321 return joyCaps
.wXmin
;
324 int wxJoystick::GetYMin() const
327 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
330 return joyCaps
.wYmin
;
333 int wxJoystick::GetZMin() const
336 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
339 return joyCaps
.wZmin
;
342 int wxJoystick::GetXMax() const
345 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
348 return joyCaps
.wXmax
;
351 int wxJoystick::GetYMax() const
354 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
357 return joyCaps
.wYmax
;
360 int wxJoystick::GetZMax() const
363 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
366 return joyCaps
.wZmax
;
369 int wxJoystick::GetNumberButtons() const
372 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
375 return joyCaps
.wNumButtons
;
378 int wxJoystick::GetNumberAxes() const
380 #if defined(__WIN32__)
382 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
385 return joyCaps
.wNumAxes
;
391 int wxJoystick::GetMaxButtons() const
393 #if defined(__WIN32__)
395 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
398 return joyCaps
.wMaxButtons
;
404 int wxJoystick::GetMaxAxes() const
406 #if defined(__WIN32__)
408 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
411 return joyCaps
.wMaxAxes
;
417 int wxJoystick::GetPollingMin() const
420 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
423 return joyCaps
.wPeriodMin
;
426 int wxJoystick::GetPollingMax() const
429 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
432 return joyCaps
.wPeriodMax
;
435 int wxJoystick::GetRudderMin() const
437 #if defined(__WIN32__)
439 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
442 return joyCaps
.wRmin
;
448 int wxJoystick::GetRudderMax() const
450 #if defined(__WIN32__)
452 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
455 return joyCaps
.wRmax
;
461 int wxJoystick::GetUMin() const
463 #if defined(__WIN32__)
465 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
468 return joyCaps
.wUmin
;
474 int wxJoystick::GetUMax() const
476 #if defined(__WIN32__)
478 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
481 return joyCaps
.wUmax
;
487 int wxJoystick::GetVMin() const
489 #if defined(__WIN32__)
491 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
494 return joyCaps
.wVmin
;
500 int wxJoystick::GetVMax() const
502 #if defined(__WIN32__)
504 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
507 return joyCaps
.wVmax
;
514 bool wxJoystick::HasRudder() const
516 #if defined(__WIN32__)
518 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
521 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
527 bool wxJoystick::HasZ() const
529 #if defined(__WIN32__)
531 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
534 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
540 bool wxJoystick::HasU() const
542 #if defined(__WIN32__)
544 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
547 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
553 bool wxJoystick::HasV() const
555 #if defined(__WIN32__)
557 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
560 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
566 bool wxJoystick::HasPOV() const
568 #if defined(__WIN32__)
570 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
573 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
579 bool wxJoystick::HasPOV4Dir() const
581 #if defined(__WIN32__)
583 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
586 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
592 bool wxJoystick::HasPOVCTS() const
594 #if defined(__WIN32__)
596 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
599 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
606 ////////////////////////////////////////////////////////////////////////////
608 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
610 BOOL changed
= (pollingFreq
== 0);
611 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
612 return (res
== JOYERR_NOERROR
);
615 bool wxJoystick::ReleaseCapture()
617 MMRESULT res
= joyReleaseCapture(m_joystick
);
618 return (res
== JOYERR_NOERROR
);