Selkies

lib/gamepad

Gamepad polling for the streaming cores: reads navigator.getGamepads() on a fixed interval and reports button and axis changes in the standard layout. The interval is the rate Chromium samples pads at, so a change waits on the page no longer than it already waited in the browser. The on-screen touch gamepad announces its own changes (touchgamepadinput), and is read the moment it does rather than on the next tick.

A client drives the server slots it holds, one pad each (GamepadManager._choose): one slot outside a token that names several. Pads that all fit drive at once, in the browser's order; past that, a pad taken up takes a slot, so a further device's resting or noisy controls (a flight stick's pots, a throttle parked at an end) never write over a pad in use.

Rumble goes the other way (rumble): the dual-rumble effect of the Gamepad API's vibrationActuator (Chromium, WebKit), else Gecko's hapticActuators pulse.

Pads the browser could not map to the standard layout are remapped through the per-platform profile database that gendb.js generates: raw button and axis indices differ across platforms for the same pad, so the lookup is scoped to the platform this browser runs on, and anything unmatched (ChromeOS, the BSDs) uses the evdev layout Linux browsers report. Such pads also carry the D-pad on axes 4 and 5, which is translated to the standard buttons 12 to 15.

Classes

GamepadManager

Defined in: lib/gamepad.js:89

Polls every connected pad and reports changes through callbacks.

Polling starts in the constructor and runs until destroy; enable and disable pause it without losing per-pad state.

Constructors

Constructor
new GamepadManager(
   gamepad, 
   onButton, 
   onAxis, 
   onHeld, 
   onActive
): GamepadManager;

Defined in: lib/gamepad.js:105

Parameters
ParameterTypeDescription
gamepadGamepadPad the manager was created for, kept for the caller.
onButton(index, button, value, pressed) => voidCalled with the pad slot and standard-layout button index on every change.
onAxis(index, axis, value) => voidCalled with the pad slot and standard-layout axis index on every change.
onHeld() => voidCalled about ten times a second while any control is away from rest, so the server can neutralize a held pad whose client died without a transport close.
onActive(gamepad, switched, position) => voidCalled with the pad that now drives the slot at position among those the client holds, or null when none does; switched when it replaces another, whose held controls the slot must drop, rather than announcing the same pad again.
Returns

GamepadManager

Properties

gamepad
gamepad: Gamepad;

Defined in: lib/gamepad.js:106

onButton
onButton: (index, button, value, pressed) => void;

Defined in: lib/gamepad.js:107

Parameters
ParameterType
indexnumber
buttonnumber
valuenumber
pressedboolean
Returns

void

onAxis
onAxis: (index, axis, value) => void;

Defined in: lib/gamepad.js:108

Parameters
ParameterType
indexnumber
axisnumber
valuenumber
Returns

void

onHeld
onHeld: () => void;

Defined in: lib/gamepad.js:109

Returns

void

onActive
onActive: (gamepad, switched, position) => void;

Defined in: lib/gamepad.js:110

Parameters
ParameterType
gamepadGamepad
switchedboolean
positionnumber
Returns

void

drivers
drivers: any[];

Defined in: lib/gamepad.js:112

Per slot the client holds, in its order, the browser index of the pad driving it, or null.

state
state: object;

Defined in: lib/gamepad.js:119

interval
interval: number;

Defined in: lib/gamepad.js:121

rumbleEnabled
rumbleEnabled: boolean;

Defined in: lib/gamepad.js:127

Pads play the rumble relayed to them (the dashboards' toggle).

Accessors

active
Get Signature
get active(): any;

Defined in: lib/gamepad.js:133

The pad driving the client's first slot, or null.

Returns

any

Methods

setSlotCount()
setSlotCount(count): void;

Defined in: lib/gamepad.js:142

Holds count slots (a token's list, else one); a pad driving one beyond them gives it up.

Parameters
ParameterTypeDescription
countnumber-
Returns

void

drivenPosition()
drivenPosition(i): number;

Defined in: lib/gamepad.js:157

The position among the client's slots of the one pad i drives, or -1.

Parameters
ParameterTypeDescription
inumber-
Returns

number

enable()
enable(): void;

Defined in: lib/gamepad.js:162

Resumes polling.

Returns

void

disable()
disable(): void;

Defined in: lib/gamepad.js:170

Pauses polling, and stops a rumble playing; the per-pad state is kept.

Returns

void

padGone()
padGone(index): void;

Defined in: lib/gamepad.js:468

A pad the browser reports gone: if it drove a slot, the slot is given up at once, even while polling is paused.

Parameters
ParameterTypeDescription
indexnumber-
Returns

void

reannounce()
reannounce(): void;

Defined in: lib/gamepad.js:480

Announces each pad that drives a slot again, and has its whole state sent anew: the slots changed, or a channel reopened.

Returns

void

rumble()
rumble(
   strong, 
   weak, 
   durationMs, 
   index?
): void;

Defined in: lib/gamepad.js:571

Plays a rumble on every connected pad that can, or on pad index alone: both motors for durationMs, at most the Gamepad API's 5 s, a new call replacing the one before; 0 on both motors stops it. Gecko's pulse has one motor, which takes the stronger level. Nothing plays while rumble is off or polling is paused, and a stop with nothing playing is not sent.

Parameters
ParameterTypeDefault valueDescription
strongnumberundefinedStrong (low-frequency) motor, 0 to 1.
weaknumberundefinedWeak (high-frequency) motor, 0 to 1.
durationMsnumberundefined-
index?numbernullThe pad driving the slot the rumble is for, where the client drives several; null for every pad.
Returns

void

stopRumble()
stopRumble(): void;

Defined in: lib/gamepad.js:606

Stops a rumble playing on the pads, if one is.

Returns

void

setRumbleEnabled()
setRumbleEnabled(on): void;

Defined in: lib/gamepad.js:616

Turns rumble on or off; off stops one playing.

Parameters
ParameterTypeDescription
onboolean-
Returns

void

destroy()
destroy(): void;

Defined in: lib/gamepad.js:622

Stops polling and any rumble, and forgets every pad.

Returns

void

Variables

GP_TIMEOUT

const GP_TIMEOUT: 4 = 4;

Defined in: lib/gamepad.js:52

Poll interval in milliseconds: Chromium samples pads every 4 ms.

Functions

DPAD_REST()

function DPAD_REST(): object;

Defined in: lib/gamepad.js:68

A pad's state before anything it holds has been reported.

Returns

object

12
12: boolean = false;
13
13: boolean = false;
14
14: boolean = false;
15
15: boolean = false;