]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/joystick.cpp
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 #ifdef HAVE_SYS_SELECT_H
33 # include <sys/select.h>
36 #include "wx/unix/private.h"
46 wxJS_AXIS_MAX
= 32767,
47 wxJS_AXIS_MIN
= -32767,
49 wxJS_MAX_BUTTONS
= sizeof(int) * 8
53 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
56 ////////////////////////////////////////////////////////////////////////////
57 // Background thread for reading the joystick device
58 ////////////////////////////////////////////////////////////////////////////
60 class wxJoystickThread
: public wxThread
63 wxJoystickThread(int device
, int joystick
);
67 void SendEvent(wxEventType type
, long ts
, int change
= 0);
70 wxPoint m_lastposition
;
71 int m_axe
[wxJS_MAX_AXES
];
77 friend class wxJoystick
;
81 wxJoystickThread::wxJoystickThread(int device
, int joystick
)
84 m_lastposition(wxDefaultPosition
),
90 memset(m_axe
, 0, sizeof(m_axe
));
93 void wxJoystickThread::SendEvent(wxEventType type
, long ts
, int change
)
95 wxJoystickEvent
jwx_event(type
, m_buttons
, m_joystick
, change
);
97 jwx_event
.SetTimestamp(ts
);
98 jwx_event
.SetPosition(m_lastposition
);
99 jwx_event
.SetZPosition(m_axe
[wxJS_AXIS_Z
]);
100 jwx_event
.SetEventObject(m_catchwin
);
103 m_catchwin
->AddPendingEvent(jwx_event
);
106 void* wxJoystickThread::Entry()
108 struct js_event j_evt
;
110 struct timeval time_out
= {0, 0};
112 wxFD_ZERO(&read_fds
);
118 // We use select when either polling or 'blocking' as even in the
119 // blocking case we need to check TestDestroy periodically
121 time_out
.tv_usec
= m_polling
* 1000;
123 time_out
.tv_usec
= 10 * 1000; // check at least every 10 msec in blocking case
125 wxFD_SET(m_device
, &read_fds
);
126 select(m_device
+1, &read_fds
, NULL
, NULL
, &time_out
);
127 if (wxFD_ISSET(m_device
, &read_fds
))
129 memset(&j_evt
, 0, sizeof(j_evt
));
130 read(m_device
, &j_evt
, sizeof(j_evt
));
132 //printf("time: %d\t value: %d\t type: %d\t number: %d\n",
133 // j_evt.time, j_evt.value, j_evt.type, j_evt.number);
135 if ((j_evt
.type
& JS_EVENT_AXIS
) && (j_evt
.number
< wxJS_MAX_AXES
))
137 if ( (m_axe
[j_evt
.number
] + m_threshold
< j_evt
.value
)
138 || (m_axe
[j_evt
.number
] - m_threshold
> j_evt
.value
) )
140 m_axe
[j_evt
.number
] = j_evt
.value
;
142 switch (j_evt
.number
)
145 m_lastposition
.x
= j_evt
.value
;
146 SendEvent(wxEVT_JOY_MOVE
, j_evt
.time
);
149 m_lastposition
.y
= j_evt
.value
;
150 SendEvent(wxEVT_JOY_MOVE
, j_evt
.time
);
153 SendEvent(wxEVT_JOY_ZMOVE
, j_evt
.time
);
156 SendEvent(wxEVT_JOY_MOVE
, j_evt
.time
);
157 // TODO: There should be a way to indicate that the event
158 // is for some other axes.
164 if ( (j_evt
.type
& JS_EVENT_BUTTON
) && (j_evt
.number
< wxJS_MAX_BUTTONS
) )
168 m_buttons
|= (1 << j_evt
.number
);
169 SendEvent(wxEVT_JOY_BUTTON_DOWN
, j_evt
.time
, j_evt
.number
);
173 m_buttons
&= ~(1 << j_evt
.number
);
174 SendEvent(wxEVT_JOY_BUTTON_UP
, j_evt
.time
, j_evt
.number
);
185 ////////////////////////////////////////////////////////////////////////////
187 wxJoystick::wxJoystick(int joystick
)
189 m_joystick(joystick
),
194 // old /dev structure
195 dev_name
.Printf( wxT("/dev/js%d"), joystick
);
196 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
198 // new /dev structure with "input" subdirectory
201 dev_name
.Printf( wxT("/dev/input/js%d"), joystick
);
202 m_device
= open(dev_name
.fn_str(), O_RDONLY
);
207 m_thread
= new wxJoystickThread(m_device
, m_joystick
);
214 wxJoystick::~wxJoystick()
218 m_thread
->Delete(); // It's detached so it will delete itself
223 ////////////////////////////////////////////////////////////////////////////
225 ////////////////////////////////////////////////////////////////////////////
227 wxPoint
wxJoystick::GetPosition() const
229 wxPoint
pos(wxDefaultPosition
);
230 if (m_thread
) pos
= m_thread
->m_lastposition
;
234 int wxJoystick::GetPosition(unsigned axis
) const
236 if (m_thread
&& (axis
< wxJS_MAX_AXES
))
237 return m_thread
->m_axe
[axis
];
241 int wxJoystick::GetZPosition() const
244 return m_thread
->m_axe
[wxJS_AXIS_Z
];
248 int wxJoystick::GetButtonState() const
251 return m_thread
->m_buttons
;
255 bool wxJoystick::GetButtonState(unsigned id
) const
257 if (m_thread
&& (id
< wxJS_MAX_BUTTONS
))
258 return (m_thread
->m_buttons
& (1 << id
)) != 0;
262 int wxJoystick::GetPOVPosition() const
267 int wxJoystick::GetPOVCTSPosition() const
272 int wxJoystick::GetRudderPosition() const
275 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
279 int wxJoystick::GetUPosition() const
282 return m_thread
->m_axe
[wxJS_AXIS_U
];
286 int wxJoystick::GetVPosition() const
289 return m_thread
->m_axe
[wxJS_AXIS_V
];
293 int wxJoystick::GetMovementThreshold() const
296 return m_thread
->m_threshold
;
300 void wxJoystick::SetMovementThreshold(int threshold
)
303 m_thread
->m_threshold
= threshold
;
306 ////////////////////////////////////////////////////////////////////////////
308 ////////////////////////////////////////////////////////////////////////////
310 bool wxJoystick::IsOk() const
312 return (m_device
!= -1);
315 int wxJoystick::GetNumberJoysticks()
320 for (j
=0; j
<4; j
++) {
321 dev_name
.Printf(wxT("/dev/js%d"), j
);
322 fd
= open(dev_name
.fn_str(), O_RDONLY
);
329 for (j
=0; j
<4; j
++) {
330 dev_name
.Printf(wxT("/dev/input/js%d"), j
);
331 fd
= open(dev_name
.fn_str(), O_RDONLY
);
341 int wxJoystick::GetManufacturerId() const
346 int wxJoystick::GetProductId() const
351 wxString
wxJoystick::GetProductName() const
355 if (ioctl(m_device
, JSIOCGNAME(sizeof(name
)), name
) < 0)
356 strcpy(name
, "Unknown");
357 return wxString(name
, wxConvLibc
);
360 int wxJoystick::GetXMin() const
362 return wxJS_AXIS_MIN
;
365 int wxJoystick::GetYMin() const
367 return wxJS_AXIS_MIN
;
370 int wxJoystick::GetZMin() const
372 return wxJS_AXIS_MIN
;
375 int wxJoystick::GetXMax() const
377 return wxJS_AXIS_MAX
;
380 int wxJoystick::GetYMax() const
382 return wxJS_AXIS_MAX
;
385 int wxJoystick::GetZMax() const
387 return wxJS_AXIS_MAX
;
390 int wxJoystick::GetNumberButtons() const
395 ioctl(m_device
, JSIOCGBUTTONS
, &nb
);
397 if ((int)nb
> wxJS_MAX_BUTTONS
)
398 nb
= wxJS_MAX_BUTTONS
;
403 int wxJoystick::GetNumberAxes() const
408 ioctl(m_device
, JSIOCGAXES
, &nb
);
410 if ((int)nb
> wxJS_MAX_AXES
)
416 int wxJoystick::GetMaxButtons() const
418 return wxJS_MAX_BUTTONS
; // internal
421 int wxJoystick::GetMaxAxes() const
423 return wxJS_MAX_AXES
; // internal
426 int wxJoystick::GetPollingMin() const
431 int wxJoystick::GetPollingMax() const
436 int wxJoystick::GetRudderMin() const
438 return wxJS_AXIS_MIN
;
441 int wxJoystick::GetRudderMax() const
443 return wxJS_AXIS_MAX
;
446 int wxJoystick::GetUMin() const
448 return wxJS_AXIS_MIN
;
451 int wxJoystick::GetUMax() const
453 return wxJS_AXIS_MAX
;
456 int wxJoystick::GetVMin() const
458 return wxJS_AXIS_MIN
;
461 int wxJoystick::GetVMax() const
463 return wxJS_AXIS_MAX
;
466 bool wxJoystick::HasRudder() const
468 return GetNumberAxes() >= wxJS_AXIS_RUDDER
;
471 bool wxJoystick::HasZ() const
473 return GetNumberAxes() >= wxJS_AXIS_Z
;
476 bool wxJoystick::HasU() const
478 return GetNumberAxes() >= wxJS_AXIS_U
;
481 bool wxJoystick::HasV() const
483 return GetNumberAxes() >= wxJS_AXIS_V
;
486 bool wxJoystick::HasPOV() const
491 bool wxJoystick::HasPOV4Dir() const
496 bool wxJoystick::HasPOVCTS() const
501 ////////////////////////////////////////////////////////////////////////////
503 ////////////////////////////////////////////////////////////////////////////
505 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
509 m_thread
->m_catchwin
= win
;
510 m_thread
->m_polling
= pollingFreq
;
516 bool wxJoystick::ReleaseCapture()
520 m_thread
->m_catchwin
= NULL
;
521 m_thread
->m_polling
= 0;
526 #endif // wxUSE_JOYSTICK