1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxJoystick class
4 // Author: Ported to Linux by Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "joystick.h"
16 #include <linux/joystick.h>
17 #include <sys/types.h>
20 #include <sys/ioctl.h>
24 #include "wx/window.h"
25 #include "wx/gtk/joystick.h"
27 #define JOYSTICK_AXE_MAX 32767
28 #define JOYSTICK_AXE_MIN -32767
30 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
32 wxJoystick::wxJoystick(int joystick
)
35 // Assume it's the same device name on all Linux systems ...
36 dev_name
.Printf("/dev/js%d", (joystick
== wxJOYSTICK1
) ? 0 : 1);
38 m_joystick
= open(dev_name
, O_RDWR
);
39 m_lastposition
= wxPoint(-1, -1);
40 for (int i
=0;i
<15;i
++)
46 ////////////////////////////////////////////////////////////////////////////
48 ////////////////////////////////////////////////////////////////////////////
49 void *wxJoystick::Entry(void)
51 struct js_event j_evt
;
52 wxJoystickEvent jwx_event
;
54 struct timeval time_out
= {0, 0};
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
));
68 read(m_joystick
, &j_evt
, sizeof(j_evt
));
71 if ((j_evt
.type
& JS_EVENT_AXIS
) == JS_EVENT_AXIS
) {
72 switch (j_evt
.number
) {
74 m_lastposition
.x
= j_evt
.value
;
75 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
78 m_lastposition
.y
= j_evt
.value
;
79 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
82 m_axe
[3] = j_evt
.value
;
83 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
86 m_axe
[j_evt
.number
] = j_evt
.value
;
87 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
90 jwx_event
.SetPosition(m_lastposition
);
91 jwx_event
.SetZPosition(m_axe
[3]);
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
;
99 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
101 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
105 jwx_event
.SetButtonState(m_buttons
);
106 jwx_event
.SetButtonChange(j_evt
.number
);
110 m_catchwin
->ProcessEvent(jwx_event
);
112 usleep(m_polling
*1000);
115 ////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////
119 wxPoint
wxJoystick::GetPosition(void) const
121 return m_lastposition
;
124 int wxJoystick::GetZPosition(void) const
129 int wxJoystick::GetButtonState(void) const
134 int wxJoystick::GetPOVPosition(void) const
139 int wxJoystick::GetPOVCTSPosition(void) const
144 int wxJoystick::GetRudderPosition(void) const
149 int wxJoystick::GetUPosition(void) const
154 int wxJoystick::GetVPosition(void) const
159 int wxJoystick::GetMovementThreshold(void) const
164 void wxJoystick::SetMovementThreshold(int threshold
)
168 ////////////////////////////////////////////////////////////////////////////
170 ////////////////////////////////////////////////////////////////////////////
172 bool wxJoystick::IsOk(void) const
174 return (m_joystick
!= -1);
177 int wxJoystick::GetNumberJoysticks(void) const
183 dev_name
.Printf("/dev/js%d", j
);
184 fd
= open(dev_name
, O_RDONLY
);
192 int wxJoystick::GetManufacturerId(void) const
197 int wxJoystick::GetProductId(void) const
202 wxString
wxJoystick::GetProductName(void) const
207 int wxJoystick::GetXMin(void) const
209 return JOYSTICK_AXE_MAX
;
212 int wxJoystick::GetYMin(void) const
214 return JOYSTICK_AXE_MAX
;
217 int wxJoystick::GetZMin(void) const
219 return JOYSTICK_AXE_MAX
;
222 int wxJoystick::GetXMax(void) const
224 return JOYSTICK_AXE_MAX
;
227 int wxJoystick::GetYMax(void) const
229 return JOYSTICK_AXE_MAX
;
232 int wxJoystick::GetZMax(void) const
234 return JOYSTICK_AXE_MAX
;
237 int wxJoystick::GetNumberButtons(void) const
241 ioctl(m_joystick
, JSIOCGBUTTONS
, &nb
);
246 int wxJoystick::GetNumberAxes(void) const
250 ioctl(m_joystick
, JSIOCGAXES
, &nb
);
255 int wxJoystick::GetMaxButtons(void) const
257 return 15; // internal
260 int wxJoystick::GetMaxAxes(void) const
262 return 15; // internal
265 int wxJoystick::GetPollingMin(void) const
270 int wxJoystick::GetPollingMax(void) const
275 int wxJoystick::GetRudderMin(void) const
277 return JOYSTICK_AXE_MIN
;
280 int wxJoystick::GetRudderMax(void) const
282 return JOYSTICK_AXE_MAX
;
285 int wxJoystick::GetUMin(void) const
287 return JOYSTICK_AXE_MIN
;
290 int wxJoystick::GetUMax(void) const
292 return JOYSTICK_AXE_MAX
;
295 int wxJoystick::GetVMin(void) const
297 return JOYSTICK_AXE_MIN
;
300 int wxJoystick::GetVMax(void) const
302 return JOYSTICK_AXE_MAX
;
305 bool wxJoystick::HasRudder(void) const
307 return GetNumberAxes() >= 4;
310 bool wxJoystick::HasZ(void) const
312 return GetNumberAxes() >= 3;
315 bool wxJoystick::HasU(void) const
317 return GetNumberAxes() >= 5;
320 bool wxJoystick::HasV(void) const
322 return GetNumberAxes() >= 6;
325 bool wxJoystick::HasPOV(void) const
330 bool wxJoystick::HasPOV4Dir(void) const
335 bool wxJoystick::HasPOVCTS(void) const
340 ////////////////////////////////////////////////////////////////////////////
342 ////////////////////////////////////////////////////////////////////////////
344 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
= 0)
347 m_polling
= pollingFreq
;
351 bool wxJoystick::ReleaseCapture(void)