]>
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::GetPosition(unsigned axis
) const
93 return GetPosition().x
;
95 return GetPosition().y
;
97 return GetZPosition();
99 return GetRudderPosition();
101 return GetUPosition();
103 return GetVPosition();
109 int wxJoystick::GetZPosition() const
112 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
113 if (res
== JOYERR_NOERROR
)
114 return joyInfo
.wZpos
;
120 johan@linkdata.se 2002-08-20:
121 Return a bitmap with all button states in it,
122 like the GTK version does and Win32 does.
124 int wxJoystick::GetButtonState() const
127 MMRESULT res
= joyGetPos(m_joystick
, & joyInfo
);
128 if (res
== JOYERR_NOERROR
)
130 return joyInfo
.wButtons
;
134 if (joyInfo
.wButtons
& JOY_BUTTON1
)
135 buttons
|= wxJOY_BUTTON1
;
136 if (joyInfo
.wButtons
& JOY_BUTTON2
)
137 buttons
|= wxJOY_BUTTON2
;
138 if (joyInfo
.wButtons
& JOY_BUTTON3
)
139 buttons
|= wxJOY_BUTTON3
;
140 if (joyInfo
.wButtons
& JOY_BUTTON4
)
141 buttons
|= wxJOY_BUTTON4
;
150 bool wxJoystick::GetButtonState(unsigned id
) const
152 if (id
> sizeof(int) * 8)
155 return (GetButtonState() & (1 << id
)) != 0;
160 Returns -1 to signify error.
162 int wxJoystick::GetPOVPosition() const
164 #ifndef NO_JOYGETPOSEX
166 joyInfo
.dwFlags
= JOY_RETURNPOV
;
167 joyInfo
.dwSize
= sizeof(joyInfo
);
168 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
169 if (res
== JOYERR_NOERROR
)
171 return joyInfo
.dwPOV
;
181 johan@linkdata.se 2002-08-20:
182 Returns -1 to signify error.
184 int wxJoystick::GetPOVCTSPosition() const
186 #ifndef NO_JOYGETPOSEX
188 joyInfo
.dwFlags
= JOY_RETURNPOVCTS
;
189 joyInfo
.dwSize
= sizeof(joyInfo
);
190 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
191 if (res
== JOYERR_NOERROR
)
193 return joyInfo
.dwPOV
;
202 int wxJoystick::GetRudderPosition() const
204 #ifndef NO_JOYGETPOSEX
206 joyInfo
.dwFlags
= JOY_RETURNR
;
207 joyInfo
.dwSize
= sizeof(joyInfo
);
208 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
209 if (res
== JOYERR_NOERROR
)
211 return joyInfo
.dwRpos
;
220 int wxJoystick::GetUPosition() const
222 #ifndef NO_JOYGETPOSEX
224 joyInfo
.dwFlags
= JOY_RETURNU
;
225 joyInfo
.dwSize
= sizeof(joyInfo
);
226 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
227 if (res
== JOYERR_NOERROR
)
229 return joyInfo
.dwUpos
;
238 int wxJoystick::GetVPosition() const
240 #ifndef NO_JOYGETPOSEX
242 joyInfo
.dwFlags
= JOY_RETURNV
;
243 joyInfo
.dwSize
= sizeof(joyInfo
);
244 MMRESULT res
= joyGetPosEx(m_joystick
, & joyInfo
);
245 if (res
== JOYERR_NOERROR
)
247 return joyInfo
.dwVpos
;
256 int wxJoystick::GetMovementThreshold() const
259 MMRESULT res
= joyGetThreshold(m_joystick
, & thresh
);
260 if (res
== JOYERR_NOERROR
)
268 void wxJoystick::SetMovementThreshold(int threshold
)
270 UINT thresh
= threshold
;
271 joySetThreshold(m_joystick
, thresh
);
275 ////////////////////////////////////////////////////////////////////////////
278 johan@linkdata.se 2002-08-20:
279 Now returns the number of connected, functioning
280 joysticks, as intended.
282 int wxJoystick::GetNumberJoysticks()
285 int i
, maxsticks
, actualsticks
;
286 maxsticks
= joyGetNumDevs();
288 for( i
=0; i
<maxsticks
; i
++ )
290 if( joyGetPos( i
, & joyInfo
) == JOYERR_NOERROR
)
299 johan@linkdata.se 2002-08-20:
300 The old code returned true if there were any
301 joystick capable drivers loaded (=always).
303 bool wxJoystick::IsOk() const
306 return (joyGetPos(m_joystick
, & joyInfo
) == JOYERR_NOERROR
);
309 int wxJoystick::GetManufacturerId() const
312 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
318 int wxJoystick::GetProductId() const
321 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
327 wxString
wxJoystick::GetProductName() const
332 if (joyGetDevCaps(m_joystick
, &joyCaps
, sizeof(joyCaps
)) != JOYERR_NOERROR
)
333 return wxEmptyString
;
335 wxRegKey
key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
336 REGSTR_PATH_JOYCONFIG
, joyCaps
.szRegKey
, REGSTR_KEY_JOYCURR
));
338 key1
.QueryValue(wxString::Format(wxT("Joystick%d%s"),
339 m_joystick
+ 1, REGSTR_VAL_JOYOEMNAME
),
342 wxRegKey
key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
343 REGSTR_PATH_JOYOEM
, str
.c_str()));
344 key2
.QueryValue(REGSTR_VAL_JOYOEMNAME
, str
);
349 int wxJoystick::GetXMin() const
352 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
355 return joyCaps
.wXmin
;
358 int wxJoystick::GetYMin() const
361 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
364 return joyCaps
.wYmin
;
367 int wxJoystick::GetZMin() const
370 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
373 return joyCaps
.wZmin
;
376 int wxJoystick::GetXMax() const
379 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
382 return joyCaps
.wXmax
;
385 int wxJoystick::GetYMax() const
388 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
391 return joyCaps
.wYmax
;
394 int wxJoystick::GetZMax() const
397 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
400 return joyCaps
.wZmax
;
403 int wxJoystick::GetNumberButtons() const
406 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
409 return joyCaps
.wNumButtons
;
412 int wxJoystick::GetNumberAxes() const
414 #if defined(__WIN32__)
416 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
419 return joyCaps
.wNumAxes
;
425 int wxJoystick::GetMaxButtons() const
427 #if defined(__WIN32__)
429 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
432 return joyCaps
.wMaxButtons
;
438 int wxJoystick::GetMaxAxes() const
440 #if defined(__WIN32__)
442 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
445 return joyCaps
.wMaxAxes
;
451 int wxJoystick::GetPollingMin() const
454 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
457 return joyCaps
.wPeriodMin
;
460 int wxJoystick::GetPollingMax() const
463 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
466 return joyCaps
.wPeriodMax
;
469 int wxJoystick::GetRudderMin() const
471 #if defined(__WIN32__)
473 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
476 return joyCaps
.wRmin
;
482 int wxJoystick::GetRudderMax() const
484 #if defined(__WIN32__)
486 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
489 return joyCaps
.wRmax
;
495 int wxJoystick::GetUMin() const
497 #if defined(__WIN32__)
499 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
502 return joyCaps
.wUmin
;
508 int wxJoystick::GetUMax() const
510 #if defined(__WIN32__)
512 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
515 return joyCaps
.wUmax
;
521 int wxJoystick::GetVMin() const
523 #if defined(__WIN32__)
525 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
528 return joyCaps
.wVmin
;
534 int wxJoystick::GetVMax() const
536 #if defined(__WIN32__)
538 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
541 return joyCaps
.wVmax
;
548 bool wxJoystick::HasRudder() const
550 #if defined(__WIN32__)
552 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
555 return ((joyCaps
.wCaps
& JOYCAPS_HASR
) == JOYCAPS_HASR
);
561 bool wxJoystick::HasZ() const
563 #if defined(__WIN32__)
565 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
568 return ((joyCaps
.wCaps
& JOYCAPS_HASZ
) == JOYCAPS_HASZ
);
574 bool wxJoystick::HasU() const
576 #if defined(__WIN32__)
578 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
581 return ((joyCaps
.wCaps
& JOYCAPS_HASU
) == JOYCAPS_HASU
);
587 bool wxJoystick::HasV() const
589 #if defined(__WIN32__)
591 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
594 return ((joyCaps
.wCaps
& JOYCAPS_HASV
) == JOYCAPS_HASV
);
600 bool wxJoystick::HasPOV() const
602 #if defined(__WIN32__)
604 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
607 return ((joyCaps
.wCaps
& JOYCAPS_HASPOV
) == JOYCAPS_HASPOV
);
613 bool wxJoystick::HasPOV4Dir() const
615 #if defined(__WIN32__)
617 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
620 return ((joyCaps
.wCaps
& JOYCAPS_POV4DIR
) == JOYCAPS_POV4DIR
);
626 bool wxJoystick::HasPOVCTS() const
628 #if defined(__WIN32__)
630 if (joyGetDevCaps(m_joystick
, & joyCaps
, sizeof(JOYCAPS
)) != JOYERR_NOERROR
)
633 return ((joyCaps
.wCaps
& JOYCAPS_POVCTS
) == JOYCAPS_POVCTS
);
640 ////////////////////////////////////////////////////////////////////////////
642 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
645 BOOL changed
= (pollingFreq
== 0);
646 MMRESULT res
= joySetCapture((HWND
) win
->GetHWND(), m_joystick
, pollingFreq
, changed
);
647 return (res
== JOYERR_NOERROR
);
650 wxUnusedVar(pollingFreq
);
655 bool wxJoystick::ReleaseCapture()
657 MMRESULT res
= joyReleaseCapture(m_joystick
);
658 return (res
== JOYERR_NOERROR
);
661 #endif // wxUSE_JOYSTICK