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