Update bigpicture.js

Make gamepad work via steam launch
This commit is contained in:
2025-12-28 21:53:14 +13:00
parent 21da448e8f
commit fa04dc4e32
+73 -14
View File
@@ -594,32 +594,91 @@ function goForward() {
// ============================================================================= // =============================================================================
function initGamepadSupport() { function initGamepadSupport() {
if (!navigator.getGamepads) {
console.warn('[BigPicture] Gamepad API not available in this environment');
return;
}
// Note: On Linux (and some controllers like handheld integrated gamepads),
// the `gamepadconnected` event may not fire until the first button press,
// or at all. We rely on continuous polling for robustness.
window.addEventListener('gamepadconnected', (e) => { window.addEventListener('gamepadconnected', (e) => {
console.log('[BigPicture] Gamepad connected:', e.gamepad.id); console.log('[BigPicture] Gamepad connected:', e.gamepad?.id || 'unknown');
state.gamepadConnected = true; // Prefer the first connected controller as the active one.
state.gamepadIndex = e.gamepad.index; if (state.gamepadIndex === null) {
showToast('Controller connected'); state.gamepadConnected = true;
state.gamepadIndex = e.gamepad.index;
showToast('Controller connected');
}
}); });
window.addEventListener('gamepaddisconnected', (e) => { window.addEventListener('gamepaddisconnected', (e) => {
console.log('[BigPicture] Gamepad disconnected'); console.log('[BigPicture] Gamepad disconnected:', e.gamepad?.id || 'unknown');
state.gamepadConnected = false; // If the active controller disconnected, clear it; polling will auto-select another.
state.gamepadIndex = null; if (state.gamepadIndex === e.gamepad.index) {
showToast('Controller disconnected'); state.gamepadConnected = false;
state.gamepadIndex = null;
showToast('Controller disconnected');
}
}); });
// Initial scan (covers controllers that are already connected at load).
refreshActiveGamepad(true);
// Start polling for gamepad input // Start polling for gamepad input
requestAnimationFrame(pollGamepad); requestAnimationFrame(pollGamepad);
} }
function pollGamepad() { function getFirstConnectedGamepad(gamepads) {
if (state.gamepadConnected && state.gamepadIndex !== null) { if (!gamepads) return null;
const gamepads = navigator.getGamepads(); for (let i = 0; i < gamepads.length; i++) {
const gamepad = gamepads[state.gamepadIndex]; const gp = gamepads[i];
if (gp) return gp;
}
return null;
}
if (gamepad) { function refreshActiveGamepad(isInitial = false) {
handleGamepadInput(gamepad); const gamepads = navigator.getGamepads();
// If we have an index, verify it still points to a real gamepad.
let active = null;
if (state.gamepadIndex !== null) {
active = gamepads[state.gamepadIndex] || null;
}
// Fallback: pick the first connected controller.
if (!active) {
active = getFirstConnectedGamepad(gamepads);
}
if (active) {
const changed = !state.gamepadConnected || state.gamepadIndex !== active.index;
state.gamepadConnected = true;
state.gamepadIndex = active.index;
if (changed && !isInitial) {
console.log('[BigPicture] Active gamepad selected:', active.id);
showToast('Controller connected');
} }
} else {
if (state.gamepadConnected) {
state.gamepadConnected = false;
state.gamepadIndex = null;
if (!isInitial) {
showToast('Controller disconnected');
}
}
state.gamepadConnected = false;
state.gamepadIndex = null;
}
return { gamepads, active };
}
function pollGamepad() {
const { active } = refreshActiveGamepad(false);
if (active) {
handleGamepadInput(active);
} }
requestAnimationFrame(pollGamepad); requestAnimationFrame(pollGamepad);