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 - [PATCH] Assigning a gamepad the same player index twice sets the wrong index
Summary: [PATCH] Assigning a gamepad the same player index twice sets the wrong index
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: joystick (show other bugs)
Version: HG 2.0
Hardware: x86 Windows 10
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-03-08 01:28 UTC by Cameron Gutman
Modified: 2020-03-08 01:39 UTC (History)
0 users

See Also:


Attachments
Fix incorrect player index when assigning a joystick the same index twice (1.21 KB, patch)
2020-03-08 01:29 UTC, Cameron Gutman
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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