From 940d393ce7db9264dc8e5c65d0ff153d39eca21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Corr=C3=AAa?= Date: Sat, 8 Aug 2026 03:32:52 -0300 Subject: [PATCH] fix: stop reading the gamepad while the window is not focused On the Steam Deck, opening the Steam menu over Hydra made the controller drive both at once: the selection moved in the Steam menu and in Hydra underneath. requestAnimationFrame throttles when a window is hidden, so polling would normally pause on its own. That does not happen here because the Steam overlay draws on top of the window without hiding it, so frames keep being delivered and the app keeps consuming input it can no longer act on. Gate the polling on window focus instead. The frame loop keeps being scheduled so input resumes by itself, and losing focus clears pending timers and accepted inputs the same way setInputEnabled(false) already does -- otherwise a button held at that moment would keep its repeat timer alive in the background. The flag is separate from inputEnabled on purpose: that one is owned by navigation-input.provider, and reusing it would let a focus event re-enable input the provider had deliberately turned off. --- .../src/services/gamepad.service.ts | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/big-picture/src/services/gamepad.service.ts b/src/big-picture/src/services/gamepad.service.ts index 9f6a9fc583..23ec95c232 100644 --- a/src/big-picture/src/services/gamepad.service.ts +++ b/src/big-picture/src/services/gamepad.service.ts @@ -91,6 +91,7 @@ export class GamepadService { private readonly axisTriggerThreshold = 0.5; private isPolling = false; + private windowFocused = true; private animationFrameId: number | null = null; private activeGamepadIndex: number | null = null; private lastActiveGamepadSwitchTime = 0; @@ -148,6 +149,13 @@ export class GamepadService { "gamepaddisconnected", this.handleGamepadDisconnection ); + + globalThis.window.addEventListener("blur", this.handleWindowBlur); + globalThis.window.addEventListener("focus", this.handleWindowFocus); + + // The window may already be in the background when the service starts, so + // seed the flag instead of assuming focus. + this.windowFocused = globalThis.document?.hasFocus?.() ?? true; } private createInitialStickState(): GamepadStickState { @@ -238,13 +246,22 @@ export class GamepadService { }; private pollGamepads() { - const gamepads = globalThis.navigator.getGamepads(); - - for (const gamepad of gamepads) { - if (!gamepad) continue; - - this.gamepads.set(gamepad.index, gamepad); - this.updateGamepadState(gamepad.index, gamepad); + // Reading the gamepad only makes sense while the window has focus. + // + // requestAnimationFrame already throttles when the window is hidden, but + // that is not enough on the Steam Deck: the Steam overlay draws on top of + // the window without hiding it, so frames keep coming and the app would + // keep consuming the controller while the user navigates the Steam menu — + // moving the selection in both places at once. + if (this.windowFocused) { + const gamepads = globalThis.navigator.getGamepads(); + + for (const gamepad of gamepads) { + if (!gamepad) continue; + + this.gamepads.set(gamepad.index, gamepad); + this.updateGamepadState(gamepad.index, gamepad); + } } this.animationFrameId = globalThis.requestAnimationFrame(() => @@ -252,6 +269,21 @@ export class GamepadService { ); } + private readonly handleWindowBlur = () => { + if (!this.windowFocused) return; + + this.windowFocused = false; + + // Same cleanup setInputEnabled(false) does: a button held when focus is + // lost would otherwise keep its repeat timer running in the background. + this.clearAllTimers(); + this.recentAcceptedInputs = []; + }; + + private readonly handleWindowFocus = () => { + this.windowFocused = true; + }; + private startPolling() { if (this.isPolling) return; @@ -1340,6 +1372,8 @@ export class GamepadService { "gamepaddisconnected", this.handleGamepadDisconnection ); + globalThis.window.removeEventListener("blur", this.handleWindowBlur); + globalThis.window.removeEventListener("focus", this.handleWindowFocus); this.gamepads.clear(); this.gamepadStates.clear(); this.buttonPressCallbacks.clear();