Throttle hotplug re-enumeration retries and reduce log noise
Some checks are pending
Build / setup (push) Waiting to run
Build / build-appimage (push) Blocked by required conditions
Build / build-steamlink (push) Blocked by required conditions
Build / build-windows-macos (push) Blocked by required conditions

This commit is contained in:
Joey Yakimowich-Payne 2026-02-12 07:40:45 -07:00
commit e94f0c5990

View file

@ -25,6 +25,7 @@ static bool isKeyboardMouseInputAllowed()
#define MOUSE_EMULATION_POLLING_INTERVAL 50 #define MOUSE_EMULATION_POLLING_INTERVAL 50
#define HOTPLUG_REENUMERATION_INTERVAL_MS 2000 #define HOTPLUG_REENUMERATION_INTERVAL_MS 2000
#define HOTPLUG_REENUMERATION_MAX_INTERVAL_MS 8000
// Determines how fast the mouse will move each interval // Determines how fast the mouse will move each interval
#define MOUSE_EMULATION_MOTION_MULTIPLIER 4 #define MOUSE_EMULATION_MOTION_MULTIPLIER 4
@ -102,6 +103,8 @@ SdlInputHandler::ensureStateForGamepad(SDL_JoystickID id)
void SdlInputHandler::pollForMissingGamepads() void SdlInputHandler::pollForMissingGamepads()
{ {
static uint32_t s_LastForcedReenumerationTick = 0; static uint32_t s_LastForcedReenumerationTick = 0;
static uint32_t s_ReenumerationIntervalMs = HOTPLUG_REENUMERATION_INTERVAL_MS;
static uint32_t s_ReenumerationAttempts = 0;
SDL_JoystickUpdate(); SDL_JoystickUpdate();
SDL_GameControllerUpdate(); SDL_GameControllerUpdate();
@ -138,6 +141,11 @@ void SdlInputHandler::pollForMissingGamepads()
m_LastJoystickCount = joystickCount; m_LastJoystickCount = joystickCount;
} }
if (joystickCount > 0) {
s_ReenumerationAttempts = 0;
s_ReenumerationIntervalMs = HOTPLUG_REENUMERATION_INTERVAL_MS;
}
for (int i = 0; i < MAX_GAMEPADS; i++) { for (int i = 0; i < MAX_GAMEPADS; i++) {
GamepadState* state = &m_GamepadState[i]; GamepadState* state = &m_GamepadState[i];
if (state->controller == nullptr) { if (state->controller == nullptr) {
@ -156,7 +164,7 @@ void SdlInputHandler::pollForMissingGamepads()
continue; continue;
} }
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Polling detected disconnected gamepad instance %d in slot %d; cleaning up", "Polling detected disconnected gamepad instance %d in slot %d; cleaning up",
state->jsId, state->jsId,
i); i);
@ -171,11 +179,14 @@ void SdlInputHandler::pollForMissingGamepads()
if (joystickCount == 0) { if (joystickCount == 0) {
uint32_t now = SDL_GetTicks(); uint32_t now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, s_LastForcedReenumerationTick + HOTPLUG_REENUMERATION_INTERVAL_MS)) { if (SDL_TICKS_PASSED(now, s_LastForcedReenumerationTick + s_ReenumerationIntervalMs)) {
s_LastForcedReenumerationTick = now; s_LastForcedReenumerationTick = now;
s_ReenumerationAttempts++;
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"No joysticks visible; forcing SDL joystick/gamecontroller re-enumeration"); "No joysticks visible; forcing SDL joystick/gamecontroller re-enumeration (attempt %u, interval %u ms)",
s_ReenumerationAttempts,
s_ReenumerationIntervalMs);
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
@ -210,6 +221,15 @@ void SdlInputHandler::pollForMissingGamepads()
} }
recoverUntrackedGamepads(joystickCount); recoverUntrackedGamepads(joystickCount);
if (joystickCount > 0) {
s_ReenumerationAttempts = 0;
s_ReenumerationIntervalMs = HOTPLUG_REENUMERATION_INTERVAL_MS;
}
else {
s_ReenumerationIntervalMs = qMin(s_ReenumerationIntervalMs * 2,
static_cast<uint32_t>(HOTPLUG_REENUMERATION_MAX_INTERVAL_MS));
}
} }
} }
} }
@ -777,7 +797,7 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve
// before we've processed the add event. // before we've processed the add event.
for (int i = 0; i < MAX_GAMEPADS; i++) { for (int i = 0; i < MAX_GAMEPADS; i++) {
if (m_GamepadState[i].controller != nullptr && m_GamepadState[i].jsId == jsId) { if (m_GamepadState[i].controller != nullptr && m_GamepadState[i].jsId == jsId) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Received duplicate add event for joystick instance ID: %d", "Received duplicate add event for joystick instance ID: %d",
jsId); jsId);
SDL_GameControllerClose(controller); SDL_GameControllerClose(controller);