aorecomp

Anarchy Online classic in the browser

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


client/runtime/linux_window.c (60091B)


      1 #include "linux_window.h"
      2 #include "linux_graphics.h"
      3 #include "guest_heap.h"
      4 #include <SDL2/SDL.h>
      5 #include <strings.h>
      6 #include <pthread.h>
      7 #ifdef __EMSCRIPTEN__
      8 #include <assert.h>
      9 #include <emscripten.h>
     10 #include <emscripten/html5.h>
     11 #include <emscripten/proxying.h>
     12 static pthread_t input_thread;
     13 void ao_test_input(int stage, int type, int a, int b, int c, int d);
     14 #endif
     15 
     16 /* Guest classes and handles contain no native pointers. SDL owns decoration. */
     17 static struct window_class {
     18     char *name;
     19     uint32_t atom, instance, procedure, icon, background;
     20     struct window_class *next;
     21 } *classes;
     22 static struct {
     23     struct window_class *class;
     24     uint32_t handle, focus, style, capture;
     25     int active, shown;
     26     int width, height;
     27 } client;
     28 static uint32_t next_atom = 0xc000, next_handle = 0xf4000000u;
     29 static struct display_context {
     30     uint32_t handle;
     31     struct display_context *next;
     32 } *display_contexts;
     33 static uint32_t next_context = 0xf4300000u;
     34 static struct window_icon {
     35     uint32_t instance, name, handle;
     36     SDL_Surface *pixels;
     37     struct window_icon *next;
     38 } *icons;
     39 static struct queued_message {
     40     struct queued_message *next;
     41     uint32_t words[7];
     42     int keyboard;
     43     SDL_Keymod modifiers;
     44 } *message_head, *message_tail;
     45 static pthread_mutex_t message_mutex = PTHREAD_MUTEX_INITIALIZER;
     46 static int quit_pending;
     47 static uint32_t quit_code;
     48 static uint8_t keyboard_state[256];
     49 static uint32_t queued_mouse_buttons;
     50 static SDL_Keymod queued_modifiers, keyboard_modifiers;
     51 #ifdef __EMSCRIPTEN__
     52 /* Browsers cannot warp the physical pointer; camera recentering is virtual. */
     53 static int cursor_x, cursor_y, cursor_warped;
     54 static int browser_mouse, browser_mouse_x, browser_mouse_y;
     55 static SDL_Cursor *browser_cursor;
     56 int browser_cursor_hidden(void);
     57 
     58 void browser_cursor_update(uint32_t owner)
     59 {
     60     /* ccr.dll:sub_27016E80 supplies the artwork and unscaled click hotspot. */
     61     uint32_t entry = 0x270485e0 + MEM32(owner + 8) * 20;
     62     uint32_t sprite = MEM32(entry + 16), image = MEM32(sprite + 0x28);
     63     /* bbr.dll:sub_250AF2C0 selects format 8 (ARGB1555) for cursor sprites. */
     64     assert(MEM32(image + 0x28) == 8 && MEM32(image + 0x24) == 2);
     65     /* The SDL window exists before CCR initializes its screen-size globals. */
     66     int window_width, window_height;
     67     SDL_GetWindowSize(linux_graphics_window(), &window_width, &window_height);
     68     uint32_t state[] = {(uint32_t)(uintptr_t)ADDR(MEM32(image)),
     69         MEM32(image + 0xc), MEM32(image + 8), MEM32(image + 0x20),
     70         MEM32(sprite + 0x3c), MEM32(sprite + 0x40), MEM32(entry), MEM32(entry + 4),
     71         window_width, window_height, MEM8(owner + 0x10) && !browser_cursor_hidden()};
     72     static uint32_t previous[11];
     73     if (browser_cursor && !memcmp(previous, state, sizeof(state))) return;
     74 
     75     /* SDL owns the custom cursor so mouse-focus changes restore it as well. */
     76     if (!browser_cursor || memcmp(previous, state, 10 * sizeof(uint32_t))) {
     77         double width, height;
     78         if (emscripten_get_element_css_size("#canvas", &width, &height) != EMSCRIPTEN_RESULT_SUCCESS) {
     79             fprintf(stderr, "Cannot read browser cursor surface size\n");
     80             abort();
     81         }
     82         double scale_x = width / state[8], scale_y = height / state[9];
     83         SDL_Surface *source = SDL_CreateRGBSurfaceWithFormatFrom((void *)ADDR(MEM32(image)),
     84             state[1], state[2], 16, state[3], SDL_PIXELFORMAT_ARGB1555);
     85         SDL_Surface *scaled = SDL_CreateRGBSurfaceWithFormat(0,
     86             lround(MEMF(sprite + 0x3c) * scale_x), lround(MEMF(sprite + 0x40) * scale_y),
     87             32, SDL_PIXELFORMAT_ARGB8888);
     88         if (!source || !scaled) { fprintf(stderr, "%s\n", SDL_GetError()); abort(); }
     89         if (SDL_SetSurfaceBlendMode(source, SDL_BLENDMODE_NONE) || SDL_BlitScaled(source, NULL, scaled, NULL)) {
     90             fprintf(stderr, "%s\n", SDL_GetError());
     91             abort();
     92         }
     93         SDL_Cursor *cursor = SDL_CreateColorCursor(scaled, lround(state[6] * scale_x), lround(state[7] * scale_y));
     94         SDL_FreeSurface(scaled);
     95         SDL_FreeSurface(source);
     96         if (!cursor) { fprintf(stderr, "%s\n", SDL_GetError()); abort(); }
     97         SDL_SetCursor(cursor);
     98         SDL_FreeCursor(browser_cursor);
     99         browser_cursor = cursor;
    100     }
    101     if (SDL_ShowCursor(state[10] ? SDL_ENABLE : SDL_DISABLE) < 0) abort();
    102     memcpy(previous, state, sizeof(state));
    103 }
    104 #endif
    105 /* The SDL default cursor has a guest handle; native pointers never enter guest state. */
    106 static uint32_t current_cursor = 0xf4500000u;
    107 static void destroy_window(void);
    108 
    109 struct SDL_Window *linux_window_native(uint32_t handle)
    110 {
    111     return handle && handle == client.handle ? linux_graphics_window() : NULL;
    112 }
    113 
    114 static void unsupported(const char *operation, uint32_t value)
    115 {
    116     fprintf(stderr, "Unimplemented native window %s: %08x caller=%08x\n",
    117             operation, value, g_cur_func);
    118     exit(3);
    119 }
    120 
    121 static char *window_text(uint32_t address, int wide)
    122 {
    123     size_t length = 0;
    124     if (address) {
    125         while (wide ? MEM16(address + length * 2) : MEM8(address + length)) ++length;
    126     }
    127     char *text = malloc(length + 1);
    128     if (!text) abort();
    129     for (size_t i = 0; i < length; ++i) {
    130         uint32_t character = wide ? MEM16(address + i * 2) : MEM8(address + i);
    131         if (character > 127) unsupported("non-ASCII window text", character);
    132         text[i] = character;
    133     }
    134     text[length] = 0;
    135     return text;
    136 }
    137 
    138 static struct window_class *find_class(uint32_t name, uint32_t instance, int wide)
    139 {
    140     char *text = name > 0xffffu ? window_text(name, wide) : NULL;
    141     struct window_class *found = NULL;
    142     for (struct window_class *c = classes; c; c = c->next)
    143         if (c->instance == instance &&
    144             (text ? !strcasecmp(c->name, text) : c->atom == name)) { found = c; break; }
    145     free(text);
    146     return found;
    147 }
    148 
    149 static uint32_t icon_resource(uint32_t instance, uint32_t type, uint32_t name)
    150 {
    151     /* Only resource-directory metadata and data are read, never executable bytes. */
    152     uint32_t root = instance + MEM32(instance + MEM32(instance + 0x3c) + 24 + 112);
    153     uint32_t directory = root, keys[] = {type, name, 1033};
    154     for (unsigned level = 0; level < 3; ++level) {
    155         uint32_t named = MEM16(directory + 12), count = MEM16(directory + 14), entry = 0;
    156         for (uint32_t i = 0; i < count; ++i) {
    157             uint32_t address = directory + 16 + (named + i) * 8;
    158             if (MEM32(address) == keys[level]) { entry = MEM32(address + 4); break; }
    159         }
    160         if (!entry) return 0;
    161         if (!!(entry & 0x80000000u) != (level != 2)) unsupported("resource directory", entry);
    162         directory = root + (entry & 0x7fffffffu);
    163     }
    164     return instance + MEM32(directory);
    165 }
    166 
    167 static void load_icon(void)
    168 {
    169     uint32_t instance = MEM32(g_esp + 4), name = MEM32(g_esp + 8);
    170     g_esp += 12;
    171     /* IDI_APPLICATION means the native window manager's default application icon. */
    172     if (!instance && name == 32512) { g_eax = 0xf4100000u; return; }
    173     if (!instance || name > 0xffff) unsupported("icon resource", name);
    174     for (struct window_icon *icon = icons; icon; icon = icon->next)
    175         if (icon->instance == instance && icon->name == name) { g_eax = icon->handle; return; }
    176     uint32_t group = icon_resource(instance, 14, name);
    177     g_eax = 0;
    178     if (!group) { MEM32(g_fs_base + 0x34) = 1813; return; }
    179     if (MEM16(group) || MEM16(group + 2) != 1 || MEM16(group + 4) != 1 ||
    180         MEM8(group + 6) != 32 || MEM8(group + 7) != 32 || MEM16(group + 12) != 4)
    181         unsupported("icon group format", name);
    182     uint32_t dib = icon_resource(instance, 3, MEM16(group + 18));
    183     if (!dib || MEM32(group + 14) != 744 || MEM32(dib) != 40 || MEM32(dib + 4) != 32 ||
    184         MEM32(dib + 8) != 64 || MEM16(dib + 12) != 1 || MEM16(dib + 14) != 4 || MEM32(dib + 16))
    185         unsupported("icon bitmap format", name);
    186 
    187     /* AO's four-bit icon has a BGR palette, bottom-up XOR rows and a one-bit AND mask. */
    188     struct window_icon *icon = calloc(1, sizeof(*icon));
    189     if (!icon) abort();
    190     icon->pixels = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 32, SDL_PIXELFORMAT_ARGB8888);
    191     if (!icon->pixels) abort();
    192     for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
    193         uint32_t index = (MEM8(dib + 104 + (31 - y) * 16 + x / 2) >> (x & 1 ? 0 : 4)) & 15;
    194         uint32_t color = MEM32(dib + 40 + index * 4) & 0xffffff;
    195         int masked = MEM8(dib + 616 + (31 - y) * 4 + x / 8) & (0x80 >> (x & 7));
    196         if (masked && color) unsupported("inverting icon pixel", name);
    197         ((uint32_t *)icon->pixels->pixels)[y * 32 + x] = color | (masked ? 0 : 0xff000000u);
    198     }
    199     icon->instance = instance; icon->name = name;
    200     icon->handle = icons ? icons->handle + 1 : 0xf4400000u;
    201     icon->next = icons; icons = icon;
    202     g_eax = icon->handle;
    203 }
    204 
    205 static void stock_object(void)
    206 {
    207     if (MEM32(g_esp + 4) != 4) unsupported("stock object", MEM32(g_esp + 4));
    208     g_eax = 0xf4100004u; /* BLACK_BRUSH resource; background painting is not implemented yet. */
    209     g_esp += 8;
    210 }
    211 
    212 static void register_class(int wide)
    213 {
    214     uint32_t record = MEM32(g_esp + 4), name = MEM32(record + 36), instance = MEM32(record + 16);
    215     g_eax = 0;
    216     if (name <= 0xffffu || !(wide ? MEM16(name) : MEM8(name)) || !MEM32(record + 4)) MEM32(g_fs_base + 0x34) = 87;
    217     else if (find_class(name, instance, wide)) MEM32(g_fs_base + 0x34) = 1410; /* CLASS_ALREADY_EXISTS */
    218     else {
    219         if (MEM32(record) & ~8u) unsupported("class style", MEM32(record));
    220         if (MEM32(record + 8) || MEM32(record + 12)) unsupported("class extra bytes", record);
    221         if (MEM32(record + 24) || MEM32(record + 32)) unsupported("class cursor/menu", record);
    222         if (MEM32(record + 20) && MEM32(record + 20) != 0xf4100000u) unsupported("class icon", MEM32(record + 20));
    223         if (MEM32(record + 28) && MEM32(record + 28) != 0xf4100004u) unsupported("class background", MEM32(record + 28));
    224         struct window_class *c = calloc(1, sizeof(*c));
    225         if (!c) abort();
    226         c->name = window_text(name, wide);
    227         if (next_atom > 0xffffu) abort();
    228         c->atom = next_atom++;
    229         c->instance = instance;
    230         c->procedure = MEM32(record + 4);
    231         c->icon = MEM32(record + 20);
    232         c->background = MEM32(record + 28);
    233         c->next = classes;
    234         classes = c;
    235         g_eax = c->atom;
    236     }
    237     g_esp += 8;
    238 }
    239 
    240 static void unregister_class(int wide)
    241 {
    242     struct window_class *c = find_class(MEM32(g_esp + 4), MEM32(g_esp + 8), wide);
    243     g_eax = 0;
    244     if (!c) MEM32(g_fs_base + 0x34) = MEM32(g_esp + 4) <= 0xffffu ? 6 : 1411;
    245     else if (client.class == c) MEM32(g_fs_base + 0x34) = 1412; /* CLASS_HAS_WINDOWS */
    246     else {
    247         struct window_class **link = &classes;
    248         while (*link != c) link = &(*link)->next;
    249         *link = c->next;
    250         free(c->name);
    251         free(c);
    252         g_eax = 1;
    253     }
    254     g_esp += 12;
    255 }
    256 
    257 static uint32_t send_message(uint32_t message, uint32_t wparam, uint32_t lparam)
    258 {
    259     uint32_t stack = g_esp, caller = g_cur_func;
    260     uint32_t ebx = g_ebx, esi = g_esi, edi = g_edi, ebp = g_ebp;
    261     recomp_func_t function = recomp_lookup(client.class->procedure);
    262     if (!function) function = recomp_lookup_import(client.class->procedure);
    263     PUSH32(g_esp, lparam);
    264     PUSH32(g_esp, wparam);
    265     PUSH32(g_esp, message);
    266     PUSH32(g_esp, client.handle);
    267     PUSH32(g_esp, RECOMP_RETADDR);
    268     function();
    269     if (g_esp != stack) { fprintf(stderr, "Window callback unbalanced the guest stack\n"); abort(); }
    270     g_cur_func = caller;
    271     g_ebx = ebx; g_esi = esi; g_edi = edi; g_ebp = ebp;
    272     return g_eax;
    273 }
    274 
    275 /* The browser routes captured pointer events through the canvas. */
    276 static int capture_mouse(SDL_bool capture)
    277 {
    278 #ifdef __EMSCRIPTEN__
    279     extern void ao_mouse_capture(int enabled);
    280     ao_mouse_capture(capture);
    281     return 0;
    282 #else
    283     return SDL_CaptureMouse(capture);
    284 #endif
    285 }
    286 
    287 static uint32_t change_capture(uint32_t handle)
    288 {
    289     uint32_t previous = client.capture;
    290     SDL_bool capture = handle && SDL_GetKeyboardFocus() == linux_graphics_window();
    291     if (capture_mouse(capture)) unsupported("mouse capture", handle);
    292     client.capture = handle;
    293     if (previous) send_message(0x215, 0, handle); /* WM_CAPTURECHANGED */
    294     return previous;
    295 }
    296 
    297 static void set_capture(void)
    298 {
    299     uint32_t handle = MEM32(g_esp + 4);
    300     if (handle && handle != client.handle) {
    301         g_eax = 0;
    302         MEM32(g_fs_base + 0x34) = 1400;
    303     } else g_eax = change_capture(handle);
    304     g_esp += 8;
    305 }
    306 
    307 static void release_capture(void)
    308 {
    309     change_capture(0);
    310     g_eax = 1;
    311     g_esp += 4;
    312 }
    313 
    314 static void get_capture(void) { g_eax = client.capture; g_esp += 4; }
    315 
    316 static void send_size_limits(void)
    317 {
    318     SDL_Rect bounds;
    319     if (SDL_GetDisplayUsableBounds(0, &bounds)) { fprintf(stderr, "%s\n", SDL_GetError()); abort(); }
    320     uint32_t limits[] = {0, 0, bounds.w, bounds.h, bounds.x, bounds.y, 1, 1, bounds.w, bounds.h};
    321     uint32_t record = guest_malloc(sizeof(limits));
    322     if (!record) abort();
    323     memcpy((void *)ADDR(record), limits, sizeof(limits));
    324     send_message(0x24, 0, record);
    325     if (memcmp(limits, (const void *)ADDR(record), sizeof(limits))) unsupported("custom size limits", record);
    326     guest_free(record);
    327 }
    328 
    329 static uint32_t change_focus(uint32_t handle)
    330 {
    331     if (client.focus == handle) return handle;
    332     if (handle && !client.active) {
    333         /* Activation can reenter SetFocus through DefWindowProc(WM_ACTIVATE). */
    334         uint32_t position = guest_malloc(28);
    335         if (!position) abort();
    336         memset((void *)ADDR(position), 0, 28);
    337         MEM32(position) = client.handle;
    338         MEM32(position + 24) = 3; /* SWP_NOSIZE | SWP_NOMOVE */
    339         send_message(0x46, 0, position);
    340         if (MEM32(position + 24) != 3) unsupported("activation positioning", MEM32(position + 24));
    341         guest_free(position);
    342         client.active = 1;
    343         send_message(0x1c, 1, 0); /* WM_ACTIVATEAPP */
    344         send_message(0x86, 1, 0); /* WM_NCACTIVATE */
    345         send_message(0x06, 1, 0); /* WM_ACTIVATE */
    346     }
    347     uint32_t previous = client.focus;
    348     client.focus = handle;
    349     if (previous) send_message(0x08, handle, 0);
    350     if (handle && client.focus == handle) send_message(0x07, previous, 0);
    351     return previous;
    352 }
    353 
    354 static void set_focus(void)
    355 {
    356     uint32_t handle = MEM32(g_esp + 4);
    357     if (handle && handle != client.handle) {
    358         MEM32(g_fs_base + 0x34) = 1400;
    359         g_eax = 0;
    360     } else {
    361         /* Thread focus can target a hidden window; OS input focus cannot. */
    362         if (handle && !(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_HIDDEN)) {
    363             SDL_RaiseWindow(linux_graphics_window());
    364             if (SDL_SetWindowInputFocus(linux_graphics_window())) unsupported("SDL input focus", handle);
    365         }
    366         g_eax = change_focus(handle);
    367     }
    368     g_esp += 8;
    369 }
    370 
    371 static void get_focus(void) { g_eax = client.focus; g_esp += 4; }
    372 
    373 static void default_procedure(void)
    374 {
    375     uint32_t handle = MEM32(g_esp + 4), message = MEM32(g_esp + 8);
    376     uint32_t wparam = MEM32(g_esp + 12);
    377     g_eax = 0;
    378     if (!client.handle || handle != client.handle) MEM32(g_fs_base + 0x34) = 1400;
    379     else switch (message) {
    380     case 0x06:
    381         if (wparam & 0xffffu) change_focus(handle);
    382         g_eax = 0;
    383         break;
    384     case 0x86: g_eax = 1; break; /* SDL draws native activation decoration. */
    385     case 0x46:
    386         if (!(MEM32(MEM32(g_esp + 16) + 24) & 1)) send_size_limits();
    387         g_eax = 0;
    388         break;
    389     case 0x47: {
    390         uint32_t position = MEM32(g_esp + 16), flags = MEM32(position + 24);
    391         if ((flags & 0x40) && !client.shown) {
    392             send_message(5, 0, (MEM32(position + 16) & 0xffffu) | (MEM32(position + 20) << 16));
    393             send_message(3, 0, (MEM32(position + 8) & 0xffffu) | (MEM32(position + 12) << 16));
    394         } else {
    395             if (!(flags & 2)) send_message(3, 0, (MEM32(position + 8) & 0xffffu) | (MEM32(position + 12) << 16));
    396             if (!(flags & 1)) send_message(5, 0, (MEM32(position + 16) & 0xffffu) | (MEM32(position + 20) << 16));
    397         }
    398         g_eax = 0;
    399         break;
    400     }
    401     case 0x03: /* WM_MOVE */
    402     case 0x18: /* WM_SHOWWINDOW; SDL applies visibility in ShowWindow. */
    403     case 0x05: /* WM_SIZE */
    404     case 0x7c: /* WM_STYLECHANGING */
    405     case 0x7d: /* WM_STYLECHANGED */
    406     case 0x07: /* WM_SETFOCUS */
    407     case 0x08: /* WM_KILLFOCUS */
    408     case 0x1c: /* WM_ACTIVATEAPP */
    409         break;
    410     case 0x81: g_eax = 1; break; /* WM_NCCREATE accepts creation. */
    411     case 0x83: /* SDL's coordinates describe the client, excluding native decoration. */
    412         break;
    413     case 0x24: /* WM_GETMINMAXINFO is initialized by create_window. */
    414     case 0x01: /* WM_CREATE */
    415     case 0x02: /* WM_DESTROY */
    416     case 0x82: /* WM_NCDESTROY */
    417     case 0x0f: /* WM_PAINT: the game presents its own rendered surface. */
    418     case 0x100: case 0x101: case 0x102: /* Unhandled keyboard input. */
    419     case 0x104: case 0x105: case 0x106:
    420     case 0x215: /* WM_CAPTURECHANGED */
    421     case 0x200: case 0x201: case 0x202: case 0x203:
    422     case 0x204: case 0x205: case 0x206: case 0x207: case 0x208: case 0x209: case 0x20a:
    423         break;
    424     case 0x10: /* WM_CLOSE */
    425         PUSH32(g_esp, handle);
    426         PUSH32(g_esp, RECOMP_RETADDR);
    427         destroy_window();
    428         g_eax = 0;
    429         break;
    430     default: if (message < 0x400) unsupported("default message", message);
    431     }
    432     g_esp += 20;
    433 }
    434 
    435 static void release_window(void)
    436 {
    437     if (client.capture) {
    438         if (capture_mouse(SDL_FALSE)) unsupported("release mouse capture", client.capture);
    439         client.capture = 0;
    440     }
    441     if (pthread_mutex_lock(&message_mutex)) abort();
    442     struct queued_message **link = &message_head;
    443     message_tail = NULL;
    444     while (*link) {
    445         if ((*link)->words[0] == client.handle) {
    446             struct queued_message *removed = *link;
    447             *link = removed->next;
    448             free(removed);
    449         } else {
    450             message_tail = *link;
    451             link = &(*link)->next;
    452         }
    453     }
    454     if (pthread_mutex_unlock(&message_mutex)) abort();
    455     SDL_HideWindow(linux_graphics_window());
    456     client.handle = 0;
    457     client.focus = 0;
    458     client.active = 0;
    459     client.shown = 0;
    460     client.class = NULL;
    461 }
    462 
    463 static void create_window(int wide)
    464 {
    465     uint32_t args[12];
    466     for (unsigned i = 0; i < 12; ++i) args[i] = MEM32(g_esp + 4 + i * 4);
    467     struct window_class *c = find_class(args[1], args[10], wide);
    468     g_eax = 0;
    469     if (!c) { MEM32(g_fs_base + 0x34) = args[1] <= 0xffffu ? 6 : 1407; g_esp += 52; return; }
    470     if (client.handle) unsupported("additional window", client.handle);
    471     if (args[0] || args[3] || args[8] || args[9]) unsupported("creation style/parent/menu", args[3]);
    472     for (unsigned i = 4; i < 8; ++i)
    473         if ((int32_t)args[i] < 0) unsupported("default/negative geometry", args[i]);
    474     SDL_Window *window = linux_graphics_window();
    475     if (!window) { MEM32(g_fs_base + 0x34) = 8; g_esp += 52; return; }
    476     char *title = window_text(args[2], wide);
    477     SDL_SetWindowTitle(window, title);
    478     free(title);
    479     SDL_SetWindowPosition(window, args[4], args[5]);
    480     client.width = args[6] ? args[6] : 1;
    481     client.height = args[7] ? args[7] : 1;
    482     SDL_SetWindowSize(window, client.width, client.height);
    483     client.class = c;
    484     client.handle = next_handle++;
    485 #ifdef __EMSCRIPTEN__
    486     input_thread = pthread_self();
    487 #endif
    488     queued_modifiers = keyboard_modifiers = SDL_GetModState();
    489     client.style = 0x04c00000u; /* Implicit WS_CAPTION and WS_CLIPSIBLINGS for style zero. */
    490 
    491     /* Win32's 32-bit CREATESTRUCT, RECT and MINMAXINFO live in guest memory. */
    492     uint32_t record = guest_malloc(64);
    493     if (!record) abort();
    494     memset((void *)ADDR(record), 0, 64);
    495     static const unsigned fields[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    496     for (unsigned i = 0; i < 12; ++i) MEM32(record + i * 4) = args[fields[i]];
    497     MEM32(record + 48) = args[4]; MEM32(record + 52) = args[5];
    498     MEM32(record + 56) = args[4] + args[6]; MEM32(record + 60) = args[5] + args[7];
    499     uint32_t sizing[4];
    500     memcpy(sizing, (const void *)ADDR(record + 48), sizeof(sizing));
    501     send_size_limits();
    502     if (!send_message(0x81, 0, record)) {
    503         send_message(0x82, 0, 0);
    504         release_window();
    505     } else {
    506         send_message(0x83, 0, record + 48);
    507         if (memcmp(sizing, (const void *)ADDR(record + 48), 16)) unsupported("custom client rectangle", record + 48);
    508         if (send_message(0x01, 0, record) == 0xffffffffu) {
    509             send_message(0x02, 0, 0);
    510             send_message(0x82, 0, 0);
    511             release_window();
    512         }
    513     }
    514     guest_free(record);
    515     g_eax = client.handle;
    516     if (client.handle) fprintf(stderr, "[linux-window] created %08x: %s\n", client.handle, SDL_GetWindowTitle(window));
    517     g_esp += 52;
    518 }
    519 
    520 static void destroy_window(void)
    521 {
    522     if (!client.handle || MEM32(g_esp + 4) != client.handle) {
    523         g_eax = 0;
    524         MEM32(g_fs_base + 0x34) = 1400;
    525     } else {
    526         if (client.active) {
    527             client.active = 0;
    528             send_message(0x86, 0, 0);
    529             send_message(0x06, 0, 0);
    530             send_message(0x1c, 0, 0);
    531         }
    532         change_focus(0);
    533         send_message(0x02, 0, 0);
    534         send_message(0x82, 0, 0);
    535         release_window();
    536         g_eax = 1;
    537     }
    538     g_esp += 8;
    539 }
    540 
    541 static void show_window(void)
    542 {
    543     uint32_t handle = MEM32(g_esp + 4), command = MEM32(g_esp + 8);
    544     g_esp += 12;
    545     g_eax = 0;
    546     if (!client.handle || handle != client.handle) { MEM32(g_fs_base + 0x34) = 1400; return; }
    547     /* The native process has no STARTF_USESHOWWINDOW override. */
    548     if (command != 0 && command != 1 && command != 5 && command != 10) unsupported("show command", command);
    549     int visible = !!(client.style & 0x10000000u), show = command != 0;
    550     SDL_Window *window = linux_graphics_window();
    551     if (visible == show) { g_eax = visible; return; }
    552 
    553     /* Announce visibility before changing the guest style or native window. */
    554     send_message(0x18, show, 0);
    555     uint32_t record = guest_malloc(28);
    556     if (!record) abort();
    557     memset((void *)ADDR(record), 0, 28);
    558     MEM32(record) = handle;
    559     MEM32(record + 24) = show ? 0x43 : 0x83; /* SHOW/HIDE | NOMOVE | NOSIZE */
    560     send_message(0x46, 0, record);
    561     if (MEM32(record + 24) != (show ? 0x43u : 0x83u)) unsupported("show positioning flags", MEM32(record + 24));
    562     if (show) {
    563         client.style |= 0x10000000u;
    564         SDL_ShowWindow(window);
    565         SDL_RaiseWindow(window);
    566         change_focus(handle);
    567     } else {
    568         client.style &= ~0x10000000u;
    569         SDL_HideWindow(window);
    570     }
    571 
    572     /* SDL coordinates describe the client; the compositor owns nonclient paint. */
    573     int x, y, width, height;
    574     SDL_GetWindowPosition(window, &x, &y);
    575     width = client.width; height = client.height;
    576     MEM32(record + 8) = x; MEM32(record + 12) = y;
    577     MEM32(record + 16) = width; MEM32(record + 20) = height;
    578     MEM32(record + 24) |= 0x1804; /* NOZORDER | NOCLIENTMOVE | NOCLIENTSIZE */
    579     send_message(0x47, 0, record);
    580     guest_free(record);
    581     if (show) client.shown = 1;
    582     else {
    583         if (client.active) {
    584             client.active = 0;
    585             send_message(0x86, 0, 0);
    586             send_message(0x06, 0, 0);
    587             send_message(0x1c, 0, 0);
    588         }
    589         change_focus(0);
    590     }
    591     g_eax = visible;
    592 }
    593 
    594 static void system_parameters(void)
    595 {
    596     uint32_t action = MEM32(g_esp + 4), enabled = MEM32(g_esp + 8);
    597     if (MEM32(g_esp + 12) || (MEM32(g_esp + 16) & ~2u)) unsupported("system parameter arguments", action);
    598     if (action == 0x11) { /* SPI_SETSCREENSAVEACTIVE: process-scoped SDL inhibition. */
    599         if (!linux_graphics_window()) { g_eax = 0; MEM32(g_fs_base + 0x34) = 8; }
    600         else {
    601             if (enabled) SDL_EnableScreenSaver();
    602             else SDL_DisableScreenSaver();
    603             g_eax = SDL_IsScreenSaverEnabled() == !!enabled;
    604         }
    605     } else if (action == 0x56) {
    606         /* SPI_SETPOWEROFFACTIVE is unsupported; do not change desktop power policy. */
    607         g_eax = 0;
    608         MEM32(g_fs_base + 0x34) = 120; /* ERROR_CALL_NOT_IMPLEMENTED */
    609     } else unsupported("system parameter action", action);
    610     g_esp += 20;
    611 }
    612 
    613 static void set_window_long(void)
    614 {
    615     uint32_t handle = MEM32(g_esp + 4), index = MEM32(g_esp + 8), value = MEM32(g_esp + 12);
    616     if (!client.handle || handle != client.handle) { g_eax = 0; MEM32(g_fs_base + 0x34) = 1400; }
    617     else {
    618         if (index != 0xfffffff0u) unsupported("window field", index);
    619         uint32_t previous = client.style, record = guest_malloc(8);
    620         if (!record) abort();
    621         MEM32(record) = previous; MEM32(record + 4) = value;
    622         send_message(0x7c, index, record);
    623         value = MEM32(record + 4) | 0x04000000u;
    624         if ((value & ~0x04ca0000u) || (value & 0x00c00000u) != 0x00c00000u) unsupported("window style", value);
    625         client.style = value;
    626         SDL_SetWindowBordered(linux_graphics_window(), SDL_TRUE);
    627         MEM32(record) = previous; MEM32(record + 4) = value;
    628         send_message(0x7d, index, record);
    629         guest_free(record);
    630         g_eax = previous;
    631     }
    632     g_esp += 16;
    633 }
    634 
    635 static void set_class_long(void)
    636 {
    637     uint32_t handle = MEM32(g_esp + 4), index = MEM32(g_esp + 8), value = MEM32(g_esp + 12);
    638     g_esp += 16;
    639     g_eax = 0;
    640     if (!client.handle || handle != client.handle) { MEM32(g_fs_base + 0x34) = 1400; return; }
    641     if (index != 0xfffffff2u) unsupported("class field", index);
    642     struct window_icon *icon = icons;
    643     while (icon && icon->handle != value) icon = icon->next;
    644     if (!icon) unsupported("class icon handle", value);
    645     g_eax = client.class->icon;
    646     client.class->icon = value;
    647     SDL_SetWindowIcon(linux_graphics_window(), icon->pixels);
    648 }
    649 
    650 static void get_window_long(void)
    651 {
    652     uint32_t handle = MEM32(g_esp + 4), index = MEM32(g_esp + 8);
    653     g_eax = 0;
    654     if (!client.handle || handle != client.handle) MEM32(g_fs_base + 0x34) = 1400;
    655     else if (index == 0xfffffff0u) g_eax = client.style;
    656     else if (index == 0xffffffecu) g_eax = 0x100; /* WS_EX_WINDOWEDGE */
    657     else unsupported("window field", index);
    658     g_esp += 12;
    659 }
    660 
    661 static void adjust_window_rect(void)
    662 {
    663     uint32_t style = MEM32(g_esp + 8), menu = MEM32(g_esp + 12), extended = MEM32(g_esp + 16);
    664     if ((style & ~0x04ca0000u) || menu || (extended & ~0x100u)) unsupported("rectangle style/menu", style);
    665     /* The facade uses client coordinates; SDL adds the native window frame. */
    666     g_eax = 1;
    667     g_esp += 20;
    668 }
    669 
    670 static void system_metrics(void)
    671 {
    672     uint32_t index = MEM32(g_esp + 4);
    673     if (index > 1) unsupported("system metric", index);
    674     SDL_Rect bounds;
    675 #ifdef __EMSCRIPTEN__
    676     SDL_DisplayMode mode;
    677     if (!linux_graphics_window() || linux_graphics_display_mode(&mode)) abort();
    678     bounds.w = mode.w; bounds.h = mode.h;
    679 #else
    680     if (!linux_graphics_window() || SDL_GetDisplayBounds(0, &bounds)) {
    681         fprintf(stderr, "Cannot query native display bounds: %s\n", SDL_GetError());
    682         abort();
    683     }
    684 #endif
    685     g_eax = index ? bounds.h : bounds.w;
    686     g_esp += 8;
    687 }
    688 
    689 static void client_rect(void)
    690 {
    691     uint32_t handle = MEM32(g_esp + 4), output = MEM32(g_esp + 8);
    692     g_eax = 0;
    693     if (!client.handle || handle != client.handle) MEM32(g_fs_base + 0x34) = 1400;
    694     else {
    695         int width, height;
    696         width = client.width; height = client.height;
    697         MEM32(output) = 0; MEM32(output + 4) = 0;
    698         MEM32(output + 8) = width; MEM32(output + 12) = height;
    699         g_eax = 1;
    700     }
    701     g_esp += 12;
    702 }
    703 
    704 static void window_rect(void)
    705 {
    706     uint32_t handle = MEM32(g_esp + 4), output = MEM32(g_esp + 8);
    707     g_eax = 0;
    708     if (!client.handle || handle != client.handle) MEM32(g_fs_base + 0x34) = 1400;
    709     else {
    710         /* Match MoveWindow/AdjustWindowRectEx's client-coordinate facade. */
    711         int x, y, width, height;
    712         SDL_GetWindowPosition(linux_graphics_window(), &x, &y);
    713         width = client.width; height = client.height;
    714         MEM32(output) = x; MEM32(output + 4) = y;
    715         MEM32(output + 8) = x + width; MEM32(output + 12) = y + height;
    716         g_eax = 1;
    717     }
    718     g_esp += 12;
    719 }
    720 
    721 static void set_window_position(void)
    722 {
    723     uint32_t handle = MEM32(g_esp + 4), after = MEM32(g_esp + 8), flags = MEM32(g_esp + 28);
    724     int x = MEM32(g_esp + 12), y = MEM32(g_esp + 16);
    725     g_esp += 32;
    726     g_eax = 0;
    727     if (!client.handle || handle != client.handle) { MEM32(g_fs_base + 0x34) = 1400; return; }
    728     if (after || flags != 5) unsupported("position flags/z-order", flags);
    729 
    730     /* AO centers its shown window with NOSIZE | NOZORDER. */
    731     uint32_t record = guest_malloc(28);
    732     if (!record) abort();
    733     memset((void *)ADDR(record), 0, 28);
    734     MEM32(record) = handle;
    735     MEM32(record + 8) = x; MEM32(record + 12) = y; MEM32(record + 24) = flags;
    736     send_message(0x46, 0, record);
    737     if (MEM32(record + 24) != 5) unsupported("position callback flags", MEM32(record + 24));
    738     x = MEM32(record + 8); y = MEM32(record + 12);
    739     SDL_Window *window = linux_graphics_window();
    740     int old_x, old_y, width, height;
    741     SDL_GetWindowPosition(window, &old_x, &old_y);
    742     width = client.width; height = client.height;
    743     SDL_SetWindowPosition(window, x, y);
    744     change_focus(handle);
    745     SDL_GetWindowPosition(window, &x, &y);
    746     if (x != old_x || y != old_y) {
    747         MEM32(record + 8) = x; MEM32(record + 12) = y;
    748         MEM32(record + 16) = width; MEM32(record + 20) = height;
    749         MEM32(record + 24) = 0x805; /* NOSIZE | NOZORDER | NOCLIENTSIZE */
    750         send_message(0x47, 0, record);
    751     }
    752     guest_free(record);
    753     g_eax = 1;
    754 }
    755 
    756 static void move_window(void)
    757 {
    758     uint32_t handle = MEM32(g_esp + 4);
    759     if (!client.handle || handle != client.handle) { g_eax = 0; MEM32(g_fs_base + 0x34) = 1400; g_esp += 28; return; }
    760     if (MEM32(g_esp + 24)) unsupported("MoveWindow repaint", MEM32(g_esp + 24));
    761     SDL_Window *window = linux_graphics_window();
    762     int x, y, width, height;
    763     SDL_GetWindowPosition(window, &x, &y);
    764     width = client.width; height = client.height;
    765 
    766     /* WINDOWPOS followed by NCCALCSIZE_PARAMS, with all pointers in guest space. */
    767     uint32_t record = guest_malloc(80);
    768     if (!record) abort();
    769     memset((void *)ADDR(record), 0, 80);
    770     MEM32(record) = handle;
    771     for (unsigned i = 0; i < 4; ++i) MEM32(record + 8 + i * 4) = MEM32(g_esp + 8 + i * 4);
    772     MEM32(record + 24) = 0x1c; /* NOZORDER | NOREDRAW | NOACTIVATE */
    773     if (MEM32(record + 8) == (uint32_t)x && MEM32(record + 12) == (uint32_t)y) MEM32(record + 24) |= 2;
    774     if (MEM32(record + 16) == (uint32_t)width && MEM32(record + 20) == (uint32_t)height) MEM32(record + 24) |= 1;
    775     send_message(0x46, 0, record);
    776     if ((MEM32(record + 24) & ~3u) != 0x1c) unsupported("MoveWindow positioning flags", MEM32(record + 24));
    777     if (!(MEM32(record + 24) & 2)) { x = MEM32(record + 8); y = MEM32(record + 12); }
    778     if (!(MEM32(record + 24) & 1)) { width = MEM32(record + 16); height = MEM32(record + 20); }
    779     if (width < 1 || height < 1) unsupported("empty window size", handle);
    780     MEM32(record + 28) = x; MEM32(record + 32) = y;
    781     MEM32(record + 36) = x + width; MEM32(record + 40) = y + height;
    782     int old_x, old_y, old_width, old_height;
    783     SDL_GetWindowPosition(window, &old_x, &old_y);
    784     old_width = client.width; old_height = client.height;
    785     MEM32(record + 44) = old_x; MEM32(record + 48) = old_y;
    786     MEM32(record + 52) = old_x + old_width; MEM32(record + 56) = old_y + old_height;
    787     memcpy((void *)ADDR(record + 60), (const void *)ADDR(record + 44), 16);
    788     MEM32(record + 76) = record;
    789     uint32_t rectangle[4];
    790     memcpy(rectangle, (const void *)ADDR(record + 28), 16);
    791     if (!(MEM32(record + 24) & 1)) {
    792         send_message(0x83, 1, record + 28);
    793         if (memcmp(rectangle, (const void *)ADDR(record + 28), 16)) unsupported("custom client rectangle", record + 28);
    794     }
    795     SDL_SetWindowPosition(window, x, y);
    796     /* Win32 geometry updates synchronously even when SDL defers a hidden resize. */
    797     client.width = width; client.height = height;
    798     SDL_SetWindowSize(window, width, height);
    799     send_message(0x47, 0, record);
    800     guest_free(record);
    801     g_eax = 1;
    802     g_esp += 28;
    803 }
    804 
    805 static void client_to_screen(void)
    806 {
    807     uint32_t handle = MEM32(g_esp + 4), point = MEM32(g_esp + 8);
    808     g_eax = 0;
    809     if (!client.handle || handle != client.handle) MEM32(g_fs_base + 0x34) = 1400;
    810     else {
    811         int x, y;
    812         SDL_GetWindowPosition(linux_graphics_window(), &x, &y);
    813         MEM32(point) += (uint32_t)x;
    814         MEM32(point + 4) += (uint32_t)y;
    815         g_eax = 1;
    816     }
    817     g_esp += 12;
    818 }
    819 
    820 static void cursor_position(void)
    821 {
    822     uint32_t output = MEM32(g_esp + 4);
    823     g_esp += 8;
    824     g_eax = 0;
    825     if (!output) { MEM32(g_fs_base + 0x34) = 998; return; }
    826     if (!linux_graphics_window()) { MEM32(g_fs_base + 0x34) = 8; return; }
    827     int x, y;
    828 #ifdef __EMSCRIPTEN__
    829     if (!cursor_warped && !browser_mouse) SDL_GetMouseState(&cursor_x, &cursor_y);
    830     x = cursor_x; y = cursor_y;
    831 #else
    832     SDL_GetGlobalMouseState(&x, &y);
    833 #endif
    834     MEM32(output) = x; MEM32(output + 4) = y;
    835     g_eax = 1;
    836 }
    837 
    838 static void set_cursor_position(void)
    839 {
    840     int x = MEM32(g_esp + 4), y = MEM32(g_esp + 8);
    841     g_esp += 12;
    842 #ifdef __EMSCRIPTEN__
    843     cursor_x = x; cursor_y = y;
    844     cursor_warped = 1;
    845     g_eax = 1;
    846 #else
    847     g_eax = SDL_WarpMouseGlobal(x, y) == 0;
    848     if (!g_eax) MEM32(g_fs_base + 0x34) = 87;
    849 #endif
    850 }
    851 
    852 static void set_cursor(void)
    853 {
    854     uint32_t cursor = MEM32(g_esp + 4);
    855     if (cursor && cursor != 0xf4500000u) unsupported("cursor handle", cursor);
    856 #ifdef __EMSCRIPTEN__
    857     if (!browser_cursor) {
    858 #endif
    859         if (cursor) SDL_SetCursor(SDL_GetDefaultCursor());
    860         if (SDL_ShowCursor(cursor ? SDL_ENABLE : SDL_DISABLE) < 0) abort();
    861 #ifdef __EMSCRIPTEN__
    862     }
    863 #endif
    864     g_eax = current_cursor;
    865     current_cursor = cursor;
    866     g_esp += 8;
    867 }
    868 
    869 static void key_state(void)
    870 {
    871     uint32_t key = MEM32(g_esp + 4);
    872     SDL_Keymod mask;
    873     if (key == 0x10) mask = KMOD_SHIFT;
    874     else if (key == 0x11) mask = KMOD_CTRL;
    875     else if (key == 0x12) mask = KMOD_ALT;
    876     else { unsupported("virtual key", key); return; }
    877     /* Read the removed message's snapshot without changing SDL's physical state. */
    878     g_eax = keyboard_modifiers & mask ? 0xffffff80u : 0;
    879     g_esp += 8;
    880 }
    881 
    882 static void desktop_window(void) { g_eax = 0xf4200000u; g_esp += 4; }
    883 
    884 static void get_dc(void)
    885 {
    886     uint32_t window = MEM32(g_esp + 4);
    887     if (window && window != 0xf4200000u) unsupported("device context window", window);
    888     struct display_context *context = malloc(sizeof(*context));
    889     if (!context) abort();
    890     context->handle = next_context++;
    891     context->next = display_contexts;
    892     display_contexts = context;
    893     g_eax = context->handle;
    894     g_esp += 8;
    895 }
    896 
    897 static void release_dc(void)
    898 {
    899     uint32_t window = MEM32(g_esp + 4), handle = MEM32(g_esp + 8);
    900     if (window && window != 0xf4200000u) unsupported("device context window", window);
    901     struct display_context **link = &display_contexts;
    902     while (*link && (*link)->handle != handle) link = &(*link)->next;
    903     g_eax = *link != NULL;
    904     if (*link) {
    905         struct display_context *context = *link;
    906         *link = context->next;
    907         free(context);
    908     }
    909     g_esp += 12;
    910 }
    911 
    912 static void device_caps(void)
    913 {
    914     uint32_t handle = MEM32(g_esp + 4), index = MEM32(g_esp + 8);
    915     struct display_context *context = display_contexts;
    916     while (context && context->handle != handle) context = context->next;
    917     g_eax = 0;
    918     if (!context) MEM32(g_fs_base + 0x34) = 6;
    919     else {
    920         if (index != 12) unsupported("display capability", index);
    921         SDL_DisplayMode mode;
    922         if (!linux_graphics_window() || SDL_GetCurrentDisplayMode(0, &mode)) {
    923             fprintf(stderr, "Cannot query native display format: %s\n", SDL_GetError());
    924             abort();
    925         }
    926         g_eax = SDL_BYTESPERPIXEL(mode.format) * 8; /* BITSPIXEL includes storage padding. */
    927     }
    928     g_esp += 12;
    929 }
    930 
    931 /* Posted messages retain their payload until the owning message loop removes them. */
    932 static void queue_message(uint32_t handle, uint32_t message, uint32_t wparam,
    933                           uint32_t lparam, int keyboard, SDL_Keymod modifiers)
    934 {
    935     struct queued_message *entry = calloc(1, sizeof(*entry));
    936     if (!entry) abort();
    937     entry->words[0] = handle;
    938     entry->words[1] = message;
    939     entry->words[2] = wparam;
    940     entry->words[3] = lparam;
    941     entry->words[4] = SDL_GetTicks();
    942     int x, y;
    943     SDL_GetGlobalMouseState(&x, &y);
    944     entry->words[5] = x; entry->words[6] = y;
    945     entry->keyboard = keyboard;
    946     entry->modifiers = modifiers;
    947     if (pthread_mutex_lock(&message_mutex)) abort();
    948     if (message_tail) message_tail->next = entry;
    949     else message_head = entry;
    950     message_tail = entry;
    951     if (pthread_mutex_unlock(&message_mutex)) abort();
    952 }
    953 
    954 static void wake_message_loop(void)
    955 {
    956     if (!SDL_WasInit(SDL_INIT_EVENTS)) return;
    957     SDL_Event wake = {.type = SDL_USEREVENT};
    958     if (SDL_PushEvent(&wake) < 0) abort();
    959 }
    960 
    961 static void post_message(void)
    962 {
    963     uint32_t handle = MEM32(g_esp + 4);
    964     g_eax = !handle || handle == client.handle;
    965     if (!g_eax) MEM32(g_fs_base + 0x34) = 1400;
    966     else {
    967         queue_message(handle, MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16), 0, 0);
    968         wake_message_loop();
    969     }
    970     g_esp += 20;
    971 }
    972 
    973 static void post_quit_message(void)
    974 {
    975     if (pthread_mutex_lock(&message_mutex)) abort();
    976     quit_pending = 1;
    977     quit_code = MEM32(g_esp + 4);
    978     if (pthread_mutex_unlock(&message_mutex)) abort();
    979     wake_message_loop();
    980     g_esp += 8;
    981 }
    982 
    983 /* US virtual keys and x86 Set-1 scan codes used by the game's input handler. */
    984 static uint32_t virtual_key(SDL_Keycode key, unsigned *scan)
    985 {
    986     if (key >= SDLK_a && key <= SDLK_z) {
    987         static const uint8_t scans[] = {30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,19,31,20,22,47,17,45,21,44};
    988         *scan = scans[key - SDLK_a];
    989         return 'A' + key - SDLK_a;
    990     }
    991     if (key >= SDLK_0 && key <= SDLK_9) {
    992         *scan = key == SDLK_0 ? 11 : key - SDLK_1 + 2;
    993         return key;
    994     }
    995     if (key >= SDLK_F1 && key <= SDLK_F12) {
    996         *scan = key <= SDLK_F10 ? 59 + key - SDLK_F1 : 87 + key - SDLK_F11;
    997         return 0x70 + key - SDLK_F1;
    998     }
    999     static const struct { SDL_Keycode key; uint16_t code, scan; } keys[] = {
   1000         {SDLK_ESCAPE,0x1b,1}, {SDLK_BACKSPACE,8,14}, {SDLK_TAB,9,15},
   1001         {SDLK_RETURN,13,28}, {SDLK_SPACE,32,57}, {SDLK_CAPSLOCK,20,58},
   1002         {SDLK_LSHIFT,16,42}, {SDLK_RSHIFT,16,54}, {SDLK_LCTRL,17,29},
   1003         {SDLK_RCTRL,17,0x11d}, {SDLK_LALT,18,56}, {SDLK_RALT,18,0x138},
   1004         {SDLK_HOME,0x24,0x147}, {SDLK_END,0x23,0x14f},
   1005         {SDLK_PAGEUP,0x21,0x149}, {SDLK_PAGEDOWN,0x22,0x151},
   1006         {SDLK_LEFT,0x25,0x14b}, {SDLK_UP,0x26,0x148},
   1007         {SDLK_RIGHT,0x27,0x14d}, {SDLK_DOWN,0x28,0x150},
   1008         {SDLK_INSERT,0x2d,0x152}, {SDLK_DELETE,0x2e,0x153},
   1009         {SDLK_KP_0,0x60,82}, {SDLK_KP_1,0x61,79}, {SDLK_KP_2,0x62,80},
   1010         {SDLK_KP_3,0x63,81}, {SDLK_KP_4,0x64,75}, {SDLK_KP_5,0x65,76},
   1011         {SDLK_KP_6,0x66,77}, {SDLK_KP_7,0x67,71}, {SDLK_KP_8,0x68,72},
   1012         {SDLK_KP_9,0x69,73}, {SDLK_KP_MULTIPLY,0x6a,55},
   1013         {SDLK_KP_PLUS,0x6b,78}, {SDLK_KP_MINUS,0x6d,74},
   1014         {SDLK_KP_PERIOD,0x6e,83}, {SDLK_KP_DIVIDE,0x6f,0x135},
   1015         {SDLK_KP_ENTER,13,0x11c},
   1016         {SDLK_SEMICOLON,0xba,39}, {SDLK_EQUALS,0xbb,13}, {SDLK_COMMA,0xbc,51},
   1017         {SDLK_MINUS,0xbd,12}, {SDLK_PERIOD,0xbe,52}, {SDLK_SLASH,0xbf,53},
   1018         {SDLK_BACKQUOTE,0xc0,41}, {SDLK_LEFTBRACKET,0xdb,26},
   1019         {SDLK_BACKSLASH,0xdc,43}, {SDLK_RIGHTBRACKET,0xdd,27}, {SDLK_QUOTE,0xde,40},
   1020     };
   1021     for (unsigned i = 0; i < sizeof(keys) / sizeof(keys[0]); ++i)
   1022         if (keys[i].key == key) { *scan = keys[i].scan; return keys[i].code; }
   1023     return 0;
   1024 }
   1025 
   1026 #ifdef __EMSCRIPTEN__
   1027 /* SDL's queue carries browser controls to the game worker in input order. */
   1028 EMSCRIPTEN_KEEPALIVE void browser_input_key(int key, int down, int repeat, int modifiers)
   1029 {
   1030     SDL_Event event = {.type = down ? SDL_KEYDOWN : SDL_KEYUP};
   1031     event.key.keysym.sym = key;
   1032     event.key.keysym.mod = modifiers;
   1033     event.key.state = down ? SDL_PRESSED : SDL_RELEASED;
   1034     event.key.repeat = repeat;
   1035     ao_test_input(0, event.type, key, down, repeat, modifiers);
   1036     if (SDL_PushEvent(&event) != 1) abort();
   1037 }
   1038 
   1039 EMSCRIPTEN_KEEPALIVE void browser_input_character(unsigned character)
   1040 {
   1041     /* Keep composed characters separate from message-loop wakeups. */
   1042     SDL_Event event = {.type = SDL_USEREVENT + 1};
   1043     event.user.code = character;
   1044     ao_test_input(0, event.type, character, 0, 0, 0);
   1045     if (SDL_PushEvent(&event) != 1) abort();
   1046 }
   1047 
   1048 static void paste_character(void *argument)
   1049 {
   1050     browser_input_character((uintptr_t)argument);
   1051 }
   1052 
   1053 EMSCRIPTEN_KEEPALIVE void browser_paste_character(unsigned character)
   1054 {
   1055     /* Physical DOM keys are delivered to SDL on this same worker queue. */
   1056     if (!emscripten_proxy_async(emscripten_proxy_get_system_queue(), input_thread,
   1057                                 paste_character, (void *)(uintptr_t)character)) abort();
   1058 }
   1059 
   1060 EMSCRIPTEN_KEEPALIVE void browser_input_motion(int x, int y)
   1061 {
   1062     SDL_Event event = {.type = SDL_MOUSEMOTION};
   1063     event.motion.which = SDL_TOUCH_MOUSEID;
   1064     event.motion.x = x; event.motion.y = y;
   1065     ao_test_input(0, event.type, x, y, 0, 0);
   1066     if (SDL_PushEvent(&event) != 1) abort();
   1067 }
   1068 
   1069 EMSCRIPTEN_KEEPALIVE void browser_input_button(unsigned button, int down, int x, int y)
   1070 {
   1071     SDL_Event event = {.type = down ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP};
   1072     event.button.which = SDL_TOUCH_MOUSEID;
   1073     event.button.button = button;
   1074     event.button.state = down ? SDL_PRESSED : SDL_RELEASED;
   1075     event.button.x = x; event.button.y = y;
   1076     ao_test_input(0, event.type, button, down, x, y);
   1077     if (SDL_PushEvent(&event) != 1) abort();
   1078 }
   1079 
   1080 EMSCRIPTEN_KEEPALIVE void browser_input_wheel(int x, int y, float vertical)
   1081 {
   1082     SDL_Event event = {.type = SDL_MOUSEWHEEL};
   1083     event.wheel.mouseX = x; event.wheel.mouseY = y;
   1084     event.wheel.preciseY = vertical;
   1085     if (SDL_PushEvent(&event) != 1) abort();
   1086 }
   1087 
   1088 #endif
   1089 
   1090 static void process_event(const SDL_Event *event)
   1091 {
   1092     if (!client.handle) return;
   1093 #ifdef __EMSCRIPTEN__
   1094     if (event->type == SDL_KEYDOWN || event->type == SDL_KEYUP)
   1095         ao_test_input(1, event->type, event->key.keysym.sym, event->key.state,
   1096             event->key.repeat, event->key.keysym.mod);
   1097     else if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP)
   1098         ao_test_input(1, event->type, event->button.button, event->button.state,
   1099             event->button.x, event->button.y);
   1100     else if (event->type == SDL_MOUSEMOTION)
   1101         ao_test_input(1, event->type, event->motion.x, event->motion.y, 0, 0);
   1102     else if (event->type == SDL_USEREVENT + 1)
   1103         ao_test_input(1, event->type, event->user.code, 0, 0, 0);
   1104     /* HTML input supplies composed text; physical keys use TranslateMessage. */
   1105     if (event->type == SDL_USEREVENT + 1) {
   1106         queue_message(client.handle, 0x102, event->user.code, 1, 0, 0);
   1107         return;
   1108     }
   1109 #endif
   1110     if (event->type == SDL_QUIT) queue_message(client.handle, 0x10, 0, 0, 0, 0);
   1111     else if (event->type == SDL_WINDOWEVENT &&
   1112              event->window.windowID == SDL_GetWindowID(linux_graphics_window())) {
   1113         switch (event->window.event) {
   1114         case SDL_WINDOWEVENT_CLOSE: queue_message(client.handle, 0x10, 0, 0, 0, 0); break;
   1115         case SDL_WINDOWEVENT_FOCUS_GAINED:
   1116             if (client.capture && capture_mouse(SDL_TRUE)) unsupported("mouse capture", client.capture);
   1117             if (!client.active) {
   1118                 client.active = 1;
   1119                 send_message(0x1c, 1, 0);
   1120                 send_message(0x86, 1, 0);
   1121                 send_message(6, 1, 0);
   1122                 change_focus(client.handle);
   1123             }
   1124             break;
   1125         case SDL_WINDOWEVENT_FOCUS_LOST:
   1126             queued_modifiers = KMOD_NONE;
   1127 #ifdef __EMSCRIPTEN__
   1128             static const uint32_t releases[][2] = {{1, 0x202}, {2, 0x205}, {16, 0x208}};
   1129             for (unsigned i = 0; i < 3; ++i) {
   1130                 if (!(queued_mouse_buttons & releases[i][0])) continue;
   1131                 queued_mouse_buttons &= ~releases[i][0];
   1132                 queue_message(client.handle, releases[i][1], queued_mouse_buttons, 0, 2, 0);
   1133             }
   1134             cursor_warped = 0;
   1135 #endif
   1136             if (client.capture) change_capture(0);
   1137             if (client.active) {
   1138                 client.active = 0;
   1139                 change_focus(0);
   1140                 send_message(0x86, 0, 0);
   1141                 send_message(6, 0, 0);
   1142                 send_message(0x1c, 0, 0);
   1143             }
   1144             break;
   1145         case SDL_WINDOWEVENT_SIZE_CHANGED:
   1146             if (!client.shown) break;
   1147             /* This fixed-size window retains its requested Win32 client area. */
   1148             send_message(5, 0, (client.width & 0xffffu) | ((uint32_t)client.height << 16));
   1149             break;
   1150         case SDL_WINDOWEVENT_MOVED:
   1151             send_message(3, 0, (event->window.data1 & 0xffffu) | ((uint32_t)event->window.data2 << 16));
   1152             break;
   1153         }
   1154     } else if (event->type == SDL_KEYDOWN || event->type == SDL_KEYUP) {
   1155         // Mouse events follow this snapshot, even when SDL has already queued a release.
   1156         queued_modifiers = event->key.keysym.mod;
   1157         unsigned scan = 0;
   1158         uint32_t key = virtual_key(event->key.keysym.sym, &scan);
   1159         if (!key) return;
   1160         int up = event->type == SDL_KEYUP, alt = !!(event->key.keysym.mod & KMOD_ALT) || key == 18;
   1161         uint32_t flags = 1 | (scan << 16) | (alt ? 0x20000000u : 0) |
   1162                          (up || event->key.repeat ? 0x40000000u : 0) | (up ? 0x80000000u : 0);
   1163         queue_message(client.handle, (alt ? 0x104 : 0x100) + up, key, flags, 1, event->key.keysym.mod);
   1164     } else if (event->type == SDL_MOUSEMOTION) {
   1165         uint32_t state = (event->motion.state & SDL_BUTTON_LMASK ? 1 : 0) |
   1166                          (event->motion.state & SDL_BUTTON_RMASK ? 2 : 0) |
   1167                          (event->motion.state & SDL_BUTTON_MMASK ? 16 : 0);
   1168 #ifdef __EMSCRIPTEN__
   1169         /* Include virtual gestures and keep lost-focus buttons released. */
   1170         state = queued_mouse_buttons;
   1171         if (event->motion.which != SDL_TOUCH_MOUSEID) {
   1172             if (cursor_warped && (state & 2)) {
   1173                 cursor_x += event->motion.xrel;
   1174                 cursor_y += event->motion.yrel;
   1175             } else cursor_warped = 0;
   1176         }
   1177 #endif
   1178         queued_mouse_buttons = state;
   1179         SDL_Keymod modifiers = queued_modifiers;
   1180         state |= modifiers & KMOD_SHIFT ? 4 : 0;
   1181         state |= modifiers & KMOD_CTRL ? 8 : 0;
   1182         int input = 2;
   1183 #ifdef __EMSCRIPTEN__
   1184         if (event->motion.which == SDL_TOUCH_MOUSEID) input = 3;
   1185 #endif
   1186         queue_message(client.handle, 0x200, state,
   1187                       (event->motion.x & 0xffffu) | ((uint32_t)event->motion.y << 16), input, modifiers);
   1188     } else if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP) {
   1189         uint32_t button = event->button.button, message, mask;
   1190         if (button == SDL_BUTTON_LEFT) { message = 0x201; mask = 1; }
   1191         else if (button == SDL_BUTTON_RIGHT) { message = 0x204; mask = 2; }
   1192         else if (button == SDL_BUTTON_MIDDLE) { message = 0x207; mask = 16; }
   1193         else return;
   1194         int up = event->type == SDL_MOUSEBUTTONUP;
   1195         queued_mouse_buttons = up ? queued_mouse_buttons & ~mask : queued_mouse_buttons | mask;
   1196         uint32_t state = queued_mouse_buttons;
   1197         SDL_Keymod modifiers = queued_modifiers;
   1198         state |= modifiers & KMOD_SHIFT ? 4 : 0;
   1199         state |= modifiers & KMOD_CTRL ? 8 : 0;
   1200         int input = 2;
   1201 #ifdef __EMSCRIPTEN__
   1202         if (event->button.which == SDL_TOUCH_MOUSEID) input = 3;
   1203 #endif
   1204         queue_message(client.handle, message + up, state,
   1205                       (event->button.x & 0xffffu) | ((uint32_t)event->button.y << 16), input, modifiers);
   1206     } else if (event->type == SDL_MOUSEWHEEL) {
   1207 #ifdef __EMSCRIPTEN__
   1208         static float remainder;
   1209         float movement = event->wheel.preciseY * 120 + remainder;
   1210         int delta = (int)movement;
   1211         remainder = movement - delta;
   1212         if (!delta) return;
   1213         uint32_t state = queued_mouse_buttons | (queued_modifiers & KMOD_SHIFT ? 4 : 0) |
   1214                          (queued_modifiers & KMOD_CTRL ? 8 : 0);
   1215         queue_message(client.handle, 0x20a, ((uint32_t)delta << 16) | state,
   1216                       (event->wheel.mouseX & 0xffffu) | ((uint32_t)event->wheel.mouseY << 16), 3, queued_modifiers);
   1217 #else
   1218         int x, y;
   1219         SDL_GetGlobalMouseState(&x, &y);
   1220         int delta = event->wheel.y * 120;
   1221         if (event->wheel.direction == SDL_MOUSEWHEEL_FLIPPED) delta = -delta;
   1222         uint32_t state = queued_mouse_buttons | (queued_modifiers & KMOD_SHIFT ? 4 : 0) |
   1223                          (queued_modifiers & KMOD_CTRL ? 8 : 0);
   1224         queue_message(client.handle, 0x20a, ((uint32_t)delta << 16) | state,
   1225                       (x & 0xffffu) | ((uint32_t)y << 16), 2, queued_modifiers);
   1226 #endif
   1227     }
   1228 }
   1229 
   1230 static int read_message(uint32_t output, uint32_t window, uint32_t first, uint32_t last, int remove)
   1231 {
   1232     if (pthread_mutex_lock(&message_mutex)) abort();
   1233     struct queued_message **link = &message_head, *previous = NULL;
   1234     while (*link) {
   1235         struct queued_message *entry = *link;
   1236         uint32_t handle = entry->words[0], message = entry->words[1];
   1237         if ((!window || (window == UINT32_MAX ? !handle : handle == window)) &&
   1238             ((!first && !last) || (message >= first && message <= last))) {
   1239             memcpy((void *)ADDR(output), entry->words, sizeof(entry->words));
   1240             if (remove) {
   1241                 if (entry->keyboard == 1) {
   1242                     keyboard_state[entry->words[2]] = message & 1 ? 0 : 0x80;
   1243                     keyboard_modifiers = entry->modifiers;
   1244                 } else if (entry->keyboard == 2 || entry->keyboard == 3) {
   1245                     keyboard_modifiers = entry->modifiers;
   1246                     keyboard_state[1] = entry->words[2] & 1 ? 0x80 : 0;
   1247                     keyboard_state[2] = entry->words[2] & 2 ? 0x80 : 0;
   1248                     keyboard_state[4] = entry->words[2] & 16 ? 0x80 : 0;
   1249 #ifdef __EMSCRIPTEN__
   1250                     /* Browser input must see its captured point before later pointer motion. */
   1251                     browser_mouse = entry->keyboard == 3;
   1252                     if (browser_mouse) {
   1253                         int x = (int16_t)entry->words[3], y = (int16_t)(entry->words[3] >> 16);
   1254                         if (message == 0x200 && cursor_warped && (entry->words[2] & 2)) {
   1255                             cursor_x += x - browser_mouse_x;
   1256                             cursor_y += y - browser_mouse_y;
   1257                         } else if (message != 0x205 || !cursor_warped) {
   1258                             /* Releasing a camera drag must not add an absolute-position jump. */
   1259                             cursor_x = x; cursor_y = y;
   1260                             cursor_warped = 0;
   1261                         }
   1262                         browser_mouse_x = x; browser_mouse_y = y;
   1263                     }
   1264 #endif
   1265                 }
   1266                 *link = entry->next;
   1267                 if (message_tail == entry) message_tail = previous;
   1268                 free(entry);
   1269             }
   1270             if (pthread_mutex_unlock(&message_mutex)) abort();
   1271             return 1;
   1272         }
   1273         previous = entry;
   1274         link = &entry->next;
   1275     }
   1276     int found = quit_pending;
   1277     if (found) {
   1278         uint32_t words[] = {0, 0x12, quit_code, 0, SDL_GetTicks(), 0, 0};
   1279         memcpy((void *)ADDR(output), words, sizeof(words));
   1280         if (remove) quit_pending = 0;
   1281     }
   1282     if (pthread_mutex_unlock(&message_mutex)) abort();
   1283     return found;
   1284 }
   1285 
   1286 void linux_window_resize(unsigned width, unsigned height)
   1287 {
   1288     client.width = width;
   1289     client.height = height;
   1290     SDL_SetWindowSize(linux_graphics_window(), width, height);
   1291 }
   1292 
   1293 static void retrieve_message(int wait)
   1294 {
   1295     uint32_t output = MEM32(g_esp + 4), window = MEM32(g_esp + 8);
   1296     uint32_t first = MEM32(g_esp + 12), last = MEM32(g_esp + 16);
   1297     uint32_t flags = wait ? 1 : MEM32(g_esp + 20);
   1298     g_esp += wait ? 20 : 24;
   1299     if (flags & ~3u) unsupported("PeekMessage flags", flags);
   1300     if (window && window != UINT32_MAX && window != client.handle) {
   1301         MEM32(g_fs_base + 0x34) = 1400;
   1302         g_eax = wait ? UINT32_MAX : 0;
   1303         return;
   1304     }
   1305     for (;;) {
   1306         SDL_Event event;
   1307         if (read_message(output, window, first, last, flags & 1)) {
   1308             g_eax = !wait || MEM32(output + 4) != 0x12;
   1309             return;
   1310         }
   1311         /* Deliver translated key messages before reading later pasted text. */
   1312         if (SDL_PollEvent(&event)) {
   1313             process_event(&event);
   1314             continue;
   1315         }
   1316         if (!wait) { g_eax = 0; return; }
   1317         if (!SDL_WaitEvent(&event)) { fprintf(stderr, "%s\n", SDL_GetError()); abort(); }
   1318         process_event(&event);
   1319     }
   1320 }
   1321 
   1322 static void peek_message(void) { retrieve_message(0); }
   1323 static void get_message(void) { retrieve_message(1); }
   1324 
   1325 static void dispatch_message(void)
   1326 {
   1327     uint32_t record = MEM32(g_esp + 4), handle = MEM32(record);
   1328     g_esp += 8;
   1329     g_eax = 0;
   1330     if (handle && handle == client.handle)
   1331         g_eax = send_message(MEM32(record + 4), MEM32(record + 8), MEM32(record + 12));
   1332     else if (handle) MEM32(g_fs_base + 0x34) = 1400;
   1333 }
   1334 
   1335 /* The native input adapter currently uses the US keyboard layout. */
   1336 static int key_character(uint32_t key, int shift, int control, int alt, int caps)
   1337 {
   1338     if (control && alt) return -1;
   1339     if (key >= 'A' && key <= 'Z')
   1340         return control ? key - 'A' + 1 : key + (shift != caps ? 0 : 32);
   1341     if (control) {
   1342         if (shift) {
   1343             if (key == '2') return 0;
   1344             if (key == '6') return 30;
   1345             if (key == 0xbd) return 31;
   1346         } else {
   1347             if (key == 3 || key == 27 || key == 32) return key;
   1348             if (key == 8) return 127;
   1349             if (key == 13) return 10;
   1350             if (key >= 0xdb && key <= 0xdd) return key - 0xdb + 27;
   1351             if (key == 0xe2) return 28;
   1352         }
   1353         return -1;
   1354     }
   1355     if (key >= '0' && key <= '9') return shift ? ")!@#$%^&*("[key - '0'] : (int)key;
   1356     if (key == 3 || key == 8 || key == 9 || key == 13 || key == 27 || key == 32) return key;
   1357     if (key >= 0x60 && key <= 0x69) return shift ? -1 : (int)key - 0x60 + '0';
   1358     if (key == 0x6a) return '*';
   1359     if (key == 0x6b) return '+';
   1360     if (key >= 0x6d && key <= 0x6f) return key - 0x6d + '-';
   1361     const char *plain = ";=,-./`[\\]'\\", *shifted = ":+<_>?~{|}\"|";
   1362     static const uint8_t codes[] = {0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xdb,0xdc,0xdd,0xde,0xe2};
   1363     for (unsigned i = 0; i < sizeof(codes); ++i)
   1364         if (key == codes[i]) return (shift ? shifted : plain)[i];
   1365     return -1;
   1366 }
   1367 
   1368 static void to_ascii(void)
   1369 {
   1370     uint32_t key = MEM32(g_esp + 4), scan = MEM32(g_esp + 8);
   1371     uint32_t state = MEM32(g_esp + 12), output = MEM32(g_esp + 16);
   1372     const uint8_t *keys = state ? (const uint8_t *)ADDR(state) : keyboard_state;
   1373     int character = scan & 0x8000 ? -1 : key_character(key, !!(keys[16] & 128),
   1374         !!(keys[17] & 128), !!(keys[18] & 128), !!(keys[20] & 1));
   1375     g_eax = character >= 0;
   1376     if (character >= 0) MEM8(output) = character;
   1377     g_esp += 24;
   1378 }
   1379 
   1380 static void translate_message(void)
   1381 {
   1382     uint32_t record = MEM32(g_esp + 4), message = MEM32(record + 4), key = MEM32(record + 8);
   1383     g_esp += 8;
   1384     g_eax = message == 0x100 || message == 0x101 || message == 0x104 || message == 0x105;
   1385     if (message != 0x100 && message != 0x104) return;
   1386     SDL_Keymod modifiers = keyboard_modifiers;
   1387     int character = key_character(key, !!(modifiers & KMOD_SHIFT), !!(modifiers & KMOD_CTRL),
   1388         !!(modifiers & KMOD_ALT), !!(modifiers & KMOD_CAPS));
   1389     if (character >= 0) queue_message(MEM32(record), message == 0x104 ? 0x106 : 0x102,
   1390                                       character, MEM32(record + 12), 0, 0);
   1391 }
   1392 
   1393 static void get_keyboard_state(void)
   1394 {
   1395     uint32_t output = MEM32(g_esp + 4);
   1396     SDL_Keymod modifiers = keyboard_modifiers;
   1397     keyboard_state[16] = modifiers & KMOD_SHIFT ? 0x80 : 0;
   1398     keyboard_state[17] = modifiers & KMOD_CTRL ? 0x80 : 0;
   1399     keyboard_state[18] = modifiers & KMOD_ALT ? 0x80 : 0;
   1400     keyboard_state[20] = modifiers & KMOD_CAPS ? 1 : 0;
   1401     keyboard_state[0x90] = modifiers & KMOD_NUM ? 1 : 0;
   1402     memcpy((void *)ADDR(output), keyboard_state, sizeof(keyboard_state));
   1403     g_eax = 1;
   1404     g_esp += 8;
   1405 }
   1406 
   1407 static void register_class_a(void) { register_class(0); }
   1408 static void register_class_w(void) { register_class(1); }
   1409 static void unregister_class_a(void) { unregister_class(0); }
   1410 static void unregister_class_w(void) { unregister_class(1); }
   1411 static void create_window_a(void) { create_window(0); }
   1412 static void create_window_w(void) { create_window(1); }
   1413 
   1414 recomp_func_t linux_window_lookup(const char *dll, const char *name)
   1415 {
   1416     if (!strcasecmp(dll, "gdi32.dll")) {
   1417         if (!strcmp(name, "GetStockObject")) return stock_object;
   1418         if (!strcmp(name, "GetDeviceCaps")) return device_caps;
   1419         return NULL;
   1420     }
   1421     if (strcasecmp(dll, "user32.dll")) return NULL;
   1422     if (!strcmp(name, "PeekMessageA") || !strcmp(name, "PeekMessageW")) return peek_message;
   1423     if (!strcmp(name, "GetMessageA") || !strcmp(name, "GetMessageW")) return get_message;
   1424     if (!strcmp(name, "DispatchMessageA") || !strcmp(name, "DispatchMessageW")) return dispatch_message;
   1425     if (!strcmp(name, "PostMessageA") || !strcmp(name, "PostMessageW")) return post_message;
   1426     if (!strcmp(name, "PostQuitMessage")) return post_quit_message;
   1427     if (!strcmp(name, "TranslateMessage")) return translate_message;
   1428     if (!strcmp(name, "GetKeyboardState")) return get_keyboard_state;
   1429     if (!strcmp(name, "ToAscii")) return to_ascii;
   1430     if (!strcmp(name, "LoadIconA")) return load_icon;
   1431     if (!strcmp(name, "RegisterClassA")) return register_class_a;
   1432     if (!strcmp(name, "RegisterClassW")) return register_class_w;
   1433     if (!strcmp(name, "UnregisterClassA")) return unregister_class_a;
   1434     if (!strcmp(name, "UnregisterClassW")) return unregister_class_w;
   1435     if (!strcmp(name, "CreateWindowExA")) return create_window_a;
   1436     if (!strcmp(name, "CreateWindowExW")) return create_window_w;
   1437     if (!strcmp(name, "DefWindowProcA") || !strcmp(name, "DefWindowProcW")) return default_procedure;
   1438     if (!strcmp(name, "DestroyWindow")) return destroy_window;
   1439     if (!strcmp(name, "ShowWindow")) return show_window;
   1440     if (!strcmp(name, "SetFocus")) return set_focus;
   1441     if (!strcmp(name, "GetFocus")) return get_focus;
   1442     if (!strcmp(name, "SetWindowLongA") || !strcmp(name, "SetWindowLongW")) return set_window_long;
   1443     if (!strcmp(name, "SetClassLongA") || !strcmp(name, "SetClassLongW")) return set_class_long;
   1444     if (!strcmp(name, "GetWindowLongA") || !strcmp(name, "GetWindowLongW")) return get_window_long;
   1445     if (!strcmp(name, "AdjustWindowRectEx")) return adjust_window_rect;
   1446     if (!strcmp(name, "GetSystemMetrics")) return system_metrics;
   1447     if (!strcmp(name, "MoveWindow")) return move_window;
   1448     if (!strcmp(name, "SetWindowPos")) return set_window_position;
   1449     if (!strcmp(name, "GetClientRect")) return client_rect;
   1450     if (!strcmp(name, "GetWindowRect")) return window_rect;
   1451     if (!strcmp(name, "ClientToScreen")) return client_to_screen;
   1452     if (!strcmp(name, "GetCursorPos")) return cursor_position;
   1453     if (!strcmp(name, "SetCursorPos")) return set_cursor_position;
   1454     if (!strcmp(name, "SetCursor")) return set_cursor;
   1455     if (!strcmp(name, "SetCapture")) return set_capture;
   1456     if (!strcmp(name, "GetCapture")) return get_capture;
   1457     if (!strcmp(name, "ReleaseCapture")) return release_capture;
   1458     if (!strcmp(name, "GetKeyState")) return key_state;
   1459     if (!strcmp(name, "GetDesktopWindow")) return desktop_window;
   1460     if (!strcmp(name, "GetDC")) return get_dc;
   1461     if (!strcmp(name, "ReleaseDC")) return release_dc;
   1462     if (!strcmp(name, "SystemParametersInfoA")) return system_parameters;
   1463     return NULL;
   1464 }