]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/string.h"
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
23 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
27 // Why doesn't BC++ have joyGetPosEx?
28 #if !defined(__WIN32__) || defined(__BORLANDC__)
29 #define NO_JOYGETPOSEX
32 #include "wx/window.h"
33 #include "wx/msw/registry.h"
34 #include "wx/msw/joystick.h"
38 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
41 ////////////////////////////////////////////////////////////////////////////
44 johan@linkdata.se 2002-08-20:
45 Now returns only valid, functioning
46 joysticks, counting from the first
47 available and upwards.
49 wxJoystick::wxJoystick(int joystick
)
54 maxsticks
= joyGetNumDevs();
55 for( i
=0; i
<maxsticks
; i
++ )
57 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
61 /* Found the one we want, store actual OS id and return */
69 /* No such joystick, return ID 0 */
74 wxPoint
wxJoystick::GetPosition() const
77 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
78 if (res
== JOYERR_NOERROR
)
79 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
84 int wxJoystick::GetZPosition() const
87 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
88 if (res
== JOYERR_NOERROR
)
95 johan@linkdata.se 2002-08-20:
96 Return a bitmap with all button states in it,
97 like the GTK version does and Win32 does.
99 int wxJoystick::GetButtonState() const
102 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
103 if (res
== JOYERR_NOERROR
)
105 return joyInfo
.wButtons
;
109 if (joyInfo
.wButtons
& JOY_BUTTON1
)
110 buttons
|= wxJOY_BUTTON1
;
111 if (joyInfo
.wButtons
& JOY_BUTTON2
)
112 buttons
|= wxJOY_BUTTON2
;
113 if (joyInfo
.wButtons
& JOY_BUTTON3
)
114 buttons
|= wxJOY_BUTTON3
;
115 if (joyInfo
.wButtons
& JOY_BUTTON4
)
116 buttons
|= wxJOY_BUTTON4
;
127 Returns -1 to signify error.
129 int wxJoystick::GetPOVPosition() const
131 #ifndef NO_JOYGETPOSEX
133 joyInfo
.dwFlags
= JOY_RETURNPOV
;
134 joyInfo
.dwSize
= sizeof(joyInfo
);
135 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
136 if (res
== JOYERR_NOERROR
)
138 return joyInfo
.dwPOV
;
148 johan@linkdata.se 2002-08-20:
149 Returns -1 to signify error.
151 int wxJoystick::GetPOVCTSPosition() const
153 #ifndef NO_JOYGETPOSEX
155 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
156 joyInfo
.dwSize
= sizeof(joyInfo
);
157 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
158 if (res
== JOYERR_NOERROR
)
160 return joyInfo
.dwPOV
;
169 int wxJoystick::GetRudderPosition() const
171 #ifndef NO_JOYGETPOSEX
173 joyInfo
.dwFlags
= JOY_RETURNR
;
174 joyInfo
.dwSize
= sizeof(joyInfo
);
175 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
176 if (res
== JOYERR_NOERROR
)
178 return joyInfo
.dwRpos
;
187 int wxJoystick::GetUPosition() const
189 #ifndef NO_JOYGETPOSEX
191 joyInfo
.dwFlags
= JOY_RETURNU
;
192 joyInfo
.dwSize
= sizeof(joyInfo
);
193 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
194 if (res
== JOYERR_NOERROR
)
196 return joyInfo
.dwUpos
;
205 int wxJoystick::GetVPosition() const
207 #ifndef NO_JOYGETPOSEX
209 joyInfo
.dwFlags
= JOY_RETURNV
;
210 joyInfo
.dwSize
= sizeof(joyInfo
);
211 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
212 if (res
== JOYERR_NOERROR
)
214 return joyInfo
.dwVpos
;
223 int wxJoystick::GetMovementThreshold() const
226 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
227 if (res
== JOYERR_NOERROR
)
235 void wxJoystick::SetMovementThreshold(int threshold
)
237 UINT thresh
= threshold
;
238 joySetThreshold(m_joystick
, thresh
);
242 ////////////////////////////////////////////////////////////////////////////
245 johan@linkdata.se 2002-08-20:
246 Now returns the number of connected, functioning
247 joysticks, as intended.
249 int wxJoystick::GetNumberJoysticks()
252 int i
, maxsticks
, actualsticks
;
253 maxsticks
= joyGetNumDevs();
255 for( i
=0; i
<maxsticks
; i
++ )
257 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
266 johan@linkdata.se 2002-08-20:
267 The old code returned true if there were any
268 joystick capable drivers loaded (=always).
270 bool wxJoystick::IsOk() const
273 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
276 int wxJoystick::GetManufacturerId() const
279 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
285 int wxJoystick::GetProductId() const
288 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
294 wxString
wxJoystick::GetProductName() const
299 if (joyGetDevCaps(m_joystick
, &joyCaps
, sizeof(joyCaps
)) != JOYERR_NOERROR
)
300 return wxEmptyString
;
302 wxRegKey
key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
303 REGSTR_PATH_JOYCONFIG
, joyCaps
.szRegKey
, REGSTR_KEY_JOYCURR
));
305 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
306 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
309 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
310 REGSTR_PATH_JOYOEM
, str
.c_str()));
311 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
316 int wxJoystick::GetXMin() const
319 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
322 return joyCaps
.wXmin
;
325 int wxJoystick::GetYMin() const
328 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
331 return joyCaps
.wYmin
;
334 int wxJoystick::GetZMin() const
337 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
340 return joyCaps
.wZmin
;
343 int wxJoystick::GetXMax() const
346 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
349 return joyCaps
.wXmax
;
352 int wxJoystick::GetYMax() const
355 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
358 return joyCaps
.wYmax
;
361 int wxJoystick::GetZMax() const
364 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
367 return joyCaps
.wZmax
;
370 int wxJoystick::GetNumberButtons() const
373 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
376 return joyCaps
.wNumButtons
;
379 int wxJoystick::GetNumberAxes() const
381 #if defined(__WIN32__)
383 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
386 return joyCaps
.wNumAxes
;
392 int wxJoystick::GetMaxButtons() const
394 #if defined(__WIN32__)
396 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
399 return joyCaps
.wMaxButtons
;
405 int wxJoystick::GetMaxAxes() const
407 #if defined(__WIN32__)
409 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
412 return joyCaps
.wMaxAxes
;
418 int wxJoystick::GetPollingMin() const
421 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
424 return joyCaps
.wPeriodMin
;
427 int wxJoystick::GetPollingMax() const
430 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
433 return joyCaps
.wPeriodMax
;
436 int wxJoystick::GetRudderMin() const
438 #if defined(__WIN32__)
440 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
443 return joyCaps
.wRmin
;
449 int wxJoystick::GetRudderMax() const
451 #if defined(__WIN32__)
453 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
456 return joyCaps
.wRmax
;
462 int wxJoystick::GetUMin() const
464 #if defined(__WIN32__)
466 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
469 return joyCaps
.wUmin
;
475 int wxJoystick::GetUMax() const
477 #if defined(__WIN32__)
479 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
482 return joyCaps
.wUmax
;
488 int wxJoystick::GetVMin() const
490 #if defined(__WIN32__)
492 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
495 return joyCaps
.wVmin
;
501 int wxJoystick::GetVMax() const
503 #if defined(__WIN32__)
505 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
508 return joyCaps
.wVmax
;
515 bool wxJoystick::HasRudder() const
517 #if defined(__WIN32__)
519 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
522 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
528 bool wxJoystick::HasZ() const
530 #if defined(__WIN32__)
532 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
535 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
541 bool wxJoystick::HasU() const
543 #if defined(__WIN32__)
545 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
548 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
554 bool wxJoystick::HasV() const
556 #if defined(__WIN32__)
558 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
561 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
567 bool wxJoystick::HasPOV() const
569 #if defined(__WIN32__)
571 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
574 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
580 bool wxJoystick::HasPOV4Dir() const
582 #if defined(__WIN32__)
584 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
587 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
593 bool wxJoystick::HasPOVCTS() const
595 #if defined(__WIN32__)
597 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
600 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
607 ////////////////////////////////////////////////////////////////////////////
609 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
611 BOOL changed
= (pollingFreq
== 0);
612 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
613 return (res
== JOYERR_NOERROR
);
616 bool wxJoystick::ReleaseCapture()
618 MMRESULT res
= joyReleaseCapture(m_joystick
);
619 return (res
== JOYERR_NOERROR
);