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"
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
213 int wxJoystick::GetXMin(void) const
215 return JOYSTICK_AXE_MAX
;
218 int wxJoystick::GetYMin(void) const
220 return JOYSTICK_AXE_MAX
;
223 int wxJoystick::GetZMin(void) const
225 return JOYSTICK_AXE_MAX
;
228 int wxJoystick::GetXMax(void) const
230 return JOYSTICK_AXE_MAX
;
233 int wxJoystick::GetYMax(void) const
235 return JOYSTICK_AXE_MAX
;
238 int wxJoystick::GetZMax(void) const
240 return JOYSTICK_AXE_MAX
;
243 int wxJoystick::GetNumberButtons(void) const
247 ioctl(m_joystick
, JSIOCGBUTTONS
, &nb
);
252 int wxJoystick::GetNumberAxes(void) const
256 ioctl(m_joystick
, JSIOCGAXES
, &nb
);
261 int wxJoystick::GetMaxButtons(void) const
263 return 15; // internal
266 int wxJoystick::GetMaxAxes(void) const
268 return 15; // internal
271 int wxJoystick::GetPollingMin(void) const
276 int wxJoystick::GetPollingMax(void) const
281 int wxJoystick::GetRudderMin(void) const
283 return JOYSTICK_AXE_MIN
;
286 int wxJoystick::GetRudderMax(void) const
288 return JOYSTICK_AXE_MAX
;
291 int wxJoystick::GetUMin(void) const
293 return JOYSTICK_AXE_MIN
;
296 int wxJoystick::GetUMax(void) const
298 return JOYSTICK_AXE_MAX
;
301 int wxJoystick::GetVMin(void) const
303 return JOYSTICK_AXE_MIN
;
306 int wxJoystick::GetVMax(void) const
308 return JOYSTICK_AXE_MAX
;
311 bool wxJoystick::HasRudder(void) const
313 return GetNumberAxes() >= 4;
316 bool wxJoystick::HasZ(void) const
318 return GetNumberAxes() >= 3;
321 bool wxJoystick::HasU(void) const
323 return GetNumberAxes() >= 5;
326 bool wxJoystick::HasV(void) const
328 return GetNumberAxes() >= 6;
331 bool wxJoystick::HasPOV(void) const
336 bool wxJoystick::HasPOV4Dir(void) const
341 bool wxJoystick::HasPOVCTS(void) const
346 ////////////////////////////////////////////////////////////////////////////
348 ////////////////////////////////////////////////////////////////////////////
350 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
353 m_polling
= pollingFreq
;
357 bool wxJoystick::ReleaseCapture(void)
363 #endif // wxUSE_JOYSTICK