]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/joystick.cpp
27b3b6cb74a8d67e254cc800c00ac3bc9c3134be
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"
25 #include "wx/thread.h"
27 #include <linux/joystick.h>
28 #include <sys/types.h>
31 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_SELECT_H
36 # include <sys/select.h>
39 #include "wx/unix/private.h"
49 wxJS_AXIS_MAX
= 32767,
50 wxJS_AXIS_MIN
= -32767,
52 wxJS_MAX_BUTTONS
= sizeof(int) * 8
56 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
59 ////////////////////////////////////////////////////////////////////////////
60 // Background thread for reading the joystick device
61 ////////////////////////////////////////////////////////////////////////////
63 class wxJoystickThread
: public wxThread
66 wxJoystickThread(int device
, int joystick
);
70 void SendEvent(wxEventType type
, long ts
, int change
= 0);
73 wxPoint m_lastposition
;
74 int m_axe
[wxJS_MAX_AXES
];
80 friend class wxJoystick
;
84 wxJoystickThread::wxJoystickThread(int device
, int joystick
)
87 m_lastposition(wxDefaultPosition
),
93 memset(m_axe
, 0, sizeof(m_axe
));
96 void wxJoystickThread::SendEvent(wxEventType type
, long ts
, int change
)
98 wxJoystickEvent
jwx_event(type
, m_buttons
, m_joystick
, change
);
100 jwx_event
.SetTimestamp(ts
);
101 jwx_event
.SetPosition(m_lastposition
);
102 jwx_event
.SetZPosition(m_axe
[wxJS_AXIS_Z
]);
103 jwx_event
.SetEventObject(m_catchwin
);
106 m_catchwin
->GetEventHandler()->AddPendingEvent(jwx_event
);
109 void* wxJoystickThread::Entry()
111 struct js_event j_evt
;
113 struct timeval time_out
= {0, 0};
115 wxFD_ZERO(&read_fds
);
121 // We use select when either polling or 'blocking' as even in the
122 // blocking case we need to check TestDestroy periodically
124 time_out
.tv_usec
= m_polling
* 1000;
126 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
128 wxFD_SET(m_device
, &read_fds
);
129 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
130 if (wxFD_ISSET(m_device
, &read_fds
))
132 memset(&j_evt
, 0, sizeof(j_evt
));
133 read(m_device
, &j_evt
, sizeof(j_evt
));
135 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
136 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
138 if ((j_evt
.type
& JS_EVENT_AXIS
) && (j_evt
.number
< wxJS_MAX_AXES
))
140 // Ignore invalid axis.
141 if ( j_evt
.number
>= wxJS_MAX_AXES
)
143 wxLogDebug(wxS("Invalid axis index %d in joystick message."),
148 if ( (m_axe
[j_evt
.number
] + m_threshold
< j_evt
.value
)
149 || (m_axe
[j_evt
.number
] - m_threshold
> j_evt
.value
) )
151 m_axe
[j_evt
.number
] = j_evt
.value
;
153 switch (j_evt
.number
)
156 m_lastposition
.x
= j_evt
.value
;
157 SendEvent(wxEVT_JOY_MOVE
, j_evt
.time
);
160 m_lastposition
.y
= j_evt
.value
;
161 SendEvent(wxEVT_JOY_MOVE
, j_evt
.time
);
164 SendEvent(wxEVT_JOY_ZMOVE
, j_evt
.time
);
167 SendEvent(wxEVT_JOY_MOVE
, j_evt
.time
);
168 // TODO: There should be a way to indicate that the event
169 // is for some other axes.
175 if ( (j_evt
.type
& JS_EVENT_BUTTON
) && (j_evt
.number
< wxJS_MAX_BUTTONS
) )
179 m_buttons
|= (1 << j_evt
.number
);
180 SendEvent(wxEVT_JOY_BUTTON_DOWN
, j_evt
.time
, j_evt
.number
);
184 m_buttons
&= ~(1 << j_evt
.number
);
185 SendEvent(wxEVT_JOY_BUTTON_UP
, j_evt
.time
, j_evt
.number
);
196 ////////////////////////////////////////////////////////////////////////////
198 wxJoystick::wxJoystick(int joystick
)
200 m_joystick(joystick
),
205 // old /dev structure
206 dev_name
.Printf( wxT("/dev/js%d"), joystick
);
207 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
209 // new /dev structure with "input" subdirectory
212 dev_name
.Printf( wxT("/dev/input/js%d"), joystick
);
213 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
218 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
225 wxJoystick::~wxJoystick()
229 m_thread
->Delete(); // It's detached so it will delete itself
234 ////////////////////////////////////////////////////////////////////////////
236 ////////////////////////////////////////////////////////////////////////////
238 wxPoint
wxJoystick::GetPosition() const
240 wxPoint
pos(wxDefaultPosition
);
241 if (m_thread
) pos
= m_thread
->m_lastposition
;
245 int wxJoystick::GetPosition(unsigned axis
) const
247 if (m_thread
&& (axis
< wxJS_MAX_AXES
))
248 return m_thread
->m_axe
[axis
];
252 int wxJoystick::GetZPosition() const
255 return m_thread
->m_axe
[wxJS_AXIS_Z
];
259 int wxJoystick::GetButtonState() const
262 return m_thread
->m_buttons
;
266 bool wxJoystick::GetButtonState(unsigned id
) const
268 if (m_thread
&& (id
< wxJS_MAX_BUTTONS
))
269 return (m_thread
->m_buttons
& (1 << id
)) != 0;
273 int wxJoystick::GetPOVPosition() const
278 int wxJoystick::GetPOVCTSPosition() const
283 int wxJoystick::GetRudderPosition() const
286 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
290 int wxJoystick::GetUPosition() const
293 return m_thread
->m_axe
[wxJS_AXIS_U
];
297 int wxJoystick::GetVPosition() const
300 return m_thread
->m_axe
[wxJS_AXIS_V
];
304 int wxJoystick::GetMovementThreshold() const
307 return m_thread
->m_threshold
;
311 void wxJoystick::SetMovementThreshold(int threshold
)
314 m_thread
->m_threshold
= threshold
;
317 ////////////////////////////////////////////////////////////////////////////
319 ////////////////////////////////////////////////////////////////////////////
321 bool wxJoystick::IsOk() const
323 return (m_device
!= -1);
326 int wxJoystick::GetNumberJoysticks()
331 for (j
=0; j
<4; j
++) {
332 dev_name
.Printf(wxT("/dev/js%d"), j
);
333 fd
= open(dev_name
.fn_str(), O_RDONLY
);
340 for (j
=0; j
<4; j
++) {
341 dev_name
.Printf(wxT("/dev/input/js%d"), j
);
342 fd
= open(dev_name
.fn_str(), O_RDONLY
);
352 int wxJoystick::GetManufacturerId() const
357 int wxJoystick::GetProductId() const
362 wxString
wxJoystick::GetProductName() const
366 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
367 strcpy(name
, "Unknown");
368 return wxString(name
, wxConvLibc
);
371 int wxJoystick::GetXMin() const
373 return wxJS_AXIS_MIN
;
376 int wxJoystick::GetYMin() const
378 return wxJS_AXIS_MIN
;
381 int wxJoystick::GetZMin() const
383 return wxJS_AXIS_MIN
;
386 int wxJoystick::GetXMax() const
388 return wxJS_AXIS_MAX
;
391 int wxJoystick::GetYMax() const
393 return wxJS_AXIS_MAX
;
396 int wxJoystick::GetZMax() const
398 return wxJS_AXIS_MAX
;
401 int wxJoystick::GetNumberButtons() const
406 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
408 if ((int)nb
> wxJS_MAX_BUTTONS
)
409 nb
= wxJS_MAX_BUTTONS
;
414 int wxJoystick::GetNumberAxes() const
419 ioctl(m_device
, JSIOCGAXES
, &nb
);
421 if ((int)nb
> wxJS_MAX_AXES
)
427 int wxJoystick::GetMaxButtons() const
429 return wxJS_MAX_BUTTONS
; // internal
432 int wxJoystick::GetMaxAxes() const
434 return wxJS_MAX_AXES
; // internal
437 int wxJoystick::GetPollingMin() const
442 int wxJoystick::GetPollingMax() const
447 int wxJoystick::GetRudderMin() const
449 return wxJS_AXIS_MIN
;
452 int wxJoystick::GetRudderMax() const
454 return wxJS_AXIS_MAX
;
457 int wxJoystick::GetUMin() const
459 return wxJS_AXIS_MIN
;
462 int wxJoystick::GetUMax() const
464 return wxJS_AXIS_MAX
;
467 int wxJoystick::GetVMin() const
469 return wxJS_AXIS_MIN
;
472 int wxJoystick::GetVMax() const
474 return wxJS_AXIS_MAX
;
477 bool wxJoystick::HasRudder() const
479 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
482 bool wxJoystick::HasZ() const
484 return GetNumberAxes() >= wxJS_AXIS_Z
;
487 bool wxJoystick::HasU() const
489 return GetNumberAxes() >= wxJS_AXIS_U
;
492 bool wxJoystick::HasV() const
494 return GetNumberAxes() >= wxJS_AXIS_V
;
497 bool wxJoystick::HasPOV() const
502 bool wxJoystick::HasPOV4Dir() const
507 bool wxJoystick::HasPOVCTS() const
512 ////////////////////////////////////////////////////////////////////////////
514 ////////////////////////////////////////////////////////////////////////////
516 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
520 m_thread
->m_catchwin
= win
;
521 m_thread
->m_polling
= pollingFreq
;
527 bool wxJoystick::ReleaseCapture()
531 m_thread
->m_catchwin
= NULL
;
532 m_thread
->m_polling
= 0;
537 #endif // wxUSE_JOYSTICK