- 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.
A couple of months ago, I have mentioned issues here:
https://discordapp.com/channels/428359196719972353/428360618102226946/666773610362568705
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:
Then
SDL_NumJoysticks
would returnsdl_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.