]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "joystick.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/string.h"
24 #include "wx/window.h"
25 #include "wx/msw/private.h"
27 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
31 #if !defined(__WIN32__) && !defined(_MMRESULT_)
32 typedef UINT MMRESULT
;
35 #ifdef __GNUWIN32_OLD__
36 #include "wx/msw/gnuwin32/extra.h"
39 // Why doesn't BC++ have joyGetPosEx?
40 #if !defined(__WIN32__) || defined(__BORLANDC__)
41 #define NO_JOYGETPOSEX
44 #include "wx/window.h"
45 #include "wx/msw/joystick.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
50 ////////////////////////////////////////////////////////////////////////////
53 johan@linkdata.se 2002-08-20:
54 Now returns only valid, functioning
55 joysticks, counting from the first
56 available and upwards.
58 wxJoystick::wxJoystick(int joystick
)
63 maxsticks
= joyGetNumDevs();
64 for( i
=0; i
<maxsticks
; i
++ )
66 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
70 /* Found the one we want, store actual OS id and return */
78 /* No such joystick, return ID 0 */
83 wxPoint
wxJoystick::GetPosition() const
86 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
87 if (res
== JOYERR_NOERROR
)
88 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
93 int wxJoystick::GetZPosition() const
96 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
97 if (res
== JOYERR_NOERROR
)
104 johan@linkdata.se 2002-08-20:
105 Return a bitmap with all button states in it,
106 like the GTK version does and Win32 does.
108 int wxJoystick::GetButtonState() const
111 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
112 if (res
== JOYERR_NOERROR
)
114 return joyInfo
.wButtons
;
118 if (joyInfo
.wButtons
& JOY_BUTTON1
)
119 buttons
|= wxJOY_BUTTON1
;
120 if (joyInfo
.wButtons
& JOY_BUTTON2
)
121 buttons
|= wxJOY_BUTTON2
;
122 if (joyInfo
.wButtons
& JOY_BUTTON3
)
123 buttons
|= wxJOY_BUTTON3
;
124 if (joyInfo
.wButtons
& JOY_BUTTON4
)
125 buttons
|= wxJOY_BUTTON4
;
136 Returns -1 to signify error.
138 int wxJoystick::GetPOVPosition() const
140 #ifndef NO_JOYGETPOSEX
142 joyInfo
.dwFlags
= JOY_RETURNPOV
;
143 joyInfo
.dwSize
= sizeof(joyInfo
);
144 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
145 if (res
== JOYERR_NOERROR
)
147 return joyInfo
.dwPOV
;
157 johan@linkdata.se 2002-08-20:
158 Returns -1 to signify error.
160 int wxJoystick::GetPOVCTSPosition() const
162 #ifndef NO_JOYGETPOSEX
164 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
165 joyInfo
.dwSize
= sizeof(joyInfo
);
166 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
167 if (res
== JOYERR_NOERROR
)
169 return joyInfo
.dwPOV
;
178 int wxJoystick::GetRudderPosition() const
180 #ifndef NO_JOYGETPOSEX
182 joyInfo
.dwFlags
= JOY_RETURNR
;
183 joyInfo
.dwSize
= sizeof(joyInfo
);
184 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
185 if (res
== JOYERR_NOERROR
)
187 return joyInfo
.dwRpos
;
196 int wxJoystick::GetUPosition() const
198 #ifndef NO_JOYGETPOSEX
200 joyInfo
.dwFlags
= JOY_RETURNU
;
201 joyInfo
.dwSize
= sizeof(joyInfo
);
202 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
203 if (res
== JOYERR_NOERROR
)
205 return joyInfo
.dwUpos
;
214 int wxJoystick::GetVPosition() const
216 #ifndef NO_JOYGETPOSEX
218 joyInfo
.dwFlags
= JOY_RETURNV
;
219 joyInfo
.dwSize
= sizeof(joyInfo
);
220 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
221 if (res
== JOYERR_NOERROR
)
223 return joyInfo
.dwVpos
;
232 int wxJoystick::GetMovementThreshold() const
235 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
236 if (res
== JOYERR_NOERROR
)
244 void wxJoystick::SetMovementThreshold(int threshold
)
246 UINT thresh
= threshold
;
247 joySetThreshold(m_joystick
, thresh
);
251 ////////////////////////////////////////////////////////////////////////////
254 johan@linkdata.se 2002-08-20:
255 Now returns the number of connected, functioning
256 joysticks, as intended.
258 int wxJoystick::GetNumberJoysticks()
261 int i
, maxsticks
, actualsticks
;
262 maxsticks
= joyGetNumDevs();
264 for( i
=0; i
<maxsticks
; i
++ )
266 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
275 johan@linkdata.se 2002-08-20:
276 The old code returned true if there were any
277 joystick capable drivers loaded (=always).
279 bool wxJoystick::IsOk() const
282 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
285 int wxJoystick::GetManufacturerId() const
288 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
294 int wxJoystick::GetProductId() const
297 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
303 wxString
wxJoystick::GetProductName() const
306 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
307 return wxEmptyString
;
309 return wxString(joyCaps
.szPname
);
312 int wxJoystick::GetXMin() const
315 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
318 return joyCaps
.wXmin
;
321 int wxJoystick::GetYMin() const
324 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
327 return joyCaps
.wYmin
;
330 int wxJoystick::GetZMin() const
333 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
336 return joyCaps
.wZmin
;
339 int wxJoystick::GetXMax() const
342 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
345 return joyCaps
.wXmax
;
348 int wxJoystick::GetYMax() const
351 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
354 return joyCaps
.wYmax
;
357 int wxJoystick::GetZMax() const
360 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
363 return joyCaps
.wZmax
;
366 int wxJoystick::GetNumberButtons() const
369 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
372 return joyCaps
.wNumButtons
;
375 int wxJoystick::GetNumberAxes() const
377 #if defined(__WIN32__)
379 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
382 return joyCaps
.wNumAxes
;
388 int wxJoystick::GetMaxButtons() const
390 #if defined(__WIN32__)
392 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
395 return joyCaps
.wMaxButtons
;
401 int wxJoystick::GetMaxAxes() const
403 #if defined(__WIN32__)
405 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
408 return joyCaps
.wMaxAxes
;
414 int wxJoystick::GetPollingMin() const
417 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
420 return joyCaps
.wPeriodMin
;
423 int wxJoystick::GetPollingMax() const
426 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
429 return joyCaps
.wPeriodMax
;
432 int wxJoystick::GetRudderMin() const
434 #if defined(__WIN32__)
436 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
439 return joyCaps
.wRmin
;
445 int wxJoystick::GetRudderMax() const
447 #if defined(__WIN32__)
449 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
452 return joyCaps
.wRmax
;
458 int wxJoystick::GetUMin() const
460 #if defined(__WIN32__)
462 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
465 return joyCaps
.wUmin
;
471 int wxJoystick::GetUMax() const
473 #if defined(__WIN32__)
475 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
478 return joyCaps
.wUmax
;
484 int wxJoystick::GetVMin() const
486 #if defined(__WIN32__)
488 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
491 return joyCaps
.wVmin
;
497 int wxJoystick::GetVMax() const
499 #if defined(__WIN32__)
501 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
504 return joyCaps
.wVmax
;
511 bool wxJoystick::HasRudder() const
513 #if defined(__WIN32__)
515 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
518 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
524 bool wxJoystick::HasZ() const
526 #if defined(__WIN32__)
528 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
531 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
537 bool wxJoystick::HasU() const
539 #if defined(__WIN32__)
541 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
544 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
550 bool wxJoystick::HasV() const
552 #if defined(__WIN32__)
554 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
557 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
563 bool wxJoystick::HasPOV() const
565 #if defined(__WIN32__)
567 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
570 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
576 bool wxJoystick::HasPOV4Dir() const
578 #if defined(__WIN32__)
580 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
583 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
589 bool wxJoystick::HasPOVCTS() const
591 #if defined(__WIN32__)
593 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
596 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
603 ////////////////////////////////////////////////////////////////////////////
605 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
607 BOOL changed
= (pollingFreq
== 0);
608 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
609 return (res
== JOYERR_NOERROR
);
612 bool wxJoystick::ReleaseCapture()
614 MMRESULT res
= joyReleaseCapture(m_joystick
);
615 return (res
== JOYERR_NOERROR
);