]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/joystick.cpp
fixed yet another printf() format warning
[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 licence
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( wxT("/dev/js%d"), (joystick == wxJOYSTICK1) ? 0 : 1); // FIXME Unicode?
43
44 m_joystick = open(dev_name.fn_str(), 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 -1;
143 }
144
145 int wxJoystick::GetPOVCTSPosition(void) const
146 {
147 return -1;
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(wxT("/dev/js%d"), j);
190 fd = open(dev_name.fn_str(), 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 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;
215 }
216
217 int wxJoystick::GetXMin(void) const
218 {
219 return JOYSTICK_AXE_MAX;
220 }
221
222 int wxJoystick::GetYMin(void) const
223 {
224 return JOYSTICK_AXE_MAX;
225 }
226
227 int wxJoystick::GetZMin(void) const
228 {
229 return JOYSTICK_AXE_MAX;
230 }
231
232 int wxJoystick::GetXMax(void) const
233 {
234 return JOYSTICK_AXE_MAX;
235 }
236
237 int wxJoystick::GetYMax(void) const
238 {
239 return JOYSTICK_AXE_MAX;
240 }
241
242 int wxJoystick::GetZMax(void) const
243 {
244 return JOYSTICK_AXE_MAX;
245 }
246
247 int wxJoystick::GetNumberButtons(void) const
248 {
249 int nb;
250
251 ioctl(m_joystick, JSIOCGBUTTONS, &nb);
252
253 return nb;
254 }
255
256 int wxJoystick::GetNumberAxes(void) const
257 {
258 int nb;
259
260 ioctl(m_joystick, JSIOCGAXES, &nb);
261
262 return nb;
263 }
264
265 int wxJoystick::GetMaxButtons(void) const
266 {
267 return 15; // internal
268 }
269
270 int wxJoystick::GetMaxAxes(void) const
271 {
272 return 15; // internal
273 }
274
275 int wxJoystick::GetPollingMin(void) const
276 {
277 return -1;
278 }
279
280 int wxJoystick::GetPollingMax(void) const
281 {
282 return -1;
283 }
284
285 int wxJoystick::GetRudderMin(void) const
286 {
287 return JOYSTICK_AXE_MIN;
288 }
289
290 int wxJoystick::GetRudderMax(void) const
291 {
292 return JOYSTICK_AXE_MAX;
293 }
294
295 int wxJoystick::GetUMin(void) const
296 {
297 return JOYSTICK_AXE_MIN;
298 }
299
300 int wxJoystick::GetUMax(void) const
301 {
302 return JOYSTICK_AXE_MAX;
303 }
304
305 int wxJoystick::GetVMin(void) const
306 {
307 return JOYSTICK_AXE_MIN;
308 }
309
310 int wxJoystick::GetVMax(void) const
311 {
312 return JOYSTICK_AXE_MAX;
313 }
314
315 bool wxJoystick::HasRudder(void) const
316 {
317 return GetNumberAxes() >= 4;
318 }
319
320 bool wxJoystick::HasZ(void) const
321 {
322 return GetNumberAxes() >= 3;
323 }
324
325 bool wxJoystick::HasU(void) const
326 {
327 return GetNumberAxes() >= 5;
328 }
329
330 bool wxJoystick::HasV(void) const
331 {
332 return GetNumberAxes() >= 6;
333 }
334
335 bool wxJoystick::HasPOV(void) const
336 {
337 return FALSE;
338 }
339
340 bool wxJoystick::HasPOV4Dir(void) const
341 {
342 return FALSE;
343 }
344
345 bool wxJoystick::HasPOVCTS(void) const
346 {
347 return FALSE;
348 }
349
350 ////////////////////////////////////////////////////////////////////////////
351 // Operations
352 ////////////////////////////////////////////////////////////////////////////
353
354 bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
355 {
356 m_catchwin = win;
357 m_polling = pollingFreq;
358 return TRUE;
359 }
360
361 bool wxJoystick::ReleaseCapture(void)
362 {
363 m_catchwin = NULL;
364 m_polling = 0;
365 return TRUE;
366 }
367 #endif // wxUSE_JOYSTICK
368