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