1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Merge pull request #746 from ferferga/servernotifications-fixes

Load gamepadtokey.js only when is necessary
This commit is contained in:
dkanada 2020-03-04 02:08:19 +09:00 committed by GitHub
commit 4cedbc1c89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 121 deletions

View file

@ -251,114 +251,150 @@ require(['apphost'], function (appHost) {
} }
} }
var inputLoopTimer;
function runInputLoop() { function runInputLoop() {
// Get the latest gamepad state. // Get the latest gamepad state.
var gamepads; var gamepads = navigator.getGamepads();
if (navigator.getGamepads) { for (var i = 0, len = gamepads.length; i < len; i++) {
gamepads = navigator.getGamepads();
} else if (navigator.webkitGetGamepads) {
gamepads = navigator.webkitGetGamepads();
}
gamepads = gamepads || [];
var i;
var j;
var len;
for (i = 0, len = gamepads.length; i < len; i++) {
var gamepad = gamepads[i]; var gamepad = gamepads[i];
if (gamepad) { if (!gamepad) {
// Iterate through the axes continue;
var axes = gamepad.axes; }
var leftStickX = axes[0]; // Iterate through the axes
var leftStickY = axes[1]; var axes = gamepad.axes;
if (leftStickX > _THUMB_STICK_THRESHOLD) { // Right var leftStickX = axes[0];
_ButtonPressedState.setleftThumbstickRight(true); var leftStickY = axes[1];
} else if (leftStickX < -_THUMB_STICK_THRESHOLD) { // Left if (leftStickX > _THUMB_STICK_THRESHOLD) { // Right
_ButtonPressedState.setleftThumbstickLeft(true); _ButtonPressedState.setleftThumbstickRight(true);
} else if (leftStickY < -_THUMB_STICK_THRESHOLD) { // Up } else if (leftStickX < -_THUMB_STICK_THRESHOLD) { // Left
_ButtonPressedState.setleftThumbstickUp(true); _ButtonPressedState.setleftThumbstickLeft(true);
} else if (leftStickY > _THUMB_STICK_THRESHOLD) { // Down } else if (leftStickY < -_THUMB_STICK_THRESHOLD) { // Up
_ButtonPressedState.setleftThumbstickDown(true); _ButtonPressedState.setleftThumbstickUp(true);
} else { } else if (leftStickY > _THUMB_STICK_THRESHOLD) { // Down
_ButtonPressedState.setleftThumbstickLeft(false); _ButtonPressedState.setleftThumbstickDown(true);
_ButtonPressedState.setleftThumbstickRight(false); } else {
_ButtonPressedState.setleftThumbstickUp(false); _ButtonPressedState.setleftThumbstickLeft(false);
_ButtonPressedState.setleftThumbstickDown(false); _ButtonPressedState.setleftThumbstickRight(false);
} _ButtonPressedState.setleftThumbstickUp(false);
// Iterate through the buttons to see if Left thumbstick, DPad, A and B are pressed. _ButtonPressedState.setleftThumbstickDown(false);
var buttons = gamepad.buttons; }
for (j = 0, len = buttons.length; j < len; j++) { // Iterate through the buttons to see if Left thumbstick, DPad, A and B are pressed.
if (ProcessedButtons.indexOf(j) !== -1) { var buttons = gamepad.buttons;
for (var j = 0, len = buttons.length; j < len; j++) {
if (buttons[j].pressed) { if (ProcessedButtons.indexOf(j) !== -1) {
switch (j) { if (buttons[j].pressed) {
case _GAMEPAD_DPAD_UP_BUTTON_INDEX: switch (j) {
_ButtonPressedState.setdPadUp(true); case _GAMEPAD_DPAD_UP_BUTTON_INDEX:
break; _ButtonPressedState.setdPadUp(true);
case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX: break;
_ButtonPressedState.setdPadDown(true); case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX:
break; _ButtonPressedState.setdPadDown(true);
case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX: break;
_ButtonPressedState.setdPadLeft(true); case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX:
break; _ButtonPressedState.setdPadLeft(true);
case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX: break;
_ButtonPressedState.setdPadRight(true); case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX:
break; _ButtonPressedState.setdPadRight(true);
case _GAMEPAD_A_BUTTON_INDEX: break;
_ButtonPressedState.setgamepadA(true); case _GAMEPAD_A_BUTTON_INDEX:
break; _ButtonPressedState.setgamepadA(true);
case _GAMEPAD_B_BUTTON_INDEX: break;
_ButtonPressedState.setgamepadB(true); case _GAMEPAD_B_BUTTON_INDEX:
break; _ButtonPressedState.setgamepadB(true);
default: break;
// No-op default:
break; // No-op
} break;
} else { }
switch (j) { } else {
case _GAMEPAD_DPAD_UP_BUTTON_INDEX: switch (j) {
if (_ButtonPressedState.getdPadUp()) { case _GAMEPAD_DPAD_UP_BUTTON_INDEX:
_ButtonPressedState.setdPadUp(false); if (_ButtonPressedState.getdPadUp()) {
} _ButtonPressedState.setdPadUp(false);
break; }
case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX: break;
if (_ButtonPressedState.getdPadDown()) { case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX:
_ButtonPressedState.setdPadDown(false); if (_ButtonPressedState.getdPadDown()) {
} _ButtonPressedState.setdPadDown(false);
break; }
case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX: break;
if (_ButtonPressedState.getdPadLeft()) { case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX:
_ButtonPressedState.setdPadLeft(false); if (_ButtonPressedState.getdPadLeft()) {
} _ButtonPressedState.setdPadLeft(false);
break; }
case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX: break;
if (_ButtonPressedState.getdPadRight()) { case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX:
_ButtonPressedState.setdPadRight(false); if (_ButtonPressedState.getdPadRight()) {
} _ButtonPressedState.setdPadRight(false);
break; }
case _GAMEPAD_A_BUTTON_INDEX: break;
if (_ButtonPressedState.getgamepadA()) { case _GAMEPAD_A_BUTTON_INDEX:
_ButtonPressedState.setgamepadA(false); if (_ButtonPressedState.getgamepadA()) {
} _ButtonPressedState.setgamepadA(false);
break; }
case _GAMEPAD_B_BUTTON_INDEX: break;
if (_ButtonPressedState.getgamepadB()) { case _GAMEPAD_B_BUTTON_INDEX:
_ButtonPressedState.setgamepadB(false); if (_ButtonPressedState.getgamepadB()) {
} _ButtonPressedState.setgamepadB(false);
break; }
default: break;
// No-op default:
break; // No-op
} break;
} }
} }
} }
} }
} }
// Schedule the next one // Schedule the next one
requestAnimationFrame(runInputLoop); inputLoopTimer = requestAnimationFrame(runInputLoop);
} }
runInputLoop(); function startInputLoop() {
if (!inputLoopTimer) {
runInputLoop();
}
}
function stopInputLoop() {
cancelAnimationFrame(inputLoopTimer);
inputLoopTimer = undefined;
}
function isGamepadConnected() {
var gamepads = navigator.getGamepads();
for (var i = 0, len = gamepads.length; i < len; i++) {
var gamepad = gamepads[i];
if (gamepad && gamepad.connected) {
return true;
}
}
return false;
}
function onFocusOrGamepadAttach(e) {
if (isGamepadConnected() && document.hasFocus()) {
console.log("Gamepad connected! Starting input loop");
startInputLoop();
}
}
function onFocusOrGamepadDetach(e) {
if (!isGamepadConnected() || !document.hasFocus()) {
console.log("Gamepad disconnected! No other gamepads are connected, stopping input loop");
stopInputLoop();
} else {
console.log("Gamepad disconnected! There are gamepads still connected.");
}
}
// Event listeners for any change in gamepads' state.
window.addEventListener("gamepaddisconnected", onFocusOrGamepadDetach);
window.addEventListener("gamepadconnected", onFocusOrGamepadAttach);
window.addEventListener("blur", onFocusOrGamepadDetach);
window.addEventListener("focus", onFocusOrGamepadAttach);
onFocusOrGamepadAttach();
// The gamepadInputEmulation is a string property that exists in JavaScript UWAs and in WebViews in UWAs. // The gamepadInputEmulation is a string property that exists in JavaScript UWAs and in WebViews in UWAs.
// It won't exist in Win8.1 style apps or browsers. // It won't exist in Win8.1 style apps or browsers.

