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