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