View file

@ -146,6 +146,17 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager)
}); });
} }
// Gamepad initialisation. No script is required if no gamepads are present at init time, saving a bit of resources.
// Whenever the gamepad is connected, we hand all the control of the gamepad to gamepadtokey.js by removing the event handler
function attachGamepadScript(e) {
console.log("Gamepad connected! Attaching gamepadtokey.js script");
window.removeEventListener("gamepadconnected", attachGamepadScript);
require(["components/input/gamepadtokey"]);
}
// No need to check for gamepads manually at load time, the eventhandler will be fired for that
window.addEventListener("gamepadconnected", attachGamepadScript);
return { return {
enable: enable, enable: enable,
getKeyName: getKeyName, getKeyName: getKeyName,

View file

@ -191,36 +191,14 @@ define(['connectionManager', 'playbackManager', 'events', 'inputManager', 'focus
events.trigger(serverNotifications, msg.MessageType, [apiClient, msg.Data]); events.trigger(serverNotifications, msg.MessageType, [apiClient, msg.Data]);
} }
} }
function bindEvents(apiClient) { function bindEvents(apiClient) {
events.off(apiClient, "message", onMessageReceived); events.off(apiClient, "message", onMessageReceived);
events.on(apiClient, "message", onMessageReceived); events.on(apiClient, "message", onMessageReceived);
} }
function enableNativeGamepadKeyMapping() {
if (window.navigator && "string" == typeof window.navigator.gamepadInputEmulation) {
window.navigator.gamepadInputEmulation = "keyboard";
return true;
}
return false;
}
function isGamepadSupported() {
return "ongamepadconnected" in window || navigator.getGamepads || navigator.webkitGetGamepads;
}
connectionManager.getApiClients().forEach(bindEvents); connectionManager.getApiClients().forEach(bindEvents);
events.on(connectionManager, 'apiclientcreated', function (e, newApiClient) { events.on(connectionManager, 'apiclientcreated', function (e, newApiClient) {
bindEvents(newApiClient); bindEvents(newApiClient);
}); });
if (!enableNativeGamepadKeyMapping() && isGamepadSupported()) {
require(["components/serverNotifications/gamepadtokey"]);
}
require(["components/serverNotifications/mouseManager"]);
return serverNotifications; return serverNotifications;
}); });

