1 /////////////////////////////////////////////////////////////////////////////
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"
19 #include "wx/joystick.h"
21 #include <linux/joystick.h>
22 #include <sys/types.h>
25 #include <sys/ioctl.h>
30 #include "wx/window.h"
31 #include "wx/unix/private.h"
41 wxJS_AXIS_MAX
= 32767,
42 wxJS_AXIS_MIN
= -32767
46 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
49 ////////////////////////////////////////////////////////////////////////////
50 // Background thread for reading the joystick device
51 ////////////////////////////////////////////////////////////////////////////
53 class wxJoystickThread
: public wxThread
56 wxJoystickThread(int device
, int joystick
);
62 wxPoint m_lastposition
;
68 friend class wxJoystick
;
72 wxJoystickThread::wxJoystickThread(int device
, int joystick
)
75 m_lastposition(wxDefaultPosition
),
80 for (int i
=0; i
<15; i
++)
85 void* wxJoystickThread::Entry()
87 struct js_event j_evt
;
89 struct timeval time_out
= {0, 0};
97 // We use select when either polling or 'blocking' as even in the
98 // blocking case we need to check TestDestroy periodically
100 time_out
.tv_usec
= m_polling
* 1000;
102 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
104 wxFD_SET(m_device
, &read_fds
);
105 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
106 if (wxFD_ISSET(m_device
, &read_fds
))
108 memset(&j_evt
, 0, sizeof(j_evt
));
109 read(m_device
, &j_evt
, sizeof(j_evt
));
111 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
112 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
114 wxJoystickEvent jwx_event
;
116 if (j_evt
.type
& JS_EVENT_AXIS
)
118 m_axe
[j_evt
.number
] = j_evt
.value
;
120 switch (j_evt
.number
)
123 m_lastposition
.x
= j_evt
.value
;
124 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
127 m_lastposition
.y
= j_evt
.value
;
128 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
131 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
134 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
135 // TODO: There should be a way to indicate that the event
136 // is for some other axes.
141 if (j_evt
.type
& JS_EVENT_BUTTON
)
145 m_buttons
|= (1 << j_evt
.number
);
146 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
150 m_buttons
&= ~(1 << j_evt
.number
);
151 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
154 jwx_event
.SetButtonChange(j_evt
.number
);
156 jwx_event
.SetTimestamp(j_evt
.time
);
157 jwx_event
.SetJoystick(m_joystick
);
158 jwx_event
.SetButtonState(m_buttons
);
159 jwx_event
.SetPosition(m_lastposition
);
160 jwx_event
.SetZPosition(m_axe
[3]);
161 jwx_event
.SetEventObject(m_catchwin
);
164 m_catchwin
->AddPendingEvent(jwx_event
);
174 ////////////////////////////////////////////////////////////////////////////
176 wxJoystick::wxJoystick(int joystick
)
178 m_joystick(joystick
),
183 // old /dev structure
184 dev_name
.Printf( wxT("/dev/js%d"), joystick
);
185 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
187 // new /dev structure with "input" subdirectory
190 dev_name
.Printf( wxT("/dev/input/js%d"), joystick
);
191 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
196 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
203 wxJoystick::~wxJoystick()
207 m_thread
->Delete(); // It's detached so it will delete itself
212 ////////////////////////////////////////////////////////////////////////////
214 ////////////////////////////////////////////////////////////////////////////
216 wxPoint
wxJoystick::GetPosition() const
218 wxPoint
pos(wxDefaultPosition
);
219 if (m_thread
) pos
= m_thread
->m_lastposition
;
223 int wxJoystick::GetZPosition() const
226 return m_thread
->m_axe
[wxJS_AXIS_Z
];
230 int wxJoystick::GetButtonState() const
233 return m_thread
->m_buttons
;
237 int wxJoystick::GetPOVPosition() const
242 int wxJoystick::GetPOVCTSPosition() const
247 int wxJoystick::GetRudderPosition() const
250 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
254 int wxJoystick::GetUPosition() const
257 return m_thread
->m_axe
[wxJS_AXIS_U
];
261 int wxJoystick::GetVPosition() const
264 return m_thread
->m_axe
[wxJS_AXIS_V
];
268 int wxJoystick::GetMovementThreshold() const
273 void wxJoystick::SetMovementThreshold(int threshold
)
277 ////////////////////////////////////////////////////////////////////////////
279 ////////////////////////////////////////////////////////////////////////////
281 bool wxJoystick::IsOk() const
283 return (m_device
!= -1);
286 int wxJoystick::GetNumberJoysticks() const
291 for (j
=0; j
<4; j
++) {
292 dev_name
.Printf(wxT("/dev/js%d"), j
);
293 fd
= open(dev_name
.fn_str(), O_RDONLY
);
300 for (j
=0; j
<4; j
++) {
301 dev_name
.Printf(wxT("/dev/input/js%d"), j
);
302 fd
= open(dev_name
.fn_str(), O_RDONLY
);
312 int wxJoystick::GetManufacturerId() const
317 int wxJoystick::GetProductId() const
322 wxString
wxJoystick::GetProductName() const
326 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
327 strcpy(name
, "Unknown");
328 return wxString(name
, wxConvLibc
);
331 int wxJoystick::GetXMin() const
333 return wxJS_AXIS_MIN
;
336 int wxJoystick::GetYMin() const
338 return wxJS_AXIS_MIN
;
341 int wxJoystick::GetZMin() const
343 return wxJS_AXIS_MIN
;
346 int wxJoystick::GetXMax() const
348 return wxJS_AXIS_MAX
;
351 int wxJoystick::GetYMax() const
353 return wxJS_AXIS_MAX
;
356 int wxJoystick::GetZMax() const
358 return wxJS_AXIS_MAX
;
361 int wxJoystick::GetNumberButtons() const
366 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
371 int wxJoystick::GetNumberAxes() const
376 ioctl(m_device
, JSIOCGAXES
, &nb
);
381 int wxJoystick::GetMaxButtons() const
383 return 15; // internal
386 int wxJoystick::GetMaxAxes() const
388 return 15; // internal
391 int wxJoystick::GetPollingMin() const
396 int wxJoystick::GetPollingMax() const
401 int wxJoystick::GetRudderMin() const
403 return wxJS_AXIS_MIN
;
406 int wxJoystick::GetRudderMax() const
408 return wxJS_AXIS_MAX
;
411 int wxJoystick::GetUMin() const
413 return wxJS_AXIS_MIN
;
416 int wxJoystick::GetUMax() const
418 return wxJS_AXIS_MAX
;
421 int wxJoystick::GetVMin() const
423 return wxJS_AXIS_MIN
;
426 int wxJoystick::GetVMax() const
428 return wxJS_AXIS_MAX
;
431 bool wxJoystick::HasRudder() const
433 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
436 bool wxJoystick::HasZ() const
438 return GetNumberAxes() >= wxJS_AXIS_Z
;
441 bool wxJoystick::HasU() const
443 return GetNumberAxes() >= wxJS_AXIS_U
;
446 bool wxJoystick::HasV() const
448 return GetNumberAxes() >= wxJS_AXIS_V
;
451 bool wxJoystick::HasPOV() const
456 bool wxJoystick::HasPOV4Dir() const
461 bool wxJoystick::HasPOVCTS() const
466 ////////////////////////////////////////////////////////////////////////////
468 ////////////////////////////////////////////////////////////////////////////
470 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
474 m_thread
->m_catchwin
= win
;
475 m_thread
->m_polling
= pollingFreq
;
481 bool wxJoystick::ReleaseCapture()
485 m_thread
->m_catchwin
= NULL
;
486 m_thread
->m_polling
= 0;
491 #endif // wxUSE_JOYSTICK