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