]> git.saurik.com Git - wxWidgets.git/blame - src/msw/joystick.cpp
bump subrelease number
[wxWidgets.git] / src / msw / joystick.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
7e044896 2// Name: src/msw/joystick.cpp
2bda0e17
KB
3// Purpose: wxJoystick class
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
a3b46648 15#ifdef __BORLANDC__
df91131c 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
7e044896
WS
19#if wxUSE_JOYSTICK
20
21#include "wx/joystick.h"
df91131c
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/string.h"
cdccdfab 25 #include "wx/window.h"
df91131c
WS
26#endif
27
0c589ad0 28#include "wx/msw/private.h"
2bda0e17 29
ae090fdb 30#if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
c42404a5 31 #include <mmsystem.h>
2bda0e17
KB
32#endif
33
2bda0e17 34// Why doesn't BC++ have joyGetPosEx?
b39dbf34 35#if !defined(__WIN32__) || defined(__BORLANDC__)
2bda0e17
KB
36#define NO_JOYGETPOSEX
37#endif
38
c9ee46c3 39#include "wx/msw/registry.h"
2bda0e17 40
c9ee46c3
VZ
41#include <regstr.h>
42
2bda0e17
KB
43IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
44
45// Attributes
46////////////////////////////////////////////////////////////////////////////
47
131f9d9b 48/**
598ddd96
WS
49 johan@linkdata.se 2002-08-20:
50 Now returns only valid, functioning
51 joysticks, counting from the first
52 available and upwards.
131f9d9b 53*/
c892a77e 54wxJoystick::wxJoystick(int joystick)
131f9d9b
JS
55{
56 JOYINFO joyInfo;
598ddd96
WS
57 int i, maxsticks;
58
59 maxsticks = joyGetNumDevs();
60 for( i=0; i<maxsticks; i++ )
61 {
62 if( joyGetPos(i, & joyInfo) == JOYERR_NOERROR )
63 {
64 if( !joystick )
65 {
66 /* Found the one we want, store actual OS id and return */
67 m_joystick = i;
68 return;
69 }
70 joystick --;
71 }
72 }
73
74 /* No such joystick, return ID 0 */
75 m_joystick = 0;
76 return;
ae199d6e 77}
131f9d9b 78
c42404a5 79wxPoint wxJoystick::GetPosition() const
2bda0e17
KB
80{
81 JOYINFO joyInfo;
82 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
83 if (res == JOYERR_NOERROR )
84 return wxPoint(joyInfo.wXpos, joyInfo.wYpos);
85 else
c47addef 86 return wxPoint(0,0);
2bda0e17
KB
87}
88
c42404a5 89int wxJoystick::GetZPosition() const
2bda0e17
KB
90{
91 JOYINFO joyInfo;
92 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
93 if (res == JOYERR_NOERROR )
94 return joyInfo.wZpos;
95 else
96 return 0;
97}
98
131f9d9b 99/**
598ddd96
WS
100 johan@linkdata.se 2002-08-20:
101 Return a bitmap with all button states in it,
102 like the GTK version does and Win32 does.
131f9d9b 103*/
c42404a5 104int wxJoystick::GetButtonState() const
2bda0e17
KB
105{
106 JOYINFO joyInfo;
107 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
108 if (res == JOYERR_NOERROR )
109 {
598ddd96 110 return joyInfo.wButtons;
131f9d9b 111#if 0
2bda0e17
KB
112 int buttons = 0;
113
114 if (joyInfo.wButtons & JOY_BUTTON1)
115 buttons |= wxJOY_BUTTON1;
116 if (joyInfo.wButtons & JOY_BUTTON2)
117 buttons |= wxJOY_BUTTON2;
118 if (joyInfo.wButtons & JOY_BUTTON3)
119 buttons |= wxJOY_BUTTON3;
120 if (joyInfo.wButtons & JOY_BUTTON4)
121 buttons |= wxJOY_BUTTON4;
131f9d9b 122
2bda0e17 123 return buttons;
131f9d9b 124#endif
2bda0e17
KB
125 }
126 else
127 return 0;
128}
129
131f9d9b 130/**
598ddd96
WS
131 JLI 2002-08-20:
132 Returns -1 to signify error.
131f9d9b 133*/
c42404a5 134int wxJoystick::GetPOVPosition() const
2bda0e17
KB
135{
136#ifndef NO_JOYGETPOSEX
137 JOYINFOEX joyInfo;
138 joyInfo.dwFlags = JOY_RETURNPOV;
b9f933ab 139 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
140 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
141 if (res == JOYERR_NOERROR )
142 {
143 return joyInfo.dwPOV;
144 }
145 else
131f9d9b 146 return -1;
2bda0e17 147#else
131f9d9b 148 return -1;
2bda0e17
KB
149#endif
150}
151
131f9d9b 152/**
598ddd96
WS
153 johan@linkdata.se 2002-08-20:
154 Returns -1 to signify error.
131f9d9b 155*/
c42404a5 156int wxJoystick::GetPOVCTSPosition() const
2bda0e17
KB
157{
158#ifndef NO_JOYGETPOSEX
159 JOYINFOEX joyInfo;
160 joyInfo.dwFlags = JOY_RETURNPOVCTS;
b9f933ab 161 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
162 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
163 if (res == JOYERR_NOERROR )
164 {
165 return joyInfo.dwPOV;
166 }
167 else
131f9d9b 168 return -1;
2bda0e17 169#else
131f9d9b 170 return -1;
2bda0e17
KB
171#endif
172}
173
c42404a5 174int wxJoystick::GetRudderPosition() const
2bda0e17
KB
175{
176#ifndef NO_JOYGETPOSEX
177 JOYINFOEX joyInfo;
178 joyInfo.dwFlags = JOY_RETURNR;
b9f933ab 179 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
180 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
181 if (res == JOYERR_NOERROR )
182 {
183 return joyInfo.dwRpos;
184 }
185 else
186 return 0;
187#else
188 return 0;
189#endif
190}
191
c42404a5 192int wxJoystick::GetUPosition() const
2bda0e17
KB
193{
194#ifndef NO_JOYGETPOSEX
195 JOYINFOEX joyInfo;
196 joyInfo.dwFlags = JOY_RETURNU;
b9f933ab 197 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
198 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
199 if (res == JOYERR_NOERROR )
200 {
201 return joyInfo.dwUpos;
202 }
203 else
204 return 0;
205#else
206 return 0;
207#endif
208}
209
c42404a5 210int wxJoystick::GetVPosition() const
2bda0e17
KB
211{
212#ifndef NO_JOYGETPOSEX
213 JOYINFOEX joyInfo;
214 joyInfo.dwFlags = JOY_RETURNV;
b9f933ab 215 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
216 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
217 if (res == JOYERR_NOERROR )
218 {
219 return joyInfo.dwVpos;
220 }
221 else
222 return 0;
223#else
224 return 0;
225#endif
226}
227
c42404a5 228int wxJoystick::GetMovementThreshold() const
2bda0e17
KB
229{
230 UINT thresh = 0;
231 MMRESULT res = joyGetThreshold(m_joystick, & thresh);
232 if (res == JOYERR_NOERROR )
233 {
234 return thresh;
235 }
236 else
237 return 0;
238}
239
240void wxJoystick::SetMovementThreshold(int threshold)
241{
242 UINT thresh = threshold;
243 joySetThreshold(m_joystick, thresh);
244}
245
246// Capabilities
247////////////////////////////////////////////////////////////////////////////
248
131f9d9b 249/**
598ddd96
WS
250 johan@linkdata.se 2002-08-20:
251 Now returns the number of connected, functioning
252 joysticks, as intended.
131f9d9b
JS
253*/
254int wxJoystick::GetNumberJoysticks()
2bda0e17
KB
255{
256 JOYINFO joyInfo;
598ddd96
WS
257 int i, maxsticks, actualsticks;
258 maxsticks = joyGetNumDevs();
259 actualsticks = 0;
260 for( i=0; i<maxsticks; i++ )
261 {
262 if( joyGetPos( i, & joyInfo ) == JOYERR_NOERROR )
263 {
264 actualsticks ++;
265 }
266 }
131f9d9b 267 return actualsticks;
2bda0e17
KB
268}
269
131f9d9b 270/**
598ddd96
WS
271 johan@linkdata.se 2002-08-20:
272 The old code returned true if there were any
273 joystick capable drivers loaded (=always).
131f9d9b
JS
274*/
275bool wxJoystick::IsOk() const
2bda0e17 276{
131f9d9b
JS
277 JOYINFO joyInfo;
278 return (joyGetPos(m_joystick, & joyInfo) == JOYERR_NOERROR);
2bda0e17
KB
279}
280
c42404a5 281int wxJoystick::GetManufacturerId() const
2bda0e17
KB
282{
283 JOYCAPS joyCaps;
284 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
285 return 0;
286 else
287 return joyCaps.wMid;
288}
289
c42404a5 290int wxJoystick::GetProductId() const
2bda0e17
KB
291{
292 JOYCAPS joyCaps;
293 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
294 return 0;
295 else
296 return joyCaps.wPid;
297}
298
c42404a5 299wxString wxJoystick::GetProductName() const
2bda0e17 300{
4708068c
JS
301 wxString str;
302#ifndef __WINE__
2bda0e17 303 JOYCAPS joyCaps;
c9ee46c3 304 if (joyGetDevCaps(m_joystick, &joyCaps, sizeof(joyCaps)) != JOYERR_NOERROR)
2b5f62a0 305 return wxEmptyString;
c9ee46c3
VZ
306
307 wxRegKey key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
308 REGSTR_PATH_JOYCONFIG, joyCaps.szRegKey, REGSTR_KEY_JOYCURR));
309
c9ee46c3
VZ
310 key1.QueryValue(wxString::Format(wxT("Joystick%d%s"),
311 m_joystick + 1, REGSTR_VAL_JOYOEMNAME),
312 str);
313
314 wxRegKey key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
315 REGSTR_PATH_JOYOEM, str.c_str()));
316 key2.QueryValue(REGSTR_VAL_JOYOEMNAME, str);
4708068c 317#endif
c9ee46c3 318 return str;
2bda0e17
KB
319}
320
c42404a5 321int wxJoystick::GetXMin() const
2bda0e17
KB
322{
323 JOYCAPS joyCaps;
324 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
325 return 0;
326 else
327 return joyCaps.wXmin;
328}
329
c42404a5 330int wxJoystick::GetYMin() const
2bda0e17
KB
331{
332 JOYCAPS joyCaps;
333 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
334 return 0;
335 else
336 return joyCaps.wYmin;
337}
338
c42404a5 339int wxJoystick::GetZMin() const
2bda0e17
KB
340{
341 JOYCAPS joyCaps;
342 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
343 return 0;
344 else
345 return joyCaps.wZmin;
346}
347
c42404a5 348int wxJoystick::GetXMax() const
2bda0e17
KB
349{
350 JOYCAPS joyCaps;
351 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
352 return 0;
353 else
354 return joyCaps.wXmax;
355}
356
c42404a5 357int wxJoystick::GetYMax() const
2bda0e17
KB
358{
359 JOYCAPS joyCaps;
360 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
361 return 0;
362 else
363 return joyCaps.wYmax;
364}
365
c42404a5 366int wxJoystick::GetZMax() const
2bda0e17
KB
367{
368 JOYCAPS joyCaps;
369 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
370 return 0;
371 else
372 return joyCaps.wZmax;
373}
374
c42404a5 375int wxJoystick::GetNumberButtons() const
2bda0e17
KB
376{
377 JOYCAPS joyCaps;
378 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
379 return 0;
380 else
381 return joyCaps.wNumButtons;
382}
383
c42404a5 384int wxJoystick::GetNumberAxes() const
2bda0e17 385{
b39dbf34 386#if defined(__WIN32__)
2bda0e17
KB
387 JOYCAPS joyCaps;
388 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
389 return 0;
390 else
391 return joyCaps.wNumAxes;
392#else
393 return 0;
394#endif
395}
396
c42404a5 397int wxJoystick::GetMaxButtons() const
2bda0e17 398{
b39dbf34 399#if defined(__WIN32__)
2bda0e17
KB
400 JOYCAPS joyCaps;
401 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
402 return 0;
403 else
404 return joyCaps.wMaxButtons;
405#else
406 return 0;
407#endif
408}
409
c42404a5 410int wxJoystick::GetMaxAxes() const
2bda0e17 411{
b39dbf34 412#if defined(__WIN32__)
2bda0e17
KB
413 JOYCAPS joyCaps;
414 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
415 return 0;
416 else
417 return joyCaps.wMaxAxes;
418#else
419 return 0;
420#endif
421}
422
c42404a5 423int wxJoystick::GetPollingMin() const
2bda0e17
KB
424{
425 JOYCAPS joyCaps;
426 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
427 return 0;
428 else
429 return joyCaps.wPeriodMin;
430}
431
c42404a5 432int wxJoystick::GetPollingMax() const
2bda0e17
KB
433{
434 JOYCAPS joyCaps;
435 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
436 return 0;
437 else
438 return joyCaps.wPeriodMax;
439}
440
c42404a5 441int wxJoystick::GetRudderMin() const
2bda0e17 442{
b39dbf34 443#if defined(__WIN32__)
2bda0e17
KB
444 JOYCAPS joyCaps;
445 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
446 return 0;
447 else
448 return joyCaps.wRmin;
449#else
450 return 0;
451#endif
452}
453
c42404a5 454int wxJoystick::GetRudderMax() const
2bda0e17 455{
b39dbf34 456#if defined(__WIN32__)
2bda0e17
KB
457 JOYCAPS joyCaps;
458 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
459 return 0;
460 else
461 return joyCaps.wRmax;
462#else
463 return 0;
464#endif
465}
466
c42404a5 467int wxJoystick::GetUMin() const
2bda0e17 468{
b39dbf34 469#if defined(__WIN32__)
2bda0e17
KB
470 JOYCAPS joyCaps;
471 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
472 return 0;
473 else
474 return joyCaps.wUmin;
475#else
476 return 0;
477#endif
478}
479
c42404a5 480int wxJoystick::GetUMax() const
2bda0e17 481{
b39dbf34 482#if defined(__WIN32__)
2bda0e17
KB
483 JOYCAPS joyCaps;
484 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
485 return 0;
486 else
487 return joyCaps.wUmax;
488#else
489 return 0;
490#endif
491}
492
c42404a5 493int wxJoystick::GetVMin() const
2bda0e17 494{
b39dbf34 495#if defined(__WIN32__)
2bda0e17
KB
496 JOYCAPS joyCaps;
497 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
498 return 0;
499 else
500 return joyCaps.wVmin;
501#else
502 return 0;
503#endif
504}
505
c42404a5 506int wxJoystick::GetVMax() const
2bda0e17 507{
b39dbf34 508#if defined(__WIN32__)
2bda0e17
KB
509 JOYCAPS joyCaps;
510 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
511 return 0;
512 else
513 return joyCaps.wVmax;
514#else
515 return 0;
516#endif
517}
518
519
c42404a5 520bool wxJoystick::HasRudder() const
2bda0e17 521{
b39dbf34 522#if defined(__WIN32__)
2bda0e17
KB
523 JOYCAPS joyCaps;
524 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 525 return false;
2bda0e17
KB
526 else
527 return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
528#else
598ddd96 529 return false;
2bda0e17
KB
530#endif
531}
532
c42404a5 533bool wxJoystick::HasZ() const
2bda0e17 534{
b39dbf34 535#if defined(__WIN32__)
2bda0e17
KB
536 JOYCAPS joyCaps;
537 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 538 return false;
2bda0e17
KB
539 else
540 return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
541#else
598ddd96 542 return false;
2bda0e17
KB
543#endif
544}
545
c42404a5 546bool wxJoystick::HasU() const
2bda0e17 547{
b39dbf34 548#if defined(__WIN32__)
2bda0e17
KB
549 JOYCAPS joyCaps;
550 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 551 return false;
2bda0e17
KB
552 else
553 return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
554#else
598ddd96 555 return false;
2bda0e17
KB
556#endif
557}
558
c42404a5 559bool wxJoystick::HasV() const
2bda0e17 560{
b39dbf34 561#if defined(__WIN32__)
2bda0e17
KB
562 JOYCAPS joyCaps;
563 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 564 return false;
2bda0e17
KB
565 else
566 return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
567#else
598ddd96 568 return false;
2bda0e17
KB
569#endif
570}
571
c42404a5 572bool wxJoystick::HasPOV() const
2bda0e17 573{
b39dbf34 574#if defined(__WIN32__)
2bda0e17
KB
575 JOYCAPS joyCaps;
576 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 577 return false;
2bda0e17
KB
578 else
579 return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
580#else
598ddd96 581 return false;
2bda0e17
KB
582#endif
583}
584
c42404a5 585bool wxJoystick::HasPOV4Dir() const
2bda0e17 586{
b39dbf34 587#if defined(__WIN32__)
2bda0e17
KB
588 JOYCAPS joyCaps;
589 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 590 return false;
2bda0e17
KB
591 else
592 return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
593#else
598ddd96 594 return false;
2bda0e17
KB
595#endif
596}
597
c42404a5 598bool wxJoystick::HasPOVCTS() const
2bda0e17 599{
b39dbf34 600#if defined(__WIN32__)
2bda0e17
KB
601 JOYCAPS joyCaps;
602 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 603 return false;
2bda0e17
KB
604 else
605 return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
606#else
598ddd96 607 return false;
2bda0e17
KB
608#endif
609}
610
611// Operations
612////////////////////////////////////////////////////////////////////////////
613
614bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
615{
616 BOOL changed = (pollingFreq == 0);
617 MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed);
618 return (res == JOYERR_NOERROR);
619}
620
c42404a5 621bool wxJoystick::ReleaseCapture()
2bda0e17
KB
622{
623 MMRESULT res = joyReleaseCapture(m_joystick);
624 return (res == JOYERR_NOERROR);
625}
626
7e044896 627#endif // wxUSE_JOYSTICK