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