]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 // Why doesn't BC++ have joyGetPosEx?
36 #if !defined(__WIN32__) || defined(__BORLANDC__)
37 #define NO_JOYGETPOSEX
40 #include "wx/window.h"
41 #include "wx/msw/joystick.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
46 ////////////////////////////////////////////////////////////////////////////
49 johan@linkdata.se 2002-08-20:
50 Now returns only valid, functioning
51 joysticks, counting from the first
52 available and upwards.
54 wxJoystick::wxJoystick(int joystick
)
59 maxsticks
= joyGetNumDevs();
60 for( i
=0; i
<maxsticks
; i
++ )
62 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
66 /* Found the one we want, store actual OS id and return */
74 /* No such joystick, return ID 0 */
79 wxPoint
wxJoystick::GetPosition() const
82 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
83 if (res
== JOYERR_NOERROR
)
84 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
89 int wxJoystick::GetZPosition() const
92 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
93 if (res
== JOYERR_NOERROR
)
100 johan@linkdata.se 2002-08-20:
101 Return a bitmap with all button states in it,
102 like the GTK version does and Win32 does.
104 int wxJoystick::GetButtonState() const
107 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
108 if (res
== JOYERR_NOERROR
)
110 return joyInfo
.wButtons
;
114 if (joyInfo
.wButtons
& JOY_BUTTON1
)
115 buttons
|= wxJOY_BUTTON1
;
116 if (joyInfo
.wButtons
& JOY_BUTTON2
)
117 buttons
|= wxJOY_BUTTON2
;
118 if (joyInfo
.wButtons
& JOY_BUTTON3
)
119 buttons
|= wxJOY_BUTTON3
;
120 if (joyInfo
.wButtons
& JOY_BUTTON4
)
121 buttons
|= wxJOY_BUTTON4
;
132 Returns -1 to signify error.
134 int wxJoystick::GetPOVPosition() const
136 #ifndef NO_JOYGETPOSEX
138 joyInfo
.dwFlags
= JOY_RETURNPOV
;
139 joyInfo
.dwSize
= sizeof(joyInfo
);
140 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
141 if (res
== JOYERR_NOERROR
)
143 return joyInfo
.dwPOV
;
153 johan@linkdata.se 2002-08-20:
154 Returns -1 to signify error.
156 int wxJoystick::GetPOVCTSPosition() const
158 #ifndef NO_JOYGETPOSEX
160 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
161 joyInfo
.dwSize
= sizeof(joyInfo
);
162 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
163 if (res
== JOYERR_NOERROR
)
165 return joyInfo
.dwPOV
;
174 int wxJoystick::GetRudderPosition() const
176 #ifndef NO_JOYGETPOSEX
178 joyInfo
.dwFlags
= JOY_RETURNR
;
179 joyInfo
.dwSize
= sizeof(joyInfo
);
180 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
181 if (res
== JOYERR_NOERROR
)
183 return joyInfo
.dwRpos
;
192 int wxJoystick::GetUPosition() const
194 #ifndef NO_JOYGETPOSEX
196 joyInfo
.dwFlags
= JOY_RETURNU
;
197 joyInfo
.dwSize
= sizeof(joyInfo
);
198 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
199 if (res
== JOYERR_NOERROR
)
201 return joyInfo
.dwUpos
;
210 int wxJoystick::GetVPosition() const
212 #ifndef NO_JOYGETPOSEX
214 joyInfo
.dwFlags
= JOY_RETURNV
;
215 joyInfo
.dwSize
= sizeof(joyInfo
);
216 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
217 if (res
== JOYERR_NOERROR
)
219 return joyInfo
.dwVpos
;
228 int wxJoystick::GetMovementThreshold() const
231 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
232 if (res
== JOYERR_NOERROR
)
240 void wxJoystick::SetMovementThreshold(int threshold
)
242 UINT thresh
= threshold
;
243 joySetThreshold(m_joystick
, thresh
);
247 ////////////////////////////////////////////////////////////////////////////
250 johan@linkdata.se 2002-08-20:
251 Now returns the number of connected, functioning
252 joysticks, as intended.
254 int wxJoystick::GetNumberJoysticks()
257 int i
, maxsticks
, actualsticks
;
258 maxsticks
= joyGetNumDevs();
260 for( i
=0; i
<maxsticks
; i
++ )
262 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
271 johan@linkdata.se 2002-08-20:
272 The old code returned true if there were any
273 joystick capable drivers loaded (=always).
275 bool wxJoystick::IsOk() const
278 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
281 int wxJoystick::GetManufacturerId() const
284 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
290 int wxJoystick::GetProductId() const
293 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
299 wxString
wxJoystick::GetProductName() const
302 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
303 return wxEmptyString
;
305 return wxString(joyCaps
.szPname
);
308 int wxJoystick::GetXMin() const
311 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
314 return joyCaps
.wXmin
;
317 int wxJoystick::GetYMin() const
320 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
323 return joyCaps
.wYmin
;
326 int wxJoystick::GetZMin() const
329 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
332 return joyCaps
.wZmin
;
335 int wxJoystick::GetXMax() const
338 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
341 return joyCaps
.wXmax
;
344 int wxJoystick::GetYMax() const
347 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
350 return joyCaps
.wYmax
;
353 int wxJoystick::GetZMax() const
356 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
359 return joyCaps
.wZmax
;
362 int wxJoystick::GetNumberButtons() const
365 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
368 return joyCaps
.wNumButtons
;
371 int wxJoystick::GetNumberAxes() const
373 #if defined(__WIN32__)
375 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
378 return joyCaps
.wNumAxes
;
384 int wxJoystick::GetMaxButtons() const
386 #if defined(__WIN32__)
388 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
391 return joyCaps
.wMaxButtons
;
397 int wxJoystick::GetMaxAxes() const
399 #if defined(__WIN32__)
401 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
404 return joyCaps
.wMaxAxes
;
410 int wxJoystick::GetPollingMin() const
413 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
416 return joyCaps
.wPeriodMin
;
419 int wxJoystick::GetPollingMax() const
422 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
425 return joyCaps
.wPeriodMax
;
428 int wxJoystick::GetRudderMin() const
430 #if defined(__WIN32__)
432 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
435 return joyCaps
.wRmin
;
441 int wxJoystick::GetRudderMax() const
443 #if defined(__WIN32__)
445 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
448 return joyCaps
.wRmax
;
454 int wxJoystick::GetUMin() const
456 #if defined(__WIN32__)
458 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
461 return joyCaps
.wUmin
;
467 int wxJoystick::GetUMax() const
469 #if defined(__WIN32__)
471 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
474 return joyCaps
.wUmax
;
480 int wxJoystick::GetVMin() const
482 #if defined(__WIN32__)
484 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
487 return joyCaps
.wVmin
;
493 int wxJoystick::GetVMax() const
495 #if defined(__WIN32__)
497 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
500 return joyCaps
.wVmax
;
507 bool wxJoystick::HasRudder() const
509 #if defined(__WIN32__)
511 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
514 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
520 bool wxJoystick::HasZ() const
522 #if defined(__WIN32__)
524 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
527 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
533 bool wxJoystick::HasU() const
535 #if defined(__WIN32__)
537 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
540 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
546 bool wxJoystick::HasV() const
548 #if defined(__WIN32__)
550 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
553 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
559 bool wxJoystick::HasPOV() const
561 #if defined(__WIN32__)
563 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
566 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
572 bool wxJoystick::HasPOV4Dir() const
574 #if defined(__WIN32__)
576 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
579 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
585 bool wxJoystick::HasPOVCTS() const
587 #if defined(__WIN32__)
589 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
592 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
599 ////////////////////////////////////////////////////////////////////////////
601 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
603 BOOL changed
= (pollingFreq
== 0);
604 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
605 return (res
== JOYERR_NOERROR
);
608 bool wxJoystick::ReleaseCapture()
610 MMRESULT res
= joyReleaseCapture(m_joystick
);
611 return (res
== JOYERR_NOERROR
);