]>
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 and Markus Holzem
9 // Licence: wxWindows license
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
;
36 #ifdef __GNUWIN32_OLD__
37 #include "wx/msw/gnuwin32/extra.h"
41 // Why doesn't BC++ have joyGetPosEx?
42 #if !defined(__WIN32__) || defined(__BORLANDC__) || defined(__TWIN32__)
43 #define NO_JOYGETPOSEX
46 #include "wx/window.h"
47 #include "wx/msw/joystick.h"
49 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
52 ////////////////////////////////////////////////////////////////////////////
55 johan@linkdata.se 2002-08-20:
56 Now returns only valid, functioning
57 joysticks, counting from the first
58 available and upwards.
60 wxJoystick::wxJoystick(int joystick
)
65 maxsticks
= joyGetNumDevs();
66 for( i
=0; i
<maxsticks
; i
++ )
68 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
72 /* Found the one we want, store actual OS id and return */
80 /* No such joystick, return ID 0 */
85 wxPoint
wxJoystick::GetPosition() const
88 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
89 if (res
== JOYERR_NOERROR
)
90 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
95 int wxJoystick::GetZPosition() const
98 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
99 if (res
== JOYERR_NOERROR
)
100 return joyInfo
.wZpos
;
106 johan@linkdata.se 2002-08-20:
107 Return a bitmap with all button states in it,
108 like the GTK version does and Win32 does.
110 int wxJoystick::GetButtonState() const
113 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
114 if (res
== JOYERR_NOERROR
)
116 return joyInfo
.wButtons
;
120 if (joyInfo
.wButtons
& JOY_BUTTON1
)
121 buttons
|= wxJOY_BUTTON1
;
122 if (joyInfo
.wButtons
& JOY_BUTTON2
)
123 buttons
|= wxJOY_BUTTON2
;
124 if (joyInfo
.wButtons
& JOY_BUTTON3
)
125 buttons
|= wxJOY_BUTTON3
;
126 if (joyInfo
.wButtons
& JOY_BUTTON4
)
127 buttons
|= wxJOY_BUTTON4
;
138 Returns -1 to signify error.
140 int wxJoystick::GetPOVPosition() const
142 #ifndef NO_JOYGETPOSEX
144 joyInfo
.dwFlags
= JOY_RETURNPOV
;
145 joyInfo
.dwSize
= sizeof(joyInfo
);
146 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
147 if (res
== JOYERR_NOERROR
)
149 return joyInfo
.dwPOV
;
159 johan@linkdata.se 2002-08-20:
160 Returns -1 to signify error.
162 int wxJoystick::GetPOVCTSPosition() const
164 #ifndef NO_JOYGETPOSEX
166 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
167 joyInfo
.dwSize
= sizeof(joyInfo
);
168 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
169 if (res
== JOYERR_NOERROR
)
171 return joyInfo
.dwPOV
;
180 int wxJoystick::GetRudderPosition() const
182 #ifndef NO_JOYGETPOSEX
184 joyInfo
.dwFlags
= JOY_RETURNR
;
185 joyInfo
.dwSize
= sizeof(joyInfo
);
186 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
187 if (res
== JOYERR_NOERROR
)
189 return joyInfo
.dwRpos
;
198 int wxJoystick::GetUPosition() const
200 #ifndef NO_JOYGETPOSEX
202 joyInfo
.dwFlags
= JOY_RETURNU
;
203 joyInfo
.dwSize
= sizeof(joyInfo
);
204 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
205 if (res
== JOYERR_NOERROR
)
207 return joyInfo
.dwUpos
;
216 int wxJoystick::GetVPosition() const
218 #ifndef NO_JOYGETPOSEX
220 joyInfo
.dwFlags
= JOY_RETURNV
;
221 joyInfo
.dwSize
= sizeof(joyInfo
);
222 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
223 if (res
== JOYERR_NOERROR
)
225 return joyInfo
.dwVpos
;
234 int wxJoystick::GetMovementThreshold() const
237 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
238 if (res
== JOYERR_NOERROR
)
246 void wxJoystick::SetMovementThreshold(int threshold
)
248 UINT thresh
= threshold
;
249 joySetThreshold(m_joystick
, thresh
);
253 ////////////////////////////////////////////////////////////////////////////
256 johan@linkdata.se 2002-08-20:
257 Now returns the number of connected, functioning
258 joysticks, as intended.
260 int wxJoystick::GetNumberJoysticks()
263 int i
, maxsticks
, actualsticks
;
264 maxsticks
= joyGetNumDevs();
266 for( i
=0; i
<maxsticks
; i
++ )
268 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
277 johan@linkdata.se 2002-08-20:
278 The old code returned true if there were any
279 joystick capable drivers loaded (=always).
281 bool wxJoystick::IsOk() const
284 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
287 int wxJoystick::GetManufacturerId() const
290 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
296 int wxJoystick::GetProductId() const
299 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
305 wxString
wxJoystick::GetProductName() const
308 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
309 return wxEmptyString
;
311 return wxString(joyCaps
.szPname
);
314 int wxJoystick::GetXMin() const
317 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
320 return joyCaps
.wXmin
;
323 int wxJoystick::GetYMin() const
326 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
329 return joyCaps
.wYmin
;
332 int wxJoystick::GetZMin() const
335 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
338 return joyCaps
.wZmin
;
341 int wxJoystick::GetXMax() const
344 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
347 return joyCaps
.wXmax
;
350 int wxJoystick::GetYMax() const
353 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
356 return joyCaps
.wYmax
;
359 int wxJoystick::GetZMax() const
362 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
365 return joyCaps
.wZmax
;
368 int wxJoystick::GetNumberButtons() const
371 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
374 return joyCaps
.wNumButtons
;
377 int wxJoystick::GetNumberAxes() const
379 #if defined(__WIN32__) && !defined(__TWIN32__)
381 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
384 return joyCaps
.wNumAxes
;
390 int wxJoystick::GetMaxButtons() const
392 #if defined(__WIN32__) && !defined(__TWIN32__)
394 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
397 return joyCaps
.wMaxButtons
;
403 int wxJoystick::GetMaxAxes() const
405 #if defined(__WIN32__) && !defined(__TWIN32__)
407 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
410 return joyCaps
.wMaxAxes
;
416 int wxJoystick::GetPollingMin() const
419 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
422 return joyCaps
.wPeriodMin
;
425 int wxJoystick::GetPollingMax() const
428 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
431 return joyCaps
.wPeriodMax
;
434 int wxJoystick::GetRudderMin() const
436 #if defined(__WIN32__) && !defined(__TWIN32__)
438 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
441 return joyCaps
.wRmin
;
447 int wxJoystick::GetRudderMax() const
449 #if defined(__WIN32__) && !defined(__TWIN32__)
451 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
454 return joyCaps
.wRmax
;
460 int wxJoystick::GetUMin() const
462 #if defined(__WIN32__) && !defined(__TWIN32__)
464 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
467 return joyCaps
.wUmin
;
473 int wxJoystick::GetUMax() const
475 #if defined(__WIN32__) && !defined(__TWIN32__)
477 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
480 return joyCaps
.wUmax
;
486 int wxJoystick::GetVMin() const
488 #if defined(__WIN32__) && !defined(__TWIN32__)
490 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
493 return joyCaps
.wVmin
;
499 int wxJoystick::GetVMax() const
501 #if defined(__WIN32__) && !defined(__TWIN32__)
503 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
506 return joyCaps
.wVmax
;
513 bool wxJoystick::HasRudder() const
515 #if defined(__WIN32__) && !defined(__TWIN32__)
517 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
520 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
526 bool wxJoystick::HasZ() const
528 #if defined(__WIN32__) && !defined(__TWIN32__)
530 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
533 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
539 bool wxJoystick::HasU() const
541 #if defined(__WIN32__) && !defined(__TWIN32__)
543 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
546 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
552 bool wxJoystick::HasV() const
554 #if defined(__WIN32__) && !defined(__TWIN32__)
556 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
559 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
565 bool wxJoystick::HasPOV() const
567 #if defined(__WIN32__) && !defined(__TWIN32__)
569 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
572 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
578 bool wxJoystick::HasPOV4Dir() const
580 #if defined(__WIN32__) && !defined(__TWIN32__)
582 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
585 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
591 bool wxJoystick::HasPOVCTS() const
593 #if defined(__WIN32__) && !defined(__TWIN32__)
595 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
598 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
605 ////////////////////////////////////////////////////////////////////////////
607 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
609 BOOL changed
= (pollingFreq
== 0);
610 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
611 return (res
== JOYERR_NOERROR
);
614 bool wxJoystick::ReleaseCapture()
616 MMRESULT res
= joyReleaseCapture(m_joystick
);
617 return (res
== JOYERR_NOERROR
);