1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxJoystick class
4 // Author: Ported to Linux by Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "joystick.h"
20 #include "wx/joystick.h"
22 #include <linux/joystick.h>
23 #include <sys/types.h>
26 #include <sys/ioctl.h>
31 #include "wx/window.h"
33 #define JOYSTICK_AXE_MAX 32767
34 #define JOYSTICK_AXE_MIN -32767
36 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
38 wxJoystick::wxJoystick(int joystick
)
41 // Assume it's the same device name on all Linux systems ...
42 dev_name
.Printf( wxT("/dev/js%d"), (joystick
== wxJOYSTICK1
) ? 0 : 1); // FIXME Unicode?
44 m_joystick
= open(dev_name
.fn_str(), O_RDWR
);
45 m_lastposition
= wxPoint(-1, -1);
46 for (int i
=0;i
<15;i
++)
52 ////////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////////
55 void *wxJoystick::Entry(void)
57 struct js_event j_evt
;
58 wxJoystickEvent jwx_event
;
60 struct timeval time_out
= {0, 0};
67 FD_SET(m_joystick
, &read_fds
);
68 select(m_joystick
+1, &read_fds
, NULL
, NULL
, &time_out
);
69 if (FD_ISSET(m_joystick
, &read_fds
))
70 read(m_joystick
, &j_evt
, sizeof(j_evt
));
74 read(m_joystick
, &j_evt
, sizeof(j_evt
));
77 if ((j_evt
.type
& JS_EVENT_AXIS
) == JS_EVENT_AXIS
) {
78 switch (j_evt
.number
) {
80 m_lastposition
.x
= j_evt
.value
;
81 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
84 m_lastposition
.y
= j_evt
.value
;
85 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
88 m_axe
[3] = j_evt
.value
;
89 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
92 m_axe
[j_evt
.number
] = j_evt
.value
;
93 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
96 jwx_event
.SetPosition(m_lastposition
);
97 jwx_event
.SetZPosition(m_axe
[3]);
99 if ((j_evt
.type
& JS_EVENT_BUTTON
) == JS_EVENT_BUTTON
) {
100 register int mask
= 1 << j_evt
.number
;
101 char button
= m_buttons
& mask
;
105 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
107 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
111 jwx_event
.SetButtonState(m_buttons
);
112 jwx_event
.SetButtonChange(j_evt
.number
);
116 m_catchwin
->ProcessEvent(jwx_event
);
118 usleep(m_polling
*1000);
121 ////////////////////////////////////////////////////////////////////////////
123 ////////////////////////////////////////////////////////////////////////////
125 wxPoint
wxJoystick::GetPosition(void) const
127 return m_lastposition
;
130 int wxJoystick::GetZPosition(void) const
135 int wxJoystick::GetButtonState(void) const
140 int wxJoystick::GetPOVPosition(void) const
145 int wxJoystick::GetPOVCTSPosition(void) const
150 int wxJoystick::GetRudderPosition(void) const
155 int wxJoystick::GetUPosition(void) const
160 int wxJoystick::GetVPosition(void) const
165 int wxJoystick::GetMovementThreshold(void) const
170 void wxJoystick::SetMovementThreshold(int threshold
)
174 ////////////////////////////////////////////////////////////////////////////
176 ////////////////////////////////////////////////////////////////////////////
178 bool wxJoystick::IsOk(void) const
180 return (m_joystick
!= -1);
183 int wxJoystick::GetNumberJoysticks(void) const
189 dev_name
.Printf(wxT("/dev/js%d"), j
);
190 fd
= open(dev_name
.fn_str(), O_RDONLY
);
198 int wxJoystick::GetManufacturerId(void) const
203 int wxJoystick::GetProductId(void) const
208 wxString
wxJoystick::GetProductName(void) const
211 // 2002-08-20 johan@linkdata.se
212 // Return the device name in lieu of a better one
213 dev_name
.Printf( wxT("/dev/js%d"), (m_joystick
== wxJOYSTICK1
) ? 0 : 1); // FIXME Unicode?
217 int wxJoystick::GetXMin(void) const
219 return JOYSTICK_AXE_MAX
;
222 int wxJoystick::GetYMin(void) const
224 return JOYSTICK_AXE_MAX
;
227 int wxJoystick::GetZMin(void) const
229 return JOYSTICK_AXE_MAX
;
232 int wxJoystick::GetXMax(void) const
234 return JOYSTICK_AXE_MAX
;
237 int wxJoystick::GetYMax(void) const
239 return JOYSTICK_AXE_MAX
;
242 int wxJoystick::GetZMax(void) const
244 return JOYSTICK_AXE_MAX
;
247 int wxJoystick::GetNumberButtons(void) const
251 ioctl(m_joystick
, JSIOCGBUTTONS
, &nb
);
256 int wxJoystick::GetNumberAxes(void) const
260 ioctl(m_joystick
, JSIOCGAXES
, &nb
);
265 int wxJoystick::GetMaxButtons(void) const
267 return 15; // internal
270 int wxJoystick::GetMaxAxes(void) const
272 return 15; // internal
275 int wxJoystick::GetPollingMin(void) const
280 int wxJoystick::GetPollingMax(void) const
285 int wxJoystick::GetRudderMin(void) const
287 return JOYSTICK_AXE_MIN
;
290 int wxJoystick::GetRudderMax(void) const
292 return JOYSTICK_AXE_MAX
;
295 int wxJoystick::GetUMin(void) const
297 return JOYSTICK_AXE_MIN
;
300 int wxJoystick::GetUMax(void) const
302 return JOYSTICK_AXE_MAX
;
305 int wxJoystick::GetVMin(void) const
307 return JOYSTICK_AXE_MIN
;
310 int wxJoystick::GetVMax(void) const
312 return JOYSTICK_AXE_MAX
;
315 bool wxJoystick::HasRudder(void) const
317 return GetNumberAxes() >= 4;
320 bool wxJoystick::HasZ(void) const
322 return GetNumberAxes() >= 3;
325 bool wxJoystick::HasU(void) const
327 return GetNumberAxes() >= 5;
330 bool wxJoystick::HasV(void) const
332 return GetNumberAxes() >= 6;
335 bool wxJoystick::HasPOV(void) const
340 bool wxJoystick::HasPOV4Dir(void) const
345 bool wxJoystick::HasPOVCTS(void) const
350 ////////////////////////////////////////////////////////////////////////////
352 ////////////////////////////////////////////////////////////////////////////
354 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
357 m_polling
= pollingFreq
;
361 bool wxJoystick::ReleaseCapture(void)
367 #endif // wxUSE_JOYSTICK