]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/joystick.cpp
Since there was no #include before the #if wxUSE_JOYSTICK then nothing
[wxWidgets.git] / src / gtk / joystick.cpp
... / ...
CommitLineData
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
34IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)
35
36wxJoystick::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////////////////////////////////////////////////////////////////////////////
53void *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
123wxPoint wxJoystick::GetPosition(void) const
124{
125 return m_lastposition;
126}
127
128int wxJoystick::GetZPosition(void) const
129{
130 return m_axe[3];
131}
132
133int wxJoystick::GetButtonState(void) const
134{
135 return m_buttons;
136}
137
138int wxJoystick::GetPOVPosition(void) const
139{
140 return 0;
141}
142
143int wxJoystick::GetPOVCTSPosition(void) const
144{
145 return 0;
146}
147
148int wxJoystick::GetRudderPosition(void) const
149{
150 return m_axe[4];
151}
152
153int wxJoystick::GetUPosition(void) const
154{
155 return m_axe[5];
156}
157
158int wxJoystick::GetVPosition(void) const
159{
160 return m_axe[6];
161}
162
163int wxJoystick::GetMovementThreshold(void) const
164{
165 return 0;
166}
167
168void wxJoystick::SetMovementThreshold(int threshold)
169{
170}
171
172////////////////////////////////////////////////////////////////////////////
173// Capabilities
174////////////////////////////////////////////////////////////////////////////
175
176bool wxJoystick::IsOk(void) const
177{
178 return (m_joystick != -1);
179}
180
181int 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
196int wxJoystick::GetManufacturerId(void) const
197{
198 return 0;
199}
200
201int wxJoystick::GetProductId(void) const
202{
203 return 0;
204}
205
206wxString wxJoystick::GetProductName(void) const
207{
208 return wxT("");
209}
210
211int wxJoystick::GetXMin(void) const
212{
213 return JOYSTICK_AXE_MAX;
214}
215
216int wxJoystick::GetYMin(void) const
217{
218 return JOYSTICK_AXE_MAX;
219}
220
221int wxJoystick::GetZMin(void) const
222{
223 return JOYSTICK_AXE_MAX;
224}
225
226int wxJoystick::GetXMax(void) const
227{
228 return JOYSTICK_AXE_MAX;
229}
230
231int wxJoystick::GetYMax(void) const
232{
233 return JOYSTICK_AXE_MAX;
234}
235
236int wxJoystick::GetZMax(void) const
237{
238 return JOYSTICK_AXE_MAX;
239}
240
241int wxJoystick::GetNumberButtons(void) const
242{
243 int nb;
244
245 ioctl(m_joystick, JSIOCGBUTTONS, &nb);
246
247 return nb;
248}
249
250int wxJoystick::GetNumberAxes(void) const
251{
252 int nb;
253
254 ioctl(m_joystick, JSIOCGAXES, &nb);
255
256 return nb;
257}
258
259int wxJoystick::GetMaxButtons(void) const
260{
261 return 15; // internal
262}
263
264int wxJoystick::GetMaxAxes(void) const
265{
266 return 15; // internal
267}
268
269int wxJoystick::GetPollingMin(void) const
270{
271 return -1;
272}
273
274int wxJoystick::GetPollingMax(void) const
275{
276 return -1;
277}
278
279int wxJoystick::GetRudderMin(void) const
280{
281 return JOYSTICK_AXE_MIN;
282}
283
284int wxJoystick::GetRudderMax(void) const
285{
286 return JOYSTICK_AXE_MAX;
287}
288
289int wxJoystick::GetUMin(void) const
290{
291 return JOYSTICK_AXE_MIN;
292}
293
294int wxJoystick::GetUMax(void) const
295{
296 return JOYSTICK_AXE_MAX;
297}
298
299int wxJoystick::GetVMin(void) const
300{
301 return JOYSTICK_AXE_MIN;
302}
303
304int wxJoystick::GetVMax(void) const
305{
306 return JOYSTICK_AXE_MAX;
307}
308
309bool wxJoystick::HasRudder(void) const
310{
311 return GetNumberAxes() >= 4;
312}
313
314bool wxJoystick::HasZ(void) const
315{
316 return GetNumberAxes() >= 3;
317}
318
319bool wxJoystick::HasU(void) const
320{
321 return GetNumberAxes() >= 5;
322}
323
324bool wxJoystick::HasV(void) const
325{
326 return GetNumberAxes() >= 6;
327}
328
329bool wxJoystick::HasPOV(void) const
330{
331 return FALSE;
332}
333
334bool wxJoystick::HasPOV4Dir(void) const
335{
336 return FALSE;
337}
338
339bool wxJoystick::HasPOVCTS(void) const
340{
341 return FALSE;
342}
343
344////////////////////////////////////////////////////////////////////////////
345// Operations
346////////////////////////////////////////////////////////////////////////////
347
348bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq = 0)
349{
350 m_catchwin = win;
351 m_polling = pollingFreq;
352 return TRUE;
353}
354
355bool wxJoystick::ReleaseCapture(void)
356{
357 m_catchwin = NULL;
358 m_polling = 0;
359 return TRUE;
360}
361#endif // wxUSE_JOYSTICK
362