]> git.saurik.com Git - wxWidgets.git/blob - src/msw/joystick.cpp
wxCocoa: Added preliminary Blit support
[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 #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
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 #ifdef __GNUWIN32_OLD__
36 #include "wx/msw/gnuwin32/extra.h"
37 #endif
38
39 // Why doesn't BC++ have joyGetPosEx?
40 #if !defined(__WIN32__) || defined(__BORLANDC__)
41 #define NO_JOYGETPOSEX
42 #endif
43
44 #include "wx/window.h"
45 #include "wx/msw/joystick.h"
46
47 IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
48
49 // Attributes
50 ////////////////////////////////////////////////////////////////////////////
51
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 */
58 wxJoystick::wxJoystick(int joystick)
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
83 wxPoint wxJoystick::GetPosition() const
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
93 int wxJoystick::GetZPosition() const
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
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 */
108 int wxJoystick::GetButtonState() const
109 {
110 JOYINFO joyInfo;
111 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
112 if (res == JOYERR_NOERROR )
113 {
114 return joyInfo.wButtons;
115 #if 0
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;
126
127 return buttons;
128 #endif
129 }
130 else
131 return 0;
132 }
133
134 /**
135 JLI 2002-08-20:
136 Returns -1 to signify error.
137 */
138 int wxJoystick::GetPOVPosition() const
139 {
140 #ifndef NO_JOYGETPOSEX
141 JOYINFOEX joyInfo;
142 joyInfo.dwFlags = JOY_RETURNPOV;
143 joyInfo.dwSize = sizeof(joyInfo);
144 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
145 if (res == JOYERR_NOERROR )
146 {
147 return joyInfo.dwPOV;
148 }
149 else
150 return -1;
151 #else
152 return -1;
153 #endif
154 }
155
156 /**
157 johan@linkdata.se 2002-08-20:
158 Returns -1 to signify error.
159 */
160 int wxJoystick::GetPOVCTSPosition() const
161 {
162 #ifndef NO_JOYGETPOSEX
163 JOYINFOEX joyInfo;
164 joyInfo.dwFlags = JOY_RETURNPOVCTS;
165 joyInfo.dwSize = sizeof(joyInfo);
166 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
167 if (res == JOYERR_NOERROR )
168 {
169 return joyInfo.dwPOV;
170 }
171 else
172 return -1;
173 #else
174 return -1;
175 #endif
176 }
177
178 int wxJoystick::GetRudderPosition() const
179 {
180 #ifndef NO_JOYGETPOSEX
181 JOYINFOEX joyInfo;
182 joyInfo.dwFlags = JOY_RETURNR;
183 joyInfo.dwSize = sizeof(joyInfo);
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
196 int wxJoystick::GetUPosition() const
197 {
198 #ifndef NO_JOYGETPOSEX
199 JOYINFOEX joyInfo;
200 joyInfo.dwFlags = JOY_RETURNU;
201 joyInfo.dwSize = sizeof(joyInfo);
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
214 int wxJoystick::GetVPosition() const
215 {
216 #ifndef NO_JOYGETPOSEX
217 JOYINFOEX joyInfo;
218 joyInfo.dwFlags = JOY_RETURNV;
219 joyInfo.dwSize = sizeof(joyInfo);
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
232 int wxJoystick::GetMovementThreshold() const
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
244 void wxJoystick::SetMovementThreshold(int threshold)
245 {
246 UINT thresh = threshold;
247 joySetThreshold(m_joystick, thresh);
248 }
249
250 // Capabilities
251 ////////////////////////////////////////////////////////////////////////////
252
253 /**
254 johan@linkdata.se 2002-08-20:
255 Now returns the number of connected, functioning
256 joysticks, as intended.
257 */
258 int wxJoystick::GetNumberJoysticks()
259 {
260 JOYINFO joyInfo;
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;
272 }
273
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 */
279 bool wxJoystick::IsOk() const
280 {
281 JOYINFO joyInfo;
282 return (joyGetPos(m_joystick, & joyInfo) == JOYERR_NOERROR);
283 }
284
285 int wxJoystick::GetManufacturerId() const
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
294 int wxJoystick::GetProductId() const
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
303 wxString wxJoystick::GetProductName() const
304 {
305 JOYCAPS joyCaps;
306 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
307 return wxEmptyString;
308 else
309 return wxString(joyCaps.szPname);
310 }
311
312 int wxJoystick::GetXMin() const
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
321 int wxJoystick::GetYMin() const
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
330 int wxJoystick::GetZMin() const
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
339 int wxJoystick::GetXMax() const
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
348 int wxJoystick::GetYMax() const
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
357 int wxJoystick::GetZMax() const
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
366 int wxJoystick::GetNumberButtons() const
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
375 int wxJoystick::GetNumberAxes() const
376 {
377 #if defined(__WIN32__)
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
388 int wxJoystick::GetMaxButtons() const
389 {
390 #if defined(__WIN32__)
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
401 int wxJoystick::GetMaxAxes() const
402 {
403 #if defined(__WIN32__)
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
414 int wxJoystick::GetPollingMin() const
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
423 int wxJoystick::GetPollingMax() const
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
432 int wxJoystick::GetRudderMin() const
433 {
434 #if defined(__WIN32__)
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
445 int wxJoystick::GetRudderMax() const
446 {
447 #if defined(__WIN32__)
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
458 int wxJoystick::GetUMin() const
459 {
460 #if defined(__WIN32__)
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
471 int wxJoystick::GetUMax() const
472 {
473 #if defined(__WIN32__)
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
484 int wxJoystick::GetVMin() const
485 {
486 #if defined(__WIN32__)
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
497 int wxJoystick::GetVMax() const
498 {
499 #if defined(__WIN32__)
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
511 bool wxJoystick::HasRudder() const
512 {
513 #if defined(__WIN32__)
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
524 bool wxJoystick::HasZ() const
525 {
526 #if defined(__WIN32__)
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
537 bool wxJoystick::HasU() const
538 {
539 #if defined(__WIN32__)
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
550 bool wxJoystick::HasV() const
551 {
552 #if defined(__WIN32__)
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
563 bool wxJoystick::HasPOV() const
564 {
565 #if defined(__WIN32__)
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
576 bool wxJoystick::HasPOV4Dir() const
577 {
578 #if defined(__WIN32__)
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
589 bool wxJoystick::HasPOVCTS() const
590 {
591 #if defined(__WIN32__)
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
605 bool 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
612 bool wxJoystick::ReleaseCapture()
613 {
614 MMRESULT res = joyReleaseCapture(m_joystick);
615 return (res == JOYERR_NOERROR);
616 }
617