]>
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 // Why doesn't BC++ have joyGetPosEx?
32 #if !defined(__WIN32__) || defined(__BORLANDC__)
33 #define NO_JOYGETPOSEX
36 #include "wx/window.h"
37 #include "wx/msw/registry.h"
38 #include "wx/msw/joystick.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
45 ////////////////////////////////////////////////////////////////////////////
48 johan@linkdata.se 2002-08-20:
49 Now returns only valid, functioning
50 joysticks, counting from the first
51 available and upwards.
53 wxJoystick::wxJoystick(int joystick
)
58 maxsticks
= joyGetNumDevs();
59 for( i
=0; i
<maxsticks
; i
++ )
61 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
65 /* Found the one we want, store actual OS id and return */
73 /* No such joystick, return ID 0 */
78 wxPoint
wxJoystick::GetPosition() const
81 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
82 if (res
== JOYERR_NOERROR
)
83 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
88 int wxJoystick::GetZPosition() const
91 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
92 if (res
== JOYERR_NOERROR
)
99 johan@linkdata.se 2002-08-20:
100 Return a bitmap with all button states in it,
101 like the GTK version does and Win32 does.
103 int wxJoystick::GetButtonState() const
106 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
107 if (res
== JOYERR_NOERROR
)
109 return joyInfo
.wButtons
;
113 if (joyInfo
.wButtons
& JOY_BUTTON1
)
114 buttons
|= wxJOY_BUTTON1
;
115 if (joyInfo
.wButtons
& JOY_BUTTON2
)
116 buttons
|= wxJOY_BUTTON2
;
117 if (joyInfo
.wButtons
& JOY_BUTTON3
)
118 buttons
|= wxJOY_BUTTON3
;
119 if (joyInfo
.wButtons
& JOY_BUTTON4
)
120 buttons
|= wxJOY_BUTTON4
;
131 Returns -1 to signify error.
133 int wxJoystick::GetPOVPosition() const
135 #ifndef NO_JOYGETPOSEX
137 joyInfo
.dwFlags
= JOY_RETURNPOV
;
138 joyInfo
.dwSize
= sizeof(joyInfo
);
139 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
140 if (res
== JOYERR_NOERROR
)
142 return joyInfo
.dwPOV
;
152 johan@linkdata.se 2002-08-20:
153 Returns -1 to signify error.
155 int wxJoystick::GetPOVCTSPosition() const
157 #ifndef NO_JOYGETPOSEX
159 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
160 joyInfo
.dwSize
= sizeof(joyInfo
);
161 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
162 if (res
== JOYERR_NOERROR
)
164 return joyInfo
.dwPOV
;
173 int wxJoystick::GetRudderPosition() const
175 #ifndef NO_JOYGETPOSEX
177 joyInfo
.dwFlags
= JOY_RETURNR
;
178 joyInfo
.dwSize
= sizeof(joyInfo
);
179 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
180 if (res
== JOYERR_NOERROR
)
182 return joyInfo
.dwRpos
;
191 int wxJoystick::GetUPosition() const
193 #ifndef NO_JOYGETPOSEX
195 joyInfo
.dwFlags
= JOY_RETURNU
;
196 joyInfo
.dwSize
= sizeof(joyInfo
);
197 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
198 if (res
== JOYERR_NOERROR
)
200 return joyInfo
.dwUpos
;
209 int wxJoystick::GetVPosition() const
211 #ifndef NO_JOYGETPOSEX
213 joyInfo
.dwFlags
= JOY_RETURNV
;
214 joyInfo
.dwSize
= sizeof(joyInfo
);
215 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
216 if (res
== JOYERR_NOERROR
)
218 return joyInfo
.dwVpos
;
227 int wxJoystick::GetMovementThreshold() const
230 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
231 if (res
== JOYERR_NOERROR
)
239 void wxJoystick::SetMovementThreshold(int threshold
)
241 UINT thresh
= threshold
;
242 joySetThreshold(m_joystick
, thresh
);
246 ////////////////////////////////////////////////////////////////////////////
249 johan@linkdata.se 2002-08-20:
250 Now returns the number of connected, functioning
251 joysticks, as intended.
253 int wxJoystick::GetNumberJoysticks()
256 int i
, maxsticks
, actualsticks
;
257 maxsticks
= joyGetNumDevs();
259 for( i
=0; i
<maxsticks
; i
++ )
261 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
270 johan@linkdata.se 2002-08-20:
271 The old code returned true if there were any
272 joystick capable drivers loaded (=always).
274 bool wxJoystick::IsOk() const
277 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
280 int wxJoystick::GetManufacturerId() const
283 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
289 int wxJoystick::GetProductId() const
292 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
298 wxString
wxJoystick::GetProductName() const
301 if (joyGetDevCaps(m_joystick
, &joyCaps
, sizeof(joyCaps
)) != JOYERR_NOERROR
)
302 return wxEmptyString
;
304 wxRegKey
key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
305 REGSTR_PATH_JOYCONFIG
, joyCaps
.szRegKey
, REGSTR_KEY_JOYCURR
));
308 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
309 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
312 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
313 REGSTR_PATH_JOYOEM
, str
.c_str()));
314 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
319 int wxJoystick::GetXMin() const
322 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
325 return joyCaps
.wXmin
;
328 int wxJoystick::GetYMin() const
331 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
334 return joyCaps
.wYmin
;
337 int wxJoystick::GetZMin() const
340 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
343 return joyCaps
.wZmin
;
346 int wxJoystick::GetXMax() const
349 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
352 return joyCaps
.wXmax
;
355 int wxJoystick::GetYMax() const
358 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
361 return joyCaps
.wYmax
;
364 int wxJoystick::GetZMax() const
367 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
370 return joyCaps
.wZmax
;
373 int wxJoystick::GetNumberButtons() const
376 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
379 return joyCaps
.wNumButtons
;
382 int wxJoystick::GetNumberAxes() const
384 #if defined(__WIN32__)
386 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
389 return joyCaps
.wNumAxes
;
395 int wxJoystick::GetMaxButtons() const
397 #if defined(__WIN32__)
399 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
402 return joyCaps
.wMaxButtons
;
408 int wxJoystick::GetMaxAxes() const
410 #if defined(__WIN32__)
412 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
415 return joyCaps
.wMaxAxes
;
421 int wxJoystick::GetPollingMin() const
424 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
427 return joyCaps
.wPeriodMin
;
430 int wxJoystick::GetPollingMax() const
433 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
436 return joyCaps
.wPeriodMax
;
439 int wxJoystick::GetRudderMin() const
441 #if defined(__WIN32__)
443 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
446 return joyCaps
.wRmin
;
452 int wxJoystick::GetRudderMax() const
454 #if defined(__WIN32__)
456 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
459 return joyCaps
.wRmax
;
465 int wxJoystick::GetUMin() const
467 #if defined(__WIN32__)
469 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
472 return joyCaps
.wUmin
;
478 int wxJoystick::GetUMax() const
480 #if defined(__WIN32__)
482 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
485 return joyCaps
.wUmax
;
491 int wxJoystick::GetVMin() const
493 #if defined(__WIN32__)
495 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
498 return joyCaps
.wVmin
;
504 int wxJoystick::GetVMax() const
506 #if defined(__WIN32__)
508 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
511 return joyCaps
.wVmax
;
518 bool wxJoystick::HasRudder() const
520 #if defined(__WIN32__)
522 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
525 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
531 bool wxJoystick::HasZ() const
533 #if defined(__WIN32__)
535 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
538 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
544 bool wxJoystick::HasU() const
546 #if defined(__WIN32__)
548 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
551 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
557 bool wxJoystick::HasV() const
559 #if defined(__WIN32__)
561 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
564 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
570 bool wxJoystick::HasPOV() const
572 #if defined(__WIN32__)
574 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
577 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
583 bool wxJoystick::HasPOV4Dir() const
585 #if defined(__WIN32__)
587 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
590 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
596 bool wxJoystick::HasPOVCTS() const
598 #if defined(__WIN32__)
600 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
603 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
610 ////////////////////////////////////////////////////////////////////////////
612 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
614 BOOL changed
= (pollingFreq
== 0);
615 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
616 return (res
== JOYERR_NOERROR
);
619 bool wxJoystick::ReleaseCapture()
621 MMRESULT res
= joyReleaseCapture(m_joystick
);
622 return (res
== JOYERR_NOERROR
);