1
/////////////////////////////////////////////////////////////////////////////
2
// Name: src/msw/joystick.cpp
3
// Purpose: wxJoystick class
4
// Author: Julian Smart
7
// Copyright: (c) Julian Smart
8
// Licence: wxWindows licence
9
/////////////////////////////////////////////////////////////////////////////
11
// For compilers that support precompilation, includes "wx.h".
12
#include "wx/wxprec.h"
20
#include "wx/joystick.h"
23
#include "wx/string.h"
24
#include "wx/window.h"
27
#include "wx/msw/private.h"
29
#if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
33
// Why doesn't BC++ have joyGetPosEx?
34
#if !defined(__WIN32__) || defined(__BORLANDC__)
35
#define NO_JOYGETPOSEX
38
#include "wx/msw/registry.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::GetPosition(unsigned axis) const
92
return GetPosition().x;
94
return GetPosition().y;
96
return GetZPosition();
98
return GetRudderPosition();
100
return GetUPosition();
102
return GetVPosition();
108
int wxJoystick::GetZPosition() const
111
MMRESULT res = joyGetPos(m_joystick, & joyInfo);
112
if (res == JOYERR_NOERROR )
113
return joyInfo.wZpos;
119
johan@linkdata.se 2002-08-20:
120
Return a bitmap with all button states in it,
121
like the GTK version does and Win32 does.
123
int wxJoystick::GetButtonState() const
126
MMRESULT res = joyGetPos(m_joystick, & joyInfo);
127
if (res == JOYERR_NOERROR )
129
return joyInfo.wButtons;
133
if (joyInfo.wButtons & JOY_BUTTON1)
134
buttons |= wxJOY_BUTTON1;
135
if (joyInfo.wButtons & JOY_BUTTON2)
136
buttons |= wxJOY_BUTTON2;
137
if (joyInfo.wButtons & JOY_BUTTON3)
138
buttons |= wxJOY_BUTTON3;
139
if (joyInfo.wButtons & JOY_BUTTON4)
140
buttons |= wxJOY_BUTTON4;
149
bool wxJoystick::GetButtonState(unsigned id) const
151
if (id > sizeof(int) * 8)
154
return (GetButtonState() & (1 << id)) != 0;
159
Returns -1 to signify error.
161
int wxJoystick::GetPOVPosition() const
163
#ifndef NO_JOYGETPOSEX
165
joyInfo.dwFlags = JOY_RETURNPOV;
166
joyInfo.dwSize = sizeof(joyInfo);
167
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
168
if (res == JOYERR_NOERROR )
170
return joyInfo.dwPOV;
180
johan@linkdata.se 2002-08-20:
181
Returns -1 to signify error.
183
int wxJoystick::GetPOVCTSPosition() const
185
#ifndef NO_JOYGETPOSEX
187
joyInfo.dwFlags = JOY_RETURNPOVCTS;
188
joyInfo.dwSize = sizeof(joyInfo);
189
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
190
if (res == JOYERR_NOERROR )
192
return joyInfo.dwPOV;
201
int wxJoystick::GetRudderPosition() const
203
#ifndef NO_JOYGETPOSEX
205
joyInfo.dwFlags = JOY_RETURNR;
206
joyInfo.dwSize = sizeof(joyInfo);
207
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
208
if (res == JOYERR_NOERROR )
210
return joyInfo.dwRpos;
219
int wxJoystick::GetUPosition() const
221
#ifndef NO_JOYGETPOSEX
223
joyInfo.dwFlags = JOY_RETURNU;
224
joyInfo.dwSize = sizeof(joyInfo);
225
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
226
if (res == JOYERR_NOERROR )
228
return joyInfo.dwUpos;
237
int wxJoystick::GetVPosition() const
239
#ifndef NO_JOYGETPOSEX
241
joyInfo.dwFlags = JOY_RETURNV;
242
joyInfo.dwSize = sizeof(joyInfo);
243
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
244
if (res == JOYERR_NOERROR )
246
return joyInfo.dwVpos;
255
int wxJoystick::GetMovementThreshold() const
258
MMRESULT res = joyGetThreshold(m_joystick, & thresh);
259
if (res == JOYERR_NOERROR )
267
void wxJoystick::SetMovementThreshold(int threshold)
269
UINT thresh = threshold;
270
joySetThreshold(m_joystick, thresh);
274
////////////////////////////////////////////////////////////////////////////
277
johan@linkdata.se 2002-08-20:
278
Now returns the number of connected, functioning
279
joysticks, as intended.
281
int wxJoystick::GetNumberJoysticks()
284
int i, maxsticks, actualsticks;
285
maxsticks = joyGetNumDevs();
287
for( i=0; i<maxsticks; i++ )
289
if( joyGetPos( i, & joyInfo ) == JOYERR_NOERROR )
298
johan@linkdata.se 2002-08-20:
299
The old code returned true if there were any
300
joystick capable drivers loaded (=always).
302
bool wxJoystick::IsOk() const
305
return (joyGetPos(m_joystick, & joyInfo) == JOYERR_NOERROR);
308
int wxJoystick::GetManufacturerId() const
311
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
317
int wxJoystick::GetProductId() const
320
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
326
wxString wxJoystick::GetProductName() const
331
if (joyGetDevCaps(m_joystick, &joyCaps, sizeof(joyCaps)) != JOYERR_NOERROR)
332
return wxEmptyString;
334
wxRegKey key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
335
REGSTR_PATH_JOYCONFIG, joyCaps.szRegKey, REGSTR_KEY_JOYCURR));
337
key1.QueryValue(wxString::Format(wxT("Joystick%d%s"),
338
m_joystick + 1, REGSTR_VAL_JOYOEMNAME),
341
wxRegKey key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
342
REGSTR_PATH_JOYOEM, str.c_str()));
343
key2.QueryValue(REGSTR_VAL_JOYOEMNAME, str);
348
int wxJoystick::GetXMin() const
351
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
354
return joyCaps.wXmin;
357
int wxJoystick::GetYMin() const
360
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
363
return joyCaps.wYmin;
366
int wxJoystick::GetZMin() const
369
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
372
return joyCaps.wZmin;
375
int wxJoystick::GetXMax() const
378
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
381
return joyCaps.wXmax;
384
int wxJoystick::GetYMax() const
387
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
390
return joyCaps.wYmax;
393
int wxJoystick::GetZMax() const
396
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
399
return joyCaps.wZmax;
402
int wxJoystick::GetNumberButtons() const
405
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
408
return joyCaps.wNumButtons;
411
int wxJoystick::GetNumberAxes() const
413
#if defined(__WIN32__)
415
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
418
return joyCaps.wNumAxes;
424
int wxJoystick::GetMaxButtons() const
426
#if defined(__WIN32__)
428
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
431
return joyCaps.wMaxButtons;
437
int wxJoystick::GetMaxAxes() const
439
#if defined(__WIN32__)
441
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
444
return joyCaps.wMaxAxes;
450
int wxJoystick::GetPollingMin() const
453
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
456
return joyCaps.wPeriodMin;
459
int wxJoystick::GetPollingMax() const
462
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
465
return joyCaps.wPeriodMax;
468
int wxJoystick::GetRudderMin() const
470
#if defined(__WIN32__)
472
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
475
return joyCaps.wRmin;
481
int wxJoystick::GetRudderMax() const
483
#if defined(__WIN32__)
485
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
488
return joyCaps.wRmax;
494
int wxJoystick::GetUMin() const
496
#if defined(__WIN32__)
498
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
501
return joyCaps.wUmin;
507
int wxJoystick::GetUMax() const
509
#if defined(__WIN32__)
511
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
514
return joyCaps.wUmax;
520
int wxJoystick::GetVMin() const
522
#if defined(__WIN32__)
524
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
527
return joyCaps.wVmin;
533
int wxJoystick::GetVMax() const
535
#if defined(__WIN32__)
537
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
540
return joyCaps.wVmax;
547
bool wxJoystick::HasRudder() const
549
#if defined(__WIN32__)
551
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
554
return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
560
bool wxJoystick::HasZ() const
562
#if defined(__WIN32__)
564
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
567
return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
573
bool wxJoystick::HasU() const
575
#if defined(__WIN32__)
577
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
580
return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
586
bool wxJoystick::HasV() const
588
#if defined(__WIN32__)
590
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
593
return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
599
bool wxJoystick::HasPOV() const
601
#if defined(__WIN32__)
603
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
606
return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
612
bool wxJoystick::HasPOV4Dir() const
614
#if defined(__WIN32__)
616
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
619
return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
625
bool wxJoystick::HasPOVCTS() const
627
#if defined(__WIN32__)
629
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
632
return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
639
////////////////////////////////////////////////////////////////////////////
641
bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
644
BOOL changed = (pollingFreq == 0);
645
MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed);
646
return (res == JOYERR_NOERROR);
649
wxUnusedVar(pollingFreq);
654
bool wxJoystick::ReleaseCapture()
656
MMRESULT res = joyReleaseCapture(m_joystick);
657
return (res == JOYERR_NOERROR);
660
#endif // wxUSE_JOYSTICK