SDL: Don't treat the Wii extension controls as a separate game controller on Linux

From 42cf6d6c56faaa0a4f8876f92b7254176030833e Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 7 Sep 2022 01:41:11 -0700
Subject: [PATCH] Don't treat the Wii extension controls as a separate game
 controller on Linux

---
 src/joystick/SDL_gamecontroller.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 93a3e28fdf0..1fd5f5f0237 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -1954,6 +1954,19 @@ SDL_IsGameController(int device_index)
     return SDL_FALSE;
 }
 
+static SDL_bool SDL_endswith(const char *string, const char *suffix)
+{
+    size_t string_length = string ? SDL_strlen(string) : 0;
+    size_t suffix_length = suffix ? SDL_strlen(suffix) : 0;
+
+    if (suffix_length > 0 && suffix_length <= string_length) {
+        if (SDL_memcmp(string + string_length - suffix_length, suffix, suffix_length) == 0) {
+            return SDL_TRUE;
+        }
+    }
+    return SDL_FALSE;
+}
+
 /*
  * Return 1 if the game controller should be ignored by SDL
  */
@@ -1966,10 +1979,17 @@ SDL_bool SDL_ShouldIgnoreGameController(const char *name, SDL_JoystickGUID guid)
     Uint32 vidpid;
 
 #if defined(__LINUX__)
-    if (name && SDL_strstr(name, "Motion Sensors")) {
+    if (SDL_endswith(name, " Motion Sensors")) {
         /* Don't treat the PS3 and PS4 motion controls as a separate game controller */
         return SDL_TRUE;
     }
+    if (SDL_endswith(name, " Accelerometer") ||
+        SDL_endswith(name, " IR") ||
+        SDL_endswith(name, " Motion Plus") ||
+        SDL_endswith(name, " Nunchuk")) {
+        /* Don't treat the Wii extension controls as a separate game controller */
+        return SDL_TRUE;
+    }
 #endif
 
     if (name && SDL_strcmp(name, "uinput-fpc") == 0) {