23 #include "acquisition_thread.h"
24 #include "force_feedback.h"
26 #include <core/threading/mutex.h>
27 #include <core/exceptions/system.h>
30 #include <linux/joystick.h>
32 #include <sys/types.h>
39 using namespace fawkes;
50 :
Thread(
"JoystickAcquisitionThread",
Thread::OPMODE_CONTINUOUS)
71 :
Thread(
"JoystickAcquisitionThread",
Thread::OPMODE_CONTINUOUS)
77 __bbhandler = handler;
90 e.
append(
"Could not read all required config values for %s",
name());
94 init(__cfg_device_file);
99 JoystickAcquisitionThread::open_joystick()
101 __fd = open(__cfg_device_file.c_str(), O_RDONLY);
104 "Opening the joystick device file failed");
107 if ( ioctl(__fd, JSIOCGNAME(
sizeof(__joystick_name)), __joystick_name) < 0) {
108 throw Exception(errno,
"Failed to get name of joystick");
110 if ( ioctl(__fd, JSIOCGAXES, &__num_axes) < 0 ) {
111 throw Exception(errno,
"Failed to get number of axes for joystick");
113 if ( ioctl(__fd, JSIOCGBUTTONS, &__num_buttons) < 0 ) {
114 throw Exception(errno,
"Failed to get number of buttons for joystick");
117 if (__axis_values == NULL) {
120 __axis_array_size = std::max((
int)__num_axes, 8);
121 __axis_values = (
float *)malloc(
sizeof(
float) * __axis_array_size);
122 }
else if ( __num_axes > std::max((
int)__axis_array_size, 8) ) {
124 __num_axes = __axis_array_size;
133 memset(__axis_values, 0,
sizeof(
float) * __axis_array_size);
134 __pressed_buttons = 0;
143 JoystickAcquisitionThread::open_forcefeedback()
169 __cfg_device_file = device_file;
172 open_forcefeedback();
177 __data_mutex =
new Mutex();
184 if ( __fd >= 0 ) close(__fd);
196 if ( read(__fd, &e,
sizeof(
struct js_event)) < (
int)
sizeof(
struct js_event) ) {
207 __data_mutex->
lock();
210 if ((e.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
212 if (e.number <= 32) {
214 __pressed_buttons |= (1 << e.number);
216 __pressed_buttons &= ~(1 << e.number);
221 }
else if ((e.type & ~JS_EVENT_INIT) == JS_EVENT_AXIS) {
222 if ( e.number >= __axis_array_size ) {
224 "Got value for axis %u, but only %u axes registered. "
225 "Plugged in a different joystick? Ignoring.",
226 e.number + 1 , __axis_array_size);
231 __axis_values[e.number] = (e.value == 0) ? 0. : (e.value / -32767.f);
249 open_forcefeedback();
269 __data_mutex->
lock();
304 return __num_buttons;
314 return __joystick_name;
324 return __pressed_buttons;
334 return __axis_values;