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};
100 // We use select when either polling or 'blocking' as even in the
101 // blocking case we need to check TestDestroy periodically
103 time_out
.tv_usec
= m_polling
* 1000;
105 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
107 FD_SET(m_device
, &read_fds
);
108 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
109 if (FD_ISSET(m_device
, &read_fds
))
111 memset(&j_evt
, 0, sizeof(j_evt
));
112 read(m_device
, &j_evt
, sizeof(j_evt
));
114 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
115 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
117 wxJoystickEvent jwx_event
;
119 if (j_evt
.type
& JS_EVENT_AXIS
)
121 m_axe
[j_evt
.number
] = j_evt
.value
;
123 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
)
148 m_buttons
|= (1 << j_evt
.number
);
149 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
153 m_buttons
&= ~(1 << j_evt
.number
);
154 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
157 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
);
177 ////////////////////////////////////////////////////////////////////////////
179 wxJoystick::wxJoystick(int joystick
)
181 m_joystick(joystick
),
186 // Assume it's the same device name on all Linux systems ...
187 dev_name
.Printf( wxT("/dev/js%d"), (joystick
== wxJOYSTICK1
) ? 0 : 1);
188 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
192 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
199 wxJoystick::~wxJoystick()
203 m_thread
->Delete(); // It's detached so it will delete itself
208 ////////////////////////////////////////////////////////////////////////////
210 ////////////////////////////////////////////////////////////////////////////
212 wxPoint
wxJoystick::GetPosition() const
214 wxPoint
pos(wxDefaultPosition
);
215 if (m_thread
) pos
= m_thread
->m_lastposition
;
219 int wxJoystick::GetZPosition() const
222 return m_thread
->m_axe
[wxJS_AXIS_Z
];
226 int wxJoystick::GetButtonState() const
229 return m_thread
->m_buttons
;
233 int wxJoystick::GetPOVPosition() const
238 int wxJoystick::GetPOVCTSPosition() const
243 int wxJoystick::GetRudderPosition() const
246 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
250 int wxJoystick::GetUPosition() const
253 return m_thread
->m_axe
[wxJS_AXIS_U
];
257 int wxJoystick::GetVPosition() const
260 return m_thread
->m_axe
[wxJS_AXIS_V
];
264 int wxJoystick::GetMovementThreshold() const
269 void wxJoystick::SetMovementThreshold(int threshold
)
273 ////////////////////////////////////////////////////////////////////////////
275 ////////////////////////////////////////////////////////////////////////////
277 bool wxJoystick::IsOk() const
279 return (m_device
!= -1);
282 int wxJoystick::GetNumberJoysticks() const
287 for (j
=0; j
<4; j
++) {
288 dev_name
.Printf(wxT("/dev/js%d"), j
);
289 fd
= open(dev_name
.fn_str(), O_RDONLY
);
297 int wxJoystick::GetManufacturerId() const
302 int wxJoystick::GetProductId() const
307 wxString
wxJoystick::GetProductName() const
311 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
312 strcpy(name
, "Unknown");
313 return wxString(name
, wxConvLibc
);
316 int wxJoystick::GetXMin() const
318 return wxJS_AXIS_MIN
;
321 int wxJoystick::GetYMin() const
323 return wxJS_AXIS_MIN
;
326 int wxJoystick::GetZMin() const
328 return wxJS_AXIS_MIN
;
331 int wxJoystick::GetXMax() const
333 return wxJS_AXIS_MAX
;
336 int wxJoystick::GetYMax() const
338 return wxJS_AXIS_MAX
;
341 int wxJoystick::GetZMax() const
343 return wxJS_AXIS_MAX
;
346 int wxJoystick::GetNumberButtons() const
351 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
356 int wxJoystick::GetNumberAxes() const
361 ioctl(m_device
, JSIOCGAXES
, &nb
);
366 int wxJoystick::GetMaxButtons() const
368 return 15; // internal
371 int wxJoystick::GetMaxAxes() const
373 return 15; // internal
376 int wxJoystick::GetPollingMin() const
381 int wxJoystick::GetPollingMax() const
386 int wxJoystick::GetRudderMin() const
388 return wxJS_AXIS_MIN
;
391 int wxJoystick::GetRudderMax() const
393 return wxJS_AXIS_MAX
;
396 int wxJoystick::GetUMin() const
398 return wxJS_AXIS_MIN
;
401 int wxJoystick::GetUMax() const
403 return wxJS_AXIS_MAX
;
406 int wxJoystick::GetVMin() const
408 return wxJS_AXIS_MIN
;
411 int wxJoystick::GetVMax() const
413 return wxJS_AXIS_MAX
;
416 bool wxJoystick::HasRudder() const
418 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
421 bool wxJoystick::HasZ() const
423 return GetNumberAxes() >= wxJS_AXIS_Z
;
426 bool wxJoystick::HasU() const
428 return GetNumberAxes() >= wxJS_AXIS_U
;
431 bool wxJoystick::HasV() const
433 return GetNumberAxes() >= wxJS_AXIS_V
;
436 bool wxJoystick::HasPOV() const
441 bool wxJoystick::HasPOV4Dir() const
446 bool wxJoystick::HasPOVCTS() const
451 ////////////////////////////////////////////////////////////////////////////
453 ////////////////////////////////////////////////////////////////////////////
455 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
459 m_thread
->m_catchwin
= win
;
460 m_thread
->m_polling
= pollingFreq
;
466 bool wxJoystick::ReleaseCapture()
470 m_thread
->m_catchwin
= NULL
;
471 m_thread
->m_polling
= 0;
476 #endif // wxUSE_JOYSTICK