]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/joystick.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/joystick.cpp
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"
21 #include "wx/joystick.h"
24 #include "wx/string.h"
25 #include "wx/window.h"
28 #include "wx/msw/private.h"
30 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
34 // Why doesn't BC++ have joyGetPosEx?
35 #if !defined(__WIN32__) || defined(__BORLANDC__)
36 #define NO_JOYGETPOSEX
39 #include "wx/msw/registry.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
304 if (joyGetDevCaps(m_joystick
, &joyCaps
, sizeof(joyCaps
)) != JOYERR_NOERROR
)
305 return wxEmptyString
;
307 wxRegKey
key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
308 REGSTR_PATH_JOYCONFIG
, joyCaps
.szRegKey
, REGSTR_KEY_JOYCURR
));
310 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
311 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
314 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
315 REGSTR_PATH_JOYOEM
, str
.c_str()));
316 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
321 int wxJoystick::GetXMin() const
324 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
327 return joyCaps
.wXmin
;
330 int wxJoystick::GetYMin() const
333 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
336 return joyCaps
.wYmin
;
339 int wxJoystick::GetZMin() const
342 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
345 return joyCaps
.wZmin
;
348 int wxJoystick::GetXMax() const
351 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
354 return joyCaps
.wXmax
;
357 int wxJoystick::GetYMax() const
360 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
363 return joyCaps
.wYmax
;
366 int wxJoystick::GetZMax() const
369 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
372 return joyCaps
.wZmax
;
375 int wxJoystick::GetNumberButtons() const
378 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
381 return joyCaps
.wNumButtons
;
384 int wxJoystick::GetNumberAxes() const
386 #if defined(__WIN32__)
388 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
391 return joyCaps
.wNumAxes
;
397 int wxJoystick::GetMaxButtons() const
399 #if defined(__WIN32__)
401 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
404 return joyCaps
.wMaxButtons
;
410 int wxJoystick::GetMaxAxes() const
412 #if defined(__WIN32__)
414 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
417 return joyCaps
.wMaxAxes
;
423 int wxJoystick::GetPollingMin() const
426 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
429 return joyCaps
.wPeriodMin
;
432 int wxJoystick::GetPollingMax() const
435 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
438 return joyCaps
.wPeriodMax
;
441 int wxJoystick::GetRudderMin() const
443 #if defined(__WIN32__)
445 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
448 return joyCaps
.wRmin
;
454 int wxJoystick::GetRudderMax() const
456 #if defined(__WIN32__)
458 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
461 return joyCaps
.wRmax
;
467 int wxJoystick::GetUMin() const
469 #if defined(__WIN32__)
471 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
474 return joyCaps
.wUmin
;
480 int wxJoystick::GetUMax() const
482 #if defined(__WIN32__)
484 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
487 return joyCaps
.wUmax
;
493 int wxJoystick::GetVMin() const
495 #if defined(__WIN32__)
497 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
500 return joyCaps
.wVmin
;
506 int wxJoystick::GetVMax() const
508 #if defined(__WIN32__)
510 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
513 return joyCaps
.wVmax
;
520 bool wxJoystick::HasRudder() const
522 #if defined(__WIN32__)
524 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
527 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
533 bool wxJoystick::HasZ() const
535 #if defined(__WIN32__)
537 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
540 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
546 bool wxJoystick::HasU() const
548 #if defined(__WIN32__)
550 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
553 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
559 bool wxJoystick::HasV() const
561 #if defined(__WIN32__)
563 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
566 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
572 bool wxJoystick::HasPOV() const
574 #if defined(__WIN32__)
576 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
579 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
585 bool wxJoystick::HasPOV4Dir() const
587 #if defined(__WIN32__)
589 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
592 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
598 bool wxJoystick::HasPOVCTS() const
600 #if defined(__WIN32__)
602 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
605 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
612 ////////////////////////////////////////////////////////////////////////////
614 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
616 BOOL changed
= (pollingFreq
== 0);
617 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
618 return (res
== JOYERR_NOERROR
);
621 bool wxJoystick::ReleaseCapture()
623 MMRESULT res
= joyReleaseCapture(m_joystick
);
624 return (res
== JOYERR_NOERROR
);
627 #endif // wxUSE_JOYSTICK