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