1
/////////////////////////////////////////////////////////////////////////////
2
// Name: src/msw/joystick.cpp
3
// Purpose: wxJoystick class
4
// Author: Julian Smart
7
// RCS-ID: $Id: joystick.cpp,v 1.23.2.2 2006/01/25 19:41:08 JS Exp $
8
// Copyright: (c) Julian Smart
9
// Licence: wxWindows licence
10
/////////////////////////////////////////////////////////////////////////////
12
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13
#pragma implementation "joystick.h"
16
// For compilers that support precompilation, includes "wx.h".
17
#include "wx/wxprec.h"
25
#include "wx/joystick.h"
26
#include "wx/string.h"
27
#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/window.h"
40
#include "wx/msw/registry.h"
44
IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
47
////////////////////////////////////////////////////////////////////////////
50
johan@linkdata.se 2002-08-20:
51
Now returns only valid, functioning
52
joysticks, counting from the first
53
available and upwards.
55
wxJoystick::wxJoystick(int joystick)
60
maxsticks = joyGetNumDevs();
61
for( i=0; i<maxsticks; i++ )
63
if( joyGetPos(i, & joyInfo) == JOYERR_NOERROR )
67
/* Found the one we want, store actual OS id and return */
75
/* No such joystick, return ID 0 */
80
wxPoint wxJoystick::GetPosition() const
83
MMRESULT res = joyGetPos(m_joystick, & joyInfo);
84
if (res == JOYERR_NOERROR )
85
return wxPoint(joyInfo.wXpos, joyInfo.wYpos);
90
int wxJoystick::GetZPosition() const
93
MMRESULT res = joyGetPos(m_joystick, & joyInfo);
94
if (res == JOYERR_NOERROR )
101
johan@linkdata.se 2002-08-20:
102
Return a bitmap with all button states in it,
103
like the GTK version does and Win32 does.
105
int wxJoystick::GetButtonState() const
108
MMRESULT res = joyGetPos(m_joystick, & joyInfo);
109
if (res == JOYERR_NOERROR )
111
return joyInfo.wButtons;
115
if (joyInfo.wButtons & JOY_BUTTON1)
116
buttons |= wxJOY_BUTTON1;
117
if (joyInfo.wButtons & JOY_BUTTON2)
118
buttons |= wxJOY_BUTTON2;
119
if (joyInfo.wButtons & JOY_BUTTON3)
120
buttons |= wxJOY_BUTTON3;
121
if (joyInfo.wButtons & JOY_BUTTON4)
122
buttons |= wxJOY_BUTTON4;
133
Returns -1 to signify error.
135
int wxJoystick::GetPOVPosition() const
137
#ifndef NO_JOYGETPOSEX
139
joyInfo.dwFlags = JOY_RETURNPOV;
140
joyInfo.dwSize = sizeof(joyInfo);
141
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
142
if (res == JOYERR_NOERROR )
144
return joyInfo.dwPOV;
154
johan@linkdata.se 2002-08-20:
155
Returns -1 to signify error.
157
int wxJoystick::GetPOVCTSPosition() const
159
#ifndef NO_JOYGETPOSEX
161
joyInfo.dwFlags = JOY_RETURNPOVCTS;
162
joyInfo.dwSize = sizeof(joyInfo);
163
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
164
if (res == JOYERR_NOERROR )
166
return joyInfo.dwPOV;
175
int wxJoystick::GetRudderPosition() const
177
#ifndef NO_JOYGETPOSEX
179
joyInfo.dwFlags = JOY_RETURNR;
180
joyInfo.dwSize = sizeof(joyInfo);
181
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
182
if (res == JOYERR_NOERROR )
184
return joyInfo.dwRpos;
193
int wxJoystick::GetUPosition() const
195
#ifndef NO_JOYGETPOSEX
197
joyInfo.dwFlags = JOY_RETURNU;
198
joyInfo.dwSize = sizeof(joyInfo);
199
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
200
if (res == JOYERR_NOERROR )
202
return joyInfo.dwUpos;
211
int wxJoystick::GetVPosition() const
213
#ifndef NO_JOYGETPOSEX
215
joyInfo.dwFlags = JOY_RETURNV;
216
joyInfo.dwSize = sizeof(joyInfo);
217
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
218
if (res == JOYERR_NOERROR )
220
return joyInfo.dwVpos;
229
int wxJoystick::GetMovementThreshold() const
232
MMRESULT res = joyGetThreshold(m_joystick, & thresh);
233
if (res == JOYERR_NOERROR )
241
void wxJoystick::SetMovementThreshold(int threshold)
243
UINT thresh = threshold;
244
joySetThreshold(m_joystick, thresh);
248
////////////////////////////////////////////////////////////////////////////
251
johan@linkdata.se 2002-08-20:
252
Now returns the number of connected, functioning
253
joysticks, as intended.
255
int wxJoystick::GetNumberJoysticks()
258
int i, maxsticks, actualsticks;
259
maxsticks = joyGetNumDevs();
261
for( i=0; i<maxsticks; i++ )
263
if( joyGetPos( i, & joyInfo ) == JOYERR_NOERROR )
272
johan@linkdata.se 2002-08-20:
273
The old code returned true if there were any
274
joystick capable drivers loaded (=always).
276
bool wxJoystick::IsOk() const
279
return (joyGetPos(m_joystick, & joyInfo) == JOYERR_NOERROR);
282
int wxJoystick::GetManufacturerId() const
285
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
291
int wxJoystick::GetProductId() const
294
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
300
wxString wxJoystick::GetProductName() const
303
return wxEmptyString;
306
if (joyGetDevCaps(m_joystick, &joyCaps, sizeof(joyCaps)) != JOYERR_NOERROR)
307
return wxEmptyString;
309
wxRegKey key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
310
REGSTR_PATH_JOYCONFIG, joyCaps.szRegKey, REGSTR_KEY_JOYCURR));
313
key1.QueryValue(wxString::Format(wxT("Joystick%d%s"),
314
m_joystick + 1, REGSTR_VAL_JOYOEMNAME),
317
wxRegKey key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
318
REGSTR_PATH_JOYOEM, str.c_str()));
319
key2.QueryValue(REGSTR_VAL_JOYOEMNAME, str);
325
int wxJoystick::GetXMin() const
328
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
331
return joyCaps.wXmin;
334
int wxJoystick::GetYMin() const
337
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
340
return joyCaps.wYmin;
343
int wxJoystick::GetZMin() const
346
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
349
return joyCaps.wZmin;
352
int wxJoystick::GetXMax() const
355
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
358
return joyCaps.wXmax;
361
int wxJoystick::GetYMax() const
364
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
367
return joyCaps.wYmax;
370
int wxJoystick::GetZMax() const
373
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
376
return joyCaps.wZmax;
379
int wxJoystick::GetNumberButtons() const
382
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
385
return joyCaps.wNumButtons;
388
int wxJoystick::GetNumberAxes() const
390
#if defined(__WIN32__)
392
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
395
return joyCaps.wNumAxes;
401
int wxJoystick::GetMaxButtons() const
403
#if defined(__WIN32__)
405
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
408
return joyCaps.wMaxButtons;
414
int wxJoystick::GetMaxAxes() const
416
#if defined(__WIN32__)
418
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
421
return joyCaps.wMaxAxes;
427
int wxJoystick::GetPollingMin() const
430
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
433
return joyCaps.wPeriodMin;
436
int wxJoystick::GetPollingMax() const
439
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
442
return joyCaps.wPeriodMax;
445
int wxJoystick::GetRudderMin() const
447
#if defined(__WIN32__)
449
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
452
return joyCaps.wRmin;
458
int wxJoystick::GetRudderMax() const
460
#if defined(__WIN32__)
462
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
465
return joyCaps.wRmax;
471
int wxJoystick::GetUMin() const
473
#if defined(__WIN32__)
475
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
478
return joyCaps.wUmin;
484
int wxJoystick::GetUMax() const
486
#if defined(__WIN32__)
488
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
491
return joyCaps.wUmax;
497
int wxJoystick::GetVMin() const
499
#if defined(__WIN32__)
501
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
504
return joyCaps.wVmin;
510
int wxJoystick::GetVMax() const
512
#if defined(__WIN32__)
514
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
517
return joyCaps.wVmax;
524
bool wxJoystick::HasRudder() const
526
#if defined(__WIN32__)
528
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
531
return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
537
bool wxJoystick::HasZ() const
539
#if defined(__WIN32__)
541
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
544
return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
550
bool wxJoystick::HasU() const
552
#if defined(__WIN32__)
554
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
557
return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
563
bool wxJoystick::HasV() const
565
#if defined(__WIN32__)
567
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
570
return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
576
bool wxJoystick::HasPOV() const
578
#if defined(__WIN32__)
580
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
583
return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
589
bool wxJoystick::HasPOV4Dir() const
591
#if defined(__WIN32__)
593
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
596
return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
602
bool wxJoystick::HasPOVCTS() const
604
#if defined(__WIN32__)
606
if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
609
return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
616
////////////////////////////////////////////////////////////////////////////
618
bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
620
BOOL changed = (pollingFreq == 0);
621
MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed);
622
return (res == JOYERR_NOERROR);
625
bool wxJoystick::ReleaseCapture()
627
MMRESULT res = joyReleaseCapture(m_joystick);
628
return (res == JOYERR_NOERROR);
631
#endif // wxUSE_JOYSTICK