DEV Community

Cover image for Press Cross, the overlay lights Circle: what the Gamepad API hands you over Bluetooth
Crane Zhou
Crane Zhou

Posted on

Press Cross, the overlay lights Circle: what the Gamepad API hands you over Bluetooth

The Gamepad API has a comforting line in its spec. If a controller's mapping property is "standard", then buttons[0] is the bottom face button, buttons[1] the right one, and so on through a fixed 17-slot layout. Write your code against that layout and every pad works.

The catch is the other value mapping can have: the empty string. That means "the browser does not know this device; here is what it sent, in the order it sent it". You get it more often than the tutorials suggest: PlayStation pads in some browser and OS combinations over Bluetooth, most third-party pads, and nearly everything in older embedded browsers.

I make a controller overlay for streamers, which is a page whose entire job is to light up the button you pressed. Here is the stock DualSense drawing fed a raw Sony report, before and after the fix:

The same button presses, raw and normalised
Left: the report taken at face value. Cross lights Circle, Circle lights Square, Square lights Cross, and the right stick is jammed upward. Right: the same input after reordering.

Both sides are the real rendering engine, filmed while a script feeds it the same sequence: Cross, Circle, Square, Triangle, right stick left and right, L2.

What a raw Sony report looks like

A DualShock 4 or DualSense that arrives unmapped puts its face buttons in HID order:

Raw index Button Standard slot it belongs in
0 Square 2
1 Cross 0
2 Circle 1
3 Triangle 3

Three of four face buttons are wrong, and wrong in a way that looks almost right, which is why bug reports say "my buttons are swapped" and not "nothing works". Triangle sits in the correct slot by coincidence and gives people false hope.

The axes are worse. The standard layout is four axes: left X, left Y, right X, right Y. A raw Sony pad reports six or more, with the triggers in the middle:

axes[0], axes[1]   left stick
axes[2]            right stick X
axes[3], axes[4]   L2, R2 (resting at -1, fully pulled at +1)
axes[5]            right stick Y
Enter fullscreen mode Exit fullscreen mode

Read axes[3] as "right stick Y" and your right stick is pinned to the top of its range for ever, because a trigger at rest reports -1.

Detecting it

You can't ask the browser "is this a Sony pad". You get an id string, and its format differs by engine. Chromium gives Wireless Controller (Vendor: 054c Product: 0ce6), Firefox gives 054c-0ce6-Wireless Controller. So:

export function isSonyRaw(pad: Pick<RawPad, 'id' | 'mapping'>): boolean {
  return pad.mapping !== 'standard'
    && /054c|dualsense|dualshock|wireless controller|playstation/i.test(pad.id || '');
}
Enter fullscreen mode Exit fullscreen mode

054c is Sony's USB vendor ID. The name matches are there because some stacks drop the IDs and keep only the product string. The mapping !== 'standard' guard matters most: when the browser has already mapped the pad, touching it would break a working controller.

The reorder table

For each of the standard slots, the table says which raw button to read:

export function sonyLayout(axisCount: number): PadLayout {
  return {
    //        ✕  ○  □  △  L1 R1 L2 R2 Sh Op L3 R3  ↑   ↓   ←   →  PS  TP
    buttons: [1, 2, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16, 17, 12, 13],
    rightStick: [2, axisCount > 5 ? 5 : 3],
    triggers: axisCount >= 6 ? [3, 4] : undefined,
  };
}
Enter fullscreen mode Exit fullscreen mode

The axisCount checks exist because not every raw Sony report has the same shape; a short one has the right stick on 2 and 3 like everyone else.

Triggers need both sources. On these pads L2 is a button and an axis, and depending on the stack only one of them may move, so the normaliser takes whichever is larger:

const fromAxis = axis === undefined ? 0 : clamp01((Number(ra[axis] ?? -1) + 1) / 2);
const fromButton = order[slot] >= 0 ? val(rb[order[slot]]) : 0;
const v = Math.max(fromAxis, fromButton);
return btn(v > 0.12, v);
Enter fullscreen mode Exit fullscreen mode

The (x + 1) / 2 turns a -1..1 axis into the 0..1 a standard trigger button reports. The 0.12 threshold keeps a trigger with a slightly sloppy rest position from flickering as pressed.

Where to put the fix

The tempting place for the fix is the drawing code. The trouble is that three things read the pad: the overlay, the studio preview, and the calibration dialog. Each would need the same correction, and the day one of them is forgotten the preview and the stream disagree.

What works is replacing navigator.getGamepads itself, once, before anything else reads it:

const rawGetGamepads = navigator.getGamepads.bind(navigator);
window.__RAW_GET_GAMEPADS__ = rawGetGamepads;

navigator.getGamepads = function () {
  const raw = rawGetGamepads();
  if (!raw) return [];
  return Array.from(raw, (p) => (p ? normalize(p) : null));
};
Enter fullscreen mode Exit fullscreen mode

Everything downstream, including a rendering engine I'd rather not patch, now sees a pad that claims mapping: "standard" and keeps the promise. The raw function stays reachable for the one caller that needs it: the calibrator, which has to show the user which raw button they just pressed.

Two things to know if you copy this:

  • A Gamepad is a host object with read-only properties. You can't mutate it; you return a plain object with the same shape. Add vibrationActuator: null and hapticActuators: [] so code that feature-tests them doesn't throw.
  • Return the original object when no change is needed. A pad the browser already maps should pass through untouched, including its real vibrationActuator.

Testing without owning every controller

I can't own every controller, and the fix has to work for pads I've never held. The answer is the same trick in reverse: in a Playwright test, replace getGamepads with a function that returns a recorded raw report, press "raw button 1", and compare where the drawing changed against where it changes for a standard pad pressing slot 0.

const got = await litAt(page, [rawIdx]);
expect(near(got, where[slot])).toBe(true);
Enter fullscreen mode Exit fullscreen mode

litAt screenshots the pad idle and held, diffs the pixels, and returns the centre of what changed. It doesn't know what a Cross button looks like. It only knows that two presses meant to be the same button should change the same part of the picture. The GIF at the top of this post comes out of that harness.

The part I haven't solved

Sony is the easy case: one vendor, one known order. The pads that hurt are the third-party ones, where every model has its own order and there is no pattern to detect. That takes a database and a way to tell a stick from a trigger without one, which are the next two posts.

If you'd rather see the result than the code, the PS5 controller overlay page runs this normaliser live: connect a pad, press a button, and the drawing follows.

Top comments (0)