SDL: Cleanup Linux joystick code

From 499d31e9cdcef65b127f76021c11cd04b4f144ba Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 13 Apr 2021 17:00:24 -0700
Subject: [PATCH] Cleanup Linux joystick code

---
 src/joystick/linux/SDL_sysjoystick.c   | 35 +++++++-------------------
 src/joystick/linux/SDL_sysjoystick_c.h |  1 +
 2 files changed, 10 insertions(+), 26 deletions(-)

diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 33d965b0b..bb51fa68a 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -56,18 +56,6 @@
 #ifndef SYN_DROPPED
 #define SYN_DROPPED 3
 #endif
-#ifndef BTN_SOUTH
-#define BTN_SOUTH       0x130
-#endif
-#ifndef BTN_EAST
-#define BTN_EAST        0x131
-#endif
-#ifndef BTN_NORTH
-#define BTN_NORTH       0x133
-#endif
-#ifndef BTN_WEST
-#define BTN_WEST        0x134
-#endif
 #ifndef BTN_DPAD_UP
 #define BTN_DPAD_UP     0x220
 #endif
@@ -115,7 +103,6 @@ typedef struct SDL_joylist_item
     SDL_bool m_bSteamController;
 
     SDL_GamepadMapping *mapping;
-    SDL_bool has_gamepad_mapping;
 } SDL_joylist_item;
 
 static SDL_joylist_item *SDL_joylist = NULL;
@@ -379,7 +366,9 @@ MaybeRemoveDevice(const char *path)
 
             SDL_PrivateJoystickRemoved(item->device_instance);
 
-            SDL_free(item->mapping);
+            if (item->mapping) {
+                SDL_free(item->mapping);
+            }
             SDL_free(item->path);
             SDL_free(item->name);
             SDL_free(item);
@@ -1409,20 +1398,11 @@ LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
     SDL_Joystick *joystick;
     SDL_joylist_item *item = JoystickByDevIndex(device_index);
 
-    if (item->has_gamepad_mapping) {
+    if (item->mapping) {
         SDL_memcpy(out, item->mapping, sizeof(*out));
         return SDL_TRUE;
     }
 
-    if (item->mapping)
-        return SDL_FALSE;
-
-    item->mapping = (SDL_GamepadMapping *) SDL_calloc(sizeof(*item->mapping), 1);
-    if (item->mapping == NULL) {
-        SDL_OutOfMemory();
-        return SDL_FALSE;
-    }
-
     joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
     if (joystick == NULL) {
         SDL_OutOfMemory();
@@ -1591,8 +1571,11 @@ LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
     LINUX_JoystickClose(joystick);
     SDL_free(joystick);
 
-    SDL_memcpy(item->mapping, out, sizeof(*out));
-    item->has_gamepad_mapping = SDL_TRUE;
+    /* Cache the mapping for later */
+    item->mapping = (SDL_GamepadMapping *)SDL_malloc(sizeof(*item->mapping));
+    if (item->mapping) {
+        SDL_memcpy(item->mapping, out, sizeof(*out));
+    }
 
     return SDL_TRUE;
 }
diff --git a/src/joystick/linux/SDL_sysjoystick_c.h b/src/joystick/linux/SDL_sysjoystick_c.h
index 342444e8b..898f183dd 100644
--- a/src/joystick/linux/SDL_sysjoystick_c.h
+++ b/src/joystick/linux/SDL_sysjoystick_c.h
@@ -74,6 +74,7 @@ struct joystick_hwdata
 
     /* Steam Controller support */
     SDL_bool m_bSteamController;
+
     /* 4 = (ABS_HAT3X-ABS_HAT0X)/2 (see input-event-codes.h in kernel) */
     int hats_indices[4];
     SDL_bool has_hat[4];