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"
44 wxJS_AXIS_MAX
= 32767,
45 wxJS_AXIS_MIN
= -32767
49 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
52 ////////////////////////////////////////////////////////////////////////////
53 // Background thread for reading the joystick device
54 ////////////////////////////////////////////////////////////////////////////
56 class wxJoystickThread
: public wxThread
59 wxJoystickThread(int device
, int joystick
);
65 wxPoint m_lastposition
;
71 friend class wxJoystick
;
75 wxJoystickThread::wxJoystickThread(int device
, int joystick
)
78 m_lastposition(wxDefaultPosition
),
83 for (int i
=0; i
<15; i
++)
88 void* wxJoystickThread::Entry()
90 struct js_event j_evt
;
92 struct timeval time_out
= {0, 0};
99 // We use select when either polling or 'blocking' as even in the
100 // blocking case we need to check TestDestroy periodically
102 time_out
.tv_usec
= m_polling
* 1000;
104 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
106 FD_SET(m_device
, &read_fds
);
107 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
108 if (FD_ISSET(m_device
, &read_fds
))
110 memset(&j_evt
, 0, sizeof(j_evt
));
111 read(m_device
, &j_evt
, sizeof(j_evt
));
113 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
114 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
118 wxJoystickEvent jwx_event
;
120 if ((j_evt
.type
& JS_EVENT_AXIS
) == JS_EVENT_AXIS
) {
122 m_axe
[j_evt
.number
] = j_evt
.value
;
124 switch (j_evt
.number
) {
126 m_lastposition
.x
= j_evt
.value
;
127 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
130 m_lastposition
.y
= j_evt
.value
;
131 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
134 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
137 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
138 // TODO: There should be a way to indicate that the event
139 // is for some other axes.
144 if ((j_evt
.type
& JS_EVENT_BUTTON
) == JS_EVENT_BUTTON
) {
147 m_buttons
|= (1 << j_evt
.number
);
148 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
152 m_buttons
&= ~(1 << j_evt
.number
);
153 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
156 jwx_event
.SetButtonChange(j_evt
.number
);
159 jwx_event
.SetTimestamp(j_evt
.time
);
160 jwx_event
.SetJoystick(m_joystick
);
161 jwx_event
.SetButtonState(m_buttons
);
162 jwx_event
.SetPosition(m_lastposition
);
163 jwx_event
.SetZPosition(m_axe
[3]);
164 jwx_event
.SetEventObject(m_catchwin
);
167 m_catchwin
->AddPendingEvent(jwx_event
);
171 // wxThread::Sleep(m_polling);
180 ////////////////////////////////////////////////////////////////////////////
182 wxJoystick::wxJoystick(int joystick
)
184 m_joystick(joystick
),
189 // Assume it's the same device name on all Linux systems ...
190 dev_name
.Printf( wxT("/dev/js%d"), (joystick
== wxJOYSTICK1
) ? 0 : 1);
191 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
195 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
202 wxJoystick::~wxJoystick()
206 m_thread
->Delete(); // It's detached so it will delete itself
211 ////////////////////////////////////////////////////////////////////////////
213 ////////////////////////////////////////////////////////////////////////////
215 wxPoint
wxJoystick::GetPosition() const
217 wxPoint
pos(wxDefaultPosition
);
218 if (m_thread
) pos
= m_thread
->m_lastposition
;
222 int wxJoystick::GetZPosition() const
225 return m_thread
->m_axe
[wxJS_AXIS_Z
];
229 int wxJoystick::GetButtonState() const
232 return m_thread
->m_buttons
;
236 int wxJoystick::GetPOVPosition() const
241 int wxJoystick::GetPOVCTSPosition() const
246 int wxJoystick::GetRudderPosition() const
249 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
253 int wxJoystick::GetUPosition() const
256 return m_thread
->m_axe
[wxJS_AXIS_U
];
260 int wxJoystick::GetVPosition() const
263 return m_thread
->m_axe
[wxJS_AXIS_V
];
267 int wxJoystick::GetMovementThreshold() const
272 void wxJoystick::SetMovementThreshold(int threshold
)
276 ////////////////////////////////////////////////////////////////////////////
278 ////////////////////////////////////////////////////////////////////////////
280 bool wxJoystick::IsOk() const
282 return (m_device
!= -1);
285 int wxJoystick::GetNumberJoysticks() const
290 for (j
=0; j
<4; j
++) {
291 dev_name
.Printf(wxT("/dev/js%d"), j
);
292 fd
= open(dev_name
.fn_str(), O_RDONLY
);
300 int wxJoystick::GetManufacturerId() const
305 int wxJoystick::GetProductId() const
310 wxString
wxJoystick::GetProductName() const
314 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
315 strcpy(name
, "Unknown");
316 return wxString(name
, wxConvLibc
);
319 int wxJoystick::GetXMin() const
321 return wxJS_AXIS_MIN
;
324 int wxJoystick::GetYMin() const
326 return wxJS_AXIS_MIN
;
329 int wxJoystick::GetZMin() const
331 return wxJS_AXIS_MIN
;
334 int wxJoystick::GetXMax() const
336 return wxJS_AXIS_MAX
;
339 int wxJoystick::GetYMax() const
341 return wxJS_AXIS_MAX
;
344 int wxJoystick::GetZMax() const
346 return wxJS_AXIS_MAX
;
349 int wxJoystick::GetNumberButtons() const
354 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
359 int wxJoystick::GetNumberAxes() const
364 ioctl(m_device
, JSIOCGAXES
, &nb
);
369 int wxJoystick::GetMaxButtons() const
371 return 15; // internal
374 int wxJoystick::GetMaxAxes() const
376 return 15; // internal
379 int wxJoystick::GetPollingMin() const
384 int wxJoystick::GetPollingMax() const
389 int wxJoystick::GetRudderMin() const
391 return wxJS_AXIS_MIN
;
394 int wxJoystick::GetRudderMax() const
396 return wxJS_AXIS_MAX
;
399 int wxJoystick::GetUMin() const
401 return wxJS_AXIS_MIN
;
404 int wxJoystick::GetUMax() const
406 return wxJS_AXIS_MAX
;
409 int wxJoystick::GetVMin() const
411 return wxJS_AXIS_MIN
;
414 int wxJoystick::GetVMax() const
416 return wxJS_AXIS_MAX
;
419 bool wxJoystick::HasRudder() const
421 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
424 bool wxJoystick::HasZ() const
426 return GetNumberAxes() >= wxJS_AXIS_Z
;
429 bool wxJoystick::HasU() const
431 return GetNumberAxes() >= wxJS_AXIS_U
;
434 bool wxJoystick::HasV() const
436 return GetNumberAxes() >= wxJS_AXIS_V
;
439 bool wxJoystick::HasPOV() const
444 bool wxJoystick::HasPOV4Dir() const
449 bool wxJoystick::HasPOVCTS() const
454 ////////////////////////////////////////////////////////////////////////////
456 ////////////////////////////////////////////////////////////////////////////
458 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
462 m_thread
->m_catchwin
= win
;
463 m_thread
->m_polling
= pollingFreq
;
469 bool wxJoystick::ReleaseCapture()
473 m_thread
->m_catchwin
= NULL
;
474 m_thread
->m_polling
= 0;
479 #endif // wxUSE_JOYSTICK