Skip to content

Commit

Permalink
Free the joystick player index when the joystick is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 13, 2020
1 parent 1d32185 commit 3a796d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/joystick/SDL_joystick.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/joystick/hidapi/SDL_hidapi_xbox360w.c
Expand Up @@ -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]);
}
Expand Down

0 comments on commit 3a796d6

Please sign in to comment.