Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(native): the desktop host drives mouse, wheel, keyboard and gamepads

esenginee5d12890a03c
diff --git a/native/host/platform/desktop.cpp b/native/host/platform/desktop.cpp
index 1b8358670..6e9ab2f9c 100644
--- a/native/host/platform/desktop.cpp
+++ b/native/host/platform/desktop.cpp
@@ -32,6 +32,8 @@
#include "Host.hpp"
+#include "platform/desktop_keymap.hpp"
+
#if defined(__APPLE__)
#include "platform/apple_common.hpp"
#endif
@@ -39,6 +41,11 @@
using esengine::u32;
using esengine::u8;
using esengine::WebGPUDevice;
+using eshost::kGamepadButtonToStandard;
+using eshost::kScancodeNames;
+using eshost::kStandardLeftTrigger;
+using eshost::kStandardRightTrigger;
+using eshost::ScancodeName;
namespace {
@@ -47,6 +54,31 @@ namespace {
constexpr int kDefaultWidth = 1280;
constexpr int kDefaultHeight = 720;
+/** Pixels per wheel notch. The same number the web adapter multiplies a
+ * line-mode wheel event by, so a scroll moves a list equally far in both. */
+constexpr float kWheelLineHeight = 16.0f;
+
+/** SDL numbers mouse buttons from 1, the DOM from 0, and they disagree about the
+ * middle and right order — hence a mapping rather than a subtraction. */
+int domButton(Uint8 sdlButton) {
+ switch (sdlButton) {
+ case SDL_BUTTON_LEFT: return 0;
+ case SDL_BUTTON_MIDDLE: return 1;
+ case SDL_BUTTON_RIGHT: return 2;
+ case SDL_BUTTON_X1: return 3;
+ case SDL_BUTTON_X2: return 4;
+ default: return 0;
+ }
+}
+
+/** The DOM `code` for a physical key, or null for one the tables do not name. */
+const char* domCode(SDL_Scancode scancode) {
+ for (const ScancodeName& entry : kScancodeNames) {
+ if (entry.scancode == scancode) return entry.code;
+ }
+ return nullptr;
+}
+
/** Read a whole file, or an empty vector. */
std::vector<u8> readFileBytes(const std::string& path) {
SDL_IOStream* io = SDL_IOFromFile(path.c_str(), "rb");
@@ -146,6 +178,41 @@ struct DesktopPlatform final : eshost::Platform {
"[Estella] %s", message);
}
+ // SDL only surfaces a device as a gamepad once it has a mapping for it, so
+ // every pad here really is in the standard layout the SDK assumes.
+ void pollGamepads(std::vector<eshost::GamepadState>& out) override {
+ out.clear();
+ int count = 0;
+ SDL_JoystickID* ids = SDL_GetGamepads(&count);
+ if (!ids) return;
+ for (int i = 0; i < count; ++i) {
+ SDL_Gamepad* pad = SDL_GetGamepadFromID(ids[i]);
+ if (!pad) continue;
+ eshost::GamepadState state;
+ state.index = i;
+ state.connected = true;
+ for (int b = 0; b < (int)(sizeof(kGamepadButtonToStandard) / sizeof(int)); ++b) {
+ if (SDL_GetGamepadButton(pad, (SDL_GamepadButton)b)) {
+ state.buttons[kGamepadButtonToStandard[b]] = 1.0f;
+ }
+ }
+ // Triggers are axes to SDL and analog BUTTONS to the standard layout,
+ // in [0,32767] rather than the sticks' signed range.
+ state.buttons[kStandardLeftTrigger] =
+ SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) / 32767.0f;
+ state.buttons[kStandardRightTrigger] =
+ SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) / 32767.0f;
+ for (int a = 0; a < 4; ++a) {
+ const float raw = SDL_GetGamepadAxis(pad, (SDL_GamepadAxis)a);
+ // 32768 negative vs 32767 positive: dividing by the smaller
+ // magnitude would let a stick read past -1.
+ state.axes[a] = raw < 0 ? raw / 32768.0f : raw / 32767.0f;
+ }
+ out.push_back(state);
+ }
+ SDL_free(ids);
+ }
+
#if defined(__APPLE__)
eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {
return eshost::appleLoadFont(family, codepoint, style);
@@ -242,7 +309,9 @@ int main(int argc, char** argv) {
}
bool running = true;
- bool pointerDown = false;
+ // SDL reports mouse positions in window coordinates; the host contract is
+ // surface pixels, as it is for touch.
+ auto pixelScale = [&] { return SDL_GetWindowPixelDensity(window); };
auto handleEvent = [&](const SDL_Event& e) {
switch (e.type) {
@@ -262,29 +331,45 @@ int main(int argc, char** argv) {
eshost::setVisible(true);
eshost::bindSurface();
break;
- // Mouse as the primary pointer, so a touch-built game is playable.
- // The real desktop surface (other buttons, wheel, keyboard, gamepads)
- // is S2 — Host.hpp takes only touch today.
case SDL_EVENT_MOUSE_BUTTON_DOWN:
- if (e.button.button == SDL_BUTTON_LEFT) {
- pointerDown = true;
- const float s = SDL_GetWindowPixelDensity(window);
- eshost::touch(0, 0, e.button.x * s, e.button.y * s);
- }
+ eshost::pointer(0, domButton(e.button.button),
+ e.button.x * pixelScale(), e.button.y * pixelScale());
break;
case SDL_EVENT_MOUSE_MOTION:
- if (pointerDown) {
- const float s = SDL_GetWindowPixelDensity(window);
- eshost::touch(1, 0, e.motion.x * s, e.motion.y * s);
- }
+ // Unconditionally, as the web does: hover is an event there and a
+ // game that only heard about motion while a button was held could
+ // not implement one.
+ eshost::pointer(1, 0, e.motion.x * pixelScale(), e.motion.y * pixelScale());
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
- if (e.button.button == SDL_BUTTON_LEFT && pointerDown) {
- pointerDown = false;
- const float s = SDL_GetWindowPixelDensity(window);
- eshost::touch(2, 0, e.button.x * s, e.button.y * s);
+ eshost::pointer(2, domButton(e.button.button),
+ e.button.x * pixelScale(), e.button.y * pixelScale());
+ break;
+ case SDL_EVENT_MOUSE_WHEEL: {
+ // SDL counts notches, positive AWAY from the user; the DOM counts
+ // pixels, positive TOWARD the content's end. So the vertical sign
+ // flips and both scale by the same line height the web adapter uses.
+ const float flip = e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1.0f : 1.0f;
+ eshost::wheel(e.wheel.x * flip * kWheelLineHeight,
+ -e.wheel.y * flip * kWheelLineHeight);
+ break;
+ }
+ case SDL_EVENT_KEY_DOWN:
+ case SDL_EVENT_KEY_UP:
+ // Repeats included: the DOM fires them too, and a game that reads
+ // the key's state is unaffected either way.
+ if (const char* code = domCode(e.key.scancode)) {
+ eshost::key(e.type == SDL_EVENT_KEY_DOWN, code);
}
break;
+ // A pad appears as a joystick first; opening it is what makes SDL
+ // report it as a gamepad, and pollGamepads only sees opened ones.
+ case SDL_EVENT_GAMEPAD_ADDED:
+ SDL_OpenGamepad(e.gdevice.which);
+ break;
+ case SDL_EVENT_GAMEPAD_REMOVED:
+ if (SDL_Gamepad* pad = SDL_GetGamepadFromID(e.gdevice.which)) SDL_CloseGamepad(pad);
+ break;
default:
break;
}
diff --git a/native/host/platform/desktop_keymap.hpp b/native/host/platform/desktop_keymap.hpp
new file mode 100644
index 000000000..3a63d7ba2
--- /dev/null
+++ b/native/host/platform/desktop_keymap.hpp
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file desktop_keymap.hpp
+ * @brief SDL scancode → DOM `KeyboardEvent.code`, and SDL gamepad → the W3C
+ * standard mapping. The two tables that decide whether a game's input
+ * means the same thing here as it does in a browser.
+ * @details Both are the silent-failure kind: a wrong entry does not error, the
+ * key or button simply never arrives — so `tools/check-key-codes.mjs`
+ * checks this file against the W3C list rather than trusting it.
+ *
+ * SDL scancodes ARE USB HID usage-page-0x07 numbers (SDL_scancode.h says
+ * so), and the W3C `code` table is defined over those same numbers, so
+ * the mapping below is a transcription with an authority, not a guess.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the Apache License, Version 2.0.
+ */
+#pragma once
+
+#include <SDL3/SDL.h>
+
+namespace eshost {
+
+struct ScancodeName {
+ SDL_Scancode scancode;
+ const char* code;
+};
+
+/** Every key a game binds. Media and browser keys are left out deliberately: a
+ * key with no DOM `code` would have to be invented one, and an invented name is
+ * a name no input map is written against. */
+constexpr ScancodeName kScancodeNames[] = {
+ {SDL_SCANCODE_A, "KeyA"}, {SDL_SCANCODE_B, "KeyB"}, {SDL_SCANCODE_C, "KeyC"},
+ {SDL_SCANCODE_D, "KeyD"}, {SDL_SCANCODE_E, "KeyE"}, {SDL_SCANCODE_F, "KeyF"},
+ {SDL_SCANCODE_G, "KeyG"}, {SDL_SCANCODE_H, "KeyH"}, {SDL_SCANCODE_I, "KeyI"},
+ {SDL_SCANCODE_J, "KeyJ"}, {SDL_SCANCODE_K, "KeyK"}, {SDL_SCANCODE_L, "KeyL"},
+ {SDL_SCANCODE_M, "KeyM"}, {SDL_SCANCODE_N, "KeyN"}, {SDL_SCANCODE_O, "KeyO"},
+ {SDL_SCANCODE_P, "KeyP"}, {SDL_SCANCODE_Q, "KeyQ"}, {SDL_SCANCODE_R, "KeyR"},
+ {SDL_SCANCODE_S, "KeyS"}, {SDL_SCANCODE_T, "KeyT"}, {SDL_SCANCODE_U, "KeyU"},
+ {SDL_SCANCODE_V, "KeyV"}, {SDL_SCANCODE_W, "KeyW"}, {SDL_SCANCODE_X, "KeyX"},
+ {SDL_SCANCODE_Y, "KeyY"}, {SDL_SCANCODE_Z, "KeyZ"},
+
+ {SDL_SCANCODE_1, "Digit1"}, {SDL_SCANCODE_2, "Digit2"}, {SDL_SCANCODE_3, "Digit3"},
+ {SDL_SCANCODE_4, "Digit4"}, {SDL_SCANCODE_5, "Digit5"}, {SDL_SCANCODE_6, "Digit6"},
+ {SDL_SCANCODE_7, "Digit7"}, {SDL_SCANCODE_8, "Digit8"}, {SDL_SCANCODE_9, "Digit9"},
+ {SDL_SCANCODE_0, "Digit0"},
+
+ {SDL_SCANCODE_RETURN, "Enter"}, {SDL_SCANCODE_ESCAPE, "Escape"},
+ {SDL_SCANCODE_BACKSPACE, "Backspace"}, {SDL_SCANCODE_TAB, "Tab"},
+ {SDL_SCANCODE_SPACE, "Space"}, {SDL_SCANCODE_CAPSLOCK, "CapsLock"},
+
+ {SDL_SCANCODE_MINUS, "Minus"}, {SDL_SCANCODE_EQUALS, "Equal"},
+ {SDL_SCANCODE_LEFTBRACKET, "BracketLeft"}, {SDL_SCANCODE_RIGHTBRACKET, "BracketRight"},
+ {SDL_SCANCODE_BACKSLASH, "Backslash"}, {SDL_SCANCODE_SEMICOLON, "Semicolon"},
+ {SDL_SCANCODE_APOSTROPHE, "Quote"}, {SDL_SCANCODE_GRAVE, "Backquote"},
+ {SDL_SCANCODE_COMMA, "Comma"}, {SDL_SCANCODE_PERIOD, "Period"},
+ {SDL_SCANCODE_SLASH, "Slash"}, {SDL_SCANCODE_NONUSBACKSLASH, "IntlBackslash"},
+ {SDL_SCANCODE_INTERNATIONAL1, "IntlRo"}, {SDL_SCANCODE_INTERNATIONAL3, "IntlYen"},
+
+ {SDL_SCANCODE_F1, "F1"}, {SDL_SCANCODE_F2, "F2"}, {SDL_SCANCODE_F3, "F3"},
+ {SDL_SCANCODE_F4, "F4"}, {SDL_SCANCODE_F5, "F5"}, {SDL_SCANCODE_F6, "F6"},
+ {SDL_SCANCODE_F7, "F7"}, {SDL_SCANCODE_F8, "F8"}, {SDL_SCANCODE_F9, "F9"},
+ {SDL_SCANCODE_F10, "F10"}, {SDL_SCANCODE_F11, "F11"}, {SDL_SCANCODE_F12, "F12"},
+ {SDL_SCANCODE_F13, "F13"}, {SDL_SCANCODE_F14, "F14"}, {SDL_SCANCODE_F15, "F15"},
+ {SDL_SCANCODE_F16, "F16"}, {SDL_SCANCODE_F17, "F17"}, {SDL_SCANCODE_F18, "F18"},
+ {SDL_SCANCODE_F19, "F19"}, {SDL_SCANCODE_F20, "F20"}, {SDL_SCANCODE_F21, "F21"},
+ {SDL_SCANCODE_F22, "F22"}, {SDL_SCANCODE_F23, "F23"}, {SDL_SCANCODE_F24, "F24"},
+
+ {SDL_SCANCODE_PRINTSCREEN, "PrintScreen"}, {SDL_SCANCODE_SCROLLLOCK, "ScrollLock"},
+ {SDL_SCANCODE_PAUSE, "Pause"}, {SDL_SCANCODE_INSERT, "Insert"},
+ {SDL_SCANCODE_HOME, "Home"}, {SDL_SCANCODE_PAGEUP, "PageUp"},
+ {SDL_SCANCODE_DELETE, "Delete"}, {SDL_SCANCODE_END, "End"},
+ {SDL_SCANCODE_PAGEDOWN, "PageDown"},
+
+ {SDL_SCANCODE_RIGHT, "ArrowRight"}, {SDL_SCANCODE_LEFT, "ArrowLeft"},
+ {SDL_SCANCODE_DOWN, "ArrowDown"}, {SDL_SCANCODE_UP, "ArrowUp"},
+
+ {SDL_SCANCODE_NUMLOCKCLEAR, "NumLock"}, {SDL_SCANCODE_KP_DIVIDE, "NumpadDivide"},
+ {SDL_SCANCODE_KP_MULTIPLY, "NumpadMultiply"}, {SDL_SCANCODE_KP_MINUS, "NumpadSubtract"},
+ {SDL_SCANCODE_KP_PLUS, "NumpadAdd"}, {SDL_SCANCODE_KP_ENTER, "NumpadEnter"},
+ {SDL_SCANCODE_KP_1, "Numpad1"}, {SDL_SCANCODE_KP_2, "Numpad2"},
+ {SDL_SCANCODE_KP_3, "Numpad3"}, {SDL_SCANCODE_KP_4, "Numpad4"},
+ {SDL_SCANCODE_KP_5, "Numpad5"}, {SDL_SCANCODE_KP_6, "Numpad6"},
+ {SDL_SCANCODE_KP_7, "Numpad7"}, {SDL_SCANCODE_KP_8, "Numpad8"},
+ {SDL_SCANCODE_KP_9, "Numpad9"}, {SDL_SCANCODE_KP_0, "Numpad0"},
+ {SDL_SCANCODE_KP_PERIOD, "NumpadDecimal"}, {SDL_SCANCODE_KP_EQUALS, "NumpadEqual"},
+
+ {SDL_SCANCODE_APPLICATION, "ContextMenu"},
+ {SDL_SCANCODE_LCTRL, "ControlLeft"}, {SDL_SCANCODE_LSHIFT, "ShiftLeft"},
+ {SDL_SCANCODE_LALT, "AltLeft"}, {SDL_SCANCODE_LGUI, "MetaLeft"},
+ {SDL_SCANCODE_RCTRL, "ControlRight"}, {SDL_SCANCODE_RSHIFT, "ShiftRight"},
+ {SDL_SCANCODE_RALT, "AltRight"}, {SDL_SCANCODE_RGUI, "MetaRight"},
+};
+
+/**
+ * SDL's gamepad button order is NOT the standard mapping's — SDL counts
+ * back/guide/start before the shoulders, the W3C layout counts the shoulders and
+ * triggers first — so the two are related by this table and by nothing else.
+ * Index into it with an SDL button; the value is the SDK's GamepadButton.
+ */
+constexpr int kGamepadButtonToStandard[] = {
+ 0, // SOUTH
+ 1, // EAST
+ 2, // WEST
+ 3, // NORTH
+ 8, // BACK
+ 16, // GUIDE
+ 9, // START
+ 10, // LEFT_STICK
+ 11, // RIGHT_STICK
+ 4, // LEFT_SHOULDER
+ 5, // RIGHT_SHOULDER
+ 12, // DPAD_UP
+ 13, // DPAD_DOWN
+ 14, // DPAD_LEFT
+ 15, // DPAD_RIGHT
+};
+
+/** The standard layout's analog triggers, which SDL reports as axes. */
+constexpr int kStandardLeftTrigger = 6;
+constexpr int kStandardRightTrigger = 7;
+
+} // namespace eshost
diff --git a/package.json b/package.json
index 4e921d45b..99a4fcc5d 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,7 @@
"emsdk:setup": "node build-tools/toolchain/setup-emsdk.js",
"asset-meta": "node tools/asset-meta.js",
"migrate-asset-refs": "node tools/migrate-asset-refs.js",
- "verify": "pnpm --filter ./sdk exec tsc --noEmit && pnpm --filter @estella/editor exec tsc --noEmit && node tools/api-surface.mjs --check && node tools/check-cycles.mjs && node tools/check-layers.mjs && node tools/check-project-settings.mjs && node tools/check-workflows.mjs && node tools/check-tool-calls.mjs && node tools/check-inspector-door.mjs && node tools/check-dirty-source.mjs && node tools/check-path-sandbox.mjs && node tools/check-comment-style.mjs && node tools/check-release-metadata.mjs && node tools/component-reference.mjs --check",
+ "verify": "pnpm --filter ./sdk exec tsc --noEmit && pnpm --filter @estella/editor exec tsc --noEmit && node tools/api-surface.mjs --check && node tools/check-cycles.mjs && node tools/check-layers.mjs && node tools/check-project-settings.mjs && node tools/check-workflows.mjs && node tools/check-tool-calls.mjs && node tools/check-inspector-door.mjs && node tools/check-dirty-source.mjs && node tools/check-path-sandbox.mjs && node tools/check-key-codes.mjs && node tools/check-comment-style.mjs && node tools/check-release-metadata.mjs && node tools/component-reference.mjs --check",
"perf": "node tools/perf-guard.mjs --check",
"scale": "node tools/perf-budget.mjs",
"stress-project": "node tools/stress-project.mjs",
diff --git a/tools/check-key-codes.mjs b/tools/check-key-codes.mjs
new file mode 100644
index 000000000..abc199782
--- /dev/null
+++ b/tools/check-key-codes.mjs
@@ -0,0 +1,118 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file check-key-codes.mjs — the desktop host's input tables, checked against
+ * the specs rather than against themselves.
+ *
+ * Both tables in `native/host/platform/desktop_keymap.hpp` fail SILENTLY: a
+ * misnamed key or a wrong gamepad index does not error, the input simply never
+ * arrives, and a game's WASD works everywhere but one platform. That is the same
+ * failure mode ANDROID_ATTR_IDS has, and it gets the same treatment — an
+ * independent list here, written from the spec, not derived from the header.
+ *
+ * The key names are W3C UI Events `code` values; the gamepad indices are the
+ * "standard mapping" the SDK's GamepadButton enum spells out.
+ */
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const source = readFileSync(path.join(root, 'native/host/platform/desktop_keymap.hpp'), 'utf8');
+
+/**
+ * Every `code` the W3C UI Events spec defines for a key on the USB HID keyboard
+ * page — written out here so this file is a SECOND source. Media, browser and
+ * power keys are omitted: the header deliberately does not map them.
+ */
+const W3C_CODES = new Set([
+ ...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('').map((c) => `Key${c}`),
+ ...'0123456789'.split('').map((d) => `Digit${d}`),
+ 'Enter', 'Escape', 'Backspace', 'Tab', 'Space', 'CapsLock',
+ 'Minus', 'Equal', 'BracketLeft', 'BracketRight', 'Backslash', 'Semicolon',
+ 'Quote', 'Backquote', 'Comma', 'Period', 'Slash', 'IntlBackslash', 'IntlRo', 'IntlYen',
+ ...Array.from({ length: 24 }, (_, i) => `F${i + 1}`),
+ 'PrintScreen', 'ScrollLock', 'Pause', 'Insert', 'Home', 'PageUp',
+ 'Delete', 'End', 'PageDown',
+ 'ArrowRight', 'ArrowLeft', 'ArrowDown', 'ArrowUp',
+ 'NumLock', 'NumpadDivide', 'NumpadMultiply', 'NumpadSubtract', 'NumpadAdd',
+ 'NumpadEnter', 'NumpadDecimal', 'NumpadEqual',
+ ...Array.from({ length: 10 }, (_, i) => `Numpad${i}`),
+ 'ContextMenu',
+ 'ControlLeft', 'ShiftLeft', 'AltLeft', 'MetaLeft',
+ 'ControlRight', 'ShiftRight', 'AltRight', 'MetaRight',
+]);
+
+/** SDL's button order (the keys, in order) against the standard mapping's index
+ * (the values). Written from the two specs, NOT copied from the header. */
+const EXPECTED_PAD = {
+ SOUTH: 0, EAST: 1, WEST: 2, NORTH: 3,
+ BACK: 8, GUIDE: 16, START: 9,
+ LEFT_STICK: 10, RIGHT_STICK: 11,
+ LEFT_SHOULDER: 4, RIGHT_SHOULDER: 5,
+ DPAD_UP: 12, DPAD_DOWN: 13, DPAD_LEFT: 14, DPAD_RIGHT: 15,
+};
+const EXPECTED_PAD_ORDER = Object.values(EXPECTED_PAD);
+const SDL_BUTTON_NAMES = Object.keys(EXPECTED_PAD);
+
+const problems = [];
+
+// -- keys ---------------------------------------------------------------------
+const entries = [...source.matchAll(/\{\s*(SDL_SCANCODE_[A-Z0-9_]+)\s*,\s*"([^"]+)"\s*\}/g)]
+ .map(([, scancode, code]) => ({ scancode, code }));
+if (entries.length < 100) {
+ problems.push(`only ${entries.length} key entries parsed — the table's shape changed`);
+}
+const seenScancode = new Map();
+const seenCode = new Map();
+for (const { scancode, code } of entries) {
+ if (!W3C_CODES.has(code)) problems.push(`"${code}" (${scancode}) is not a W3C code`);
+ if (seenScancode.has(scancode)) problems.push(`${scancode} mapped twice`);
+ if (seenCode.has(code)) problems.push(`"${code}" produced by ${seenCode.get(code)} and ${scancode}`);
+ seenScancode.set(scancode, code);
+ seenCode.set(code, scancode);
+}
+// Anchors: a table that parsed and is internally consistent can still be shifted
+// by one, which these catch and nothing else does.
+for (const [scancode, code] of [
+ ['SDL_SCANCODE_A', 'KeyA'], ['SDL_SCANCODE_Z', 'KeyZ'], ['SDL_SCANCODE_1', 'Digit1'],
+ ['SDL_SCANCODE_SPACE', 'Space'], ['SDL_SCANCODE_LEFT', 'ArrowLeft'],
+ ['SDL_SCANCODE_LSHIFT', 'ShiftLeft'], ['SDL_SCANCODE_KP_0', 'Numpad0'],
+]) {
+ const got = seenScancode.get(scancode);
+ if (got !== code) problems.push(`${scancode} should be "${code}", is ${got ? `"${got}"` : 'absent'}`);
+}
+
+// -- gamepad ------------------------------------------------------------------
+const padBlock = source.match(/kGamepadButtonToStandard\[\]\s*=\s*\{([\s\S]*?)\};/);
+if (!padBlock) {
+ problems.push('kGamepadButtonToStandard not found');
+} else {
+ const got = [...padBlock[1].matchAll(/^\s*(\d+),/gm)].map(([, n]) => Number(n));
+ if (got.length !== EXPECTED_PAD_ORDER.length) {
+ problems.push(`gamepad table has ${got.length} entries, expected ${EXPECTED_PAD_ORDER.length}`);
+ }
+ got.forEach((value, i) => {
+ if (value !== EXPECTED_PAD_ORDER[i]) {
+ problems.push(`${SDL_BUTTON_NAMES[i]} maps to ${value}, `
+ + `the standard layout says ${EXPECTED_PAD_ORDER[i]}`);
+ }
+ });
+ // Every standard button reachable exactly once. The triggers are the two the
+ // table cannot carry — SDL reports them as axes — so they are added here; if
+ // this fails, some button is unreachable and its game action never fires.
+ const covered = [...got, 6, 7].sort((a, b) => a - b);
+ const complete = Array.from({ length: 17 }, (_, i) => i);
+ if (JSON.stringify(covered) !== JSON.stringify(complete)) {
+ problems.push(`the standard buttons are not covered exactly once: ${covered.join(',')}`);
+ }
+}
+
+if (problems.length) {
+ console.log('check-key-codes: the desktop input tables disagree with the specs.\n');
+ for (const p of problems) console.log(` ${p}`);
+ console.log('\nSee native/host/platform/desktop_keymap.hpp.');
+ process.exit(1);
+}
+console.log(`check-key-codes: ${entries.length} keys and the gamepad layout match the specs.`);