1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/joystick.cpp
3 // Purpose: wxJoystick class
4 // Author: Ported to Linux by Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/joystick.h"
21 #include "wx/window.h"
24 #include <linux/joystick.h>
25 #include <sys/types.h>
28 #include <sys/ioctl.h>
32 #include "wx/unix/private.h"
42 wxJS_AXIS_MAX
= 32767,
43 wxJS_AXIS_MIN
= -32767
47 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
50 ////////////////////////////////////////////////////////////////////////////
51 // Background thread for reading the joystick device
52 ////////////////////////////////////////////////////////////////////////////
54 class wxJoystickThread
: public wxThread
57 wxJoystickThread(int device
, int joystick
);
63 wxPoint m_lastposition
;
69 friend class wxJoystick
;
73 wxJoystickThread::wxJoystickThread(int device
, int joystick
)
76 m_lastposition(wxDefaultPosition
),
81 for (int i
=0; i
<15; i
++)
86 void* wxJoystickThread::Entry()
88 struct js_event j_evt
;
90 struct timeval time_out
= {0, 0};
98 // We use select when either polling or 'blocking' as even in the
99 // blocking case we need to check TestDestroy periodically
101 time_out
.tv_usec
= m_polling
* 1000;
103 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
105 wxFD_SET(m_device
, &read_fds
);
106 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
107 if (wxFD_ISSET(m_device
, &read_fds
))
109 memset(&j_evt
, 0, sizeof(j_evt
));
110 read(m_device
, &j_evt
, sizeof(j_evt
));
112 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
113 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
115 wxJoystickEvent jwx_event
;
117 if (j_evt
.type
& JS_EVENT_AXIS
)
119 m_axe
[j_evt
.number
] = j_evt
.value
;
121 switch (j_evt
.number
)
124 m_lastposition
.x
= j_evt
.value
;
125 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
128 m_lastposition
.y
= j_evt
.value
;
129 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
132 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
135 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
136 // TODO: There should be a way to indicate that the event
137 // is for some other axes.
142 if (j_evt
.type
& JS_EVENT_BUTTON
)
146 m_buttons
|= (1 << j_evt
.number
);
147 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
151 m_buttons
&= ~(1 << j_evt
.number
);
152 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
155 jwx_event
.SetButtonChange(j_evt
.number
);
157 jwx_event
.SetTimestamp(j_evt
.time
);
158 jwx_event
.SetJoystick(m_joystick
);
159 jwx_event
.SetButtonState(m_buttons
);
160 jwx_event
.SetPosition(m_lastposition
);
161 jwx_event
.SetZPosition(m_axe
[3]);
162 jwx_event
.SetEventObject(m_catchwin
);
165 m_catchwin
->AddPendingEvent(jwx_event
);
175 ////////////////////////////////////////////////////////////////////////////
177 wxJoystick::wxJoystick(int joystick
)
179 m_joystick(joystick
),
184 // old /dev structure
185 dev_name
.Printf( wxT("/dev/js%d"), joystick
);
186 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
188 // new /dev structure with "input" subdirectory
191 dev_name
.Printf( wxT("/dev/input/js%d"), joystick
);
192 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
197 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
204 wxJoystick::~wxJoystick()
208 m_thread
->Delete(); // It's detached so it will delete itself
213 ////////////////////////////////////////////////////////////////////////////
215 ////////////////////////////////////////////////////////////////////////////
217 wxPoint
wxJoystick::GetPosition() const
219 wxPoint
pos(wxDefaultPosition
);
220 if (m_thread
) pos
= m_thread
->m_lastposition
;
224 int wxJoystick::GetZPosition() const
227 return m_thread
->m_axe
[wxJS_AXIS_Z
];
231 int wxJoystick::GetButtonState() const
234 return m_thread
->m_buttons
;
238 int wxJoystick::GetPOVPosition() const
243 int wxJoystick::GetPOVCTSPosition() const
248 int wxJoystick::GetRudderPosition() const
251 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
255 int wxJoystick::GetUPosition() const
258 return m_thread
->m_axe
[wxJS_AXIS_U
];
262 int wxJoystick::GetVPosition() const
265 return m_thread
->m_axe
[wxJS_AXIS_V
];
269 int wxJoystick::GetMovementThreshold() const
274 void wxJoystick::SetMovementThreshold(int threshold
)
278 ////////////////////////////////////////////////////////////////////////////
280 ////////////////////////////////////////////////////////////////////////////
282 bool wxJoystick::IsOk() const
284 return (m_device
!= -1);
287 int wxJoystick::GetNumberJoysticks()
292 for (j
=0; j
<4; j
++) {
293 dev_name
.Printf(wxT("/dev/js%d"), j
);
294 fd
= open(dev_name
.fn_str(), O_RDONLY
);
301 for (j
=0; j
<4; j
++) {
302 dev_name
.Printf(wxT("/dev/input/js%d"), j
);
303 fd
= open(dev_name
.fn_str(), O_RDONLY
);
313 int wxJoystick::GetManufacturerId() const
318 int wxJoystick::GetProductId() const
323 wxString
wxJoystick::GetProductName() const
327 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
328 strcpy(name
, "Unknown");
329 return wxString(name
, wxConvLibc
);
332 int wxJoystick::GetXMin() const
334 return wxJS_AXIS_MIN
;
337 int wxJoystick::GetYMin() const
339 return wxJS_AXIS_MIN
;
342 int wxJoystick::GetZMin() const
344 return wxJS_AXIS_MIN
;
347 int wxJoystick::GetXMax() const
349 return wxJS_AXIS_MAX
;
352 int wxJoystick::GetYMax() const
354 return wxJS_AXIS_MAX
;
357 int wxJoystick::GetZMax() const
359 return wxJS_AXIS_MAX
;
362 int wxJoystick::GetNumberButtons() const
367 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
372 int wxJoystick::GetNumberAxes() const
377 ioctl(m_device
, JSIOCGAXES
, &nb
);
382 int wxJoystick::GetMaxButtons() const
384 return 15; // internal
387 int wxJoystick::GetMaxAxes() const
389 return 15; // internal
392 int wxJoystick::GetPollingMin() const
397 int wxJoystick::GetPollingMax() const
402 int wxJoystick::GetRudderMin() const
404 return wxJS_AXIS_MIN
;
407 int wxJoystick::GetRudderMax() const
409 return wxJS_AXIS_MAX
;
412 int wxJoystick::GetUMin() const
414 return wxJS_AXIS_MIN
;
417 int wxJoystick::GetUMax() const
419 return wxJS_AXIS_MAX
;
422 int wxJoystick::GetVMin() const
424 return wxJS_AXIS_MIN
;
427 int wxJoystick::GetVMax() const
429 return wxJS_AXIS_MAX
;
432 bool wxJoystick::HasRudder() const
434 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
437 bool wxJoystick::HasZ() const
439 return GetNumberAxes() >= wxJS_AXIS_Z
;
442 bool wxJoystick::HasU() const
444 return GetNumberAxes() >= wxJS_AXIS_U
;
447 bool wxJoystick::HasV() const
449 return GetNumberAxes() >= wxJS_AXIS_V
;
452 bool wxJoystick::HasPOV() const
457 bool wxJoystick::HasPOV4Dir() const
462 bool wxJoystick::HasPOVCTS() const
467 ////////////////////////////////////////////////////////////////////////////
469 ////////////////////////////////////////////////////////////////////////////
471 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
475 m_thread
->m_catchwin
= win
;
476 m_thread
->m_polling
= pollingFreq
;
482 bool wxJoystick::ReleaseCapture()
486 m_thread
->m_catchwin
= NULL
;
487 m_thread
->m_polling
= 0;
492 #endif // wxUSE_JOYSTICK