From fdd945f2af0a3da1ec0609afc94e5799c3175a31 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 11 Nov 2020 19:14:52 -0800 Subject: [PATCH] joystick: Use a better heuristic to guess what is a joystick Previously we only checked for at least one button or key and at least the X and Y absolute axes, but this has both false positives and false negatives. Graphics tablets, trackpads and touchscreens all have buttons and absolute X and Y axes, but we don't want to detect those as joysticks. On normal Linux systems ordinary users do not have access to these device nodes, but members of the 'input' group do. Conversely, some game controllers only have digital buttons and no analogue axes (the Nintendo Wiimote is an example), and some have axes and no buttons (steering wheels or flight simulator rudders might not have buttons). Use the more elaborate heuristic factored out from SDL's udev code path to handle these cases. In an ideal world we could use exactly the same heuristic as udev's input_id builtin, but that isn't under a suitable license for inclusion in SDL, so we have to use a parallel implementation of something vaguely similar. Signed-off-by: Simon McVittie --- src/core/linux/SDL_evdev_capabilities.c | 2 +- src/core/linux/SDL_evdev_capabilities.h | 4 ++-- src/joystick/linux/SDL_sysjoystick.c | 12 ++++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/core/linux/SDL_evdev_capabilities.c b/src/core/linux/SDL_evdev_capabilities.c index d96b9edb6..82b11188f 100644 --- a/src/core/linux/SDL_evdev_capabilities.c +++ b/src/core/linux/SDL_evdev_capabilities.c @@ -22,7 +22,7 @@ #include "SDL_evdev_capabilities.h" -#if HAVE_LIBUDEV_H +#if HAVE_LIBUDEV_H || defined(SDL_JOYSTICK_LINUX) extern int SDL_EVDEV_GuessDeviceClass(unsigned long bitmask_ev[NBITS(EV_MAX)], diff --git a/src/core/linux/SDL_evdev_capabilities.h b/src/core/linux/SDL_evdev_capabilities.h index cef151331..10bceb5a2 100644 --- a/src/core/linux/SDL_evdev_capabilities.h +++ b/src/core/linux/SDL_evdev_capabilities.h @@ -25,7 +25,7 @@ #ifndef SDL_evdev_capabilities_h_ #define SDL_evdev_capabilities_h_ -#if HAVE_LIBUDEV_H +#if HAVE_LIBUDEV_H || defined(SDL_JOYSTICK_LINUX) #include @@ -51,7 +51,7 @@ extern int SDL_EVDEV_GuessDeviceClass(unsigned long bitmask_ev[NBITS(EV_MAX)], unsigned long bitmask_key[NBITS(KEY_MAX)], unsigned long bitmask_rel[NBITS(REL_MAX)]); -#endif /* HAVE_LIBUDEV_H */ +#endif /* HAVE_LIBUDEV_H || defined(SDL_JOYSTICK_LINUX) */ #endif /* SDL_evdev_capabilities_h_ */ diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index a94bfe39f..fc145e9ca 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -160,19 +160,23 @@ GuessIsJoystick(int fd) unsigned long evbit[NBITS(EV_MAX)] = { 0 }; unsigned long keybit[NBITS(KEY_MAX)] = { 0 }; unsigned long absbit[NBITS(ABS_MAX)] = { 0 }; + unsigned long relbit[NBITS(REL_MAX)] = { 0 }; + int devclass; if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) || (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) || + (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) < 0) || (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) { return (0); } - if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) && - test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit))) { - return 0; + devclass = SDL_EVDEV_GuessDeviceClass(evbit, absbit, keybit, relbit); + + if (devclass & SDL_UDEV_DEVICE_JOYSTICK) { + return 1; } - return 1; + return 0; } static int