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