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