]> git.saurik.com Git - wxWidgets.git/blame - src/unix/joystick.cpp
Added 'Back' to accelerator possibilities
[wxWidgets.git] / src / unix / joystick.cpp
CommitLineData
101ceb40
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: joystick.cpp
3// Purpose: wxJoystick class
4// Author: Ported to Linux by Guilhem Lavaux
5// Modified by:
6// Created: 05/23/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
6c9a19aa 9// Licence: wxWindows licence
101ceb40
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
101ceb40
JS
13#pragma implementation "joystick.h"
14#endif
2b510feb 15
14f355c2
VS
16// for compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
5260937e 19#include "wx/defs.h"
2b510feb 20
bb462424 21#if wxUSE_JOYSTICK
101ceb40 22
5260937e
VZ
23#include "wx/joystick.h"
24
101ceb40
JS
25#include <linux/joystick.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/ioctl.h>
30#include <fcntl.h>
31#include <unistd.h>
2b510feb 32
101ceb40
JS
33#include "wx/event.h"
34#include "wx/window.h"
101ceb40
JS
35
36#define JOYSTICK_AXE_MAX 32767
37#define JOYSTICK_AXE_MIN -32767
38
39IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
40
41wxJoystick::wxJoystick(int joystick)
42{
43 wxString dev_name;
44 // Assume it's the same device name on all Linux systems ...
fab591c5 45 dev_name.Printf( wxT("/dev/js%d"), (joystick == wxJOYSTICK1) ? 0 : 1); // FIXME Unicode?
101ceb40 46
fab591c5 47 m_joystick = open(dev_name.fn_str(), O_RDWR);
101ceb40
JS
48 m_lastposition = wxPoint(-1, -1);
49 for (int i=0;i<15;i++)
50 m_axe[i] = 0;
51 if (m_joystick != -1)
52 Create();
53}
54
55////////////////////////////////////////////////////////////////////////////
56// Background thread
57////////////////////////////////////////////////////////////////////////////
58void *wxJoystick::Entry(void)
59{
60 struct js_event j_evt;
61 wxJoystickEvent jwx_event;
62 fd_set read_fds;
63 struct timeval time_out = {0, 0};
64
65 FD_ZERO(&read_fds);
101ceb40
JS
66 while (1) {
67 TestDestroy();
68
69 if (m_polling) {
70 FD_SET(m_joystick, &read_fds);
71 select(m_joystick+1, &read_fds, NULL, NULL, &time_out);
72 if (FD_ISSET(m_joystick, &read_fds))
73 read(m_joystick, &j_evt, sizeof(j_evt));
74 else
75 j_evt.type = 0;
76 } else {
77 read(m_joystick, &j_evt, sizeof(j_evt));
78 }
79
80 if ((j_evt.type & JS_EVENT_AXIS) == JS_EVENT_AXIS) {
81 switch (j_evt.number) {
82 case 1:
83 m_lastposition.x = j_evt.value;
84 jwx_event.SetEventType(wxEVT_JOY_MOVE);
85 break;
86 case 2:
87 m_lastposition.y = j_evt.value;
88 jwx_event.SetEventType(wxEVT_JOY_MOVE);
89 break;
90 case 3:
91 m_axe[3] = j_evt.value;
92 jwx_event.SetEventType(wxEVT_JOY_ZMOVE);
93 break;
94 default:
95 m_axe[j_evt.number] = j_evt.value;
96 jwx_event.SetEventType(wxEVT_JOY_MOVE);
97 break;
98 }
99 jwx_event.SetPosition(m_lastposition);
100 jwx_event.SetZPosition(m_axe[3]);
101 }
102 if ((j_evt.type & JS_EVENT_BUTTON) == JS_EVENT_BUTTON) {
103 register int mask = 1 << j_evt.number;
104 char button = m_buttons & mask;
105
106 m_buttons &= ~mask;
107 if (button) {
108 jwx_event.SetEventType(wxEVT_JOY_BUTTON_UP);
109 } else {
110 jwx_event.SetEventType(wxEVT_JOY_BUTTON_DOWN);
111 m_buttons |= mask;
112 }
113
114 jwx_event.SetButtonState(m_buttons);
115 jwx_event.SetButtonChange(j_evt.number);
116 }
117 }
118 if (m_catchwin)
119 m_catchwin->ProcessEvent(jwx_event);
120 if (m_polling)
121 usleep(m_polling*1000);
122}
123
124////////////////////////////////////////////////////////////////////////////
125// State
126////////////////////////////////////////////////////////////////////////////
127
128wxPoint wxJoystick::GetPosition(void) const
129{
130 return m_lastposition;
131}
132
133int wxJoystick::GetZPosition(void) const
134{
135 return m_axe[3];
136}
137
138int wxJoystick::GetButtonState(void) const
139{
140 return m_buttons;
141}
142
143int wxJoystick::GetPOVPosition(void) const
144{
131f9d9b 145 return -1;
101ceb40
JS
146}
147
148int wxJoystick::GetPOVCTSPosition(void) const
149{
131f9d9b 150 return -1;
101ceb40
JS
151}
152
153int wxJoystick::GetRudderPosition(void) const
154{
155 return m_axe[4];
156}
157
158int wxJoystick::GetUPosition(void) const
159{
160 return m_axe[5];
161}
162
163int wxJoystick::GetVPosition(void) const
164{
165 return m_axe[6];
166}
167
168int wxJoystick::GetMovementThreshold(void) const
169{
170 return 0;
171}
172
173void wxJoystick::SetMovementThreshold(int threshold)
174{
175}
176
177////////////////////////////////////////////////////////////////////////////
178// Capabilities
179////////////////////////////////////////////////////////////////////////////
180
181bool wxJoystick::IsOk(void) const
182{
183 return (m_joystick != -1);
184}
185
186int wxJoystick::GetNumberJoysticks(void) const
187{
188 wxString dev_name;
189 int fd, j;
190
191 for (j=0;j<2;j++) {
fab591c5
RR
192 dev_name.Printf(wxT("/dev/js%d"), j);
193 fd = open(dev_name.fn_str(), O_RDONLY);
101ceb40
JS
194 if (fd == -1)
195 return j;
196 close(fd);
197 }
198 return j;
199}
200
201int wxJoystick::GetManufacturerId(void) const
202{
203 return 0;
204}
205
206int wxJoystick::GetProductId(void) const
207{
208 return 0;
209}
210
211wxString wxJoystick::GetProductName(void) const
212{
131f9d9b
JS
213 wxString dev_name;
214 // 2002-08-20 johan@linkdata.se
215 // Return the device name in lieu of a better one
216 dev_name.Printf( wxT("/dev/js%d"), (m_joystick == wxJOYSTICK1) ? 0 : 1); // FIXME Unicode?
217 return dev_name;
101ceb40
JS
218}
219
220int wxJoystick::GetXMin(void) const
221{
222 return JOYSTICK_AXE_MAX;
223}
224
225int wxJoystick::GetYMin(void) const
226{
227 return JOYSTICK_AXE_MAX;
228}
229
230int wxJoystick::GetZMin(void) const
231{
232 return JOYSTICK_AXE_MAX;
233}
234
235int wxJoystick::GetXMax(void) const
236{
237 return JOYSTICK_AXE_MAX;
238}
239
240int wxJoystick::GetYMax(void) const
241{
242 return JOYSTICK_AXE_MAX;
243}
244
245int wxJoystick::GetZMax(void) const
246{
247 return JOYSTICK_AXE_MAX;
248}
249
250int wxJoystick::GetNumberButtons(void) const
251{
252 int nb;
253
254 ioctl(m_joystick, JSIOCGBUTTONS, &nb);
255
256 return nb;
257}
258
259int wxJoystick::GetNumberAxes(void) const
260{
261 int nb;
262
263 ioctl(m_joystick, JSIOCGAXES, &nb);
264
265 return nb;
266}
267
268int wxJoystick::GetMaxButtons(void) const
269{
270 return 15; // internal
271}
272
273int wxJoystick::GetMaxAxes(void) const
274{
275 return 15; // internal
276}
277
278int wxJoystick::GetPollingMin(void) const
279{
280 return -1;
281}
282
283int wxJoystick::GetPollingMax(void) const
284{
285 return -1;
286}
287
288int wxJoystick::GetRudderMin(void) const
289{
290 return JOYSTICK_AXE_MIN;
291}
292
293int wxJoystick::GetRudderMax(void) const
294{
295 return JOYSTICK_AXE_MAX;
296}
297
298int wxJoystick::GetUMin(void) const
299{
300 return JOYSTICK_AXE_MIN;
301}
302
303int wxJoystick::GetUMax(void) const
304{
305 return JOYSTICK_AXE_MAX;
306}
307
308int wxJoystick::GetVMin(void) const
309{
310 return JOYSTICK_AXE_MIN;
311}
312
313int wxJoystick::GetVMax(void) const
314{
315 return JOYSTICK_AXE_MAX;
316}
317
318bool wxJoystick::HasRudder(void) const
319{
320 return GetNumberAxes() >= 4;
321}
322
323bool wxJoystick::HasZ(void) const
324{
325 return GetNumberAxes() >= 3;
326}
327
328bool wxJoystick::HasU(void) const
329{
330 return GetNumberAxes() >= 5;
331}
332
333bool wxJoystick::HasV(void) const
334{
335 return GetNumberAxes() >= 6;
336}
337
338bool wxJoystick::HasPOV(void) const
339{
340 return FALSE;
341}
342
343bool wxJoystick::HasPOV4Dir(void) const
344{
345 return FALSE;
346}
347
348bool wxJoystick::HasPOVCTS(void) const
349{
350 return FALSE;
351}
352
353////////////////////////////////////////////////////////////////////////////
354// Operations
355////////////////////////////////////////////////////////////////////////////
356
be5fced5 357bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
101ceb40
JS
358{
359 m_catchwin = win;
360 m_polling = pollingFreq;
361 return TRUE;
362}
363
364bool wxJoystick::ReleaseCapture(void)
365{
366 m_catchwin = NULL;
367 m_polling = 0;
368 return TRUE;
369}
f6bcfd97 370#endif // wxUSE_JOYSTICK
101ceb40 371