client/tests/window.c (39262B)
1 #include <assert.h> 2 #include <ctype.h> 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <string.h> 6 #ifdef _WIN32 7 #include <windows.h> 8 _Static_assert(sizeof(WNDCLASSA) == 40, "guest window class size"); 9 _Static_assert(sizeof(CREATESTRUCTA) == 48, "guest creation record size"); 10 static uint32_t scratch[256]; 11 uint32_t guest_map(const char *name, uint32_t base, uint32_t size) 12 { 13 assert(VirtualAlloc((void *)(uintptr_t)base, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE) == (void *)(uintptr_t)base); 14 return size; 15 } 16 #else 17 #include "linux_window.h" 18 #include "linux_graphics.h" 19 #include "guest_memory.h" 20 #include <SDL2/SDL.h> 21 #include <SDL2/SDL_syswm.h> 22 #include <pthread.h> 23 static void procedure(void); 24 recomp_func_t recomp_lookup(uint32_t address) { assert(address == 0x11110000u); return procedure; } 25 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 26 #endif 27 void game_data_Clientr_exe(void); 28 29 static uint32_t instance, callback, atom, args[12], messages[32], message_count, reject; 30 static int wide; 31 static int capture_focus, clear_in_callback; 32 static int capture_geometry; 33 static int capture_visibility; 34 static unsigned mouse_capture_changes; 35 static unsigned queue_dispatches; 36 37 static uint32_t workspace(void) 38 { 39 #ifdef _WIN32 40 return (uintptr_t)scratch; 41 #else 42 return 0x50001000u; 43 #endif 44 } 45 46 static uint32_t *memory(uint32_t address) 47 { 48 #ifdef _WIN32 49 return (void *)(uintptr_t)address; 50 #else 51 return (void *)ADDR(address); 52 #endif 53 } 54 55 static uint32_t call(const char *name, unsigned count, const uint32_t *arguments) 56 { 57 char symbol[32]; 58 const char *dll = !strcmp(name, "GetStockObject") || !strcmp(name, "GetDeviceCaps") ? "gdi32.dll" : "user32.dll"; 59 strcpy(symbol, name); 60 if (wide && strcmp(name, "LoadIconA") && symbol[strlen(symbol) - 1] == 'A') symbol[strlen(symbol) - 1] = 'W'; 61 #ifdef _WIN32 62 HMODULE module = GetModuleHandleA(dll); 63 FARPROC function = GetProcAddress(module, symbol); 64 assert(function); 65 if (!count) return ((uint32_t (WINAPI *)(void))function)(); 66 if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))function)(arguments[0]); 67 if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))function)(arguments[0], arguments[1]); 68 if (count == 3) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))function)(arguments[0], arguments[1], arguments[2]); 69 if (count == 4) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))function)(arguments[0], arguments[1], arguments[2], arguments[3]); 70 if (count == 5) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))function)(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]); 71 if (count == 6) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))function)(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]); 72 if (count == 7) return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))function)(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]); 73 assert(count == 12); 74 return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, 75 uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))function)( 76 arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], 77 arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11]); 78 #else 79 uint32_t stack = g_esp, caller = g_cur_func; 80 uint32_t ebx = g_ebx, esi = g_esi, edi = g_edi, ebp = g_ebp; 81 g_esp -= (count + 1) * 4; 82 MEM32(g_esp) = RECOMP_RETADDR; 83 for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i]; 84 recomp_func_t function = linux_window_lookup(dll, symbol); 85 assert(function); 86 function(); 87 assert(g_esp == stack && g_cur_func == caller); 88 assert(g_ebx == ebx && g_esi == esi && g_edi == edi && g_ebp == ebp); 89 return g_eax; 90 #endif 91 } 92 93 static void write_text(uint32_t address, const char *text) 94 { 95 if (!wide) strcpy((char *)memory(address), text); 96 else for (size_t i = 0; i <= strlen(text); ++i) ((uint16_t *)memory(address))[i] = text[i]; 97 } 98 99 static void check_text(uint32_t address, const char *text) 100 { 101 if (!wide) assert(!strcmp((const char *)memory(address), text)); 102 else for (size_t i = 0; i <= strlen(text); ++i) assert(((uint16_t *)memory(address))[i] == (unsigned char)text[i]); 103 } 104 105 static uint32_t error(void) 106 { 107 #ifdef _WIN32 108 return GetLastError(); 109 #else 110 return MEM32(g_fs_base + 0x34); 111 #endif 112 } 113 114 static uint32_t receive(uint32_t handle, uint32_t message, uint32_t wparam, uint32_t lparam) 115 { 116 if (message == 0x215) { 117 ++mouse_capture_changes; 118 assert(!wparam && call("GetCapture", 0, NULL) == lparam); 119 } 120 if (message == 0x8001 || message == 0x8002) { 121 ++queue_dispatches; 122 return wparam ^ lparam; 123 } 124 if (message == 0x24 || message == 0x81 || message == 0x83 || 125 message == 1 || message == 2 || message == 0x82 || 126 (capture_visibility && (message == 0x18 || message == 0x46 || message == 0x47 || message == 3 || message == 5)) || 127 (capture_geometry && (message == 0x7c || message == 0x7d || message == 0x46 || message == 0x47 || message == 3 || message == 5)) || 128 (capture_focus && (message == 0x46 || message == 0x1c || message == 0x86 || 129 message == 6 || message == 7 || message == 8))) { 130 assert(message_count < 32); 131 messages[message_count++] = message; 132 } 133 if (capture_geometry && (message == 0x7c || message == 0x7d)) { 134 assert(wparam == 0xfffffff0u); 135 assert(memory(lparam)[0] == 0x04c00000u); 136 assert(memory(lparam)[1] == (message == 0x7c ? 0x00ca0000u : 0x04ca0000u)); 137 } 138 if (capture_visibility && message == 0x18) { 139 uint32_t query[] = {handle, 0xfffffff0u}; 140 assert(!lparam && !!(call("GetWindowLongA", 2, query) & 0x10000000u) != !!wparam); 141 } 142 if (capture_geometry && message == 5) { 143 assert(!wparam && lparam == 0x01e00280u); 144 uint32_t query[] = {handle, workspace() + 256}; 145 assert(call("GetClientRect", 2, query)); 146 assert(memory(query[1])[2] == 640 && memory(query[1])[3] == 480); 147 } 148 if (message == 7 || message == 8) { 149 assert(!lparam); 150 assert(call("GetFocus", 0, NULL) == (message == 7 ? handle : wparam)); 151 if (message == 7 && clear_in_callback) { 152 uint32_t none = 0; 153 assert(call("SetFocus", 1, &none) == handle); 154 } 155 } 156 if (message == 0x81 || message == 1) { 157 uint32_t *record = memory(lparam); 158 assert(handle && !wparam); 159 assert(record[0] == 0x12345678u && record[1] == instance); 160 assert(!record[2] && !record[3]); 161 assert(record[4] == 240 && record[5] == 320 && record[6] == 60 && record[7] == 50); 162 assert(!record[8] && !record[11]); 163 check_text(record[9], "AO window test"); 164 if (record[10] <= 0xffffu) assert(record[10] == atom); 165 else for (unsigned i = 0; i < 14; ++i) { 166 unsigned character = wide ? ((uint16_t *)memory(record[10]))[i] : ((unsigned char *)memory(record[10]))[i]; 167 assert(character < 128 && tolower(character) == "ao class test"[i]); 168 } 169 if (reject == message) return message == 0x81 ? 0 : 0xffffffffu; 170 } 171 uint32_t forwarded[] = {handle, message, wparam, lparam}; 172 return call("DefWindowProcA", 4, forwarded); 173 } 174 175 #ifdef _WIN32 176 static LRESULT CALLBACK procedure(HWND handle, UINT message, WPARAM wparam, LPARAM lparam) 177 { 178 return receive((uintptr_t)handle, message, wparam, lparam); 179 } 180 #else 181 static void procedure(void) 182 { 183 g_cur_func = 0x11110000u; 184 g_eax = receive(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16)); 185 g_esp += 20; 186 } 187 #endif 188 189 static void class_registration_owns_its_name(void) 190 { 191 uint32_t icon_args[] = {0, 32512}, brush_arg = 4; 192 uint32_t *record = memory(workspace()); 193 record[0] = 8; record[1] = callback; record[4] = instance; 194 record[5] = call("LoadIconA", 2, icon_args); 195 record[7] = call("GetStockObject", 1, &brush_arg); 196 record[9] = workspace() + 64; 197 write_text(record[9], "AO class test"); 198 uint32_t address = workspace(); 199 200 atom = call("RegisterClassA", 1, &address); 201 uint32_t duplicate = call("RegisterClassA", 1, &address), duplicate_error = error(); 202 memset(memory(record[9]), 'x', 13); 203 write_text(workspace() + 96, "aO ClAsS TeSt"); 204 205 assert(atom && atom <= 0xffffu && record[5] && record[7]); 206 assert(!duplicate && duplicate_error == 1410); 207 } 208 209 static void create_and_destroy_dispatch_guest_messages(void) 210 { 211 args[1] = workspace() + 96; 212 args[2] = workspace() + 128; 213 write_text(args[2], "AO window test"); 214 args[4] = 50; args[5] = 60; args[6] = 320; args[7] = 240; 215 args[10] = instance; args[11] = 0x12345678u; 216 uint32_t unregister_args[] = {atom, instance}; 217 218 uint32_t window = call("CreateWindowExA", 12, args); 219 uint32_t busy = call("UnregisterClassA", 2, unregister_args), busy_error = error(); 220 #ifndef _WIN32 221 uint32_t query[] = {window, workspace() + 192}; 222 assert(call("GetClientRect", 2, query)); 223 assert(memory(query[1])[2] == 320 && memory(query[1])[3] == 240); 224 assert(!strcmp(SDL_GetWindowTitle(linux_graphics_window()), "AO window test")); 225 assert(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_HIDDEN); 226 assert(!(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_FULLSCREEN)); 227 #endif 228 uint32_t destroyed = call("DestroyWindow", 1, &window); 229 230 assert(window && destroyed && !busy && busy_error == 1412); 231 static const uint32_t expected[] = {0x24, 0x81, 0x83, 1, 2, 0x82}; 232 assert(message_count == 6 && !memcmp(messages, expected, sizeof(expected))); 233 } 234 235 static void rejected_creation_releases_the_window(void) 236 { 237 args[1] = atom; 238 reject = 0x81; 239 message_count = 0; 240 241 uint32_t rejected_nonclient = call("CreateWindowExA", 12, args); 242 243 static const uint32_t expected_nc[] = {0x24, 0x81, 0x82}; 244 assert(!rejected_nonclient && message_count == 3); 245 assert(!memcmp(messages, expected_nc, sizeof(expected_nc))); 246 247 reject = 1; 248 message_count = 0; 249 uint32_t rejected_client = call("CreateWindowExA", 12, args); 250 static const uint32_t expected_create[] = {0x24, 0x81, 0x83, 1, 2, 0x82}; 251 assert(!rejected_client && message_count == 6); 252 assert(!memcmp(messages, expected_create, sizeof(expected_create))); 253 } 254 255 static void focus_activation_and_reentrant_callbacks(void) 256 { 257 reject = 0; 258 uint32_t window = call("CreateWindowExA", 12, args), none = 0; 259 assert(window); 260 capture_focus = 1; 261 message_count = 0; 262 263 uint32_t previous = call("SetFocus", 1, &window); 264 uint32_t focused = call("GetFocus", 0, NULL); 265 uint32_t same = call("SetFocus", 1, &window); 266 267 static const uint32_t activation[] = {0x46, 0x1c, 0x86, 6, 7, 8, 7}; 268 assert(previous == window && focused == window && same == window); 269 assert(message_count == 7 && !memcmp(messages, activation, sizeof(activation))); 270 271 /* Clearing thread focus keeps activation, and a nested callback can clear it again. */ 272 message_count = 0; 273 assert(call("SetFocus", 1, &none) == window); 274 assert(!call("GetFocus", 0, NULL)); 275 clear_in_callback = 1; 276 assert(!call("SetFocus", 1, &window)); 277 clear_in_callback = 0; 278 assert(!call("GetFocus", 0, NULL)); 279 static const uint32_t reentrant[] = {8, 7, 8}; 280 assert(message_count == 3 && !memcmp(messages, reentrant, sizeof(reentrant))); 281 282 /* Invalid handles leave focus intact; destruction clears it and deactivates. */ 283 assert(!call("SetFocus", 1, &window)); 284 uint32_t invalid = 0x12345678u; 285 assert(!call("SetFocus", 1, &invalid) && error() == 1400); 286 assert(call("GetFocus", 0, NULL) == window); 287 message_count = 0; 288 assert(call("DestroyWindow", 1, &window)); 289 assert(!call("GetFocus", 0, NULL)); 290 static const uint32_t destruction[] = {0x86, 6, 0x1c, 8, 2, 0x82}; 291 /* Desktop activation can already be lost before destruction is dispatched. */ 292 assert(message_count == 2 || message_count == 6); 293 assert(!memcmp(messages, destruction + (message_count == 2 ? 4 : 0), message_count * 4)); 294 capture_focus = 0; 295 } 296 297 static void visibility_dispatches_callbacks_and_preserves_geometry(void) 298 { 299 uint32_t window = call("CreateWindowExA", 12, args); 300 assert(window); 301 call("SetFocus", 1, &window); 302 uint32_t show[] = {window, 10}, query[] = {window, workspace() + 256}; 303 assert(call("GetClientRect", 2, query)); 304 uint32_t original[4]; 305 memcpy(original, memory(query[1]), sizeof(original)); 306 capture_visibility = 1; 307 message_count = 0; 308 309 uint32_t previous = call("ShowWindow", 2, show); 310 311 const uint32_t first[] = {0x18, 0x46, 0x47, 5, 3}; 312 assert(!previous && message_count == 5 && !memcmp(messages, first, sizeof(first))); 313 assert(call("GetClientRect", 2, query) && !memcmp(original, memory(query[1]), sizeof(original))); 314 uint32_t style[] = {window, 0xfffffff0u}; 315 assert(call("GetWindowLongA", 2, style) & 0x10000000u); 316 #ifndef _WIN32 317 assert(!(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_HIDDEN)); 318 #endif 319 message_count = 0; 320 show[1] = 5; 321 assert(call("ShowWindow", 2, show) && !message_count); 322 assert(call("GetWindowRect", 2, query)); 323 uint32_t position[] = {window, 0, memory(query[1])[0] + 40, memory(query[1])[1] + 30, 0, 0, 5}; 324 assert(call("SetWindowPos", 7, position)); 325 const uint32_t moved[] = {0x46, 0x47, 3}; 326 assert(message_count == 3 && !memcmp(messages, moved, sizeof(moved))); 327 assert(call("GetClientRect", 2, query) && !memcmp(original, memory(query[1]), sizeof(original))); 328 assert(call("GetWindowRect", 2, query)); 329 #ifdef _WIN32 330 assert(memory(query[1])[0] == position[2] && memory(query[1])[1] == position[3]); 331 #endif 332 position[2] = memory(query[1])[0]; position[3] = memory(query[1])[1]; 333 message_count = 0; 334 assert(call("SetWindowPos", 7, position)); 335 assert(message_count == 1 && messages[0] == 0x46); 336 message_count = 0; 337 show[1] = 0; 338 assert(call("ShowWindow", 2, show)); 339 const uint32_t changed[] = {0x18, 0x46, 0x47}; 340 assert(message_count == 3 && !memcmp(messages, changed, sizeof(changed))); 341 assert(!(call("GetWindowLongA", 2, style) & 0x10000000u)); 342 assert(!call("GetFocus", 0, NULL)); 343 #ifndef _WIN32 344 assert(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_HIDDEN); 345 #endif 346 message_count = 0; 347 assert(!call("ShowWindow", 2, show) && !message_count); 348 show[1] = 1; 349 assert(!call("ShowWindow", 2, show)); 350 const uint32_t restored[] = {0x18, 0x46, 0x46, 0x47}; 351 /* The Windows desktop may deny foreground activation on a repeated show. */ 352 assert((message_count == 3 && !memcmp(messages, changed, sizeof(changed))) || 353 (message_count == 4 && !memcmp(messages, restored, sizeof(restored)))); 354 #ifndef _WIN32 355 assert(call("GetFocus", 0, NULL) == window); 356 #endif 357 capture_visibility = 0; 358 show[1] = 0; 359 assert(call("ShowWindow", 2, show)); 360 assert(call("DestroyWindow", 1, &window)); 361 assert(!call("ShowWindow", 2, show) && error() == 1400); 362 memset(memory(query[1]), 0xa5, 20); 363 assert(!call("GetWindowRect", 2, query) && error() == 1400); 364 assert(!call("SetWindowPos", 7, position) && error() == 1400); 365 for (unsigned i = 0; i < 5; ++i) assert(memory(query[1])[i] == 0xa5a5a5a5); 366 } 367 368 static void window_geometry_preserves_requested_client_area(void) 369 { 370 uint32_t window = call("CreateWindowExA", 12, args); 371 uint32_t style[] = {window, 0xfffffff0u, 0x00ca0000u}; 372 assert(window); 373 capture_geometry = 1; 374 message_count = 0; 375 uint32_t rectangle = workspace() + 192; 376 memory(rectangle)[0] = 0; memory(rectangle)[1] = 0; 377 memory(rectangle)[2] = 640; memory(rectangle)[3] = 480; 378 379 uint32_t previous = call("SetWindowLongA", 3, style); 380 uint32_t current = call("GetWindowLongA", 2, style); 381 style[1] = 0xffffffecu; 382 uint32_t extended = call("GetWindowLongA", 2, style); 383 uint32_t adjust[] = {rectangle, current, 0, extended}; 384 assert(call("AdjustWindowRectEx", 4, adjust)); 385 uint32_t move[] = {window, 20, 30, memory(rectangle)[2] - memory(rectangle)[0], memory(rectangle)[3] - memory(rectangle)[1], 0}; 386 assert(call("MoveWindow", 6, move)); 387 uint32_t query[] = {window, rectangle}; 388 assert(call("GetClientRect", 2, query)); 389 390 assert(previous == 0x04c00000u && current == 0x04ca0000u && extended == 0x100); 391 assert(!memory(rectangle)[0] && !memory(rectangle)[1]); 392 assert(memory(rectangle)[2] == 640 && memory(rectangle)[3] == 480); 393 static const uint32_t expected[] = {0x7c, 0x7d, 0x46, 0x24, 0x83, 0x47, 3, 5}; 394 assert(message_count == 8 && !memcmp(messages, expected, sizeof(expected))); 395 memory(rectangle)[4] = 0xa5a5a5a5; 396 assert(call("GetWindowRect", 2, query)); 397 uint32_t window_x = memory(rectangle)[0], window_y = memory(rectangle)[1]; 398 #ifdef _WIN32 399 assert(window_x == 20 && window_y == 30); 400 #endif 401 assert(memory(rectangle)[2] == window_x + move[3] && memory(rectangle)[3] == window_y + move[4]); 402 assert(memory(rectangle)[4] == 0xa5a5a5a5); 403 memory(rectangle)[0] = 0; memory(rectangle)[1] = 0; 404 assert(call("ClientToScreen", 2, query)); 405 uint32_t origin_x = memory(rectangle)[0], origin_y = memory(rectangle)[1]; 406 memory(rectangle)[0] = 13; memory(rectangle)[1] = 17; 407 assert(call("ClientToScreen", 2, query)); 408 assert(memory(rectangle)[0] == origin_x + 13 && memory(rectangle)[1] == origin_y + 17); 409 uint32_t index = 0; 410 assert(call("GetSystemMetrics", 1, &index) >= 640); 411 index = 1; 412 assert(call("GetSystemMetrics", 1, &index) >= 480); 413 #ifndef _WIN32 414 assert(!(SDL_GetWindowFlags(linux_graphics_window()) & SDL_WINDOW_BORDERLESS)); 415 int x, y; 416 SDL_GetWindowPosition(linux_graphics_window(), &x, &y); 417 assert(origin_x == (uint32_t)x && origin_y == (uint32_t)y); 418 assert(window_x == origin_x && window_y == origin_y); 419 /* SDL may defer a hidden native resize until the window is mapped. */ 420 SDL_ShowWindow(linux_graphics_window()); 421 SDL_PumpEvents(); 422 int width, height; 423 SDL_GetWindowSize(linux_graphics_window(), &width, &height); 424 assert(width == 640 && height == 480); 425 #endif 426 capture_geometry = 0; 427 assert(call("DestroyWindow", 1, &window)); 428 } 429 430 static void reconstructed_icon_matches_native_window_pixels(void) 431 { 432 uint32_t icon_args[] = {0x28000000u, 120}, default_args[] = {0, 32512}; 433 uint32_t window = call("CreateWindowExA", 12, args); 434 assert(window); 435 uint32_t icon = call("LoadIconA", 2, icon_args), expected_previous = call("LoadIconA", 2, default_args); 436 assert(icon && call("LoadIconA", 2, icon_args) == icon); 437 uint32_t set[] = {window, 0xfffffff2u, icon}; 438 uint32_t rgba[1024]; 439 440 uint32_t previous = call("SetClassLongA", 3, set); 441 #ifdef _WIN32 442 BITMAPINFO info = {0}; 443 info.bmiHeader.biSize = 40; info.bmiHeader.biWidth = 32; info.bmiHeader.biHeight = -32; 444 info.bmiHeader.biPlanes = 1; info.bmiHeader.biBitCount = 32; 445 uint32_t *pixels; 446 HDC dc = CreateCompatibleDC(NULL); 447 HBITMAP bitmap = CreateDIBSection(dc, &info, DIB_RGB_COLORS, (void **)&pixels, NULL, 0); 448 assert(dc && bitmap); 449 HGDIOBJ original = SelectObject(dc, bitmap); 450 memset(pixels, 0, sizeof(rgba)); 451 assert(DrawIconEx(dc, 0, 0, (HICON)(uintptr_t)icon, 32, 32, 0, NULL, DI_NORMAL)); 452 GdiFlush(); 453 memcpy(rgba, pixels, sizeof(rgba)); 454 memset(pixels, 0xff, sizeof(rgba)); 455 assert(DrawIconEx(dc, 0, 0, (HICON)(uintptr_t)icon, 32, 32, 0, NULL, DI_NORMAL)); 456 GdiFlush(); 457 for (unsigned i = 0; i < 1024; ++i) { 458 uint32_t rgb = rgba[i] & 0xffffff; 459 assert((pixels[i] & 0xffffff) == rgb || (pixels[i] & 0xffffff) == (rgb ^ 0xffffff)); 460 rgba[i] = rgb | ((pixels[i] & 0xffffff) == rgb ? 0xff000000u : 0); 461 } 462 SelectObject(dc, original); DeleteObject(bitmap); DeleteDC(dc); 463 #else 464 SDL_SysWMinfo info; 465 SDL_VERSION(&info.version); 466 assert(SDL_GetWindowWMInfo(linux_graphics_window(), &info) && info.subsystem == SDL_SYSWM_X11); 467 Atom property = XInternAtom(info.info.x11.display, "_NET_WM_ICON", False), type; 468 int format; 469 unsigned long count, remaining; 470 unsigned char *pixels; 471 assert(!XGetWindowProperty(info.info.x11.display, info.info.x11.window, property, 472 0, 1026, False, AnyPropertyType, &type, &format, &count, &remaining, &pixels)); 473 assert(format == 32 && count == 1026 && !remaining); 474 unsigned long *values = (void *)pixels; 475 assert(values[0] == 32 && values[1] == 32); 476 for (unsigned i = 0; i < 1024; ++i) rgba[i] = values[i + 2]; 477 XFree(pixels); 478 #endif 479 480 assert(previous == expected_previous && call("SetClassLongA", 3, set) == icon); 481 uint32_t digest = 2166136261u, transparent = 0; 482 for (unsigned i = 0; i < 1024; ++i) { 483 transparent += rgba[i] >> 24 == 0; 484 for (unsigned byte = 0; byte < 4; ++byte) digest = (digest ^ ((rgba[i] >> (byte * 8)) & 255)) * 16777619u; 485 } 486 assert(digest == 0x0f22d6ecu && transparent == 646); 487 printf("icon: 1024 ARGB pixels, digest=%08x, transparent=%u\n", digest, transparent); 488 icon_args[1] = 121; 489 uint32_t missing = call("LoadIconA", 2, icon_args), missing_error = error(); 490 assert(!missing && missing_error == 1813); 491 assert(call("DestroyWindow", 1, &window)); 492 assert(!call("SetClassLongA", 3, set) && error() == 1400); 493 } 494 495 static void class_release_invalidates_its_atom(void) 496 { 497 uint32_t unregister_args[] = {atom, instance}; 498 499 uint32_t released = call("UnregisterClassA", 2, unregister_args); 500 uint32_t missing = call("UnregisterClassA", 2, unregister_args), missing_error = error(); 501 uint32_t window = call("CreateWindowExA", 12, args), create_error = error(); 502 503 assert(released && !missing && missing_error == 6); 504 assert(!window && create_error == 6); 505 } 506 507 static void mouse_capture_reports_ownership_and_releases_on_destruction(void) 508 { 509 message_count = 0; 510 uint32_t handle = call("CreateWindowExA", 12, args), null = 0; 511 assert(handle && !call("GetCapture", 0, NULL)); 512 mouse_capture_changes = 0; 513 514 assert(!call("SetCapture", 1, &handle)); 515 assert(call("GetCapture", 0, NULL) == handle); 516 assert(call("SetCapture", 1, &handle) == handle); 517 assert(mouse_capture_changes == 1); 518 assert(call("SetCapture", 1, &null) == handle); 519 assert(!call("GetCapture", 0, NULL) && mouse_capture_changes == 2); 520 assert(!call("SetCapture", 1, &handle)); 521 assert(call("ReleaseCapture", 0, NULL)); 522 assert(!call("GetCapture", 0, NULL) && mouse_capture_changes == 3); 523 assert(call("ReleaseCapture", 0, NULL)); 524 assert(!call("SetCapture", 1, &handle)); 525 assert(call("DestroyWindow", 1, &handle)); 526 527 assert(!call("GetCapture", 0, NULL) && mouse_capture_changes == 3); 528 } 529 530 static void cursor_query_writes_a_guest_point(void) 531 { 532 uint32_t output = workspace() + 256, null = 0; 533 memset(memory(output - 4), 0xa5, 16); 534 #ifdef _WIN32 535 SetLastError(77); 536 #else 537 MEM32(g_fs_base + 0x34) = 77; 538 SDL_SysWMinfo info; 539 SDL_VERSION(&info.version); 540 assert(SDL_GetWindowWMInfo(linux_graphics_window(), &info) && info.subsystem == SDL_SYSWM_X11); 541 Window root, child; 542 int before_x, before_y, after_x, after_y, window_x, window_y; 543 unsigned buttons; 544 assert(XQueryPointer(info.info.x11.display, info.info.x11.window, &root, &child, 545 &before_x, &before_y, &window_x, &window_y, &buttons)); 546 #endif 547 548 uint32_t result = call("GetCursorPos", 1, &output); 549 550 assert(result && error() == 77); 551 assert(memory(output)[0] != 0xa5a5a5a5 && memory(output)[1] != 0xa5a5a5a5); 552 assert(*memory(output - 4) == 0xa5a5a5a5 && *memory(output + 8) == 0xa5a5a5a5); 553 #ifndef _WIN32 554 assert(XQueryPointer(info.info.x11.display, info.info.x11.window, &root, &child, 555 &after_x, &after_y, &window_x, &window_y, &buttons)); 556 /* A physical mouse movement during the two queries is not an adapter failure. */ 557 if (before_x == after_x && before_y == after_y) 558 assert((int32_t)memory(output)[0] == before_x && (int32_t)memory(output)[1] == before_y); 559 #endif 560 assert(!call("GetCursorPos", 1, &null) && error() == 998); 561 } 562 563 static void cursor_warp_updates_screen_coordinates(void) 564 { 565 uint32_t output = workspace() + 256; 566 assert(call("GetCursorPos", 1, &output)); 567 uint32_t original[] = {memory(output)[0], memory(output)[1]}; 568 uint32_t target[] = {123, 97}; 569 570 assert(call("SetCursorPos", 2, target)); 571 assert(call("GetCursorPos", 1, &output)); 572 573 assert(memory(output)[0] == 123 && memory(output)[1] == 97); 574 assert(call("SetCursorPos", 2, original)); 575 } 576 577 static void null_cursor_hides_and_previous_cursor_can_be_restored(void) 578 { 579 uint32_t null = 0; 580 #ifdef _WIN32 581 SetLastError(77); 582 #else 583 MEM32(g_fs_base + 0x34) = 77; 584 assert(SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE); 585 #endif 586 587 uint32_t previous = call("SetCursor", 1, &null); 588 uint32_t repeated = call("SetCursor", 1, &null); 589 590 assert(!repeated && error() == 77); 591 #ifdef _WIN32 592 assert(GetCursor() == NULL); 593 #else 594 assert(previous && SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE); 595 #endif 596 assert(!call("SetCursor", 1, &previous)); 597 assert(call("SetCursor", 1, &previous) == previous && error() == 77); 598 #ifdef _WIN32 599 assert(GetCursor() == (HCURSOR)(uintptr_t)previous); 600 #else 601 assert(SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE); 602 assert(SDL_GetCursor() == SDL_GetDefaultCursor()); 603 #endif 604 } 605 606 static void modifier_queries_combine_left_and_right_keys(void) 607 { 608 #ifdef _WIN32 609 BYTE original[256], state[256] = {0}; 610 assert(GetKeyboardState(original)); 611 #else 612 uint32_t window = call("CreateWindowExA", 12, args); 613 SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); 614 uint32_t peek[] = {workspace() + 512, window, 0x100, 0x105, 1}; 615 const SDL_Keymod modifiers[] = {KMOD_LSHIFT, KMOD_RSHIFT, KMOD_LCTRL, KMOD_RCTRL, KMOD_LALT, KMOD_RALT}; 616 #endif 617 for (unsigned pressed = 0; pressed < 64; ++pressed) { 618 #ifdef _WIN32 619 state[VK_SHIFT] = pressed & 3 ? 0x80 : 0; 620 state[VK_CONTROL] = pressed & 12 ? 0x80 : 0; 621 state[VK_MENU] = pressed & 48 ? 0x80 : 0; 622 assert(SetKeyboardState(state)); 623 #else 624 SDL_Keymod state = KMOD_NONE; 625 for (unsigned i = 0; i < 6; ++i) if (pressed & (1u << i)) state |= modifiers[i]; 626 SDL_Event event = {.type = SDL_KEYUP}; 627 event.key.keysym.sym = SDLK_w; 628 event.key.keysym.mod = state; 629 assert(SDL_PushEvent(&event) == 1); 630 assert(call("PeekMessageA", 5, peek)); 631 #endif 632 633 uint32_t key = 0x10, shift = call("GetKeyState", 1, &key); 634 key = 0x11; 635 uint32_t control = call("GetKeyState", 1, &key); 636 key = 0x12; 637 uint32_t alt = call("GetKeyState", 1, &key); 638 639 assert((uint16_t)shift == (pressed & 3 ? 0xff80 : 0)); 640 assert((uint16_t)control == (pressed & 12 ? 0xff80 : 0)); 641 assert((uint16_t)alt == (pressed & 48 ? 0xff80 : 0)); 642 } 643 #ifdef _WIN32 644 assert(SetKeyboardState(original)); 645 #else 646 assert(call("DestroyWindow", 1, &window)); 647 #endif 648 } 649 650 static void desktop_context_reports_native_pixel_storage(void) 651 { 652 uint32_t desktop = call("GetDesktopWindow", 0, NULL), none = 0; 653 assert(desktop && desktop == call("GetDesktopWindow", 0, NULL)); 654 uint32_t contexts[] = {call("GetDC", 1, &desktop), call("GetDC", 1, &none)}; 655 assert(contexts[0] && contexts[1]); 656 657 uint32_t query[] = {contexts[0], 12}; 658 uint32_t bits = call("GetDeviceCaps", 2, query); 659 query[0] = contexts[1]; 660 uint32_t screen_bits = call("GetDeviceCaps", 2, query); 661 uint32_t release[] = {desktop, contexts[0]}; 662 uint32_t released = call("ReleaseDC", 2, release); 663 release[0] = 0; release[1] = contexts[1]; 664 uint32_t released_screen = call("ReleaseDC", 2, release); 665 666 assert(bits == 32 && screen_bits == 32 && released && released_screen); 667 #ifndef _WIN32 668 SDL_DisplayMode mode; 669 assert(!SDL_GetCurrentDisplayMode(0, &mode)); 670 assert(bits == SDL_BYTESPERPIXEL(mode.format) * 8); 671 assert(!call("GetDeviceCaps", 2, query) && error() == 6); 672 assert(!call("ReleaseDC", 2, release)); 673 #endif 674 } 675 676 #ifndef _WIN32 677 static void screensaver_inhibition_is_process_scoped(void) 678 { 679 uint32_t parameters[] = {0x11, 1, 0, 2}; 680 SDL_bool original = SDL_IsScreenSaverEnabled(); 681 682 assert(call("SystemParametersInfoA", 4, parameters)); 683 assert(SDL_IsScreenSaverEnabled()); 684 parameters[1] = 0; 685 uint32_t inhibited = call("SystemParametersInfoA", 4, parameters); 686 parameters[0] = 0x56; 687 uint32_t power_policy = call("SystemParametersInfoA", 4, parameters), power_error = error(); 688 689 assert(inhibited && !SDL_IsScreenSaverEnabled()); 690 assert(!power_policy && power_error == 120); 691 if (original) SDL_EnableScreenSaver(); 692 } 693 #endif 694 695 static void posted_message_filtering_and_dispatch(void) 696 { 697 message_count = 0; 698 uint32_t window = call("CreateWindowExA", 12, args); 699 uint32_t output = workspace() + 512; 700 uint32_t peek[] = {output, window, 0x8001, 0x8002, 0}; 701 uint32_t post[] = {window, 0x8001, 0x12345678, 0x76543210}; 702 assert(call("PostMessageA", 4, post)); 703 post[1] = 0x8002; 704 assert(call("PostMessageA", 4, post)); 705 memset(memory(output - 4), 0xa5, 36); 706 queue_dispatches = 0; 707 708 assert(call("PeekMessageA", 5, peek)); 709 uint32_t first[7]; 710 memcpy(first, memory(output), sizeof(first)); 711 assert(call("PeekMessageA", 5, peek)); 712 713 assert(!memcmp(first, memory(output), sizeof(first))); 714 assert(first[0] == window && first[1] == 0x8001); 715 assert(first[2] == 0x12345678 && first[3] == 0x76543210); 716 assert(memory(output)[-1] == 0xa5a5a5a5 && memory(output)[7] == 0xa5a5a5a5); 717 assert(queue_dispatches == 0); 718 assert(call("DispatchMessageA", 1, &output) == 0x64606468); 719 assert(queue_dispatches == 1); 720 721 /* Remove the second message by filter, retaining the first one's position. */ 722 peek[2] = peek[3] = 0x8002; peek[4] = 1; 723 assert(call("PeekMessageA", 5, peek) && memory(output)[1] == 0x8002); 724 assert(!call("PeekMessageA", 5, peek)); 725 assert(memory(output)[1] == 0x8002); 726 peek[2] = peek[3] = 0x8001; 727 assert(call("GetMessageA", 4, peek) && memory(output)[1] == 0x8001); 728 assert(!call("TranslateMessage", 1, &output)); 729 assert(!call("PeekMessageA", 5, peek)); 730 assert(call("DestroyWindow", 1, &window)); 731 } 732 733 static void thread_messages_and_quit(void) 734 { 735 uint32_t output = workspace() + 512; 736 uint32_t post[] = {0, 0x8001, 41, 57}; 737 uint32_t peek[] = {output, UINT32_MAX, 0x8001, 0x8001, 1}; 738 assert(call("PostMessageA", 4, post)); 739 740 assert(call("PeekMessageA", 5, peek)); 741 uint32_t result = call("DispatchMessageA", 1, &output); 742 743 assert(!result && memory(output)[0] == 0 && memory(output)[1] == 0x8001); 744 assert(memory(output)[2] == 41 && memory(output)[3] == 57); 745 assert(!call("PeekMessageA", 5, peek)); 746 uint32_t code = 73; 747 call("PostQuitMessage", 1, &code); 748 peek[1] = 0; peek[4] = 0; 749 assert(call("PeekMessageA", 5, peek)); 750 assert(memory(output)[1] == 0x12 && memory(output)[2] == 73); 751 assert(call("PeekMessageA", 5, peek)); 752 assert(!call("GetMessageA", 4, peek)); 753 assert(memory(output)[1] == 0x12 && memory(output)[2] == 73); 754 assert(!call("PeekMessageA", 5, peek)); 755 } 756 757 static void translation_preserves_the_input_message(void) 758 { 759 message_count = 0; 760 uint32_t window = call("CreateWindowExA", 12, args); 761 uint32_t output = workspace() + 512; 762 uint32_t input[] = {window, 0x100, 32, 0x00390001, 0, 0, 0}; 763 memcpy(memory(output), input, sizeof(input)); 764 uint32_t peek[] = {output + 64, window, 0x102, 0x102, 1}; 765 766 uint32_t result = call("TranslateMessage", 1, &output); 767 768 assert(result && !memcmp(memory(output), input, sizeof(input))); 769 assert(call("PeekMessageA", 5, peek)); 770 assert(memory(output + 64)[1] == 0x102 && memory(output + 64)[2] == 32); 771 assert(memory(output + 64)[3] == 0x00390001); 772 memory(output)[1] = 0x101; 773 memory(output)[3] = 0xc0390001; 774 assert(call("TranslateMessage", 1, &output)); 775 assert(!call("PeekMessageA", 5, peek)); 776 assert(call("DestroyWindow", 1, &window)); 777 } 778 779 #ifdef _WIN32 780 static DWORD WINAPI post_from_thread(void *argument) 781 #else 782 static void *post_from_thread(void *argument) 783 #endif 784 { 785 #ifdef _WIN32 786 Sleep(20); 787 #else 788 g_esp = 0x5000d000; 789 SDL_Delay(20); 790 #endif 791 assert(call("PostMessageA", 4, argument)); 792 return 0; 793 } 794 795 static void waiting_message_loop_wakes_for_post(void) 796 { 797 message_count = 0; 798 uint32_t window = call("CreateWindowExA", 12, args); 799 uint32_t post[] = {window, 0x8001, 123, 456}; 800 uint32_t output = workspace() + 512; 801 uint32_t get[] = {output, window, 0x8001, 0x8001}; 802 #ifdef _WIN32 803 HANDLE thread = CreateThread(NULL, 0, post_from_thread, post, 0, NULL); 804 assert(thread); 805 #else 806 pthread_t thread; 807 assert(!pthread_create(&thread, NULL, post_from_thread, post)); 808 #endif 809 810 uint32_t result = call("GetMessageA", 4, get); 811 812 assert(result && memory(output)[1] == 0x8001); 813 assert(memory(output)[2] == 123 && memory(output)[3] == 456); 814 #ifdef _WIN32 815 assert(WaitForSingleObject(thread, 1000) == WAIT_OBJECT_0); 816 assert(CloseHandle(thread)); 817 #else 818 assert(!pthread_join(thread, NULL)); 819 #endif 820 assert(call("DestroyWindow", 1, &window)); 821 } 822 823 #ifndef _WIN32 824 static void sdl_keyboard_and_mouse_reach_guest_messages(void) 825 { 826 message_count = 0; 827 uint32_t window = call("CreateWindowExA", 12, args); 828 SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); 829 SDL_Event event = {.type = SDL_KEYDOWN}; 830 event.key.windowID = SDL_GetWindowID(linux_graphics_window()); 831 event.key.keysym.sym = SDLK_w; 832 event.key.keysym.mod = KMOD_LSHIFT; 833 assert(SDL_PushEvent(&event) == 1); 834 event.type = SDL_KEYUP; 835 event.key.keysym.mod = KMOD_NONE; 836 assert(SDL_PushEvent(&event) == 1); 837 uint32_t output = workspace() + 512, keys = workspace() + 640; 838 uint32_t peek[] = {output, window, 0x100, 0x101, 1}; 839 840 assert(call("PeekMessageA", 5, peek)); 841 assert(call("GetKeyboardState", 1, &keys)); 842 843 assert(memory(output)[1] == 0x100 && memory(output)[2] == 'W'); 844 assert(memory(output)[3] == 0x00110001); 845 assert(((uint8_t *)memory(keys))['W'] == 0x80 && ((uint8_t *)memory(keys))[16] == 0x80); 846 assert(call("PeekMessageA", 5, peek)); 847 assert(memory(output)[1] == 0x101 && memory(output)[3] == 0xc0110001); 848 assert(call("GetKeyboardState", 1, &keys)); 849 assert(!((uint8_t *)memory(keys))['W'] && !((uint8_t *)memory(keys))[16]); 850 851 memset(&event, 0, sizeof(event)); 852 event.type = SDL_MOUSEBUTTONDOWN; 853 event.button.button = SDL_BUTTON_LEFT; 854 event.button.x = -3; event.button.y = 29; 855 assert(SDL_PushEvent(&event) == 1); 856 event.type = SDL_MOUSEBUTTONUP; 857 assert(SDL_PushEvent(&event) == 1); 858 peek[2] = 0x201; peek[3] = 0x202; 859 assert(call("PeekMessageA", 5, peek)); 860 assert(memory(output)[1] == 0x201 && memory(output)[2] == 1 && memory(output)[3] == 0x001dfffd); 861 assert(call("GetKeyboardState", 1, &keys) && ((uint8_t *)memory(keys))[1] == 0x80); 862 assert(call("PeekMessageA", 5, peek)); 863 assert(memory(output)[1] == 0x202 && memory(output)[2] == 0); 864 assert(call("GetKeyboardState", 1, &keys) && !((uint8_t *)memory(keys))[1]); 865 assert(call("DestroyWindow", 1, &window)); 866 } 867 #endif 868 869 static void ascii_translation_uses_supplied_keyboard_state(void) 870 { 871 #ifdef _WIN32 872 HKL previous = ActivateKeyboardLayout(LoadKeyboardLayoutA("00000409", 0), 0); 873 assert(previous); 874 #endif 875 uint32_t digest = 2166136261u; 876 const uint32_t scans[] = {0, 0x8000, 0x80000000}; 877 for (unsigned modifier = 0; modifier < 16; ++modifier) 878 for (unsigned scan = 0; scan < 3; ++scan) 879 for (unsigned menu = 0; menu < 2; ++menu) 880 for (unsigned key = 0; key < 0xe7; ++key) { 881 unsigned char *state = (void *)memory(workspace()); 882 memset(state, 0, 256); 883 state[16] = modifier & 1 ? 128 : 0; 884 state[17] = modifier & 2 ? 128 : 0; 885 state[18] = modifier & 4 ? 128 : 0; 886 state[20] = modifier & 8 ? 1 : 0; 887 *memory(workspace() + 256) = 0xa5a5a5a5; 888 uint32_t arguments[] = {key, scans[scan], workspace(), workspace() + 257, menu}; 889 890 uint32_t count = call("ToAscii", 5, arguments); 891 892 uint32_t output = *memory(workspace() + 256); 893 assert(count <= 1 && (output & 0xffff00ff) == 0xa5a500a5); 894 assert(state[16] == (modifier & 1 ? 128 : 0)); 895 digest = (digest ^ count) * 16777619u; 896 digest = (digest ^ output) * 16777619u; 897 } 898 #ifdef _WIN32 899 ActivateKeyboardLayout(previous, 0); 900 #endif 901 assert(digest == 0x8d4cfc65); 902 printf("ToAscii: 22176 US key/modifier/scan/menu combinations, digest %08x, canaries and ABI passed\n", digest); 903 } 904 905 int main(void) 906 { 907 game_data_Clientr_exe(); 908 #ifdef _WIN32 909 assert(LoadLibraryA("user32.dll") && LoadLibraryA("gdi32.dll")); 910 instance = (uintptr_t)GetModuleHandleA(NULL); 911 callback = (uintptr_t)procedure; 912 #else 913 assert(guest_map("window test", 0x50000000u, 0x10000u)); 914 g_fs_base = 0x50004000u; 915 g_esp = 0x5000ff00u; 916 g_cur_func = 0x28004c70u; 917 g_ebx = 0x12345678u; g_esi = 0x23456789u; 918 g_edi = 0x3456789au; g_ebp = 0x456789abu; 919 instance = 0x28000000u; 920 callback = 0x11110000u; 921 #endif 922 for (wide = 0; wide < 2; ++wide) { 923 message_count = 0; reject = 0; 924 memset(memory(workspace()), 0, 1024); 925 class_registration_owns_its_name(); 926 create_and_destroy_dispatch_guest_messages(); 927 rejected_creation_releases_the_window(); 928 focus_activation_and_reentrant_callbacks(); 929 window_geometry_preserves_requested_client_area(); 930 visibility_dispatches_callbacks_and_preserves_geometry(); 931 reconstructed_icon_matches_native_window_pixels(); 932 posted_message_filtering_and_dispatch(); 933 thread_messages_and_quit(); 934 translation_preserves_the_input_message(); 935 waiting_message_loop_wakes_for_post(); 936 #ifndef _WIN32 937 sdl_keyboard_and_mouse_reach_guest_messages(); 938 #endif 939 mouse_capture_reports_ownership_and_releases_on_destruction(); 940 modifier_queries_combine_left_and_right_keys(); 941 class_release_invalidates_its_atom(); 942 } 943 wide = 0; 944 ascii_translation_uses_supplied_keyboard_state(); 945 desktop_context_reports_native_pixel_storage(); 946 cursor_query_writes_a_guest_point(); 947 cursor_warp_updates_screen_coordinates(); 948 null_cursor_hides_and_previous_cursor_can_be_restored(); 949 #ifndef _WIN32 950 screensaver_inhibition_is_process_scoped(); 951 #endif 952 puts("window: ANSI/UTF-16 creation, rejection, focus/activation, reentrancy, client geometry, destruction and desktop format passed"); 953 puts("messages: posting, filtering, peek/remove, dispatch, thread messages, quit and character translation passed"); 954 return 0; 955 }