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