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