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 4090

Summary: SDL_JoystickID first assignment is in the wrong order
Product: SDL Reporter: Pasculator <pascodagama>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: NEW --- QA Contact: Sam Lantinga <slouken>
Severity: minor    
Priority: P2    
Version: 2.0.7   
Hardware: x86_64   
OS: Windows 10   

Description Pasculator 2018-02-21 20:18:01 UTC
I am using this code to detect controllers connected to my system.

int numJoysticksEnumerated = SDL_NumJoysticks();
std::cout << "Number of joysticks to check: " << numJoysticksEnumerated << std::endl;
for (int i = 0; i < numJoysticksEnumerated; ++i) {
    std::cout << "Checking joystick ID " << i << std::endl;
    if (SDL_IsGameController(i)) {
        SDL_GameController* gamePad = SDL_GameControllerOpen(i);
        if (gamePad) {
            std::cout << "Controller detected." << std::endl;
        } else {
            logSDLError(std::cout, "Controller detection failure.");
        }
    }
}

When I call

SDL_ControllerButtonEvent::which

afterwards, the lowest index is the one of the controller I connected most recently and the highest index is the one of the controller I connected first. This seems counter-intuitive since the Xbox-controller lights show the controller that was connected first as the first controller and so on. If I connect an additional controller after the first detection and run the detection again, the new controller will get a higher ID.

If for example I do this in order:

Controller 1 connected
Controller 2 connected
Controller 3 connected
Start game
Run controller detection
Controller 4 connected
Run controller detection

The result is:

Controller    SDL_JoystickID
1             2
2             1
3             0
4             3