]>
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"
22 #include "wx/string.h"
23 #include "wx/window.h"
24 #include "wx/msw/private.h"
26 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
30 // Why doesn't BC++ have joyGetPosEx?
31 #if !defined(__WIN32__) || defined(__BORLANDC__)
32 #define NO_JOYGETPOSEX
35 #include "wx/window.h"
36 #include "wx/msw/registry.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
43 ////////////////////////////////////////////////////////////////////////////
46 johan@linkdata.se 2002-08-20:
47 Now returns only valid, functioning
48 joysticks, counting from the first
49 available and upwards.
51 wxJoystick::wxJoystick(int joystick
)
56 maxsticks
= joyGetNumDevs();
57 for( i
=0; i
<maxsticks
; i
++ )
59 if( joyGetPos(i
, & joyInfo
) == JOYERR_NOERROR
)
63 /* Found the one we want, store actual OS id and return */
71 /* No such joystick, return ID 0 */
76 wxPoint
wxJoystick::GetPosition() const
79 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
80 if (res
== JOYERR_NOERROR
)
81 return wxPoint(joyInfo
.wXpos
, joyInfo
.wYpos
);
86 int wxJoystick::GetZPosition() const
89 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
90 if (res
== JOYERR_NOERROR
)
97 johan@linkdata.se 2002-08-20:
98 Return a bitmap with all button states in it,
99 like the GTK version does and Win32 does.
101 int wxJoystick::GetButtonState() const
104 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
105 if (res
== JOYERR_NOERROR
)
107 return joyInfo
.wButtons
;
111 if (joyInfo
.wButtons
& JOY_BUTTON1
)
112 buttons
|= wxJOY_BUTTON1
;
113 if (joyInfo
.wButtons
& JOY_BUTTON2
)
114 buttons
|= wxJOY_BUTTON2
;
115 if (joyInfo
.wButtons
& JOY_BUTTON3
)
116 buttons
|= wxJOY_BUTTON3
;
117 if (joyInfo
.wButtons
& JOY_BUTTON4
)
118 buttons
|= wxJOY_BUTTON4
;
129 Returns -1 to signify error.
131 int wxJoystick::GetPOVPosition() const
133 #ifndef NO_JOYGETPOSEX
135 joyInfo
.dwFlags
= JOY_RETURNPOV
;
136 joyInfo
.dwSize
= sizeof(joyInfo
);
137 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
138 if (res
== JOYERR_NOERROR
)
140 return joyInfo
.dwPOV
;
150 johan@linkdata.se 2002-08-20:
151 Returns -1 to signify error.
153 int wxJoystick::GetPOVCTSPosition() const
155 #ifndef NO_JOYGETPOSEX
157 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
158 joyInfo
.dwSize
= sizeof(joyInfo
);
159 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
160 if (res
== JOYERR_NOERROR
)
162 return joyInfo
.dwPOV
;
171 int wxJoystick::GetRudderPosition() const
173 #ifndef NO_JOYGETPOSEX
175 joyInfo
.dwFlags
= JOY_RETURNR
;
176 joyInfo
.dwSize
= sizeof(joyInfo
);
177 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
178 if (res
== JOYERR_NOERROR
)
180 return joyInfo
.dwRpos
;
189 int wxJoystick::GetUPosition() const
191 #ifndef NO_JOYGETPOSEX
193 joyInfo
.dwFlags
= JOY_RETURNU
;
194 joyInfo
.dwSize
= sizeof(joyInfo
);
195 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
196 if (res
== JOYERR_NOERROR
)
198 return joyInfo
.dwUpos
;
207 int wxJoystick::GetVPosition() const
209 #ifndef NO_JOYGETPOSEX
211 joyInfo
.dwFlags
= JOY_RETURNV
;
212 joyInfo
.dwSize
= sizeof(joyInfo
);
213 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
214 if (res
== JOYERR_NOERROR
)
216 return joyInfo
.dwVpos
;
225 int wxJoystick::GetMovementThreshold() const
228 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
229 if (res
== JOYERR_NOERROR
)
237 void wxJoystick::SetMovementThreshold(int threshold
)
239 UINT thresh
= threshold
;
240 joySetThreshold(m_joystick
, thresh
);
244 ////////////////////////////////////////////////////////////////////////////
247 johan@linkdata.se 2002-08-20:
248 Now returns the number of connected, functioning
249 joysticks, as intended.
251 int wxJoystick::GetNumberJoysticks()
254 int i
, maxsticks
, actualsticks
;
255 maxsticks
= joyGetNumDevs();
257 for( i
=0; i
<maxsticks
; i
++ )
259 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
268 johan@linkdata.se 2002-08-20:
269 The old code returned true if there were any
270 joystick capable drivers loaded (=always).
272 bool wxJoystick::IsOk() const
275 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
278 int wxJoystick::GetManufacturerId() const
281 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
287 int wxJoystick::GetProductId() const
290 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
296 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
));
307 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
308 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
311 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
312 REGSTR_PATH_JOYOEM
, str
.c_str()));
313 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
318 int wxJoystick::GetXMin() const
321 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
324 return joyCaps
.wXmin
;
327 int wxJoystick::GetYMin() const
330 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
333 return joyCaps
.wYmin
;
336 int wxJoystick::GetZMin() const
339 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
342 return joyCaps
.wZmin
;
345 int wxJoystick::GetXMax() const
348 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
351 return joyCaps
.wXmax
;
354 int wxJoystick::GetYMax() const
357 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
360 return joyCaps
.wYmax
;
363 int wxJoystick::GetZMax() const
366 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
369 return joyCaps
.wZmax
;
372 int wxJoystick::GetNumberButtons() const
375 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
378 return joyCaps
.wNumButtons
;
381 int wxJoystick::GetNumberAxes() const
383 #if defined(__WIN32__)
385 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
388 return joyCaps
.wNumAxes
;
394 int wxJoystick::GetMaxButtons() const
396 #if defined(__WIN32__)
398 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
401 return joyCaps
.wMaxButtons
;
407 int wxJoystick::GetMaxAxes() const
409 #if defined(__WIN32__)
411 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
414 return joyCaps
.wMaxAxes
;
420 int wxJoystick::GetPollingMin() const
423 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
426 return joyCaps
.wPeriodMin
;
429 int wxJoystick::GetPollingMax() const
432 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
435 return joyCaps
.wPeriodMax
;
438 int wxJoystick::GetRudderMin() const
440 #if defined(__WIN32__)
442 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
445 return joyCaps
.wRmin
;
451 int wxJoystick::GetRudderMax() const
453 #if defined(__WIN32__)
455 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
458 return joyCaps
.wRmax
;
464 int wxJoystick::GetUMin() const
466 #if defined(__WIN32__)
468 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
471 return joyCaps
.wUmin
;
477 int wxJoystick::GetUMax() const
479 #if defined(__WIN32__)
481 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
484 return joyCaps
.wUmax
;
490 int wxJoystick::GetVMin() const
492 #if defined(__WIN32__)
494 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
497 return joyCaps
.wVmin
;
503 int wxJoystick::GetVMax() const
505 #if defined(__WIN32__)
507 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
510 return joyCaps
.wVmax
;
517 bool wxJoystick::HasRudder() const
519 #if defined(__WIN32__)
521 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
524 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
530 bool wxJoystick::HasZ() const
532 #if defined(__WIN32__)
534 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
537 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
543 bool wxJoystick::HasU() const
545 #if defined(__WIN32__)
547 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
550 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
556 bool wxJoystick::HasV() const
558 #if defined(__WIN32__)
560 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
563 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
569 bool wxJoystick::HasPOV() const
571 #if defined(__WIN32__)
573 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
576 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
582 bool wxJoystick::HasPOV4Dir() const
584 #if defined(__WIN32__)
586 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
589 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
595 bool wxJoystick::HasPOVCTS() const
597 #if defined(__WIN32__)
599 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
602 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
609 ////////////////////////////////////////////////////////////////////////////
611 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
613 BOOL changed
= (pollingFreq
== 0);
614 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
615 return (res
== JOYERR_NOERROR
);
618 bool wxJoystick::ReleaseCapture()
620 MMRESULT res
= joyReleaseCapture(m_joystick
);
621 return (res
== JOYERR_NOERROR
);
624 #endif // wxUSE_JOYSTICK