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