We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5017

Summary: [PATCH] Assigning a gamepad the same player index twice sets the wrong index
Product: SDL Reporter: Cameron Gutman <cameron.gutman>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.0   
Hardware: x86   
OS: Windows 10   
Attachments: Fix incorrect player index when assigning a joystick the same index twice

Description Cameron Gutman 2020-03-08 01:28:08 UTC
When I call SDL_GameControllerSetPlayerIndex() to set a gamepad to a player index that it happens to already be using, the result is that the gamepad is moved to a new player index that isn't the one I requested.

Reproducer:
SDL_GameControllerSetPlayerIndex(controller, 0);
SDL_GameControllerSetPlayerIndex(controller, 0);

The problem is that we hit the following code:
    /* Move any existing joystick to another slot */
    if (existing_instance >= 0) {
        SDL_SetJoystickIDForPlayerIndex(SDL_FindFreePlayerIndex(), existing_instance);
    }

However, existing_instance == instance_id at that time, so we end up moving the joystick again. We can detect this case earlier to avoid this issue.
Comment 1 Cameron Gutman 2020-03-08 01:29:10 UTC
Created attachment 4241 [details]
Fix incorrect player index when assigning a joystick the same index twice
Comment 2 Sam Lantinga 2020-03-08 01:39:57 UTC
Patch added, thanks!
https://hg.libsdl.org/SDL/rev/36aabb2051e9