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