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"
19 #include <linux/joystick.h>
20 #include <sys/types.h>
23 #include <sys/ioctl.h>
28 #include "wx/window.h"
29 #include "wx/unix/private.h"
39 wxJS_AXIS_MAX
= 32767,
40 wxJS_AXIS_MIN
= -32767
44 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
47 ////////////////////////////////////////////////////////////////////////////
48 // Background thread for reading the joystick device
49 ////////////////////////////////////////////////////////////////////////////
51 class wxJoystickThread
: public wxThread
54 wxJoystickThread(int device
, int joystick
);
60 wxPoint m_lastposition
;
66 friend class wxJoystick
;
70 wxJoystickThread::wxJoystickThread(int device
, int joystick
)
73 m_lastposition(wxDefaultPosition
),
78 for (int i
=0; i
<15; i
++)
83 void* wxJoystickThread::Entry()
85 struct js_event j_evt
;
87 struct timeval time_out
= {0, 0};
95 // We use select when either polling or 'blocking' as even in the
96 // blocking case we need to check TestDestroy periodically
98 time_out
.tv_usec
= m_polling
* 1000;
100 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
102 wxFD_SET(m_device
, &read_fds
);
103 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
104 if (wxFD_ISSET(m_device
, &read_fds
))
106 memset(&j_evt
, 0, sizeof(j_evt
));
107 read(m_device
, &j_evt
, sizeof(j_evt
));
109 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
110 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
112 wxJoystickEvent jwx_event
;
114 if (j_evt
.type
& JS_EVENT_AXIS
)
116 m_axe
[j_evt
.number
] = j_evt
.value
;
118 switch (j_evt
.number
)
121 m_lastposition
.x
= j_evt
.value
;
122 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
125 m_lastposition
.y
= j_evt
.value
;
126 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
129 jwx_event
.SetEventType(wxEVT_JOY_ZMOVE
);
132 jwx_event
.SetEventType(wxEVT_JOY_MOVE
);
133 // TODO: There should be a way to indicate that the event
134 // is for some other axes.
139 if (j_evt
.type
& JS_EVENT_BUTTON
)
143 m_buttons
|= (1 << j_evt
.number
);
144 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
148 m_buttons
&= ~(1 << j_evt
.number
);
149 jwx_event
.SetEventType(wxEVT_JOY_BUTTON_UP
);
152 jwx_event
.SetButtonChange(j_evt
.number
);
154 jwx_event
.SetTimestamp(j_evt
.time
);
155 jwx_event
.SetJoystick(m_joystick
);
156 jwx_event
.SetButtonState(m_buttons
);
157 jwx_event
.SetPosition(m_lastposition
);
158 jwx_event
.SetZPosition(m_axe
[3]);
159 jwx_event
.SetEventObject(m_catchwin
);
162 m_catchwin
->AddPendingEvent(jwx_event
);
172 ////////////////////////////////////////////////////////////////////////////
174 wxJoystick::wxJoystick(int joystick
)
176 m_joystick(joystick
),
181 // old /dev structure
182 dev_name
.Printf( wxT("/dev/js%d"), joystick
);
183 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
185 // new /dev structure with "input" subdirectory
188 dev_name
.Printf( wxT("/dev/input/js%d"), joystick
);
189 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
194 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
201 wxJoystick::~wxJoystick()
205 m_thread
->Delete(); // It's detached so it will delete itself
210 ////////////////////////////////////////////////////////////////////////////
212 ////////////////////////////////////////////////////////////////////////////
214 wxPoint
wxJoystick::GetPosition() const
216 wxPoint
pos(wxDefaultPosition
);
217 if (m_thread
) pos
= m_thread
->m_lastposition
;
221 int wxJoystick::GetZPosition() const
224 return m_thread
->m_axe
[wxJS_AXIS_Z
];
228 int wxJoystick::GetButtonState() const
231 return m_thread
->m_buttons
;
235 int wxJoystick::GetPOVPosition() const
240 int wxJoystick::GetPOVCTSPosition() const
245 int wxJoystick::GetRudderPosition() const
248 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
252 int wxJoystick::GetUPosition() const
255 return m_thread
->m_axe
[wxJS_AXIS_U
];
259 int wxJoystick::GetVPosition() const
262 return m_thread
->m_axe
[wxJS_AXIS_V
];
266 int wxJoystick::GetMovementThreshold() const
271 void wxJoystick::SetMovementThreshold(int threshold
)
275 ////////////////////////////////////////////////////////////////////////////
277 ////////////////////////////////////////////////////////////////////////////
279 bool wxJoystick::IsOk() const
281 return (m_device
!= -1);
284 int wxJoystick::GetNumberJoysticks()
289 for (j
=0; j
<4; j
++) {
290 dev_name
.Printf(wxT("/dev/js%d"), j
);
291 fd
= open(dev_name
.fn_str(), O_RDONLY
);
298 for (j
=0; j
<4; j
++) {
299 dev_name
.Printf(wxT("/dev/input/js%d"), j
);
300 fd
= open(dev_name
.fn_str(), O_RDONLY
);
310 int wxJoystick::GetManufacturerId() const
315 int wxJoystick::GetProductId() const
320 wxString
wxJoystick::GetProductName() const
324 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
325 strcpy(name
, "Unknown");
326 return wxString(name
, wxConvLibc
);
329 int wxJoystick::GetXMin() const
331 return wxJS_AXIS_MIN
;
334 int wxJoystick::GetYMin() const
336 return wxJS_AXIS_MIN
;
339 int wxJoystick::GetZMin() const
341 return wxJS_AXIS_MIN
;
344 int wxJoystick::GetXMax() const
346 return wxJS_AXIS_MAX
;
349 int wxJoystick::GetYMax() const
351 return wxJS_AXIS_MAX
;
354 int wxJoystick::GetZMax() const
356 return wxJS_AXIS_MAX
;
359 int wxJoystick::GetNumberButtons() const
364 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
369 int wxJoystick::GetNumberAxes() const
374 ioctl(m_device
, JSIOCGAXES
, &nb
);
379 int wxJoystick::GetMaxButtons() const
381 return 15; // internal
384 int wxJoystick::GetMaxAxes() const
386 return 15; // internal
389 int wxJoystick::GetPollingMin() const
394 int wxJoystick::GetPollingMax() const
399 int wxJoystick::GetRudderMin() const
401 return wxJS_AXIS_MIN
;
404 int wxJoystick::GetRudderMax() const
406 return wxJS_AXIS_MAX
;
409 int wxJoystick::GetUMin() const
411 return wxJS_AXIS_MIN
;
414 int wxJoystick::GetUMax() const
416 return wxJS_AXIS_MAX
;
419 int wxJoystick::GetVMin() const
421 return wxJS_AXIS_MIN
;
424 int wxJoystick::GetVMax() const
426 return wxJS_AXIS_MAX
;
429 bool wxJoystick::HasRudder() const
431 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
434 bool wxJoystick::HasZ() const
436 return GetNumberAxes() >= wxJS_AXIS_Z
;
439 bool wxJoystick::HasU() const
441 return GetNumberAxes() >= wxJS_AXIS_U
;
444 bool wxJoystick::HasV() const
446 return GetNumberAxes() >= wxJS_AXIS_V
;
449 bool wxJoystick::HasPOV() const
454 bool wxJoystick::HasPOV4Dir() const
459 bool wxJoystick::HasPOVCTS() const
464 ////////////////////////////////////////////////////////////////////////////
466 ////////////////////////////////////////////////////////////////////////////
468 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
472 m_thread
->m_catchwin
= win
;
473 m_thread
->m_polling
= pollingFreq
;
479 bool wxJoystick::ReleaseCapture()
483 m_thread
->m_catchwin
= NULL
;
484 m_thread
->m_polling
= 0;
489 #endif // wxUSE_JOYSTICK