Skip to content

Commit

Permalink
Joystick: add try/catch around navigator.getGamepads() because it mig…
Browse files Browse the repository at this point in the history
…ht throw a JS SecurityError if we don't have permission to call it (#1728)

Fixes the following exception when we don't have permissions:

> Failed to execute 'getGamepads' on 'Navigator': Access to the feature "gamepad" is disallowed by permissions policy.

By catching the exception, it should now behave the same as older browsers, where navigator.getGamepads() doesn't exist at all.

In the future, it might make sense to set a flag if navigator.getGamepads() throws, and skip calling it more than once. However, we may want to listen for some kind of browser event that indicates that permission was granted later, and clear the flag when appropriate. Perhaps the gamepadconnected event?

---------

Co-authored-by: Josh Tynjala <[email protected]>
  • Loading branch information
andrew-git and joshtynjala authored Dec 1, 2023
1 parent d68bce1 commit c40ec31
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/lime/ui/Joystick.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,19 @@ class Joystick
#if (js && html5)
@:noCompletion private static function __getDeviceData():Array<Dynamic>
{
return
(untyped navigator.getGamepads) ? untyped navigator.getGamepads() : (untyped navigator.webkitGetGamepads) ? untyped navigator.webkitGetGamepads() : null;
var res:Dynamic = null;

try
{
res = (untyped navigator.getGamepads) ? untyped navigator.getGamepads() : (untyped navigator.webkitGetGamepads) ? untyped navigator.webkitGetGamepads() : null;
}
catch (err:Dynamic)
{
// if something went wrong, treat it the same as when navigator.getGamepads doesn't exist
// we probably don't have permission to use this feature
}

return res;
}
#end

Expand Down

0 comments on commit c40ec31

Please sign in to comment.