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};
62 FD_SET(m_joystick
, &read_fds
);
63 select(m_joystick
+1, &read_fds
, NULL
, NULL
, &time_out
);
64 if (FD_ISSET(m_joystick
, &read_fds
))
65 read(m_joystick
, &j_evt
, sizeof(j_evt
));
69 read(m_joystick
, &j_evt
, sizeof(j_evt
));
72 if ((j_evt
.type
& JS_EVENT_AXIS
) == JS_EVENT_AXIS
) {
73 switch (j_evt
.number
) {
75 m_lastposition
.x
= j_evt
.value
;
76 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
79 m_lastposition
.y
= j_evt
.value
;
80 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
83 m_axe
[3] = j_evt
.value
;
84 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
87 m_axe
[j_evt
.number
] = j_evt
.value
;
88 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
91 jwx_event
.SetPosition(m_lastposition
);
92 jwx_event
.SetZPosition(m_axe
[3]);
94 if ((j_evt
.type
& JS_EVENT_BUTTON
) == JS_EVENT_BUTTON
) {
95 register int mask
= 1 << j_evt
.number
;
96 char button
= m_buttons
& mask
;
100 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
102 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
106 jwx_event
.SetButtonState(m_buttons
);
107 jwx_event
.SetButtonChange(j_evt
.number
);
111 m_catchwin
->ProcessEvent(jwx_event
);
113 usleep(m_polling
*1000);
116 ////////////////////////////////////////////////////////////////////////////
118 ////////////////////////////////////////////////////////////////////////////
120 wxPoint
wxJoystick::GetPosition(void) const
122 return m_lastposition
;
125 int wxJoystick::GetZPosition(void) const
130 int wxJoystick::GetButtonState(void) const
135 int wxJoystick::GetPOVPosition(void) const
140 int wxJoystick::GetPOVCTSPosition(void) const
145 int wxJoystick::GetRudderPosition(void) const
150 int wxJoystick::GetUPosition(void) const
155 int wxJoystick::GetVPosition(void) const
160 int wxJoystick::GetMovementThreshold(void) const
165 void wxJoystick::SetMovementThreshold(int threshold
)
169 ////////////////////////////////////////////////////////////////////////////
171 ////////////////////////////////////////////////////////////////////////////
173 bool wxJoystick::IsOk(void) const
175 return (m_joystick
!= -1);
178 int wxJoystick::GetNumberJoysticks(void) const
184 dev_name
.Printf("/dev/js%d", j
);
185 fd
= open(dev_name
, O_RDONLY
);
193 int wxJoystick::GetManufacturerId(void) const
198 int wxJoystick::GetProductId(void) const
203 wxString
wxJoystick::GetProductName(void) const
208 int wxJoystick::GetXMin(void) const
210 return JOYSTICK_AXE_MAX
;
213 int wxJoystick::GetYMin(void) const
215 return JOYSTICK_AXE_MAX
;
218 int wxJoystick::GetZMin(void) const
220 return JOYSTICK_AXE_MAX
;
223 int wxJoystick::GetXMax(void) const
225 return JOYSTICK_AXE_MAX
;
228 int wxJoystick::GetYMax(void) const
230 return JOYSTICK_AXE_MAX
;
233 int wxJoystick::GetZMax(void) const
235 return JOYSTICK_AXE_MAX
;
238 int wxJoystick::GetNumberButtons(void) const
242 ioctl(m_joystick
, JSIOCGBUTTONS
, &nb
);
247 int wxJoystick::GetNumberAxes(void) const
251 ioctl(m_joystick
, JSIOCGAXES
, &nb
);
256 int wxJoystick::GetMaxButtons(void) const
258 return 15; // internal
261 int wxJoystick::GetMaxAxes(void) const
263 return 15; // internal
266 int wxJoystick::GetPollingMin(void) const
271 int wxJoystick::GetPollingMax(void) const
276 int wxJoystick::GetRudderMin(void) const
278 return JOYSTICK_AXE_MIN
;
281 int wxJoystick::GetRudderMax(void) const
283 return JOYSTICK_AXE_MAX
;
286 int wxJoystick::GetUMin(void) const
288 return JOYSTICK_AXE_MIN
;
291 int wxJoystick::GetUMax(void) const
293 return JOYSTICK_AXE_MAX
;
296 int wxJoystick::GetVMin(void) const
298 return JOYSTICK_AXE_MIN
;
301 int wxJoystick::GetVMax(void) const
303 return JOYSTICK_AXE_MAX
;
306 bool wxJoystick::HasRudder(void) const
308 return GetNumberAxes() >= 4;
311 bool wxJoystick::HasZ(void) const
313 return GetNumberAxes() >= 3;
316 bool wxJoystick::HasU(void) const
318 return GetNumberAxes() >= 5;
321 bool wxJoystick::HasV(void) const
323 return GetNumberAxes() >= 6;
326 bool wxJoystick::HasPOV(void) const
331 bool wxJoystick::HasPOV4Dir(void) const
336 bool wxJoystick::HasPOVCTS(void) const
341 ////////////////////////////////////////////////////////////////////////////
343 ////////////////////////////////////////////////////////////////////////////
345 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
= 0)
348 m_polling
= pollingFreq
;
352 bool wxJoystick::ReleaseCapture(void)