Cleanup Linux joystick code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 33d965b..bb51fa6 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 342444e..898f183 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];