Improved reliability of gamepad message ordering

The gamepad vs joystick events always happen in this order:
SDL_EVENT_JOYSTICK_ADDED
SDL_EVENT_GAMEPAD_ADDED
SDL_EVENT_GAMEPAD_REMAPPED
SDL_EVENT_GAMEPAD_REMOVED
SDL_EVENT_JOYSTICK_REMOVED

Whenever a mapping is changed, any controller affected by that mapping will generate a gamepad event. You will only get one SDL_EVENT_GAMEPAD_REMAPPED event per controller per batch of mapping changes, where SDL_AddGamepadMappingsFromFile() and SDL_AddGamepadMapping() are each a batch of changes.
This commit is contained in:
Sam Lantinga
2023-07-18 11:52:56 -07:00
parent ccefce8321
commit dfc6e8825e
5 changed files with 356 additions and 178 deletions

View File

@@ -113,7 +113,8 @@ SDL_Mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
static SDL_AtomicInt SDL_joystick_lock_pending;
static int SDL_joysticks_locked;
static SDL_bool SDL_joysticks_initialized;
static SDL_bool SDL_joysticks_quitting = SDL_FALSE;
static SDL_bool SDL_joysticks_quitting;
static SDL_bool SDL_joystick_being_added;
static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static SDL_AtomicInt SDL_last_joystick_instance_id SDL_GUARDED_BY(SDL_joystick_lock);
static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;
@@ -1621,6 +1622,8 @@ void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
return;
}
SDL_joystick_being_added = SDL_TRUE;
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
player_index = driver->GetDevicePlayerIndex(device_index);
}
@@ -1644,6 +1647,17 @@ void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_joystick_being_added = SDL_FALSE;
if (SDL_IsGamepad(instance_id)) {
SDL_PrivateGamepadAdded(instance_id);
}
}
SDL_bool SDL_IsJoystickBeingAdded(void)
{
return SDL_joystick_being_added;
}
void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
@@ -1696,6 +1710,13 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id)
}
}
/* FIXME: The driver no longer provides the name and GUID at this point, so we
* don't know whether this was a gamepad. For now always send the event.
*/
if (SDL_TRUE /*SDL_IsGamepad(instance_id)*/) {
SDL_PrivateGamepadRemoved(instance_id);
}
#ifndef SDL_EVENTS_DISABLED
event.type = SDL_EVENT_JOYSTICK_REMOVED;
event.common.timestamp = 0;