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