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