Applied patch [ 597398 ] Generic MDI, wxNotebook based.
[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 wxPoint wxJoystick::GetPosition() const
55 {
56 JOYINFO joyInfo;
57 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
58 if (res == JOYERR_NOERROR )
59 return wxPoint(joyInfo.wXpos, joyInfo.wYpos);
60 else
61 return wxPoint(0, 0);
62 }
63
64 int wxJoystick::GetZPosition() const
65 {
66 JOYINFO joyInfo;
67 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
68 if (res == JOYERR_NOERROR )
69 return joyInfo.wZpos;
70 else
71 return 0;
72 }
73
74 int wxJoystick::GetButtonState() const
75 {
76 JOYINFO joyInfo;
77 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
78 if (res == JOYERR_NOERROR )
79 {
80 int buttons = 0;
81
82 if (joyInfo.wButtons & JOY_BUTTON1)
83 buttons |= wxJOY_BUTTON1;
84 if (joyInfo.wButtons & JOY_BUTTON2)
85 buttons |= wxJOY_BUTTON2;
86 if (joyInfo.wButtons & JOY_BUTTON3)
87 buttons |= wxJOY_BUTTON3;
88 if (joyInfo.wButtons & JOY_BUTTON4)
89 buttons |= wxJOY_BUTTON4;
90 return buttons;
91 }
92 else
93 return 0;
94 }
95
96 int wxJoystick::GetPOVPosition() const
97 {
98 #ifndef NO_JOYGETPOSEX
99 JOYINFOEX joyInfo;
100 joyInfo.dwFlags = JOY_RETURNPOV;
101 joyInfo.dwSize = sizeof(joyInfo);
102 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
103 if (res == JOYERR_NOERROR )
104 {
105 return joyInfo.dwPOV;
106 }
107 else
108 return 0;
109 #else
110 return 0;
111 #endif
112 }
113
114 int wxJoystick::GetPOVCTSPosition() const
115 {
116 #ifndef NO_JOYGETPOSEX
117 JOYINFOEX joyInfo;
118 joyInfo.dwFlags = JOY_RETURNPOVCTS;
119 joyInfo.dwSize = sizeof(joyInfo);
120 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
121 if (res == JOYERR_NOERROR )
122 {
123 return joyInfo.dwPOV;
124 }
125 else
126 return 0;
127 #else
128 return 0;
129 #endif
130 }
131
132 int wxJoystick::GetRudderPosition() const
133 {
134 #ifndef NO_JOYGETPOSEX
135 JOYINFOEX joyInfo;
136 joyInfo.dwFlags = JOY_RETURNR;
137 joyInfo.dwSize = sizeof(joyInfo);
138 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
139 if (res == JOYERR_NOERROR )
140 {
141 return joyInfo.dwRpos;
142 }
143 else
144 return 0;
145 #else
146 return 0;
147 #endif
148 }
149
150 int wxJoystick::GetUPosition() const
151 {
152 #ifndef NO_JOYGETPOSEX
153 JOYINFOEX joyInfo;
154 joyInfo.dwFlags = JOY_RETURNU;
155 joyInfo.dwSize = sizeof(joyInfo);
156 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
157 if (res == JOYERR_NOERROR )
158 {
159 return joyInfo.dwUpos;
160 }
161 else
162 return 0;
163 #else
164 return 0;
165 #endif
166 }
167
168 int wxJoystick::GetVPosition() const
169 {
170 #ifndef NO_JOYGETPOSEX
171 JOYINFOEX joyInfo;
172 joyInfo.dwFlags = JOY_RETURNV;
173 joyInfo.dwSize = sizeof(joyInfo);
174 MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
175 if (res == JOYERR_NOERROR )
176 {
177 return joyInfo.dwVpos;
178 }
179 else
180 return 0;
181 #else
182 return 0;
183 #endif
184 }
185
186 int wxJoystick::GetMovementThreshold() const
187 {
188 UINT thresh = 0;
189 MMRESULT res = joyGetThreshold(m_joystick, & thresh);
190 if (res == JOYERR_NOERROR )
191 {
192 return thresh;
193 }
194 else
195 return 0;
196 }
197
198 void wxJoystick::SetMovementThreshold(int threshold)
199 {
200 UINT thresh = threshold;
201 joySetThreshold(m_joystick, thresh);
202 }
203
204 // Capabilities
205 ////////////////////////////////////////////////////////////////////////////
206
207 bool wxJoystick::IsOk() const
208 {
209 JOYINFO joyInfo;
210 MMRESULT res = joyGetPos(m_joystick, & joyInfo);
211 return ((joyGetNumDevs() > 0) || (res == JOYERR_NOERROR));
212 }
213
214 int wxJoystick::GetNumberJoysticks() const
215 {
216 return joyGetNumDevs();
217 }
218
219 int wxJoystick::GetManufacturerId() const
220 {
221 JOYCAPS joyCaps;
222 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
223 return 0;
224 else
225 return joyCaps.wMid;
226 }
227
228 int wxJoystick::GetProductId() const
229 {
230 JOYCAPS joyCaps;
231 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
232 return 0;
233 else
234 return joyCaps.wPid;
235 }
236
237 wxString wxJoystick::GetProductName() const
238 {
239 JOYCAPS joyCaps;
240 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
241 return wxString("");
242 else
243 return wxString(joyCaps.szPname);
244 }
245
246 int wxJoystick::GetXMin() const
247 {
248 JOYCAPS joyCaps;
249 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
250 return 0;
251 else
252 return joyCaps.wXmin;
253 }
254
255 int wxJoystick::GetYMin() const
256 {
257 JOYCAPS joyCaps;
258 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
259 return 0;
260 else
261 return joyCaps.wYmin;
262 }
263
264 int wxJoystick::GetZMin() const
265 {
266 JOYCAPS joyCaps;
267 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
268 return 0;
269 else
270 return joyCaps.wZmin;
271 }
272
273 int wxJoystick::GetXMax() const
274 {
275 JOYCAPS joyCaps;
276 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
277 return 0;
278 else
279 return joyCaps.wXmax;
280 }
281
282 int wxJoystick::GetYMax() const
283 {
284 JOYCAPS joyCaps;
285 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
286 return 0;
287 else
288 return joyCaps.wYmax;
289 }
290
291 int wxJoystick::GetZMax() const
292 {
293 JOYCAPS joyCaps;
294 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
295 return 0;
296 else
297 return joyCaps.wZmax;
298 }
299
300 int wxJoystick::GetNumberButtons() const
301 {
302 JOYCAPS joyCaps;
303 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
304 return 0;
305 else
306 return joyCaps.wNumButtons;
307 }
308
309 int wxJoystick::GetNumberAxes() const
310 {
311 #if defined(__WIN32__) && !defined(__TWIN32__)
312 JOYCAPS joyCaps;
313 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
314 return 0;
315 else
316 return joyCaps.wNumAxes;
317 #else
318 return 0;
319 #endif
320 }
321
322 int wxJoystick::GetMaxButtons() const
323 {
324 #if defined(__WIN32__) && !defined(__TWIN32__)
325 JOYCAPS joyCaps;
326 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
327 return 0;
328 else
329 return joyCaps.wMaxButtons;
330 #else
331 return 0;
332 #endif
333 }
334
335 int wxJoystick::GetMaxAxes() const
336 {
337 #if defined(__WIN32__) && !defined(__TWIN32__)
338 JOYCAPS joyCaps;
339 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
340 return 0;
341 else
342 return joyCaps.wMaxAxes;
343 #else
344 return 0;
345 #endif
346 }
347
348 int wxJoystick::GetPollingMin() const
349 {
350 JOYCAPS joyCaps;
351 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
352 return 0;
353 else
354 return joyCaps.wPeriodMin;
355 }
356
357 int wxJoystick::GetPollingMax() const
358 {
359 JOYCAPS joyCaps;
360 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
361 return 0;
362 else
363 return joyCaps.wPeriodMax;
364 }
365
366 int wxJoystick::GetRudderMin() const
367 {
368 #if defined(__WIN32__) && !defined(__TWIN32__)
369 JOYCAPS joyCaps;
370 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
371 return 0;
372 else
373 return joyCaps.wRmin;
374 #else
375 return 0;
376 #endif
377 }
378
379 int wxJoystick::GetRudderMax() const
380 {
381 #if defined(__WIN32__) && !defined(__TWIN32__)
382 JOYCAPS joyCaps;
383 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
384 return 0;
385 else
386 return joyCaps.wRmax;
387 #else
388 return 0;
389 #endif
390 }
391
392 int wxJoystick::GetUMin() const
393 {
394 #if defined(__WIN32__) && !defined(__TWIN32__)
395 JOYCAPS joyCaps;
396 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
397 return 0;
398 else
399 return joyCaps.wUmin;
400 #else
401 return 0;
402 #endif
403 }
404
405 int wxJoystick::GetUMax() const
406 {
407 #if defined(__WIN32__) && !defined(__TWIN32__)
408 JOYCAPS joyCaps;
409 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
410 return 0;
411 else
412 return joyCaps.wUmax;
413 #else
414 return 0;
415 #endif
416 }
417
418 int wxJoystick::GetVMin() const
419 {
420 #if defined(__WIN32__) && !defined(__TWIN32__)
421 JOYCAPS joyCaps;
422 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
423 return 0;
424 else
425 return joyCaps.wVmin;
426 #else
427 return 0;
428 #endif
429 }
430
431 int wxJoystick::GetVMax() const
432 {
433 #if defined(__WIN32__) && !defined(__TWIN32__)
434 JOYCAPS joyCaps;
435 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
436 return 0;
437 else
438 return joyCaps.wVmax;
439 #else
440 return 0;
441 #endif
442 }
443
444
445 bool wxJoystick::HasRudder() const
446 {
447 #if defined(__WIN32__) && !defined(__TWIN32__)
448 JOYCAPS joyCaps;
449 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
450 return FALSE;
451 else
452 return ((joyCaps.wCaps & JOYCAPS_HASR) == JOYCAPS_HASR);
453 #else
454 return FALSE;
455 #endif
456 }
457
458 bool wxJoystick::HasZ() const
459 {
460 #if defined(__WIN32__) && !defined(__TWIN32__)
461 JOYCAPS joyCaps;
462 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
463 return FALSE;
464 else
465 return ((joyCaps.wCaps & JOYCAPS_HASZ) == JOYCAPS_HASZ);
466 #else
467 return FALSE;
468 #endif
469 }
470
471 bool wxJoystick::HasU() const
472 {
473 #if defined(__WIN32__) && !defined(__TWIN32__)
474 JOYCAPS joyCaps;
475 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
476 return FALSE;
477 else
478 return ((joyCaps.wCaps & JOYCAPS_HASU) == JOYCAPS_HASU);
479 #else
480 return FALSE;
481 #endif
482 }
483
484 bool wxJoystick::HasV() const
485 {
486 #if defined(__WIN32__) && !defined(__TWIN32__)
487 JOYCAPS joyCaps;
488 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
489 return FALSE;
490 else
491 return ((joyCaps.wCaps & JOYCAPS_HASV) == JOYCAPS_HASV);
492 #else
493 return FALSE;
494 #endif
495 }
496
497 bool wxJoystick::HasPOV() const
498 {
499 #if defined(__WIN32__) && !defined(__TWIN32__)
500 JOYCAPS joyCaps;
501 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
502 return FALSE;
503 else
504 return ((joyCaps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV);
505 #else
506 return FALSE;
507 #endif
508 }
509
510 bool wxJoystick::HasPOV4Dir() const
511 {
512 #if defined(__WIN32__) && !defined(__TWIN32__)
513 JOYCAPS joyCaps;
514 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
515 return FALSE;
516 else
517 return ((joyCaps.wCaps & JOYCAPS_POV4DIR) == JOYCAPS_POV4DIR);
518 #else
519 return FALSE;
520 #endif
521 }
522
523 bool wxJoystick::HasPOVCTS() const
524 {
525 #if defined(__WIN32__) && !defined(__TWIN32__)
526 JOYCAPS joyCaps;
527 if (joyGetDevCaps(m_joystick, & joyCaps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
528 return FALSE;
529 else
530 return ((joyCaps.wCaps & JOYCAPS_POVCTS) == JOYCAPS_POVCTS);
531 #else
532 return FALSE;
533 #endif
534 }
535
536 // Operations
537 ////////////////////////////////////////////////////////////////////////////
538
539 bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
540 {
541 BOOL changed = (pollingFreq == 0);
542 MMRESULT res = joySetCapture((HWND) win->GetHWND(), m_joystick, pollingFreq, changed);
543 return (res == JOYERR_NOERROR);
544 }
545
546 bool wxJoystick::ReleaseCapture()
547 {
548 MMRESULT res = joyReleaseCapture(m_joystick);
549 return (res == JOYERR_NOERROR);
550 }
551