]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/joystick.cpp
use WXUNUSED(foo), not (void)foo
[wxWidgets.git] / src / gtk1 / joystick.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "joystick.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #if wxUSE_JOYSTICK
19
20 #include "wx/joystick.h"
21
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>
29
30 #include "wx/event.h"
31 #include "wx/window.h"
32
33 #define JOYSTICK_AXE_MAX 32767
34 #define JOYSTICK_AXE_MIN -32767
35
36 IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
37
38 wxJoystick::wxJoystick(int joystick)
39 {
40 wxString dev_name;
41 // Assume it's the same device name on all Linux systems ...
42 dev_name.Printf("/dev/js%d", (joystick == wxJOYSTICK1) ? 0 : 1);
43
44 m_joystick = open(dev_name, O_RDWR);
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 ////////////////////////////////////////////////////////////////////////////
55 void *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);
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
125 wxPoint wxJoystick::GetPosition(void) const
126 {
127 return m_lastposition;
128 }
129
130 int wxJoystick::GetZPosition(void) const
131 {
132 return m_axe[3];
133 }
134
135 int wxJoystick::GetButtonState(void) const
136 {
137 return m_buttons;
138 }
139
140 int wxJoystick::GetPOVPosition(void) const
141 {
142 return 0;
143 }
144
145 int wxJoystick::GetPOVCTSPosition(void) const
146 {
147 return 0;
148 }
149
150 int wxJoystick::GetRudderPosition(void) const
151 {
152 return m_axe[4];
153 }
154
155 int wxJoystick::GetUPosition(void) const
156 {
157 return m_axe[5];
158 }
159
160 int wxJoystick::GetVPosition(void) const
161 {
162 return m_axe[6];
163 }
164
165 int wxJoystick::GetMovementThreshold(void) const
166 {
167 return 0;
168 }
169
170 void wxJoystick::SetMovementThreshold(int threshold)
171 {
172 }
173
174 ////////////////////////////////////////////////////////////////////////////
175 // Capabilities
176 ////////////////////////////////////////////////////////////////////////////
177
178 bool wxJoystick::IsOk(void) const
179 {
180 return (m_joystick != -1);
181 }
182
183 int wxJoystick::GetNumberJoysticks(void) const
184 {
185 wxString dev_name;
186 int fd, j;
187
188 for (j=0;j<2;j++) {
189 dev_name.Printf("/dev/js%d", j);
190 fd = open(dev_name, O_RDONLY);
191 if (fd == -1)
192 return j;
193 close(fd);
194 }
195 return j;
196 }
197
198 int wxJoystick::GetManufacturerId(void) const
199 {
200 return 0;
201 }
202
203 int wxJoystick::GetProductId(void) const
204 {
205 return 0;
206 }
207
208 wxString wxJoystick::GetProductName(void) const
209 {
210 return wxT("");
211 }
212
213 int wxJoystick::GetXMin(void) const
214 {
215 return JOYSTICK_AXE_MAX;
216 }
217
218 int wxJoystick::GetYMin(void) const
219 {
220 return JOYSTICK_AXE_MAX;
221 }
222
223 int wxJoystick::GetZMin(void) const
224 {
225 return JOYSTICK_AXE_MAX;
226 }
227
228 int wxJoystick::GetXMax(void) const
229 {
230 return JOYSTICK_AXE_MAX;
231 }
232
233 int wxJoystick::GetYMax(void) const
234 {
235 return JOYSTICK_AXE_MAX;
236 }
237
238 int wxJoystick::GetZMax(void) const
239 {
240 return JOYSTICK_AXE_MAX;
241 }
242
243 int wxJoystick::GetNumberButtons(void) const
244 {
245 int nb;
246
247 ioctl(m_joystick, JSIOCGBUTTONS, &nb);
248
249 return nb;
250 }
251
252 int wxJoystick::GetNumberAxes(void) const
253 {
254 int nb;
255
256 ioctl(m_joystick, JSIOCGAXES, &nb);
257
258 return nb;
259 }
260
261 int wxJoystick::GetMaxButtons(void) const
262 {
263 return 15; // internal
264 }
265
266 int wxJoystick::GetMaxAxes(void) const
267 {
268 return 15; // internal
269 }
270
271 int wxJoystick::GetPollingMin(void) const
272 {
273 return -1;
274 }
275
276 int wxJoystick::GetPollingMax(void) const
277 {
278 return -1;
279 }
280
281 int wxJoystick::GetRudderMin(void) const
282 {
283 return JOYSTICK_AXE_MIN;
284 }
285
286 int wxJoystick::GetRudderMax(void) const
287 {
288 return JOYSTICK_AXE_MAX;
289 }
290
291 int wxJoystick::GetUMin(void) const
292 {
293 return JOYSTICK_AXE_MIN;
294 }
295
296 int wxJoystick::GetUMax(void) const
297 {
298 return JOYSTICK_AXE_MAX;
299 }
300
301 int wxJoystick::GetVMin(void) const
302 {
303 return JOYSTICK_AXE_MIN;
304 }
305
306 int wxJoystick::GetVMax(void) const
307 {
308 return JOYSTICK_AXE_MAX;
309 }
310
311 bool wxJoystick::HasRudder(void) const
312 {
313 return GetNumberAxes() >= 4;
314 }
315
316 bool wxJoystick::HasZ(void) const
317 {
318 return GetNumberAxes() >= 3;
319 }
320
321 bool wxJoystick::HasU(void) const
322 {
323 return GetNumberAxes() >= 5;
324 }
325
326 bool wxJoystick::HasV(void) const
327 {
328 return GetNumberAxes() >= 6;
329 }
330
331 bool wxJoystick::HasPOV(void) const
332 {
333 return FALSE;
334 }
335
336 bool wxJoystick::HasPOV4Dir(void) const
337 {
338 return FALSE;
339 }
340
341 bool wxJoystick::HasPOVCTS(void) const
342 {
343 return FALSE;
344 }
345
346 ////////////////////////////////////////////////////////////////////////////
347 // Operations
348 ////////////////////////////////////////////////////////////////////////////
349
350 bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq = 0)
351 {
352 m_catchwin = win;
353 m_polling = pollingFreq;
354 return TRUE;
355 }
356
357 bool wxJoystick::ReleaseCapture(void)
358 {
359 m_catchwin = NULL;
360 m_polling = 0;
361 return TRUE;
362 }
363 #endif // wxUSE_JOYSTICK
364