SDL: Improve sensor detection for Linux gamepad v2

From bcf239e413d7307e5d7dcc6c789a33e1f85889ee Mon Sep 17 00:00:00 2001
From: Mathieu Eyraud <[EMAIL REDACTED]>
Date: Fri, 7 Jul 2023 15:03:15 +0200
Subject: [PATCH] Improve sensor detection for Linux gamepad v2

Detecting by name such as "Accelerometer and "Motion Sensor" may be too broad.
Sensors on Dualshock and Switch gamepad are correctly detected by SDL_EVDEV_GuessDeviceClass().
Use vendor and product ID for Wii extension controls.
---
 src/joystick/linux/SDL_sysjoystick.c | 29 ++--------------------------
 1 file changed, 2 insertions(+), 27 deletions(-)

diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 5dada52445a0..c23d3d551ad0 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -46,6 +46,7 @@
 #include "../../core/linux/SDL_evdev.h"
 #include "../SDL_sysjoystick.h"
 #include "../SDL_joystick_c.h"
+#include "../usb_ids.h"
 #include "../steam/SDL_steamcontroller.h"
 #include "SDL_sysjoystick_c.h"
 #include "../hidapi/SDL_hidapijoystick_c.h"
@@ -317,43 +318,17 @@ static int IsJoystick(const char *path, int fd, char **name_return, SDL_Joystick
 static int IsSensor(const char *path, int fd)
 {
     struct input_id inpid;
-    char *name;
-    char product_string[128];
 
     if (ioctl(fd, EVIOCGID, &inpid) < 0) {
         return 0;
     }
 
-    if (ioctl(fd, EVIOCGNAME(sizeof(product_string)), product_string) < 0) {
-        return 0;
-    }
-
-    name = SDL_CreateJoystickName(inpid.vendor, inpid.product, NULL, product_string);
-    if (name == NULL) {
-        return 0;
-    }
-
-    if (SDL_endswith(name, " Motion Sensors")) {
-        /* PS3 and PS4 motion controls */
-        SDL_free(name);
-        return 1;
-    }
-    if (SDL_strncmp(name, "Nintendo ", 9) == 0 && SDL_strstr(name, " IMU") != NULL) {
-        /* Nintendo Switch Joy-Con and Pro Controller IMU */
-        SDL_free(name);
-        return 1;
-    }
-    if (SDL_endswith(name, " Accelerometer") ||
-        SDL_endswith(name, " IR") ||
-        SDL_endswith(name, " Motion Plus") ||
-        SDL_endswith(name, " Nunchuk")) {
+    if (inpid.vendor == USB_VENDOR_NINTENDO && inpid.product == USB_PRODUCT_NINTENDO_WII_REMOTE) {
         /* Wii extension controls */
         /* These may create 3 sensor devices but we only support reading from 1: ignore them */
-        SDL_free(name);
         return 0;
     }
 
-    SDL_free(name);
     return GuessIsSensor(fd);
 }