]>
Commit | Line | Data |
---|---|---|
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 | wxString str; | |
297 | #ifndef __WINE__ | |
298 | JOYCAPS joyCaps; | |
299 | if (joyGetDevCaps(m_joystick, &joyCaps, sizeof(joyCaps)) != JOYERR_NOERROR) | |
300 | return wxEmptyString; | |
301 | ||
302 | wxRegKey key1(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s\\%s"), | |
303 | REGSTR_PATH_JOYCONFIG, joyCaps.szRegKey, REGSTR_KEY_JOYCURR)); | |
304 | ||
305 | key1.QueryValue(wxString::Format(wxT("Joystick%d%s"), | |
306 | m_joystick + 1, REGSTR_VAL_JOYOEMNAME), | |
307 | str); | |
308 | ||
309 | wxRegKey key2(wxString::Format(wxT("HKEY_LOCAL_MACHINE\\%s\\%s"), | |
310 | REGSTR_PATH_JOYOEM, str.c_str())); | |
311 | key2.QueryValue(REGSTR_VAL_JOYOEMNAME, str); | |
312 | #endif | |
313 | return str; | |
314 | } | |
315 | ||
316 | int wxJoystick::GetXMin() const | |
317 | { | |
318 | JOYCAPS joyCaps; | |
319 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
320 | return 0; | |
321 | else | |
322 | return joyCaps.wXmin; | |
323 | } | |
324 | ||
325 | int wxJoystick::GetYMin() const | |
326 | { | |
327 | JOYCAPS joyCaps; | |
328 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
329 | return 0; | |
330 | else | |
331 | return joyCaps.wYmin; | |
332 | } | |
333 | ||
334 | int wxJoystick::GetZMin() const | |
335 | { | |
336 | JOYCAPS joyCaps; | |
337 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
338 | return 0; | |
339 | else | |
340 | return joyCaps.wZmin; | |
341 | } | |
342 | ||
343 | int wxJoystick::GetXMax() const | |
344 | { | |
345 | JOYCAPS joyCaps; | |
346 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
347 | return 0; | |
348 | else | |
349 | return joyCaps.wXmax; | |
350 | } | |
351 | ||
352 | int wxJoystick::GetYMax() const | |
353 | { | |
354 | JOYCAPS joyCaps; | |
355 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
356 | return 0; | |
357 | else | |
358 | return joyCaps.wYmax; | |
359 | } | |
360 | ||
361 | int wxJoystick::GetZMax() const | |
362 | { | |
363 | JOYCAPS joyCaps; | |
364 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
365 | return 0; | |
366 | else | |
367 | return joyCaps.wZmax; | |
368 | } | |
369 | ||
370 | int wxJoystick::GetNumberButtons() const | |
371 | { | |
372 | JOYCAPS joyCaps; | |
373 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
374 | return 0; | |
375 | else | |
376 | return joyCaps.wNumButtons; | |
377 | } | |
378 | ||
379 | int wxJoystick::GetNumberAxes() const | |
380 | { | |
381 | #if defined(__WIN32__) | |
382 | JOYCAPS joyCaps; | |
383 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
384 | return 0; | |
385 | else | |
386 | return joyCaps.wNumAxes; | |
387 | #else | |
388 | return 0; | |
389 | #endif | |
390 | } | |
391 | ||
392 | int wxJoystick::GetMaxButtons() const | |
393 | { | |
394 | #if defined(__WIN32__) | |
395 | JOYCAPS joyCaps; | |
396 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
397 | return 0; | |
398 | else | |
399 | return joyCaps.wMaxButtons; | |
400 | #else | |
401 | return 0; | |
402 | #endif | |
403 | } | |
404 | ||
405 | int wxJoystick::GetMaxAxes() const | |
406 | { | |
407 | #if defined(__WIN32__) | |
408 | JOYCAPS joyCaps; | |
409 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
410 | return 0; | |
411 | else | |
412 | return joyCaps.wMaxAxes; | |
413 | #else | |
414 | return 0; | |
415 | #endif | |
416 | } | |
417 | ||
418 | int wxJoystick::GetPollingMin() const | |
419 | { | |
420 | JOYCAPS joyCaps; | |
421 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
422 | return 0; | |
423 | else | |
424 | return joyCaps.wPeriodMin; | |
425 | } | |
426 | ||
427 | int wxJoystick::GetPollingMax() const | |
428 | { | |
429 | JOYCAPS joyCaps; | |
430 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
431 | return 0; | |
432 | else | |
433 | return joyCaps.wPeriodMax; | |
434 | } | |
435 | ||
436 | int wxJoystick::GetRudderMin() const | |
437 | { | |
438 | #if defined(__WIN32__) | |
439 | JOYCAPS joyCaps; | |
440 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
441 | return 0; | |
442 | else | |
443 | return joyCaps.wRmin; | |
444 | #else | |
445 | return 0; | |
446 | #endif | |
447 | } | |
448 | ||
449 | int wxJoystick::GetRudderMax() const | |
450 | { | |
451 | #if defined(__WIN32__) | |
452 | JOYCAPS joyCaps; | |
453 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
454 | return 0; | |
455 | else | |
456 | return joyCaps.wRmax; | |
457 | #else | |
458 | return 0; | |
459 | #endif | |
460 | } | |
461 | ||
462 | int wxJoystick::GetUMin() const | |
463 | { | |
464 | #if defined(__WIN32__) | |
465 | JOYCAPS joyCaps; | |
466 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
467 | return 0; | |
468 | else | |
469 | return joyCaps.wUmin; | |
470 | #else | |
471 | return 0; | |
472 | #endif | |
473 | } | |
474 | ||
475 | int wxJoystick::GetUMax() const | |
476 | { | |
477 | #if defined(__WIN32__) | |
478 | JOYCAPS joyCaps; | |
479 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
480 | return 0; | |
481 | else | |
482 | return joyCaps.wUmax; | |
483 | #else | |
484 | return 0; | |
485 | #endif | |
486 | } | |
487 | ||
488 | int wxJoystick::GetVMin() const | |
489 | { | |
490 | #if defined(__WIN32__) | |
491 | JOYCAPS joyCaps; | |
492 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
493 | return 0; | |
494 | else | |
495 | return joyCaps.wVmin; | |
496 | #else | |
497 | return 0; | |
498 | #endif | |
499 | } | |
500 | ||
501 | int wxJoystick::GetVMax() const | |
502 | { | |
503 | #if defined(__WIN32__) | |
504 | JOYCAPS joyCaps; | |
505 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
506 | return 0; | |
507 | else | |
508 | return joyCaps.wVmax; | |
509 | #else | |
510 | return 0; | |
511 | #endif | |
512 | } | |
513 | ||
514 | ||
515 | bool wxJoystick::HasRudder() const | |
516 | { | |
517 | #if defined(__WIN32__) | |
518 | JOYCAPS joyCaps; | |
519 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
520 | return false; | |
521 | else | |
522 | return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR); | |
523 | #else | |
524 | return false; | |
525 | #endif | |
526 | } | |
527 | ||
528 | bool wxJoystick::HasZ() const | |
529 | { | |
530 | #if defined(__WIN32__) | |
531 | JOYCAPS joyCaps; | |
532 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
533 | return false; | |
534 | else | |
535 | return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ); | |
536 | #else | |
537 | return false; | |
538 | #endif | |
539 | } | |
540 | ||
541 | bool wxJoystick::HasU() const | |
542 | { | |
543 | #if defined(__WIN32__) | |
544 | JOYCAPS joyCaps; | |
545 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
546 | return false; | |
547 | else | |
548 | return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU); | |
549 | #else | |
550 | return false; | |
551 | #endif | |
552 | } | |
553 | ||
554 | bool wxJoystick::HasV() const | |
555 | { | |
556 | #if defined(__WIN32__) | |
557 | JOYCAPS joyCaps; | |
558 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
559 | return false; | |
560 | else | |
561 | return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV); | |
562 | #else | |
563 | return false; | |
564 | #endif | |
565 | } | |
566 | ||
567 | bool wxJoystick::HasPOV() const | |
568 | { | |
569 | #if defined(__WIN32__) | |
570 | JOYCAPS joyCaps; | |
571 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
572 | return false; | |
573 | else | |
574 | return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV); | |
575 | #else | |
576 | return false; | |
577 | #endif | |
578 | } | |
579 | ||
580 | bool wxJoystick::HasPOV4Dir() const | |
581 | { | |
582 | #if defined(__WIN32__) | |
583 | JOYCAPS joyCaps; | |
584 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
585 | return false; | |
586 | else | |
587 | return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR); | |
588 | #else | |
589 | return false; | |
590 | #endif | |
591 | } | |
592 | ||
593 | bool wxJoystick::HasPOVCTS() const | |
594 | { | |
595 | #if defined(__WIN32__) | |
596 | JOYCAPS joyCaps; | |
597 | if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR) | |
598 | return false; | |
599 | else | |
600 | return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS); | |
601 | #else | |
602 | return false; | |
603 | #endif | |
604 | } | |
605 | ||
606 | // Operations | |
607 | //////////////////////////////////////////////////////////////////////////// | |
608 | ||
609 | bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq) | |
610 | { | |
611 | BOOL changed = (pollingFreq == 0); | |
612 | MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed); | |
613 | return (res == JOYERR_NOERROR); | |
614 | } | |
615 | ||
616 | bool wxJoystick::ReleaseCapture() | |
617 | { | |
618 | MMRESULT res = joyReleaseCapture(m_joystick); | |
619 | return (res == JOYERR_NOERROR); | |
620 | } | |
621 |