0.0.5 • Published 5 years ago

@maulingmonkey/gamepad v0.0.5

Weekly downloads
8
License
Apache-2.0
Repository
github
Last release
5 years ago

mmk.gamepad

MaulingMonkey's typescript gamepad API for consistent cross-browser and cross-gamepad binding, polling for input, etc.

What? Why?

The vanilla browser gamepad APIs are unusable without a lot work, except for maybe if you stick to vanilla Microsoft 360/XB1 gamepads.

  1. Noop fallbacks for unsupported browsers so you don't have to do your own feature testing.
  2. Edge has conflicting gamepad browser navigation that needs to be disabled, if you don't want basic input like pressing (B) to navigate away.
  3. Many browsers poorly standardize gamepads. Even ignoring outright buggy "standard" layout gamepads, I've seen dpads exposed as -1/8..+1 axises, int -> float bitcasts of accelerometer data, gamepads that start initialized to non-resting states... testing hardware you don't have is hard, I guess.
  4. Basic gamepad APIs don't handle basic things like deadzone functionality, events (although Edge has some keydown events for gamepads)

Browser Support

OSBrowserMax GamepadsAPI(s)Notes
Windows 7Opera 444XInput or RawInput
Windows 7Chrome 574XInput or RawInput
Windows 7FireFox 535+XInput or RawInput
Windows 10Edge4?Only XInput?4
Windows 7IE 110No gamepad API 1!isSupported 4
Windows 7IE 8XXX 2No gamepad APICompatability bugs
Ubuntu 18.04 LTSChromium 650No gamepad API 1"isSupported" lies 3
Ubuntu 18.04 LTSChrome 664+Unknown
Ubuntu 18.04 LTSFireFox 594+Unknown
  1. All mmk.gamepad functions should still "work" as if there were no gamepads connected.
  2. This browser may still have compatability bugs even providing the "no gamepads" interface.
  3. This browser implements a gamepad API, but no gamepads work.
  4. You may need to specify <meta http-equiv="X-UA-Compatible" content="IE=9" /> or better to avoid JS errors.
  5. Edge has some built-in gamepad navigation behavior. mmk.gamepad will disable this by default.

Gamepad support

OSGamepadChromeOperaFireFoxEdgeNotes
WindowsXbox 360OKOKOK 1OK
WindowsXbox OneOK 1OK 1OK 1OK 1
WindowsDualShock 4 (Micro-USB)OKOKNo DPadNo touch/gyros
WindowsDualShock 4 (Wireless)OKOKNo DPadNo touch/gyros
WindowsDualShock 3 (Mini-USB)OKOKNo DPadNeeds PS Now
WindowsNintendo Switch Pro ContPendingPending?2
WindowsSaitek X52No ScrollNo ScrollNo HAT 13
  1. This gamepad displays an incorrect or generic name which we cannot work around (e.g. "Xbox 360 Controller" for XB1 controllers, or "xinput")
  2. Host drivers don't fully initialize this, Chrome adding support.
  3. This has a custom .mapping !== "standard"
OSGamepadChrome 4FireFoxNotes
UbuntuXbox 360OKOK
UbuntuXbox OneOKOK
UbuntuDualShock 4 (Micro-USB)OK 5OKNo gyros, bad init 6, touchpad is mouse
UbuntuDualShock 4 (Wireless)OK 5OKNo gyros, bad init 6, touchpad is mouse
UbuntuDualShock 3 (Mini-USB)OKOKNo gyros
  1. Not Chromium - which still defines the gamepad API, but doesn't appear to actually export info about connected gamepads even when Chrome will on the same system.

  2. Note that the baseline gamepad API lies about this gamepad being in the standard layout already in Chrome 66 on Ubuntu 18.04 LTS. tryRemapStdLayout will fix these to use the correct layout anyways.

  3. Some triggers/axises of this gamepad currently misinitialize to non-zero values (e.g. -1, 0.5), although they'll correct themselves after some use. Could be worked around with some more stateful mapping (e.g. treating known bad values as 0.0 until the right stick / triggers are sufficiently exercised.)

Basic API

See demo/demo.ts

// In case you want to display a hint to switch browsers ;)
if (!mmk.gamepad.isSupported()) console.warn("Gamepads not available in this browser");

// The rough equivalent of navigator.getGamepads(), with fallbacks.
var gamepads = mmk.gamepad.getRawGamepads() // Can return undefined elements (for e.g. disconnected controllers)

var snapshot = mmk.gamepad.cloneGamepads(gamepads); // Deep copy of basic fields
snapshot[0]  = mmk.gamepad.cloneGamepad(snapshot[0]); // Hey why not

// Works despite the lack of "connectedgamepad" and "disconnectedgamepad" events:
mmk.gamepad.addRawConnectedListener   ( function(gamepad) { console.log("Connected gamepad:",   gamepad); } );
mmk.gamepad.addRawDisconnectedListener( function(gamepad) { console.log("Disconnected gamepad:", gamepad); } );
// (note that addRawConnectedListener will immediately invoke your callback on already connected gamepads!)

var standard = mmk.gamepad.tryRemapStdLayout(snapshot[0]);
if (standard) { console.assert(standard.mapping === "standard"); } // Well, that was easy!
else          { ... } // Fallback to non-standard mapping prompts etc...

Installation

Via npm

  • Grab NPM via node.js
  • Install per project
    npm i @maulingmonkey/gamepad

Contribution Notes

Adding New Devices

Negative values should always be left/up/forward/counterclockwise, with positive values corresponding to their opposites (right/down/backwards/clockwise). This might be unintuitive for some axises on an individual basis (yes, joystick forward is negative!), but this has a couple of advantages. Firstly, it keeps everything nice and consistent for remapping purpouses (thumbstick up is negative too, and you might want to use that as your throttle!) Secondly, it reduces the importance of properly categorizing your axises ("should I map this as a throttle, or as a slider?") as this won't impact how the values are actually mapped.

Additionally, axises should almost always span the entire [-1..+1] range. While you might want to remap this to [0..+1] in your game (possibly with a button for reversing), consistency at the lowest layer will make such remaps more straightforward. The exception to this would be any unidirectional, spring-loaded axises - any spring-loaded resting position should be 0. It's possible such axises should be considered buttons instead - e.g. gamepad triggers are considered buttons in the "standard" mapping.

Axis (-1..+1)Min (-)Max (+)Notes
Thumbstick X"left""right"
Thumbstick Y"up""down"
Joystick X"left""right"
Joystick Y"forward""backward"
Joystick Twist"ccw""cw"
Throttle"forward""backward"Forward being negative may be suprising, but keeps consistent with thumbsticks.
Mouse X"left""right"
Mouse Y"up""down"
Buttons (0..+1)Notes
TriggerThis includes analog triggers like the "standard" gamepad.
DPad Up/Down/L/RDigital direction pads should be treated as 4 buttons, not axises.
HAT Up/Down/L/RDigital HATs, like direction pads, should be treated as 4 buttons, not axises.