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