diff options
Diffstat (limited to 'platform/x11/joystick_linux.cpp')
-rw-r--r-- | platform/x11/joystick_linux.cpp | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/platform/x11/joystick_linux.cpp b/platform/x11/joystick_linux.cpp index ef866d5d3a..9fd19a767e 100644 --- a/platform/x11/joystick_linux.cpp +++ b/platform/x11/joystick_linux.cpp @@ -99,14 +99,18 @@ void joystick_linux::joy_thread_func(void *p_user) { } void joystick_linux::run_joystick_thread() { - +#ifdef UDEV_ENABLED udev *_udev = udev_new(); ERR_FAIL_COND(!_udev); enumerate_joysticks(_udev); monitor_joysticks(_udev); udev_unref(_udev); +#else + monitor_joysticks(); +#endif } +#ifdef UDEV_ENABLED void joystick_linux::enumerate_joysticks(udev *p_udev) { udev_enumerate *enumerate; @@ -192,6 +196,23 @@ void joystick_linux::monitor_joysticks(udev *p_udev) { //printf("exit udev\n"); udev_monitor_unref(mon); } +#endif + +void joystick_linux::monitor_joysticks() { + + while (!exit_udev) { + joy_mutex->lock(); + for (int i = 0; i < 32; i++) { + char fname[64]; + sprintf(fname, "/dev/input/event%d", i); + if (attached_devices.find(fname) == -1) { + open_joystick(fname); + } + } + joy_mutex->unlock(); + usleep(1000000); // 1s + } +} int joystick_linux::get_free_joy_slot() const { @@ -229,6 +250,7 @@ void joystick_linux::close_joystick(int p_id) { close(joy.fd); joy.fd = -1; + attached_devices.remove(attached_devices.find(joy.devpath)); input->joy_connection_changed(p_id, false, ""); }; }; @@ -302,6 +324,9 @@ void joystick_linux::open_joystick(const char *p_path) { unsigned long keybit[NBITS(KEY_MAX)] = { 0 }; unsigned long absbit[NBITS(ABS_MAX)] = { 0 }; + // add to attached devices so we don't try to open it again + attached_devices.push_back(String(p_path)); + if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) || (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) || (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) { @@ -446,6 +471,9 @@ uint32_t joystick_linux::process_joysticks(uint32_t p_event_id) { p_event_id = input->joy_axis(p_event_id, i, index, joy->curr_axis[index]); } } + if (len == 0 || (len < 0 && errno != EAGAIN)) { + close_joystick(i); + }; } joy_mutex->unlock(); return p_event_id; |