View file

@ -492,6 +492,7 @@ var AppInfo = {};
require(["keyboardnavigation"], function(keyboardnavigation) { require(["keyboardnavigation"], function(keyboardnavigation) {
keyboardnavigation.enable(); keyboardnavigation.enable();
}); });
require(["mouseManager"]);
require(["focusPreventScroll"]); require(["focusPreventScroll"]);
require(["autoFocuser"], function(autoFocuser) { require(["autoFocuser"], function(autoFocuser) {
autoFocuser.enable(); autoFocuser.enable();
@ -904,9 +905,10 @@ var AppInfo = {};
define("htmlMediaHelper", [componentsPath + "/htmlMediaHelper"], returnFirstDependency); define("htmlMediaHelper", [componentsPath + "/htmlMediaHelper"], returnFirstDependency);
define("viewContainer", [componentsPath + "/viewContainer"], returnFirstDependency); define("viewContainer", [componentsPath + "/viewContainer"], returnFirstDependency);
define("dialogHelper", [componentsPath + "/dialogHelper/dialogHelper"], returnFirstDependency); define("dialogHelper", [componentsPath + "/dialogHelper/dialogHelper"], returnFirstDependency);
define("serverNotifications", [componentsPath + "/serverNotifications/serverNotifications"], returnFirstDependency); define("serverNotifications", [componentsPath + "/serverNotifications"], returnFirstDependency);
define("skinManager", [componentsPath + "/skinManager"], returnFirstDependency); define("skinManager", [componentsPath + "/skinManager"], returnFirstDependency);
define("keyboardnavigation", [componentsPath + "/keyboardnavigation"], returnFirstDependency); define("keyboardnavigation", [componentsPath + "/input/keyboardnavigation"], returnFirstDependency);
define("mouseManager", [componentsPath + "/input/mouseManager"], returnFirstDependency);
define("scrollManager", [componentsPath + "/scrollManager"], returnFirstDependency); define("scrollManager", [componentsPath + "/scrollManager"], returnFirstDependency);
define("autoFocuser", [componentsPath + "/autoFocuser"], returnFirstDependency); define("autoFocuser", [componentsPath + "/autoFocuser"], returnFirstDependency);
define("connectionManager", [], function () { define("connectionManager", [], function () {