Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL Joystick indices are probably bad #30

Open
JayFoxRox opened this issue May 4, 2020 · 0 comments
Open

SDL Joystick indices are probably bad #30

JayFoxRox opened this issue May 4, 2020 · 0 comments

Comments

@JayFoxRox
Copy link
Member

JayFoxRox commented May 4, 2020

A couple of months ago, I have mentioned issues here:

https://discordapp.com/channels/428359196719972353/428360618102226946/666773610362568705

Look at: https://github.com/XboxDev/nxdk/blob/d531533bf26741ced7b017d3854fbd9424f7b1a5/lib/hal/input.c#L246

- Imagine only physical ports 1 and 4 are connected to gamepads.

Then SDL will report 2 SDL joysticks, but only the first one is successfully reported:

  • Joystick 0 will be from port 1 (which is correct)
  • Joystick 1 will be from port 2 (which is not connected, so this is bad)

Joystick 3 (on port 4), which has an actual gamepad again, is never checked.

There are also likely race conditions where someone gets the number of joysticks (SDL_NumJoysticks), but before they can query / loop over the joysticks (SDL_JoystickNameForIndex for example), one of the joysticks might already be disconnected.
So I assume that SDL has a method to synchronize front and backend. I'm not sure if this is transparently handled somewhere already.
(Edit: This looks like it should be handled by this code - but our driver implements it incorrectly!)

To avoid all of these issues, I proposed to use something like this:

    // Globals
    static int map_sdl_to_xpad[4];
    static int sdl_numpads = 0;
    // This would be in the SDL detect routine which synchronizes the SDL frontend with the backend:
    for(xpad_pad=0; xpad_pad<4; xpad_pad++)    {

        // Don't assign disconnected pads
        if(XPAD_current[xpad_pad].hPresent)
            continue;

        // Assign xpad pad to SDL index
        map_sdl_to_xpad[sdl_numpads] = xpad_pad;
        sdl_numpads++;
    }

Then SDL_NumJoysticks would return sdl_numpads. If we have any other device information (like the name or inputs) we might also have to cache them in this kind of of routine to avoid the race condition above.

However, I'm not sure if the joystick backend actually works like this. I suggested to also look at other backends how they do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant