]> git.saurik.com Git - wxWidgets.git/blob - src/msw/joystick.cpp
fixes for s.Printf(s) crashes
[wxWidgets.git] / src / msw / joystick.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "joystick.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/string.h"
24 #include "wx/window.h"
25 #include "wx/msw/private.h"
26
27 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
28 #include <mmsystem.h>
29 #endif
30
31 #if !defined(__WIN32__) && !defined(_MMRESULT_)
32 typedef UINT MMRESULT;
33 #endif
34
35 // Why doesn't BC++ have joyGetPosEx?
36 #if !defined(__WIN32__) || defined(__BORLANDC__)
37 #define NO_JOYGETPOSEX
38 #endif
39
40 #include "wx/window.h"
41 #include "wx/msw/joystick.h"
42
43 IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
44
45 // Attributes
46 ////////////////////////////////////////////////////////////////////////////
47
48 /**
49 johan@linkdata.se 2002-08-20:
50 Now returns only valid, functioning
51 joysticks, counting from the first
52 available and upwards.
53 */
54 wxJoystick::wxJoystick(int joystick)
55 {
56 JOYINFO joyInfo;
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;
77 };
78
79 wxPoint wxJoystick::GetPosition() const
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
86 return wxPoint(0,0);
87 }
88
89 int wxJoystick::GetZPosition() const
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
99 /**
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.
103 */
104 int wxJoystick::GetButtonState() const
105 {
106 JOYINFO joyInfo;
107 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
108 if (res == JOYERR_NOERROR )
109 {
110 return joyInfo.wButtons;
111 #if 0
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;
122
123 return buttons;
124 #endif
125 }
126 else
127 return 0;
128 }
129
130 /**
131 JLI 2002-08-20:
132 Returns -1 to signify error.
133 */
134 int wxJoystick::GetPOVPosition() const
135 {
136 #ifndef NO_JOYGETPOSEX
137 JOYINFOEX joyInfo;
138 joyInfo.dwFlags = JOY_RETURNPOV;
139 joyInfo.dwSize = sizeof(joyInfo);
140 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
141 if (res == JOYERR_NOERROR )
142 {
143 return joyInfo.dwPOV;
144 }
145 else
146 return -1;
147 #else
148 return -1;
149 #endif
150 }
151
152 /**
153 johan@linkdata.se 2002-08-20:
154 Returns -1 to signify error.
155 */
156 int wxJoystick::GetPOVCTSPosition() const
157 {
158 #ifndef NO_JOYGETPOSEX
159 JOYINFOEX joyInfo;
160 joyInfo.dwFlags = JOY_RETURNPOVCTS;
161 joyInfo.dwSize = sizeof(joyInfo);
162 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
163 if (res == JOYERR_NOERROR )
164 {
165 return joyInfo.dwPOV;
166 }
167 else
168 return -1;
169 #else
170 return -1;
171 #endif
172 }
173
174 int wxJoystick::GetRudderPosition() const
175 {
176 #ifndef NO_JOYGETPOSEX
177 JOYINFOEX joyInfo;
178 joyInfo.dwFlags = JOY_RETURNR;
179 joyInfo.dwSize = sizeof(joyInfo);
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
192 int wxJoystick::GetUPosition() const
193 {
194 #ifndef NO_JOYGETPOSEX
195 JOYINFOEX joyInfo;
196 joyInfo.dwFlags = JOY_RETURNU;
197 joyInfo.dwSize = sizeof(joyInfo);
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
210 int wxJoystick::GetVPosition() const
211 {
212 #ifndef NO_JOYGETPOSEX
213 JOYINFOEX joyInfo;
214 joyInfo.dwFlags = JOY_RETURNV;
215 joyInfo.dwSize = sizeof(joyInfo);
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
228 int wxJoystick::GetMovementThreshold() const
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
240 void wxJoystick::SetMovementThreshold(int threshold)
241 {
242 UINT thresh = threshold;
243 joySetThreshold(m_joystick, thresh);
244 }
245
246 // Capabilities
247 ////////////////////////////////////////////////////////////////////////////
248
249 /**
250 johan@linkdata.se 2002-08-20:
251 Now returns the number of connected, functioning
252 joysticks, as intended.
253 */
254 int wxJoystick::GetNumberJoysticks()
255 {
256 JOYINFO joyInfo;
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 }
267 return actualsticks;
268 }
269
270 /**
271 johan@linkdata.se 2002-08-20:
272 The old code returned true if there were any
273 joystick capable drivers loaded (=always).
274 */
275 bool wxJoystick::IsOk() const
276 {
277 JOYINFO joyInfo;
278 return (joyGetPos(m_joystick, & joyInfo) == JOYERR_NOERROR);
279 }
280
281 int wxJoystick::GetManufacturerId() const
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
290 int wxJoystick::GetProductId() const
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
299 wxString wxJoystick::GetProductName() const
300 {
301 JOYCAPS joyCaps;
302 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
303 return wxEmptyString;
304 else
305 return wxString(joyCaps.szPname);
306 }
307
308 int wxJoystick::GetXMin() const
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
317 int wxJoystick::GetYMin() const
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
326 int wxJoystick::GetZMin() const
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
335 int wxJoystick::GetXMax() const
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
344 int wxJoystick::GetYMax() const
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
353 int wxJoystick::GetZMax() const
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
362 int wxJoystick::GetNumberButtons() const
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
371 int wxJoystick::GetNumberAxes() const
372 {
373 #if defined(__WIN32__)
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
384 int wxJoystick::GetMaxButtons() const
385 {
386 #if defined(__WIN32__)
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
397 int wxJoystick::GetMaxAxes() const
398 {
399 #if defined(__WIN32__)
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
410 int wxJoystick::GetPollingMin() const
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
419 int wxJoystick::GetPollingMax() const
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
428 int wxJoystick::GetRudderMin() const
429 {
430 #if defined(__WIN32__)
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
441 int wxJoystick::GetRudderMax() const
442 {
443 #if defined(__WIN32__)
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
454 int wxJoystick::GetUMin() const
455 {
456 #if defined(__WIN32__)
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
467 int wxJoystick::GetUMax() const
468 {
469 #if defined(__WIN32__)
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
480 int wxJoystick::GetVMin() const
481 {
482 #if defined(__WIN32__)
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
493 int wxJoystick::GetVMax() const
494 {
495 #if defined(__WIN32__)
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
507 bool wxJoystick::HasRudder() const
508 {
509 #if defined(__WIN32__)
510 JOYCAPS joyCaps;
511 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
512 return false;
513 else
514 return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
515 #else
516 return false;
517 #endif
518 }
519
520 bool wxJoystick::HasZ() const
521 {
522 #if defined(__WIN32__)
523 JOYCAPS joyCaps;
524 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
525 return false;
526 else
527 return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
528 #else
529 return false;
530 #endif
531 }
532
533 bool wxJoystick::HasU() const
534 {
535 #if defined(__WIN32__)
536 JOYCAPS joyCaps;
537 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
538 return false;
539 else
540 return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
541 #else
542 return false;
543 #endif
544 }
545
546 bool wxJoystick::HasV() const
547 {
548 #if defined(__WIN32__)
549 JOYCAPS joyCaps;
550 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
551 return false;
552 else
553 return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
554 #else
555 return false;
556 #endif
557 }
558
559 bool wxJoystick::HasPOV() const
560 {
561 #if defined(__WIN32__)
562 JOYCAPS joyCaps;
563 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
564 return false;
565 else
566 return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
567 #else
568 return false;
569 #endif
570 }
571
572 bool wxJoystick::HasPOV4Dir() const
573 {
574 #if defined(__WIN32__)
575 JOYCAPS joyCaps;
576 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
577 return false;
578 else
579 return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
580 #else
581 return false;
582 #endif
583 }
584
585 bool wxJoystick::HasPOVCTS() const
586 {
587 #if defined(__WIN32__)
588 JOYCAPS joyCaps;
589 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
590 return false;
591 else
592 return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
593 #else
594 return false;
595 #endif
596 }
597
598 // Operations
599 ////////////////////////////////////////////////////////////////////////////
600
601 bool 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
608 bool wxJoystick::ReleaseCapture()
609 {
610 MMRESULT res = joyReleaseCapture(m_joystick);
611 return (res == JOYERR_NOERROR);
612 }
613