]> git.saurik.com Git - wxWidgets.git/blame - src/msw/joystick.cpp
Corrected WM_KEYDOWN bug (missing 'break')
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
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
23#include <windows.h>
24
25#ifndef __GNUWIN32__
26#include <mmsystem.h>
27#endif
28
29#if !defined(__WIN32__) && !defined(_MMRESULT_)
30typedef UINT MMRESULT;
31#endif
32
57c208c5 33#ifndef __TWIN32__
2bda0e17
KB
34#ifdef __GNUWIN32__
35#include <wx/msw/gnuwin32/extra.h>
36#endif
57c208c5 37#endif
2bda0e17
KB
38
39// Why doesn't BC++ have joyGetPosEx?
57c208c5 40#if !defined(__WIN32__) || defined(__BORLANDC__) || defined(__TWIN32__)
2bda0e17
KB
41#define NO_JOYGETPOSEX
42#endif
43
ad5c34f3 44#include <wx/window.h>
2bda0e17
KB
45#include <wx/msw/joystick.h>
46
47IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
48
49// Attributes
50////////////////////////////////////////////////////////////////////////////
51
52wxPoint wxJoystick::GetPosition(void) const
53{
54 JOYINFO joyInfo;
55 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
56 if (res == JOYERR_NOERROR )
57 return wxPoint(joyInfo.wXpos, joyInfo.wYpos);
58 else
59 return wxPoint(0, 0);
60}
61
62int wxJoystick::GetZPosition(void) const
63{
64 JOYINFO joyInfo;
65 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
66 if (res == JOYERR_NOERROR )
67 return joyInfo.wZpos;
68 else
69 return 0;
70}
71
72int wxJoystick::GetButtonState(void) const
73{
74 JOYINFO joyInfo;
75 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
76 if (res == JOYERR_NOERROR )
77 {
78 int buttons = 0;
79
80 if (joyInfo.wButtons & JOY_BUTTON1)
81 buttons |= wxJOY_BUTTON1;
82 if (joyInfo.wButtons & JOY_BUTTON2)
83 buttons |= wxJOY_BUTTON2;
84 if (joyInfo.wButtons & JOY_BUTTON3)
85 buttons |= wxJOY_BUTTON3;
86 if (joyInfo.wButtons & JOY_BUTTON4)
87 buttons |= wxJOY_BUTTON4;
88 return buttons;
89 }
90 else
91 return 0;
92}
93
94int wxJoystick::GetPOVPosition(void) const
95{
96#ifndef NO_JOYGETPOSEX
97 JOYINFOEX joyInfo;
98 joyInfo.dwFlags = JOY_RETURNPOV;
99 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
100 if (res == JOYERR_NOERROR )
101 {
102 return joyInfo.dwPOV;
103 }
104 else
105 return 0;
106#else
107 return 0;
108#endif
109}
110
111int wxJoystick::GetPOVCTSPosition(void) const
112{
113#ifndef NO_JOYGETPOSEX
114 JOYINFOEX joyInfo;
115 joyInfo.dwFlags = JOY_RETURNPOVCTS;
116 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
117 if (res == JOYERR_NOERROR )
118 {
119 return joyInfo.dwPOV;
120 }
121 else
122 return 0;
123#else
124 return 0;
125#endif
126}
127
128int wxJoystick::GetRudderPosition(void) const
129{
130#ifndef NO_JOYGETPOSEX
131 JOYINFOEX joyInfo;
132 joyInfo.dwFlags = JOY_RETURNR;
133 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
134 if (res == JOYERR_NOERROR )
135 {
136 return joyInfo.dwRpos;
137 }
138 else
139 return 0;
140#else
141 return 0;
142#endif
143}
144
145int wxJoystick::GetUPosition(void) const
146{
147#ifndef NO_JOYGETPOSEX
148 JOYINFOEX joyInfo;
149 joyInfo.dwFlags = JOY_RETURNU;
150 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
151 if (res == JOYERR_NOERROR )
152 {
153 return joyInfo.dwUpos;
154 }
155 else
156 return 0;
157#else
158 return 0;
159#endif
160}
161
162int wxJoystick::GetVPosition(void) const
163{
164#ifndef NO_JOYGETPOSEX
165 JOYINFOEX joyInfo;
166 joyInfo.dwFlags = JOY_RETURNV;
167 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
168 if (res == JOYERR_NOERROR )
169 {
170 return joyInfo.dwVpos;
171 }
172 else
173 return 0;
174#else
175 return 0;
176#endif
177}
178
179int wxJoystick::GetMovementThreshold(void) const
180{
181 UINT thresh = 0;
182 MMRESULT res = joyGetThreshold(m_joystick, & thresh);
183 if (res == JOYERR_NOERROR )
184 {
185 return thresh;
186 }
187 else
188 return 0;
189}
190
191void wxJoystick::SetMovementThreshold(int threshold)
192{
193 UINT thresh = threshold;
194 joySetThreshold(m_joystick, thresh);
195}
196
197// Capabilities
198////////////////////////////////////////////////////////////////////////////
199
200bool wxJoystick::IsOk(void) const
201{
202 JOYINFO joyInfo;
203 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
204 return ((joyGetNumDevs() > 0) || (res == JOYERR_NOERROR));
205}
206
207int wxJoystick::GetNumberJoysticks(void) const
208{
209 return joyGetNumDevs();
210}
211
212int wxJoystick::GetManufacturerId(void) const
213{
214 JOYCAPS joyCaps;
215 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
216 return 0;
217 else
218 return joyCaps.wMid;
219}
220
221int wxJoystick::GetProductId(void) const
222{
223 JOYCAPS joyCaps;
224 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
225 return 0;
226 else
227 return joyCaps.wPid;
228}
229
230wxString wxJoystick::GetProductName(void) const
231{
232 JOYCAPS joyCaps;
233 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
234 return wxString("");
235 else
236 return wxString(joyCaps.szPname);
237}
238
239int wxJoystick::GetXMin(void) const
240{
241 JOYCAPS joyCaps;
242 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
243 return 0;
244 else
245 return joyCaps.wXmin;
246}
247
248int wxJoystick::GetYMin(void) const
249{
250 JOYCAPS joyCaps;
251 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
252 return 0;
253 else
254 return joyCaps.wYmin;
255}
256
257int wxJoystick::GetZMin(void) const
258{
259 JOYCAPS joyCaps;
260 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
261 return 0;
262 else
263 return joyCaps.wZmin;
264}
265
266int wxJoystick::GetXMax(void) const
267{
268 JOYCAPS joyCaps;
269 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
270 return 0;
271 else
272 return joyCaps.wXmax;
273}
274
275int wxJoystick::GetYMax(void) const
276{
277 JOYCAPS joyCaps;
278 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
279 return 0;
280 else
281 return joyCaps.wYmax;
282}
283
284int wxJoystick::GetZMax(void) const
285{
286 JOYCAPS joyCaps;
287 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
288 return 0;
289 else
290 return joyCaps.wZmax;
291}
292
293int wxJoystick::GetNumberButtons(void) const
294{
295 JOYCAPS joyCaps;
296 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
297 return 0;
298 else
299 return joyCaps.wNumButtons;
300}
301
302int wxJoystick::GetNumberAxes(void) const
303{
57c208c5 304#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
305 JOYCAPS joyCaps;
306 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
307 return 0;
308 else
309 return joyCaps.wNumAxes;
310#else
311 return 0;
312#endif
313}
314
315int wxJoystick::GetMaxButtons(void) const
316{
57c208c5 317#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
318 JOYCAPS joyCaps;
319 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
320 return 0;
321 else
322 return joyCaps.wMaxButtons;
323#else
324 return 0;
325#endif
326}
327
328int wxJoystick::GetMaxAxes(void) const
329{
57c208c5 330#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
331 JOYCAPS joyCaps;
332 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
333 return 0;
334 else
335 return joyCaps.wMaxAxes;
336#else
337 return 0;
338#endif
339}
340
341int wxJoystick::GetPollingMin(void) const
342{
343 JOYCAPS joyCaps;
344 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
345 return 0;
346 else
347 return joyCaps.wPeriodMin;
348}
349
350int wxJoystick::GetPollingMax(void) const
351{
352 JOYCAPS joyCaps;
353 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
354 return 0;
355 else
356 return joyCaps.wPeriodMax;
357}
358
359int wxJoystick::GetRudderMin(void) const
360{
57c208c5 361#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
362 JOYCAPS joyCaps;
363 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
364 return 0;
365 else
366 return joyCaps.wRmin;
367#else
368 return 0;
369#endif
370}
371
372int wxJoystick::GetRudderMax(void) const
373{
57c208c5 374#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
375 JOYCAPS joyCaps;
376 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
377 return 0;
378 else
379 return joyCaps.wRmax;
380#else
381 return 0;
382#endif
383}
384
385int wxJoystick::GetUMin(void) const
386{
57c208c5 387#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
388 JOYCAPS joyCaps;
389 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
390 return 0;
391 else
392 return joyCaps.wUmin;
393#else
394 return 0;
395#endif
396}
397
398int wxJoystick::GetUMax(void) const
399{
57c208c5 400#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
401 JOYCAPS joyCaps;
402 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
403 return 0;
404 else
405 return joyCaps.wUmax;
406#else
407 return 0;
408#endif
409}
410
411int wxJoystick::GetVMin(void) const
412{
57c208c5 413#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
414 JOYCAPS joyCaps;
415 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
416 return 0;
417 else
418 return joyCaps.wVmin;
419#else
420 return 0;
421#endif
422}
423
424int wxJoystick::GetVMax(void) const
425{
57c208c5 426#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
427 JOYCAPS joyCaps;
428 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
429 return 0;
430 else
431 return joyCaps.wVmax;
432#else
433 return 0;
434#endif
435}
436
437
438bool wxJoystick::HasRudder(void) const
439{
57c208c5 440#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
441 JOYCAPS joyCaps;
442 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
443 return FALSE;
444 else
445 return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
446#else
447 return FALSE;
448#endif
449}
450
451bool wxJoystick::HasZ(void) const
452{
57c208c5 453#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
454 JOYCAPS joyCaps;
455 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
456 return FALSE;
457 else
458 return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
459#else
460 return FALSE;
461#endif
462}
463
464bool wxJoystick::HasU(void) const
465{
57c208c5 466#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
467 JOYCAPS joyCaps;
468 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
469 return FALSE;
470 else
471 return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
472#else
473 return FALSE;
474#endif
475}
476
477bool wxJoystick::HasV(void) const
478{
57c208c5 479#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
480 JOYCAPS joyCaps;
481 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
482 return FALSE;
483 else
484 return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
485#else
486 return FALSE;
487#endif
488}
489
490bool wxJoystick::HasPOV(void) const
491{
57c208c5 492#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
493 JOYCAPS joyCaps;
494 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
495 return FALSE;
496 else
497 return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
498#else
499 return FALSE;
500#endif
501}
502
503bool wxJoystick::HasPOV4Dir(void) const
504{
57c208c5 505#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
506 JOYCAPS joyCaps;
507 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
508 return FALSE;
509 else
510 return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
511#else
512 return FALSE;
513#endif
514}
515
516bool wxJoystick::HasPOVCTS(void) const
517{
57c208c5 518#if defined(__WIN32__) && !defined(__TWIN32__)
2bda0e17
KB
519 JOYCAPS joyCaps;
520 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
521 return FALSE;
522 else
523 return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
524#else
525 return FALSE;
526#endif
527}
528
529// Operations
530////////////////////////////////////////////////////////////////////////////
531
532bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
533{
534 BOOL changed = (pollingFreq == 0);
535 MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed);
536 return (res == JOYERR_NOERROR);
537}
538
539bool wxJoystick::ReleaseCapture(void)
540{
541 MMRESULT res = joyReleaseCapture(m_joystick);
542 return (res == JOYERR_NOERROR);
543}
544