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