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