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