]> git.saurik.com Git - wxWidgets.git/blame - src/msw/joystick.cpp
Fixed non-precompiled headers compilation
[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
e6733873
VZ
89int wxJoystick::GetPosition(unsigned axis) const
90{
91 switch (axis) {
92 case 0:
93 return GetPosition().x;
94 case 1:
95 return GetPosition().y;
96 case 2:
97 return GetZPosition();
98 case 3:
99 return GetRudderPosition();
100 case 4:
101 return GetUPosition();
102 case 5:
103 return GetVPosition();
104 default:
105 return 0;
106 }
107}
108
c42404a5 109int wxJoystick::GetZPosition() const
2bda0e17
KB
110{
111 JOYINFO joyInfo;
112 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
113 if (res == JOYERR_NOERROR )
114 return joyInfo.wZpos;
115 else
116 return 0;
117}
118
131f9d9b 119/**
598ddd96
WS
120 johan@linkdata.se 2002-08-20:
121 Return a bitmap with all button states in it,
122 like the GTK version does and Win32 does.
131f9d9b 123*/
c42404a5 124int wxJoystick::GetButtonState() const
2bda0e17
KB
125{
126 JOYINFO joyInfo;
127 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
128 if (res == JOYERR_NOERROR )
129 {
598ddd96 130 return joyInfo.wButtons;
131f9d9b 131#if 0
2bda0e17
KB
132 int buttons = 0;
133
134 if (joyInfo.wButtons & JOY_BUTTON1)
135 buttons |= wxJOY_BUTTON1;
136 if (joyInfo.wButtons & JOY_BUTTON2)
137 buttons |= wxJOY_BUTTON2;
138 if (joyInfo.wButtons & JOY_BUTTON3)
139 buttons |= wxJOY_BUTTON3;
140 if (joyInfo.wButtons & JOY_BUTTON4)
141 buttons |= wxJOY_BUTTON4;
131f9d9b 142
2bda0e17 143 return buttons;
131f9d9b 144#endif
2bda0e17
KB
145 }
146 else
147 return 0;
148}
149
e6733873
VZ
150bool wxJoystick::GetButtonState(unsigned id) const
151{
152 if (id > sizeof(int) * 8)
153 return false;
154
155 return (GetButtonState() & (1 << id)) != 0;
156}
157
131f9d9b 158/**
598ddd96
WS
159 JLI 2002-08-20:
160 Returns -1 to signify error.
131f9d9b 161*/
c42404a5 162int wxJoystick::GetPOVPosition() const
2bda0e17
KB
163{
164#ifndef NO_JOYGETPOSEX
165 JOYINFOEX joyInfo;
166 joyInfo.dwFlags = JOY_RETURNPOV;
b9f933ab 167 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
168 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
169 if (res == JOYERR_NOERROR )
170 {
171 return joyInfo.dwPOV;
172 }
173 else
131f9d9b 174 return -1;
2bda0e17 175#else
131f9d9b 176 return -1;
2bda0e17
KB
177#endif
178}
179
131f9d9b 180/**
598ddd96
WS
181 johan@linkdata.se 2002-08-20:
182 Returns -1 to signify error.
131f9d9b 183*/
c42404a5 184int wxJoystick::GetPOVCTSPosition() const
2bda0e17
KB
185{
186#ifndef NO_JOYGETPOSEX
187 JOYINFOEX joyInfo;
188 joyInfo.dwFlags = JOY_RETURNPOVCTS;
b9f933ab 189 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
190 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
191 if (res == JOYERR_NOERROR )
192 {
193 return joyInfo.dwPOV;
194 }
195 else
131f9d9b 196 return -1;
2bda0e17 197#else
131f9d9b 198 return -1;
2bda0e17
KB
199#endif
200}
201
c42404a5 202int wxJoystick::GetRudderPosition() const
2bda0e17
KB
203{
204#ifndef NO_JOYGETPOSEX
205 JOYINFOEX joyInfo;
206 joyInfo.dwFlags = JOY_RETURNR;
b9f933ab 207 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
208 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
209 if (res == JOYERR_NOERROR )
210 {
211 return joyInfo.dwRpos;
212 }
213 else
214 return 0;
215#else
216 return 0;
217#endif
218}
219
c42404a5 220int wxJoystick::GetUPosition() const
2bda0e17
KB
221{
222#ifndef NO_JOYGETPOSEX
223 JOYINFOEX joyInfo;
224 joyInfo.dwFlags = JOY_RETURNU;
b9f933ab 225 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
226 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
227 if (res == JOYERR_NOERROR )
228 {
229 return joyInfo.dwUpos;
230 }
231 else
232 return 0;
233#else
234 return 0;
235#endif
236}
237
c42404a5 238int wxJoystick::GetVPosition() const
2bda0e17
KB
239{
240#ifndef NO_JOYGETPOSEX
241 JOYINFOEX joyInfo;
242 joyInfo.dwFlags = JOY_RETURNV;
b9f933ab 243 joyInfo.dwSize = sizeof(joyInfo);
2bda0e17
KB
244 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
245 if (res == JOYERR_NOERROR )
246 {
247 return joyInfo.dwVpos;
248 }
249 else
250 return 0;
251#else
252 return 0;
253#endif
254}
255
c42404a5 256int wxJoystick::GetMovementThreshold() const
2bda0e17
KB
257{
258 UINT thresh = 0;
259 MMRESULT res = joyGetThreshold(m_joystick, & thresh);
260 if (res == JOYERR_NOERROR )
261 {
262 return thresh;
263 }
264 else
265 return 0;
266}
267
268void wxJoystick::SetMovementThreshold(int threshold)
269{
270 UINT thresh = threshold;
271 joySetThreshold(m_joystick, thresh);
272}
273
274// Capabilities
275////////////////////////////////////////////////////////////////////////////
276
131f9d9b 277/**
598ddd96
WS
278 johan@linkdata.se 2002-08-20:
279 Now returns the number of connected, functioning
280 joysticks, as intended.
131f9d9b
JS
281*/
282int wxJoystick::GetNumberJoysticks()
2bda0e17
KB
283{
284 JOYINFO joyInfo;
598ddd96
WS
285 int i, maxsticks, actualsticks;
286 maxsticks = joyGetNumDevs();
287 actualsticks = 0;
288 for( i=0; i<maxsticks; i++ )
289 {
290 if( joyGetPos( i, & joyInfo ) == JOYERR_NOERROR )
291 {
292 actualsticks ++;
293 }
294 }
131f9d9b 295 return actualsticks;
2bda0e17
KB
296}
297
131f9d9b 298/**
598ddd96
WS
299 johan@linkdata.se 2002-08-20:
300 The old code returned true if there were any
301 joystick capable drivers loaded (=always).
131f9d9b
JS
302*/
303bool wxJoystick::IsOk() const
2bda0e17 304{
131f9d9b
JS
305 JOYINFO joyInfo;
306 return (joyGetPos(m_joystick, & joyInfo) == JOYERR_NOERROR);
2bda0e17
KB
307}
308
c42404a5 309int wxJoystick::GetManufacturerId() const
2bda0e17
KB
310{
311 JOYCAPS joyCaps;
312 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
313 return 0;
314 else
315 return joyCaps.wMid;
316}
317
c42404a5 318int wxJoystick::GetProductId() 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.wPid;
325}
326
c42404a5 327wxString wxJoystick::GetProductName() const
2bda0e17 328{
4708068c
JS
329 wxString str;
330#ifndef __WINE__
2bda0e17 331 JOYCAPS joyCaps;
c9ee46c3 332 if (joyGetDevCaps(m_joystick, &joyCaps, sizeof(joyCaps)) != JOYERR_NOERROR)
2b5f62a0 333 return wxEmptyString;
c9ee46c3
VZ
334
335 wxRegKey key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"),
336 REGSTR_PATH_JOYCONFIG, joyCaps.szRegKey, REGSTR_KEY_JOYCURR));
337
c9ee46c3
VZ
338 key1.QueryValue(wxString::Format(wxT("Joystick%d%s"),
339 m_joystick + 1, REGSTR_VAL_JOYOEMNAME),
340 str);
341
342 wxRegKey key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"),
343 REGSTR_PATH_JOYOEM, str.c_str()));
344 key2.QueryValue(REGSTR_VAL_JOYOEMNAME, str);
4708068c 345#endif
c9ee46c3 346 return str;
2bda0e17
KB
347}
348
c42404a5 349int wxJoystick::GetXMin() 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.wXmin;
356}
357
c42404a5 358int wxJoystick::GetYMin() 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.wYmin;
365}
366
c42404a5 367int wxJoystick::GetZMin() 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.wZmin;
374}
375
c42404a5 376int wxJoystick::GetXMax() 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.wXmax;
383}
384
c42404a5 385int wxJoystick::GetYMax() const
2bda0e17
KB
386{
387 JOYCAPS joyCaps;
388 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
389 return 0;
390 else
391 return joyCaps.wYmax;
392}
393
c42404a5 394int wxJoystick::GetZMax() const
2bda0e17
KB
395{
396 JOYCAPS joyCaps;
397 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
398 return 0;
399 else
400 return joyCaps.wZmax;
401}
402
c42404a5 403int wxJoystick::GetNumberButtons() const
2bda0e17
KB
404{
405 JOYCAPS joyCaps;
406 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
407 return 0;
408 else
409 return joyCaps.wNumButtons;
410}
411
c42404a5 412int wxJoystick::GetNumberAxes() const
2bda0e17 413{
b39dbf34 414#if defined(__WIN32__)
2bda0e17
KB
415 JOYCAPS joyCaps;
416 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
417 return 0;
418 else
419 return joyCaps.wNumAxes;
420#else
421 return 0;
422#endif
423}
424
c42404a5 425int wxJoystick::GetMaxButtons() const
2bda0e17 426{
b39dbf34 427#if defined(__WIN32__)
2bda0e17
KB
428 JOYCAPS joyCaps;
429 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
430 return 0;
431 else
432 return joyCaps.wMaxButtons;
433#else
434 return 0;
435#endif
436}
437
c42404a5 438int wxJoystick::GetMaxAxes() 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.wMaxAxes;
446#else
447 return 0;
448#endif
449}
450
c42404a5 451int wxJoystick::GetPollingMin() const
2bda0e17
KB
452{
453 JOYCAPS joyCaps;
454 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
455 return 0;
456 else
457 return joyCaps.wPeriodMin;
458}
459
c42404a5 460int wxJoystick::GetPollingMax() const
2bda0e17
KB
461{
462 JOYCAPS joyCaps;
463 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
464 return 0;
465 else
466 return joyCaps.wPeriodMax;
467}
468
c42404a5 469int wxJoystick::GetRudderMin() const
2bda0e17 470{
b39dbf34 471#if defined(__WIN32__)
2bda0e17
KB
472 JOYCAPS joyCaps;
473 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
474 return 0;
475 else
476 return joyCaps.wRmin;
477#else
478 return 0;
479#endif
480}
481
c42404a5 482int wxJoystick::GetRudderMax() const
2bda0e17 483{
b39dbf34 484#if defined(__WIN32__)
2bda0e17
KB
485 JOYCAPS joyCaps;
486 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
487 return 0;
488 else
489 return joyCaps.wRmax;
490#else
491 return 0;
492#endif
493}
494
c42404a5 495int wxJoystick::GetUMin() const
2bda0e17 496{
b39dbf34 497#if defined(__WIN32__)
2bda0e17
KB
498 JOYCAPS joyCaps;
499 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
500 return 0;
501 else
502 return joyCaps.wUmin;
503#else
504 return 0;
505#endif
506}
507
c42404a5 508int wxJoystick::GetUMax() const
2bda0e17 509{
b39dbf34 510#if defined(__WIN32__)
2bda0e17
KB
511 JOYCAPS joyCaps;
512 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
513 return 0;
514 else
515 return joyCaps.wUmax;
516#else
517 return 0;
518#endif
519}
520
c42404a5 521int wxJoystick::GetVMin() const
2bda0e17 522{
b39dbf34 523#if defined(__WIN32__)
2bda0e17
KB
524 JOYCAPS joyCaps;
525 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
526 return 0;
527 else
528 return joyCaps.wVmin;
529#else
530 return 0;
531#endif
532}
533
c42404a5 534int wxJoystick::GetVMax() const
2bda0e17 535{
b39dbf34 536#if defined(__WIN32__)
2bda0e17
KB
537 JOYCAPS joyCaps;
538 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
539 return 0;
540 else
541 return joyCaps.wVmax;
542#else
543 return 0;
544#endif
545}
546
547
c42404a5 548bool wxJoystick::HasRudder() const
2bda0e17 549{
b39dbf34 550#if defined(__WIN32__)
2bda0e17
KB
551 JOYCAPS joyCaps;
552 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 553 return false;
2bda0e17
KB
554 else
555 return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
556#else
598ddd96 557 return false;
2bda0e17
KB
558#endif
559}
560
c42404a5 561bool wxJoystick::HasZ() const
2bda0e17 562{
b39dbf34 563#if defined(__WIN32__)
2bda0e17
KB
564 JOYCAPS joyCaps;
565 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 566 return false;
2bda0e17
KB
567 else
568 return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
569#else
598ddd96 570 return false;
2bda0e17
KB
571#endif
572}
573
c42404a5 574bool wxJoystick::HasU() const
2bda0e17 575{
b39dbf34 576#if defined(__WIN32__)
2bda0e17
KB
577 JOYCAPS joyCaps;
578 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 579 return false;
2bda0e17
KB
580 else
581 return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
582#else
598ddd96 583 return false;
2bda0e17
KB
584#endif
585}
586
c42404a5 587bool wxJoystick::HasV() const
2bda0e17 588{
b39dbf34 589#if defined(__WIN32__)
2bda0e17
KB
590 JOYCAPS joyCaps;
591 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 592 return false;
2bda0e17
KB
593 else
594 return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
595#else
598ddd96 596 return false;
2bda0e17
KB
597#endif
598}
599
c42404a5 600bool wxJoystick::HasPOV() const
2bda0e17 601{
b39dbf34 602#if defined(__WIN32__)
2bda0e17
KB
603 JOYCAPS joyCaps;
604 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 605 return false;
2bda0e17
KB
606 else
607 return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
608#else
598ddd96 609 return false;
2bda0e17
KB
610#endif
611}
612
c42404a5 613bool wxJoystick::HasPOV4Dir() const
2bda0e17 614{
b39dbf34 615#if defined(__WIN32__)
2bda0e17
KB
616 JOYCAPS joyCaps;
617 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 618 return false;
2bda0e17
KB
619 else
620 return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
621#else
598ddd96 622 return false;
2bda0e17
KB
623#endif
624}
625
c42404a5 626bool wxJoystick::HasPOVCTS() const
2bda0e17 627{
b39dbf34 628#if defined(__WIN32__)
2bda0e17
KB
629 JOYCAPS joyCaps;
630 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
598ddd96 631 return false;
2bda0e17
KB
632 else
633 return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
634#else
598ddd96 635 return false;
2bda0e17
KB
636#endif
637}
638
639// Operations
640////////////////////////////////////////////////////////////////////////////
641
642bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
643{
644 BOOL changed = (pollingFreq == 0);
645 MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed);
646 return (res == JOYERR_NOERROR);
647}
648
c42404a5 649bool wxJoystick::ReleaseCapture()
2bda0e17
KB
650{
651 MMRESULT res = joyReleaseCapture(m_joystick);
652 return (res == JOYERR_NOERROR);
653}
654
7e044896 655#endif // wxUSE_JOYSTICK