Free the joystick player index when the joystick is removed
authorSam Lantinga <slouken@libsdl.org>
Mon, 13 Jan 2020 15:35:54 -0800
changeset 134124d575a1ef70a
parent 13411 26cf14ba2193
child 13413 e025090ec0f9
Free the joystick player index when the joystick is removed
src/joystick/SDL_joystick.c
src/joystick/hidapi/SDL_hidapi_xbox360w.c
     1.1 --- a/src/joystick/SDL_joystick.c	Mon Jan 13 15:35:52 2020 -0800
     1.2 +++ b/src/joystick/SDL_joystick.c	Mon Jan 13 15:35:54 2020 -0800
     1.3 @@ -158,8 +158,8 @@
     1.4          }
     1.5  
     1.6          SDL_joystick_players = new_players;
     1.7 -		SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0]));
     1.8 -		SDL_joystick_player_count = player_index + 1;
     1.9 +        SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0]));
    1.10 +        SDL_joystick_player_count = player_index + 1;
    1.11      }
    1.12  
    1.13      SDL_joystick_players[player_index] = instance_id;
    1.14 @@ -947,6 +947,7 @@
    1.15  void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
    1.16  {
    1.17      SDL_Joystick *joystick;
    1.18 +    int player_index;
    1.19  
    1.20  #if !SDL_EVENTS_DISABLED
    1.21      SDL_Event event;
    1.22 @@ -969,6 +970,13 @@
    1.23              break;
    1.24          }
    1.25      }
    1.26 +
    1.27 +    SDL_LockJoysticks();
    1.28 +    player_index = SDL_GetPlayerIndexForJoystickID(device_instance);
    1.29 +    if (player_index >= 0) {
    1.30 +        SDL_joystick_players[player_index] = -1;
    1.31 +    }
    1.32 +    SDL_UnlockJoysticks();
    1.33  }
    1.34  
    1.35  int
     2.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360w.c	Mon Jan 13 15:35:52 2020 -0800
     2.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360w.c	Mon Jan 13 15:35:54 2020 -0800
     2.3 @@ -245,9 +245,6 @@
     2.4  
     2.5                      HIDAPI_JoystickConnected(device, &joystickID);
     2.6  
     2.7 -                    /* Set the controller LED */
     2.8 -                    SetSlotLED(device->dev, joystickID);
     2.9 -
    2.10                  } else if (device->num_joysticks > 0) {
    2.11                      HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
    2.12                  }