From 3a796d6a58409afcf02ce369fdc49d11b25a9e3c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 13 Jan 2020 15:35:54 -0800 Subject: [PATCH] Free the joystick player index when the joystick is removed --- src/joystick/SDL_joystick.c | 12 ++++++++++-- src/joystick/hidapi/SDL_hidapi_xbox360w.c | 3 --- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index 93772ec8a750e..2868cc5b9e52c 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -158,8 +158,8 @@ SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id) } SDL_joystick_players = new_players; - SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0])); - SDL_joystick_player_count = player_index + 1; + SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0])); + SDL_joystick_player_count = player_index + 1; } SDL_joystick_players[player_index] = instance_id; @@ -947,6 +947,7 @@ static void UpdateEventsForDeviceRemoval() void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance) { SDL_Joystick *joystick; + int player_index; #if !SDL_EVENTS_DISABLED SDL_Event event; @@ -969,6 +970,13 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance) break; } } + + SDL_LockJoysticks(); + player_index = SDL_GetPlayerIndexForJoystickID(device_instance); + if (player_index >= 0) { + SDL_joystick_players[player_index] = -1; + } + SDL_UnlockJoysticks(); } int diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360w.c b/src/joystick/hidapi/SDL_hidapi_xbox360w.c index a87ff9d92d1ed..4800625ea81d8 100644 --- a/src/joystick/hidapi/SDL_hidapi_xbox360w.c +++ b/src/joystick/hidapi/SDL_hidapi_xbox360w.c @@ -245,9 +245,6 @@ HIDAPI_DriverXbox360W_UpdateDevice(SDL_HIDAPI_Device *device) HIDAPI_JoystickConnected(device, &joystickID); - /* Set the controller LED */ - SetSlotLED(device->dev, joystickID); - } else if (device->num_joysticks > 0) { HIDAPI_JoystickDisconnected(device, device->joysticks[0]); }