aorecomp

Anarchy Online classic in the browser

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


commit b19dc59119ef309581bc0bd1ef0bfc8196e7524d
parent fc4a1a886d94ee5132c8262e0eb8a7e33fec72ee
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 06:16:27 +0000

Implement ToAscii and share tested US keyboard character mapping

Diffstat:
Mdecompiled/runtime/linux_window.c | 65++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Mdecompiled/tests/window.c | 37+++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 15 deletions(-)

diff --git a/decompiled/runtime/linux_window.c b/decompiled/runtime/linux_window.c @@ -1008,6 +1008,51 @@ static void dispatch_message(void) else if (handle) MEM32(g_fs_base + 0x34) = 1400; } +/* The native input adapter currently uses the US keyboard layout. */ +static int key_character(uint32_t key, int shift, int control, int alt, int caps) +{ + if (control && alt) return -1; + if (key >= 'A' && key <= 'Z') + return control ? key - 'A' + 1 : key + (shift != caps ? 0 : 32); + if (control) { + if (shift) { + if (key == '2') return 0; + if (key == '6') return 30; + if (key == 0xbd) return 31; + } else { + if (key == 3 || key == 27 || key == 32) return key; + if (key == 8) return 127; + if (key == 13) return 10; + if (key >= 0xdb && key <= 0xdd) return key - 0xdb + 27; + if (key == 0xe2) return 28; + } + return -1; + } + if (key >= '0' && key <= '9') return shift ? ")!@#$%^&*("[key - '0'] : key; + if (key == 3 || key == 8 || key == 9 || key == 13 || key == 27 || key == 32) return key; + if (key >= 0x60 && key <= 0x69) return shift ? -1 : key - 0x60 + '0'; + if (key == 0x6a) return '*'; + if (key == 0x6b) return '+'; + if (key >= 0x6d && key <= 0x6f) return key - 0x6d + '-'; + const char *plain = ";=,-./`[\\]'\\", *shifted = ":+<_>?~{|}\"|"; + static const uint8_t codes[] = {0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xdb,0xdc,0xdd,0xde,0xe2}; + for (unsigned i = 0; i < sizeof(codes); ++i) + if (key == codes[i]) return (shift ? shifted : plain)[i]; + return -1; +} + +static void to_ascii(void) +{ + uint32_t key = MEM32(g_esp + 4), scan = MEM32(g_esp + 8); + uint32_t state = MEM32(g_esp + 12), output = MEM32(g_esp + 16); + const uint8_t *keys = state ? (const uint8_t *)ADDR(state) : keyboard_state; + int character = scan & 0x8000 ? -1 : key_character(key, !!(keys[16] & 128), + !!(keys[17] & 128), !!(keys[18] & 128), !!(keys[20] & 1)); + g_eax = character >= 0; + if (character >= 0) MEM8(output) = character; + g_esp += 24; +} + static void translate_message(void) { uint32_t record = MEM32(g_esp + 4), message = MEM32(record + 4), key = MEM32(record + 8); @@ -1015,21 +1060,10 @@ static void translate_message(void) g_eax = message == 0x100 || message == 0x101 || message == 0x104 || message == 0x105; if (message != 0x100 && message != 0x104) return; SDL_Keymod modifiers = SDL_GetModState(); - uint32_t character = 0; - if (key >= 'A' && key <= 'Z') { - character = modifiers & KMOD_CTRL ? key - 'A' + 1 : - key + (!!(modifiers & KMOD_SHIFT) != !!(modifiers & KMOD_CAPS) ? 0 : 32); - } else if (key >= '0' && key <= '9') { - character = modifiers & KMOD_SHIFT ? ")!@#$%^&*("[key - '0'] : key; - } else if (key == 8 || key == 9 || key == 13 || key == 32) character = key; - else { - const char *plain = ";=,-./`[\\]'", *shifted = ":+<_>?~{|}\""; - static const uint8_t codes[] = {0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xdb,0xdc,0xdd,0xde}; - for (unsigned i = 0; i < sizeof(codes); ++i) - if (key == codes[i]) character = (modifiers & KMOD_SHIFT ? shifted : plain)[i]; - } - if (character) queue_message(MEM32(record), message == 0x104 ? 0x106 : 0x102, - character, MEM32(record + 12), 0, 0); + int character = key_character(key, !!(modifiers & KMOD_SHIFT), !!(modifiers & KMOD_CTRL), + !!(modifiers & KMOD_ALT), !!(modifiers & KMOD_CAPS)); + if (character >= 0) queue_message(MEM32(record), message == 0x104 ? 0x106 : 0x102, + character, MEM32(record + 12), 0, 0); } static void get_keyboard_state(void) @@ -1067,6 +1101,7 @@ recomp_func_t linux_window_lookup(const char *dll, const char *name) if (!strcmp(name, "PostQuitMessage")) return post_quit_message; if (!strcmp(name, "TranslateMessage")) return translate_message; if (!strcmp(name, "GetKeyboardState")) return get_keyboard_state; + if (!strcmp(name, "ToAscii")) return to_ascii; if (!strcmp(name, "LoadIconA")) return load_icon; if (!strcmp(name, "RegisterClassA")) return register_class_a; if (!strcmp(name, "RegisterClassW")) return register_class_w; diff --git a/decompiled/tests/window.c b/decompiled/tests/window.c @@ -786,6 +786,42 @@ static void sdl_keyboard_and_mouse_reach_guest_messages(void) } #endif +static void ascii_translation_uses_supplied_keyboard_state(void) +{ +#ifdef _WIN32 + HKL previous = ActivateKeyboardLayout(LoadKeyboardLayoutA("00000409", 0), 0); + assert(previous); +#endif + uint32_t digest = 2166136261u; + const uint32_t scans[] = {0, 0x8000, 0x80000000}; + for (unsigned modifier = 0; modifier < 16; ++modifier) + for (unsigned scan = 0; scan < 3; ++scan) + for (unsigned menu = 0; menu < 2; ++menu) + for (unsigned key = 0; key < 0xe7; ++key) { + unsigned char *state = (void *)memory(workspace()); + memset(state, 0, 256); + state[16] = modifier & 1 ? 128 : 0; + state[17] = modifier & 2 ? 128 : 0; + state[18] = modifier & 4 ? 128 : 0; + state[20] = modifier & 8 ? 1 : 0; + *memory(workspace() + 256) = 0xa5a5a5a5; + uint32_t arguments[] = {key, scans[scan], workspace(), workspace() + 257, menu}; + + uint32_t count = call("ToAscii", 5, arguments); + + uint32_t output = *memory(workspace() + 256); + assert(count <= 1 && (output & 0xffff00ff) == 0xa5a500a5); + assert(state[16] == (modifier & 1 ? 128 : 0)); + digest = (digest ^ count) * 16777619u; + digest = (digest ^ output) * 16777619u; + } +#ifdef _WIN32 + ActivateKeyboardLayout(previous, 0); +#endif + assert(digest == 0x8d4cfc65); + printf("ToAscii: 22176 US key/modifier/scan/menu combinations, digest %08x, canaries and ABI passed\n", digest); +} + int main(void) { game_data_Clientr_exe(); @@ -823,6 +859,7 @@ int main(void) class_release_invalidates_its_atom(); } wide = 0; + ascii_translation_uses_supplied_keyboard_state(); desktop_context_reports_native_pixel_storage(); cursor_query_writes_a_guest_point(); modifier_queries_combine_left_and_right_keys();