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 <wx/joystick.h>
20 #include <linux/joystick.h>
21 #include <sys/types.h>
24 #include <sys/ioctl.h>
29 #include "wx/window.h"
31 #define JOYSTICK_AXE_MAX 32767
32 #define JOYSTICK_AXE_MIN -32767
34 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
36 wxJoystick::wxJoystick(int joystick
)
39 // Assume it's the same device name on all Linux systems ...
40 dev_name
.Printf("/dev/js%d", (joystick
== wxJOYSTICK1
) ? 0 : 1);
42 m_joystick
= open(dev_name
, O_RDWR
);
43 m_lastposition
= wxPoint(-1, -1);
44 for (int i
=0;i
<15;i
++)
50 ////////////////////////////////////////////////////////////////////////////
52 ////////////////////////////////////////////////////////////////////////////
53 void *wxJoystick::Entry(void)
55 struct js_event j_evt
;
56 wxJoystickEvent jwx_event
;
58 struct timeval time_out
= {0, 0};
65 FD_SET(m_joystick
, &read_fds
);
66 select(m_joystick
+1, &read_fds
, NULL
, NULL
, &time_out
);
67 if (FD_ISSET(m_joystick
, &read_fds
))
68 read(m_joystick
, &j_evt
, sizeof(j_evt
));
72 read(m_joystick
, &j_evt
, sizeof(j_evt
));
75 if ((j_evt
.type
& JS_EVENT_AXIS
) == JS_EVENT_AXIS
) {
76 switch (j_evt
.number
) {
78 m_lastposition
.x
= j_evt
.value
;
79 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
82 m_lastposition
.y
= j_evt
.value
;
83 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
86 m_axe
[3] = j_evt
.value
;
87 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
90 m_axe
[j_evt
.number
] = j_evt
.value
;
91 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
94 jwx_event
.SetPosition(m_lastposition
);
95 jwx_event
.SetZPosition(m_axe
[3]);
97 if ((j_evt
.type
& JS_EVENT_BUTTON
) == JS_EVENT_BUTTON
) {
98 register int mask
= 1 << j_evt
.number
;
99 char button
= m_buttons
& mask
;
103 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
105 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
109 jwx_event
.SetButtonState(m_buttons
);
110 jwx_event
.SetButtonChange(j_evt
.number
);
114 m_catchwin
->ProcessEvent(jwx_event
);
116 usleep(m_polling
*1000);
119 ////////////////////////////////////////////////////////////////////////////
121 ////////////////////////////////////////////////////////////////////////////
123 wxPoint
wxJoystick::GetPosition(void) const
125 return m_lastposition
;
128 int wxJoystick::GetZPosition(void) const
133 int wxJoystick::GetButtonState(void) const
138 int wxJoystick::GetPOVPosition(void) const
143 int wxJoystick::GetPOVCTSPosition(void) const
148 int wxJoystick::GetRudderPosition(void) const
153 int wxJoystick::GetUPosition(void) const
158 int wxJoystick::GetVPosition(void) const
163 int wxJoystick::GetMovementThreshold(void) const
168 void wxJoystick::SetMovementThreshold(int threshold
)
172 ////////////////////////////////////////////////////////////////////////////
174 ////////////////////////////////////////////////////////////////////////////
176 bool wxJoystick::IsOk(void) const
178 return (m_joystick
!= -1);
181 int wxJoystick::GetNumberJoysticks(void) const
187 dev_name
.Printf("/dev/js%d", j
);
188 fd
= open(dev_name
, O_RDONLY
);
196 int wxJoystick::GetManufacturerId(void) const
201 int wxJoystick::GetProductId(void) const
206 wxString
wxJoystick::GetProductName(void) const
211 int wxJoystick::GetXMin(void) const
213 return JOYSTICK_AXE_MAX
;
216 int wxJoystick::GetYMin(void) const
218 return JOYSTICK_AXE_MAX
;
221 int wxJoystick::GetZMin(void) const
223 return JOYSTICK_AXE_MAX
;
226 int wxJoystick::GetXMax(void) const
228 return JOYSTICK_AXE_MAX
;
231 int wxJoystick::GetYMax(void) const
233 return JOYSTICK_AXE_MAX
;
236 int wxJoystick::GetZMax(void) const
238 return JOYSTICK_AXE_MAX
;
241 int wxJoystick::GetNumberButtons(void) const
245 ioctl(m_joystick
, JSIOCGBUTTONS
, &nb
);
250 int wxJoystick::GetNumberAxes(void) const
254 ioctl(m_joystick
, JSIOCGAXES
, &nb
);
259 int wxJoystick::GetMaxButtons(void) const
261 return 15; // internal
264 int wxJoystick::GetMaxAxes(void) const
266 return 15; // internal
269 int wxJoystick::GetPollingMin(void) const
274 int wxJoystick::GetPollingMax(void) const
279 int wxJoystick::GetRudderMin(void) const
281 return JOYSTICK_AXE_MIN
;
284 int wxJoystick::GetRudderMax(void) const
286 return JOYSTICK_AXE_MAX
;
289 int wxJoystick::GetUMin(void) const
291 return JOYSTICK_AXE_MIN
;
294 int wxJoystick::GetUMax(void) const
296 return JOYSTICK_AXE_MAX
;
299 int wxJoystick::GetVMin(void) const
301 return JOYSTICK_AXE_MIN
;
304 int wxJoystick::GetVMax(void) const
306 return JOYSTICK_AXE_MAX
;
309 bool wxJoystick::HasRudder(void) const
311 return GetNumberAxes() >= 4;
314 bool wxJoystick::HasZ(void) const
316 return GetNumberAxes() >= 3;
319 bool wxJoystick::HasU(void) const
321 return GetNumberAxes() >= 5;
324 bool wxJoystick::HasV(void) const
326 return GetNumberAxes() >= 6;
329 bool wxJoystick::HasPOV(void) const
334 bool wxJoystick::HasPOV4Dir(void) const
339 bool wxJoystick::HasPOVCTS(void) const
344 ////////////////////////////////////////////////////////////////////////////
346 ////////////////////////////////////////////////////////////////////////////
348 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
= 0)
351 m_polling
= pollingFreq
;
355 bool wxJoystick::ReleaseCapture(void)
361 #endif // wxUSE_JOYSTICK