Handle joystick hotplug fallback and avoid stale gamepad matches
This commit is contained in:
parent
ca41cf32cc
commit
cf652190a3
1 changed files with 34 additions and 26 deletions
|
|
@ -52,7 +52,7 @@ SdlInputHandler::findStateForGamepad(SDL_JoystickID id)
|
|||
int i;
|
||||
|
||||
for (i = 0; i < MAX_GAMEPADS; i++) {
|
||||
if (m_GamepadState[i].jsId == id) {
|
||||
if (m_GamepadState[i].controller != nullptr && m_GamepadState[i].jsId == id) {
|
||||
SDL_assert(!m_MultiController || m_GamepadState[i].index == i);
|
||||
return &m_GamepadState[i];
|
||||
}
|
||||
|
|
@ -618,15 +618,17 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve
|
|||
return;
|
||||
}
|
||||
|
||||
SDL_JoystickID jsId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
|
||||
|
||||
// SDL_CONTROLLERDEVICEADDED can be reported multiple times for the same
|
||||
// gamepad in rare cases, because SDL doesn't fixup the device index in
|
||||
// the SDL_CONTROLLERDEVICEADDED event if an unopened gamepad disappears
|
||||
// before we've processed the add event.
|
||||
for (int i = 0; i < MAX_GAMEPADS; i++) {
|
||||
if (m_GamepadState[i].controller == controller) {
|
||||
if (m_GamepadState[i].controller != nullptr && m_GamepadState[i].jsId == jsId) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Received duplicate add event for controller index: %d",
|
||||
event->which);
|
||||
"Received duplicate add event for joystick instance ID: %d",
|
||||
jsId);
|
||||
SDL_GameControllerClose(controller);
|
||||
return;
|
||||
}
|
||||
|
|
@ -682,7 +684,7 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve
|
|||
}
|
||||
|
||||
state->controller = controller;
|
||||
state->jsId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(state->controller));
|
||||
state->jsId = jsId;
|
||||
|
||||
hapticCaps = 0;
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 18)
|
||||
|
|
@ -886,7 +888,14 @@ void SdlInputHandler::handleJoystickArrivalEvent(SDL_JoyDeviceEvent* event)
|
|||
{
|
||||
SDL_assert(event->type == SDL_JOYDEVICEADDED);
|
||||
|
||||
if (!SDL_IsGameController(event->which)) {
|
||||
if (SDL_IsGameController(event->which)) {
|
||||
SDL_ControllerDeviceEvent controllerEvent = {};
|
||||
controllerEvent.type = SDL_CONTROLLERDEVICEADDED;
|
||||
controllerEvent.which = event->which;
|
||||
handleControllerDeviceEvent(&controllerEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
char guidStr[33];
|
||||
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(event->which),
|
||||
guidStr, sizeof(guidStr));
|
||||
|
|
@ -908,7 +917,6 @@ void SdlInputHandler::handleJoystickArrivalEvent(SDL_JoyDeviceEvent* event)
|
|||
"Unable to open joystick for query: %s",
|
||||
SDL_GetError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SdlInputHandler::handleJoystickRemovalEvent(SDL_JoyDeviceEvent* event)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue