1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxJoystick class
4 // Author: Ported to Linux by Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "joystick.h"
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/joystick.h"
25 #include <linux/joystick.h>
26 #include <sys/types.h>
29 #include <sys/ioctl.h>
34 #include "wx/window.h"
36 #define JOYSTICK_AXE_MAX 32767
37 #define JOYSTICK_AXE_MIN -32767
39 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
41 wxJoystick::wxJoystick(int joystick
)
44 // Assume it's the same device name on all Linux systems ...
45 dev_name
.Printf( wxT("/dev/js%d"), (joystick
== wxJOYSTICK1
) ? 0 : 1); // FIXME Unicode?
47 m_joystick
= open(dev_name
.fn_str(), O_RDWR
);
48 m_lastposition
= wxPoint(-1, -1);
49 for (int i
=0;i
<15;i
++)
55 ////////////////////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////////////////////
58 void *wxJoystick::Entry(void)
60 struct js_event j_evt
;
61 wxJoystickEvent jwx_event
;
63 struct timeval time_out
= {0, 0};
70 FD_SET(m_joystick
, &read_fds
);
71 select(m_joystick
+1, &read_fds
, NULL
, NULL
, &time_out
);
72 if (FD_ISSET(m_joystick
, &read_fds
))
73 read(m_joystick
, &j_evt
, sizeof(j_evt
));
77 read(m_joystick
, &j_evt
, sizeof(j_evt
));
80 if ((j_evt
.type
& JS_EVENT_AXIS
) == JS_EVENT_AXIS
) {
81 switch (j_evt
.number
) {
83 m_lastposition
.x
= j_evt
.value
;
84 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
87 m_lastposition
.y
= j_evt
.value
;
88 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
91 m_axe
[3] = j_evt
.value
;
92 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
95 m_axe
[j_evt
.number
] = j_evt
.value
;
96 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
99 jwx_event
.SetPosition(m_lastposition
);
100 jwx_event
.SetZPosition(m_axe
[3]);
102 if ((j_evt
.type
& JS_EVENT_BUTTON
) == JS_EVENT_BUTTON
) {
103 register int mask
= 1 << j_evt
.number
;
104 char button
= m_buttons
& mask
;
108 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
110 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
114 jwx_event
.SetButtonState(m_buttons
);
115 jwx_event
.SetButtonChange(j_evt
.number
);
119 m_catchwin
->ProcessEvent(jwx_event
);
121 usleep(m_polling
*1000);
124 ////////////////////////////////////////////////////////////////////////////
126 ////////////////////////////////////////////////////////////////////////////
128 wxPoint
wxJoystick::GetPosition(void) const
130 return m_lastposition
;
133 int wxJoystick::GetZPosition(void) const
138 int wxJoystick::GetButtonState(void) const
143 int wxJoystick::GetPOVPosition(void) const
148 int wxJoystick::GetPOVCTSPosition(void) const
153 int wxJoystick::GetRudderPosition(void) const
158 int wxJoystick::GetUPosition(void) const
163 int wxJoystick::GetVPosition(void) const
168 int wxJoystick::GetMovementThreshold(void) const
173 void wxJoystick::SetMovementThreshold(int threshold
)
177 ////////////////////////////////////////////////////////////////////////////
179 ////////////////////////////////////////////////////////////////////////////
181 bool wxJoystick::IsOk(void) const
183 return (m_joystick
!= -1);
186 int wxJoystick::GetNumberJoysticks(void) const
192 dev_name
.Printf(wxT("/dev/js%d"), j
);
193 fd
= open(dev_name
.fn_str(), O_RDONLY
);
201 int wxJoystick::GetManufacturerId(void) const
206 int wxJoystick::GetProductId(void) const
211 wxString
wxJoystick::GetProductName(void) const
214 // 2002-08-20 johan@linkdata.se
215 // Return the device name in lieu of a better one
216 dev_name
.Printf( wxT("/dev/js%d"), (m_joystick
== wxJOYSTICK1
) ? 0 : 1); // FIXME Unicode?
220 int wxJoystick::GetXMin(void) const
222 return JOYSTICK_AXE_MAX
;
225 int wxJoystick::GetYMin(void) const
227 return JOYSTICK_AXE_MAX
;
230 int wxJoystick::GetZMin(void) const
232 return JOYSTICK_AXE_MAX
;
235 int wxJoystick::GetXMax(void) const
237 return JOYSTICK_AXE_MAX
;
240 int wxJoystick::GetYMax(void) const
242 return JOYSTICK_AXE_MAX
;
245 int wxJoystick::GetZMax(void) const
247 return JOYSTICK_AXE_MAX
;
250 int wxJoystick::GetNumberButtons(void) const
254 ioctl(m_joystick
, JSIOCGBUTTONS
, &nb
);
259 int wxJoystick::GetNumberAxes(void) const
263 ioctl(m_joystick
, JSIOCGAXES
, &nb
);
268 int wxJoystick::GetMaxButtons(void) const
270 return 15; // internal
273 int wxJoystick::GetMaxAxes(void) const
275 return 15; // internal
278 int wxJoystick::GetPollingMin(void) const
283 int wxJoystick::GetPollingMax(void) const
288 int wxJoystick::GetRudderMin(void) const
290 return JOYSTICK_AXE_MIN
;
293 int wxJoystick::GetRudderMax(void) const
295 return JOYSTICK_AXE_MAX
;
298 int wxJoystick::GetUMin(void) const
300 return JOYSTICK_AXE_MIN
;
303 int wxJoystick::GetUMax(void) const
305 return JOYSTICK_AXE_MAX
;
308 int wxJoystick::GetVMin(void) const
310 return JOYSTICK_AXE_MIN
;
313 int wxJoystick::GetVMax(void) const
315 return JOYSTICK_AXE_MAX
;
318 bool wxJoystick::HasRudder(void) const
320 return GetNumberAxes() >= 4;
323 bool wxJoystick::HasZ(void) const
325 return GetNumberAxes() >= 3;
328 bool wxJoystick::HasU(void) const
330 return GetNumberAxes() >= 5;
333 bool wxJoystick::HasV(void) const
335 return GetNumberAxes() >= 6;
338 bool wxJoystick::HasPOV(void) const
343 bool wxJoystick::HasPOV4Dir(void) const
348 bool wxJoystick::HasPOVCTS(void) const
353 ////////////////////////////////////////////////////////////////////////////
355 ////////////////////////////////////////////////////////////////////////////
357 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
360 m_polling
= pollingFreq
;
364 bool wxJoystick::ReleaseCapture(void)
370 #endif // wxUSE_JOYSTICK