aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 7f5d27c87dd99e391c9be51c7894ecc84ea3c0e3
parent c7b8a5b72e7fab85ab895979f8b27b56785792c8
Author: anon <anon@example.com>
Date:   Thu, 22 Oct 2026 19:59:26 +0000

Restore native keypad controls in the browser client

Diffstat:
Mclient/browser/index.html | 23++++++++++++++++++++++-
Mclient/runtime/linux_window.c | 12+++++++++++-
Mclient/tests/browser_camera.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mclient/tests/browser_controls.mjs | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 158 insertions(+), 9 deletions(-)

diff --git a/client/browser/index.html b/client/browser/index.html @@ -390,7 +390,7 @@ show(); // Queue virtual keys through SDL, including releases before focus changes. function setKey(key, down) { if (!gameReady || heldKeys.has(key) === down) return; - Module._browser_input_key(key, down); + Module._browser_input_key(key, down, false, 0); if (down) heldKeys.add(key); else heldKeys.delete(key); } @@ -791,6 +791,15 @@ show(); updateResolution(); updateChatViewport(); }); + // Preserve physical SDL keypad keys when NumLock changes browser key codes. + const keypadKeys = { + Numpad0: 1073741922, Numpad1: 1073741913, Numpad2: 1073741914, + Numpad3: 1073741915, Numpad4: 1073741916, Numpad5: 1073741917, + Numpad6: 1073741918, Numpad7: 1073741919, Numpad8: 1073741920, + Numpad9: 1073741921, NumpadMultiply: 1073741909, NumpadAdd: 1073741911, + NumpadSubtract: 1073741910, NumpadDecimal: 1073741923, + NumpadDivide: 1073741908, NumpadEnter: 1073741912, + }; for (const type of ['keydown', 'keyup', 'keypress']) { window.addEventListener(type, event => { if (type === 'keydown' && event.ctrlKey && event.altKey && event.code === 'KeyM') { @@ -805,6 +814,18 @@ show(); event.preventDefault(); closeChat(true); } + } else if (gameReady && keypadKeys[event.code]) { + event.preventDefault(); + event.stopImmediatePropagation(); + if (type === 'keypress') return; + const key = keypadKeys[event.code], down = type === 'keydown'; + const modifiers = (event.shiftKey ? 0x0003 : 0) | (event.ctrlKey ? 0x00c0 : 0) | + (event.altKey ? 0x0300 : 0) | (event.metaKey ? 0x0c00 : 0) | + (event.getModifierState('NumLock') ? 0x1000 : 0) | + (event.getModifierState('CapsLock') ? 0x2000 : 0); + Module._browser_input_key(key, down, event.repeat, modifiers); + if (down) heldKeys.add(key); + else heldKeys.delete(key); } }, true); } diff --git a/client/runtime/linux_window.c b/client/runtime/linux_window.c @@ -949,6 +949,13 @@ static uint32_t virtual_key(SDL_Keycode key, unsigned *scan) {SDLK_LEFT,0x25,0x14b}, {SDLK_UP,0x26,0x148}, {SDLK_RIGHT,0x27,0x14d}, {SDLK_DOWN,0x28,0x150}, {SDLK_INSERT,0x2d,0x152}, {SDLK_DELETE,0x2e,0x153}, + {SDLK_KP_0,0x60,82}, {SDLK_KP_1,0x61,79}, {SDLK_KP_2,0x62,80}, + {SDLK_KP_3,0x63,81}, {SDLK_KP_4,0x64,75}, {SDLK_KP_5,0x65,76}, + {SDLK_KP_6,0x66,77}, {SDLK_KP_7,0x67,71}, {SDLK_KP_8,0x68,72}, + {SDLK_KP_9,0x69,73}, {SDLK_KP_MULTIPLY,0x6a,55}, + {SDLK_KP_PLUS,0x6b,78}, {SDLK_KP_MINUS,0x6d,74}, + {SDLK_KP_PERIOD,0x6e,83}, {SDLK_KP_DIVIDE,0x6f,0x135}, + {SDLK_KP_ENTER,13,0x11c}, {SDLK_SEMICOLON,0xba,39}, {SDLK_EQUALS,0xbb,13}, {SDLK_COMMA,0xbc,51}, {SDLK_MINUS,0xbd,12}, {SDLK_PERIOD,0xbe,52}, {SDLK_SLASH,0xbf,53}, {SDLK_BACKQUOTE,0xc0,41}, {SDLK_LEFTBRACKET,0xdb,26}, @@ -961,11 +968,13 @@ static uint32_t virtual_key(SDL_Keycode key, unsigned *scan) #ifdef __EMSCRIPTEN__ /* SDL's queue carries browser controls to the game worker in input order. */ -EMSCRIPTEN_KEEPALIVE void browser_input_key(int key, int down) +EMSCRIPTEN_KEEPALIVE void browser_input_key(int key, int down, int repeat, int modifiers) { SDL_Event event = {.type = down ? SDL_KEYDOWN : SDL_KEYUP}; event.key.keysym.sym = key; + event.key.keysym.mod = modifiers; event.key.state = down ? SDL_PRESSED : SDL_RELEASED; + event.key.repeat = repeat; if (SDL_PushEvent(&event) != 1) abort(); } @@ -1309,6 +1318,7 @@ static void get_keyboard_state(void) keyboard_state[17] = modifiers & KMOD_CTRL ? 0x80 : 0; keyboard_state[18] = modifiers & KMOD_ALT ? 0x80 : 0; keyboard_state[20] = modifiers & KMOD_CAPS ? 1 : 0; + keyboard_state[0x90] = modifiers & KMOD_NUM ? 1 : 0; memcpy((void *)ADDR(output), keyboard_state, sizeof(keyboard_state)); g_eax = 1; g_esp += 8; diff --git a/client/tests/browser_camera.c b/client/tests/browser_camera.c @@ -8,7 +8,7 @@ static SDL_Window *native_window; static uint32_t window; -void browser_input_key(int key, int down); +void browser_input_key(int key, int down, int repeat, int modifiers); void browser_input_character(unsigned character); void browser_input_motion(int x, int y); void browser_input_button(unsigned button, int down, int x, int y); @@ -138,12 +138,12 @@ static void browser_controls_preserve_key_and_text_order(void) { uint32_t output = 0x50003000, keys = 0x50003100; uint32_t peek[] = {output, window, 0x100, 0x102, 1}; - browser_input_key('w', 1); - browser_input_key('w', 0); + browser_input_key('w', 1, 0, 0); + browser_input_key('w', 0, 0, 0); browser_input_character('H'); browser_input_character(0xe9); - browser_input_key(13, 1); - browser_input_key(13, 0); + browser_input_key(13, 1, 0, 0); + browser_input_key(13, 0, 0, 0); assert(call("PeekMessageA", 5, peek)); assert(call("GetKeyboardState", 1, &keys)); @@ -185,6 +185,63 @@ static void touch_drag_preserves_the_press_position(void) assert(!mouse_message()); } +static void keypad_keys_preserve_physical_scans_and_releases(void) +{ + static const struct { SDL_Keycode key; uint32_t code, flags; } keys[] = { + {SDLK_KP_0,0x60,0x00520001}, {SDLK_KP_1,0x61,0x004f0001}, + {SDLK_KP_2,0x62,0x00500001}, {SDLK_KP_3,0x63,0x00510001}, + {SDLK_KP_4,0x64,0x004b0001}, {SDLK_KP_5,0x65,0x004c0001}, + {SDLK_KP_6,0x66,0x004d0001}, {SDLK_KP_7,0x67,0x00470001}, + {SDLK_KP_8,0x68,0x00480001}, {SDLK_KP_9,0x69,0x00490001}, + {SDLK_KP_MULTIPLY,0x6a,0x00370001}, {SDLK_KP_PLUS,0x6b,0x004e0001}, + {SDLK_KP_MINUS,0x6d,0x004a0001}, {SDLK_KP_PERIOD,0x6e,0x00530001}, + {SDLK_KP_DIVIDE,0x6f,0x01350001}, {SDLK_KP_ENTER,13,0x011c0001}, + {SDLK_2,'2',0x00030001}, {SDLK_DOWN,0x28,0x01500001}, + }; + uint32_t output = 0x50003000, state = 0x50003100; + uint32_t peek[] = {output, window, 0x100, 0x101, 1}; + for (unsigned i = 0; i < sizeof(keys) / sizeof(keys[0]); ++i) { + browser_input_key(keys[i].key, 1, 0, 0); + browser_input_key(keys[i].key, 1, 1, 0); + browser_input_key(keys[i].key, 0, 0, 0); + + assert(call("PeekMessageA", 5, peek)); + assert(MEM32(output + 4) == 0x100 && MEM32(output + 8) == keys[i].code); + assert(MEM32(output + 12) == keys[i].flags); + assert(call("GetKeyboardState", 1, &state) && MEM8(state + keys[i].code) == 0x80); + assert(call("PeekMessageA", 5, peek)); + assert(MEM32(output + 4) == 0x100 && MEM32(output + 8) == keys[i].code); + assert(MEM32(output + 12) == (keys[i].flags | 0x40000000)); + assert(call("PeekMessageA", 5, peek)); + assert(MEM32(output + 4) == 0x101 && MEM32(output + 8) == keys[i].code); + assert(MEM32(output + 12) == (keys[i].flags | 0xc0000000)); + assert(call("GetKeyboardState", 1, &state) && !MEM8(state + keys[i].code)); + assert(!call("PeekMessageA", 5, peek)); + } +} + +static void keypad_input_preserves_modifiers_in_queue_order(void) +{ + uint32_t output = 0x50003000, state = 0x50003100; + uint32_t peek[] = {output, window, 0x100, 0x105, 1}; + + browser_input_key(SDLK_KP_2, 1, 0, KMOD_LSHIFT | KMOD_LCTRL | KMOD_LALT | KMOD_CAPS | KMOD_NUM); + browser_input_key(SDLK_KP_2, 0, 0, 0); + + assert(call("PeekMessageA", 5, peek)); + assert(MEM32(output + 4) == 0x104 && MEM32(output + 8) == 0x62); + assert(MEM32(output + 12) == 0x20500001); + assert(call("GetKeyboardState", 1, &state)); + assert(MEM8(state + 16) == 0x80 && MEM8(state + 17) == 0x80 && MEM8(state + 18) == 0x80); + assert(MEM8(state + 20) == 1 && MEM8(state + 0x90) == 1); + assert(call("PeekMessageA", 5, peek)); + assert(MEM32(output + 4) == 0x101 && MEM32(output + 12) == 0xc0500001); + assert(call("GetKeyboardState", 1, &state)); + assert(!MEM8(state + 16) && !MEM8(state + 17) && !MEM8(state + 18)); + assert(!MEM8(state + 20) && !MEM8(state + 0x90)); + assert(!call("PeekMessageA", 5, peek)); +} + static void touch_camera_accumulates_motion_after_recentering(void) { browser_input_motion(320, 240); @@ -251,6 +308,8 @@ int main(void) wheel_rotation_keeps_fractional_motion_and_ends(); focus_loss_releases_camera_button(); browser_controls_preserve_key_and_text_order(); + keypad_keys_preserve_physical_scans_and_releases(); + keypad_input_preserves_modifiers_in_queue_order(); touch_drag_preserves_the_press_position(); touch_camera_accumulates_motion_after_recentering(); touch_camera_release_preserves_recentered_position(); diff --git a/client/tests/browser_controls.mjs b/client/tests/browser_controls.mjs @@ -7,6 +7,8 @@ function page({navigator = {userAgent: 'Mozilla/5.0 (Android) Gecko Firefox/149. const elements = new Map(); const keys = [], characters = [], mouse = [], orbits = [], sizes = [], chatViewports = [], cursorHidden = [], hudInsets = []; const scrolls = [], zooms = []; + const repeats = []; + const modifiers = []; const timers = new Map(); const storage = new Map(saved === null ? [] : [['ao-touch-controls', saved]]); const reloads = []; @@ -72,7 +74,9 @@ function page({navigator = {userAgent: 'Mozilla/5.0 (Android) Gecko Firefox/149. window.visualViewport.dispatchEvent(new Event('resize')); window.visualViewport.dispatchEvent(new Event('scroll')); } - context.Module._browser_input_key = (key, down) => keys.push([key, down]); + context.Module._browser_input_key = (key, down, repeat, mods) => { + keys.push([key, down]); repeats.push(!!repeat); modifiers.push(mods || 0); + }; context.Module._browser_input_character = character => characters.push(character); context.Module._browser_input_motion = (x, y) => mouse.push(['mousemove', x, y, 0]); context.Module._browser_input_button = (button, down, x, y) => mouse.push([down ? 'mousedown' : 'mouseup', x, y, button === 3 ? 2 : 0]); @@ -101,7 +105,7 @@ function page({navigator = {userAgent: 'Mozilla/5.0 (Android) Gecko Firefox/149. document.querySelector(selector).dispatchEvent(event); return event; } - return {document, window, navigator, canvas, audio, keys, characters, mouse, orbits, sizes, chatViewports, cursorHidden, hudInsets, scrolls, zooms, storage, actions, fire, reloads, + return {document, window, navigator, canvas, audio, keys, repeats, modifiers, characters, mouse, orbits, sizes, chatViewports, cursorHidden, hudInsets, scrolls, zooms, storage, actions, fire, reloads, firstFrame: () => context.Module.onFirstFrame(), closed: replaced => context.Module.onGameClosed(replaced), graphics: lost => context.Module.onGraphicsStatus(lost), @@ -111,6 +115,61 @@ function page({navigator = {userAgent: 'Mozilla/5.0 (Android) Gecko Firefox/149. hold: () => { for (const callback of timers.values()) callback(); timers.clear(); }}; } +test('physical keypad keys survive NumLock aliases and keypad Enter remains distinct', () => { + const game = page({navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}}); + const events = [ + {code: 'Numpad2', key: 'ArrowDown', keyCode: 40}, + {code: 'Numpad2', key: '2', keyCode: 98}, + {code: 'NumpadEnter', key: 'Enter', keyCode: 13}, + {code: 'NumpadAdd', key: '+', keyCode: 107}, + ]; + + for (const values of events) { + for (const type of ['keydown', 'keypress', 'keyup']) { + const event = new Event(type, {cancelable: true}); + Object.assign(event, values, {location: type === 'keyup' ? 1 : 3, getModifierState: () => false}); + game.window.dispatchEvent(event); + assert.equal(event.defaultPrevented, true); + } + } + + assert.deepEqual(game.keys, [ + [1073741914, true], [1073741914, false], + [1073741914, true], [1073741914, false], + [1073741912, true], [1073741912, false], + [1073741911, true], [1073741911, false], + ]); + assert.deepEqual(game.characters, []); +}); + +test('held keypad camera input preserves repeat and releases on blur', () => { + const game = page(); + const down = new Event('keydown', {cancelable: true}); + Object.assign(down, {code: 'Numpad8', key: 'ArrowUp', keyCode: 38, repeat: false, getModifierState: () => false}); + const repeat = new Event('keydown', {cancelable: true}); + Object.assign(repeat, {code: 'Numpad8', key: 'ArrowUp', keyCode: 38, repeat: true, getModifierState: () => false}); + + game.window.dispatchEvent(down); + game.window.dispatchEvent(repeat); + game.window.dispatchEvent(new Event('blur')); + + assert.deepEqual(game.keys, [[1073741920, true], [1073741920, true], [1073741920, false]]); + assert.deepEqual(game.repeats, [false, true, false]); +}); + +test('physical keypad input preserves Ctrl Shift Alt and lock modifiers', () => { + const game = page(); + const event = new Event('keydown', {cancelable: true}); + Object.assign(event, {code: 'Numpad2', key: '2', keyCode: 98, + ctrlKey: true, shiftKey: true, altKey: true, + getModifierState: key => key === 'CapsLock' || key === 'NumLock'}); + + game.window.dispatchEvent(event); + + assert.deepEqual(game.keys, [[1073741914, true]]); + assert.deepEqual(game.modifiers, [0x33c3]); +}); + test('dragging chat scrolls history without clicks, camera movement or a held right click', () => { const game = page(); game.chatBounds({x: 13, y: 201, width: 336, height: 14, screenWidth: 844, screenHeight: 390,