//- optional apis // NOTE: looked up at runtime so the exe still starts on older windows 10 builds typedef BOOL WINAPI Win32SetProcessDpiAwarenessContext(HANDLE context); typedef UINT WINAPI Win32GetDpiForWindow(HWND hwnd); typedef UINT WINAPI Win32GetDpiForSystem(void); typedef BOOL WINAPI Win32AdjustWindowRectExForDpi(RECT *rect, DWORD style, BOOL menu, DWORD ex_style, UINT dpi); typedef HRESULT WINAPI Win32DwmSetWindowAttribute(HWND hwnd, DWORD attribute, LPCVOID value, DWORD size); global_variable Win32GetDpiForWindow *win32_get_dpi_for_window; global_variable Win32AdjustWindowRectExForDpi *win32_adjust_window_rect_for_dpi; #define WIN32_CLASS_NAME L"design-it-window" //- input helpers internal void win32_button_update(PlatformButton *button, b32 down) { if(button->ended_down != down) { button->ended_down = down; ++button->half_transition_count; } } internal void win32_release_buttons(Win32WindowState *state) { for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) { win32_button_update(&state->input.mouse[index], false); } } internal b32 win32_key_down(int vk) { return (GetKeyState(vk) & 0x8000) != 0; } // NOTE: AltGr arrives as LCtrl + RAlt. it types characters, so it must not count as ctrl / alt internal b32 win32_altgr_down(void) { return win32_key_down(VK_RMENU) && win32_key_down(VK_LCONTROL); } internal u32 win32_current_mods(void) { u32 mods = 0; if(win32_key_down(VK_SHIFT)) mods |= PLATFORM_MOD_SHIFT; if(!win32_altgr_down()) { if(win32_key_down(VK_CONTROL)) mods |= PLATFORM_MOD_CTRL; if(win32_key_down(VK_MENU)) mods |= PLATFORM_MOD_ALT; } return mods; } internal u32 win32_scale_for_dpi(u32 dpi) { u32 scale = (dpi + 48) / 96; return scale ? scale : 1; } internal void win32_update_size(Win32WindowState *state, u32 physical_width, u32 physical_height) { u32 scale = win32_scale_for_dpi(state->dpi); u32 width = physical_width / scale; u32 height = physical_height / scale; if(width != state->width || height != state->height || scale != state->scale || physical_width != state->physical_width || physical_height != state->physical_height) { state->physical_width = physical_width; state->physical_height = physical_height; state->width = width; state->height = height; state->scale = scale; state->resized = true; } } internal void win32_append_text(Win32WindowState *state, u32 codepoint) { PlatformInput *input = &state->input; if(codepoint < 0x20 || codepoint == 0x7f) return; u8 utf8[4]; u32 length = 0; if(codepoint < 0x80) { utf8[length++] = (u8)codepoint; } else if(codepoint < 0x800) { utf8[length++] = (u8)(0xC0 | (codepoint >> 6)); utf8[length++] = (u8)(0x80 | (codepoint & 0x3F)); } else if(codepoint < 0x10000) { utf8[length++] = (u8)(0xE0 | (codepoint >> 12)); utf8[length++] = (u8)(0x80 | ((codepoint >> 6) & 0x3F)); utf8[length++] = (u8)(0x80 | (codepoint & 0x3F)); } else { utf8[length++] = (u8)(0xF0 | (codepoint >> 18)); utf8[length++] = (u8)(0x80 | ((codepoint >> 12) & 0x3F)); utf8[length++] = (u8)(0x80 | ((codepoint >> 6) & 0x3F)); utf8[length++] = (u8)(0x80 | (codepoint & 0x3F)); } if(input->text_size + length <= sizeof(input->text)) { MemCpy(input->text + input->text_size, utf8, length); input->text_size += length; } } //- virtual key -> xkb keysym // NOTE: printable keys go through ToUnicode with ctrl / alt masked out, so ctrl+r is 'r' and // shift+g is 'G', layout aware, the same thing xkb_state_key_get_one_sym gives on wayland internal u32 win32_keysym(u32 vk, u32 scan, b32 extended) { b32 shift = win32_key_down(VK_SHIFT); switch(vk) { case VK_BACK: return XKB_KEY_BackSpace; case VK_TAB: return shift ? XKB_KEY_ISO_Left_Tab : XKB_KEY_Tab; case VK_RETURN: return extended ? XKB_KEY_KP_Enter : XKB_KEY_Return; case VK_ESCAPE: return XKB_KEY_Escape; case VK_SPACE: return XKB_KEY_space; case VK_DELETE: return XKB_KEY_Delete; case VK_INSERT: return XKB_KEY_Insert; case VK_HOME: return XKB_KEY_Home; case VK_END: return XKB_KEY_End; case VK_PRIOR: return XKB_KEY_Page_Up; case VK_NEXT: return XKB_KEY_Page_Down; case VK_LEFT: return XKB_KEY_Left; case VK_RIGHT: return XKB_KEY_Right; case VK_UP: return XKB_KEY_Up; case VK_DOWN: return XKB_KEY_Down; case VK_PAUSE: return XKB_KEY_Pause; case VK_SNAPSHOT: return XKB_KEY_Print; case VK_SCROLL: return XKB_KEY_Scroll_Lock; case VK_NUMLOCK: return XKB_KEY_Num_Lock; case VK_CAPITAL: return XKB_KEY_Caps_Lock; case VK_APPS: return XKB_KEY_Menu; case VK_ADD: return XKB_KEY_KP_Add; case VK_SUBTRACT: return XKB_KEY_KP_Subtract; case VK_MULTIPLY: return XKB_KEY_KP_Multiply; case VK_DIVIDE: return XKB_KEY_KP_Divide; case VK_DECIMAL: return XKB_KEY_KP_Decimal; case VK_LWIN: return XKB_KEY_Super_L; case VK_RWIN: return XKB_KEY_Super_R; case VK_SHIFT: return (MapVirtualKeyW(scan, MAPVK_VSC_TO_VK_EX) == VK_RSHIFT) ? XKB_KEY_Shift_R : XKB_KEY_Shift_L; case VK_CONTROL: return extended ? XKB_KEY_Control_R : XKB_KEY_Control_L; case VK_MENU: return extended ? XKB_KEY_Alt_R : XKB_KEY_Alt_L; default: break; } if(vk >= VK_F1 && vk <= VK_F12) return XKB_KEY_F1 + (vk - VK_F1); if(vk >= VK_NUMPAD0 && vk <= VK_NUMPAD9) return XKB_KEY_KP_0 + (vk - VK_NUMPAD0); BYTE keys[256]; if(!GetKeyboardState(keys)) MemSetZero(keys, sizeof(keys)); if(!win32_altgr_down()) { keys[VK_CONTROL] = keys[VK_LCONTROL] = keys[VK_RCONTROL] = 0; keys[VK_MENU] = keys[VK_LMENU] = keys[VK_RMENU] = 0; } WCHAR chars[4] = {0}; // NOTE: flag 0x4 = don't touch the kernel's dead key state (windows 10 1607+) int count = ToUnicode(vk, scan, keys, chars, ArrayCount(chars), 0x4); u32 codepoint = 0; if(count != 0) { codepoint = chars[0]; if(count >= 2 && chars[0] >= 0xD800 && chars[0] < 0xDC00 && chars[1] >= 0xDC00 && chars[1] < 0xE000) { codepoint = 0x10000 + (((u32)chars[0] - 0xD800) << 10) + ((u32)chars[1] - 0xDC00); } } else if(vk >= 'A' && vk <= 'Z') { codepoint = shift ? vk : vk + ('a' - 'A'); } else if(vk >= '0' && vk <= '9') { codepoint = vk; } if(codepoint < 0x20 || codepoint == 0x7f) return 0; return (codepoint < 0x100) ? codepoint : (0x01000000u | codepoint); } internal void win32_emit_key(Win32WindowState *state, u32 vk, LPARAM lparam) { PlatformInput *input = &state->input; u32 scan = (u32)((lparam >> 16) & 0xFF); b32 extended = (lparam >> 24) & 1; b32 repeat = (lparam >> 30) & 1; u32 keysym = win32_keysym(vk, scan, extended); input->mods = win32_current_mods(); if(keysym && input->key_count < PLATFORM_MAX_KEY_EVENTS) { PlatformKeyEvent *event = &input->keys[input->key_count++]; event->keysym = keysym; event->mods = input->mods; event->repeat = repeat; } } //- cursor internal HCURSOR win32_cursor_handle(PlatformCursor cursor) { // NOTE: numeric IDC_* ids, the IDC_ macros flip between A and W with UNICODE u32 id = 32512; // IDC_ARROW switch(cursor) { case PLATFORM_CURSOR_DEFAULT: id = 32512; break; // IDC_ARROW case PLATFORM_CURSOR_POINTER: id = 32649; break; // IDC_HAND case PLATFORM_CURSOR_GRABBING: id = 32646; break; // IDC_SIZEALL, there is no closed hand case PLATFORM_CURSOR_RESIZE_X: id = 32644; break; // IDC_SIZEWE case PLATFORM_CURSOR_RESIZE_Y: id = 32645; break; // IDC_SIZENS case PLATFORM_CURSOR_CROSSHAIR: id = 32515; break; // IDC_CROSS case PLATFORM_CURSOR_MOVE: id = 32646; break; // IDC_SIZEALL case PLATFORM_CURSOR_RESIZE_NWSE: id = 32642; break; // IDC_SIZENWSE } return LoadCursorW(NULL, MAKEINTRESOURCEW(id)); } internal void win32_window_set_cursor(PlatformWindow *window, PlatformCursor cursor) { Win32WindowState *state = &window->win32; if(state->cursor == (u32)cursor) return; state->cursor = (u32)cursor; state->cursor_handle = win32_cursor_handle(cursor); if(state->input.mouse_in_window) SetCursor(state->cursor_handle); } //- drag and drop internal void win32_handle_drop(Win32WindowState *state, HDROP drop) { PlatformInput *input = &state->input; UINT count = DragQueryFileW(drop, 0xFFFFFFFF, NULL, 0); for(UINT index = 0; index < count && input->drop_count < PLATFORM_MAX_DROPS; ++index) { WCHAR wide[PLATFORM_DROP_PATH_SIZE]; UINT length = DragQueryFileW(drop, index, wide, ArrayCount(wide)); if(length == 0 || length >= ArrayCount(wide)) continue; char *out = input->drop_paths[input->drop_count]; if(win32_wide_to_utf8(wide, (s64)length, out, PLATFORM_DROP_PATH_SIZE) > 0) { // NOTE: forward slashes like everywhere else, win32 file apis take both for(char *at = out; *at; ++at) if(*at == '\\') *at = '/'; ++input->drop_count; } } POINT point = {}; DragQueryPoint(drop, &point); input->drop_x = (f32)point.x / (f32)state->scale; input->drop_y = (f32)point.y / (f32)state->scale; input->changed = true; DragFinish(drop); SetForegroundWindow(state->hwnd); } //- window procedure internal void win32_client_rect_for(Win32WindowState *state, u32 client_width, u32 client_height, DWORD style, DWORD ex_style, s32 *out_width, s32 *out_height) { RECT rect = {0, 0, (LONG)client_width, (LONG)client_height}; if(win32_adjust_window_rect_for_dpi) win32_adjust_window_rect_for_dpi(&rect, style, FALSE, ex_style, state->dpi); else AdjustWindowRectEx(&rect, style, FALSE, ex_style); *out_width = rect.right - rect.left; *out_height = rect.bottom - rect.top; } internal LRESULT CALLBACK win32_window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { if(message == WM_NCCREATE) { CREATESTRUCTW *create = (CREATESTRUCTW *)lparam; SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR)create->lpCreateParams); return DefWindowProcW(hwnd, message, wparam, lparam); } Win32WindowState *state = (Win32WindowState *)GetWindowLongPtrW(hwnd, GWLP_USERDATA); if(!state) return DefWindowProcW(hwnd, message, wparam, lparam); PlatformInput *input = &state->input; switch(message) { //- lifetime case WM_CLOSE: { state->should_close = true; input->changed = true; return 0; } case WM_ERASEBKGND: return 1; case WM_PAINT: { ValidateRect(hwnd, NULL); input->changed = true; return 0; } //- size case WM_SIZE: { input->changed = true; state->suspended = (wparam == SIZE_MINIMIZED); state->maximized = (wparam == SIZE_MAXIMIZED); if(!state->suspended) win32_update_size(state, LOWORD(lparam), HIWORD(lparam)); // NOTE: inside the modal size loop our main loop is stuck in DispatchMessage, draw from here if(state->in_size_move && state->refresh && !state->suspended) state->refresh(state->refresh_user); return 0; } case WM_ENTERSIZEMOVE: { // NOTE: a timer keeps animations / the cursor blink alive while the edge is held still state->in_size_move = true; SetTimer(hwnd, 1, USER_TIMER_MINIMUM, NULL); return 0; } case WM_TIMER: { if(wparam == 1 && state->in_size_move && state->refresh) state->refresh(state->refresh_user); return 0; } case WM_DPICHANGED: { state->dpi = HIWORD(wparam); input->changed = true; RECT *suggested = (RECT *)lparam; SetWindowPos(hwnd, NULL, suggested->left, suggested->top, suggested->right - suggested->left, suggested->bottom - suggested->top, SWP_NOZORDER | SWP_NOACTIVATE); RECT client; GetClientRect(hwnd, &client); win32_update_size(state, (u32)client.right, (u32)client.bottom); return 0; } case WM_GETMINMAXINFO: { if(state->min_width && state->min_height) { MINMAXINFO *info = (MINMAXINFO *)lparam; s32 width = 0, height = 0; win32_client_rect_for(state, state->min_width * state->scale, state->min_height * state->scale, (DWORD)GetWindowLongW(hwnd, GWL_STYLE), (DWORD)GetWindowLongW(hwnd, GWL_EXSTYLE), &width, &height); info->ptMinTrackSize.x = width; info->ptMinTrackSize.y = height; } return 0; } case WM_EXITSIZEMOVE: { // NOTE: the modal move / size loop ate the button release state->in_size_move = false; KillTimer(hwnd, 1); win32_release_buttons(state); input->changed = true; return 0; } //- focus case WM_SETFOCUS: { input->focused = true; input->changed = true; input->mods = win32_current_mods(); return 0; } case WM_KILLFOCUS: { input->focused = false; input->changed = true; input->mods = 0; win32_release_buttons(state); return 0; } //- keyboard case WM_KEYDOWN: case WM_SYSKEYDOWN: { input->changed = true; // NOTE: let alt+f4 through to DefWindowProc, it posts WM_CLOSE if(message == WM_SYSKEYDOWN && wparam == VK_F4 && !win32_altgr_down()) break; win32_emit_key(state, (u32)wparam, lparam); return 0; } case WM_KEYUP: case WM_SYSKEYUP: { input->changed = true; input->mods = win32_current_mods(); // NOTE: returning keeps a lone alt / f10 from activating the (non existent) menu return 0; } case WM_CHAR: { input->changed = true; WCHAR unit = (WCHAR)wparam; if(unit >= 0xD800 && unit < 0xDC00) { state->high_surrogate = unit; return 0; } u32 codepoint = unit; if(unit >= 0xDC00 && unit < 0xE000) { if(!state->high_surrogate) return 0; codepoint = 0x10000 + (((u32)state->high_surrogate - 0xD800) << 10) + ((u32)unit - 0xDC00); state->high_surrogate = 0; } // NOTE: same rule as wayland: ctrl / alt chords are commands, not text (AltGr is fine) if(!(win32_current_mods() & (PLATFORM_MOD_CTRL | PLATFORM_MOD_ALT))) { win32_append_text(state, codepoint); } return 0; } case WM_SYSCHAR: return 0; // no beep for alt chords case WM_SYSCOMMAND: { if((wparam & 0xFFF0) == SC_KEYMENU) return 0; break; } //- mouse case WM_MOUSEMOVE: { input->changed = true; if(!state->tracking_mouse) { TRACKMOUSEEVENT track = {sizeof(track), TME_LEAVE, hwnd, 0}; TrackMouseEvent(&track); state->tracking_mouse = true; input->mouse_in_window = true; SetCursor(state->cursor_handle); } input->mouse_x = (f32)GET_X_LPARAM(lparam) / (f32)state->scale; input->mouse_y = (f32)GET_Y_LPARAM(lparam) / (f32)state->scale; return 0; } case WM_MOUSELEAVE: { input->changed = true; state->tracking_mouse = false; input->mouse_in_window = false; if(GetCapture() != hwnd) win32_release_buttons(state); return 0; } case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: case WM_LBUTTONUP: case WM_RBUTTONUP: case WM_MBUTTONUP: { input->changed = true; u32 index = (message == WM_LBUTTONDOWN || message == WM_LBUTTONUP) ? 0 : (message == WM_RBUTTONDOWN || message == WM_RBUTTONUP) ? 1 : 2; b32 down = (message == WM_LBUTTONDOWN || message == WM_RBUTTONDOWN || message == WM_MBUTTONDOWN); input->mouse_x = (f32)GET_X_LPARAM(lparam) / (f32)state->scale; input->mouse_y = (f32)GET_Y_LPARAM(lparam) / (f32)state->scale; win32_button_update(&input->mouse[index], down); // NOTE: capture so drags keep reporting outside the client area, like a wayland grab if(down) { SetCapture(hwnd); } else if(!input->mouse[0].ended_down && !input->mouse[1].ended_down && !input->mouse[2].ended_down) { ReleaseCapture(); } return 0; } case WM_CAPTURECHANGED: { if((HWND)lparam != hwnd) { input->changed = true; win32_release_buttons(state); } return 0; } case WM_MOUSEWHEEL: { input->changed = true; // NOTE: windows positive = away from the user (up), ours is positive = down input->scroll_y -= (f32)GET_WHEEL_DELTA_WPARAM(wparam) / (f32)WHEEL_DELTA; return 0; } case WM_MOUSEHWHEEL: { input->changed = true; input->scroll_x += (f32)GET_WHEEL_DELTA_WPARAM(wparam) / (f32)WHEEL_DELTA; return 0; } case WM_SETCURSOR: { if(LOWORD(lparam) == HTCLIENT) { SetCursor(state->cursor_handle); return TRUE; } break; } //- files case WM_DROPFILES: { win32_handle_drop(state, (HDROP)wparam); return 0; } } return DefWindowProcW(hwnd, message, wparam, lparam); } //- errors internal void platform_show_error(const char *title, const char *message) { WCHAR wide_title[256]; WCHAR wide_message[2048]; win32_utf8_to_wide(title, -1, wide_title, ArrayCount(wide_title)); win32_utf8_to_wide(message, -1, wide_message, ArrayCount(wide_message)); fprintf(stderr, "%s: %s\n", title, message); MessageBoxW(NULL, wide_message, wide_title, MB_OK | MB_ICONERROR); } //- public api internal PlatformWindow *win32_window_open(Arena *arena, WindowParams params) { PlatformWindow *window = PushStructZero(arena, PlatformWindow); Win32WindowState *state = &window->win32; HMODULE user32 = GetModuleHandleW(L"user32.dll"); Win32SetProcessDpiAwarenessContext *set_awareness = (Win32SetProcessDpiAwarenessContext *)(void *)GetProcAddress(user32, "SetProcessDpiAwarenessContext"); Win32GetDpiForSystem *dpi_for_system = (Win32GetDpiForSystem *)(void *)GetProcAddress(user32, "GetDpiForSystem"); win32_get_dpi_for_window = (Win32GetDpiForWindow *)(void *)GetProcAddress(user32, "GetDpiForWindow"); win32_adjust_window_rect_for_dpi = (Win32AdjustWindowRectExForDpi *)(void *)GetProcAddress(user32, "AdjustWindowRectExForDpi"); // NOTE: per monitor v2 = we get WM_DPICHANGED and real pixels, never bitmap stretched if(!set_awareness || !set_awareness((HANDLE)(smm)-4 /* DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 */)) { SetProcessDPIAware(); } state->instance = GetModuleHandleW(NULL); state->dpi = dpi_for_system ? dpi_for_system() : 96; state->scale = win32_scale_for_dpi(state->dpi); state->width = params.width ? params.width : 960; state->height = params.height ? params.height : 600; state->min_width = params.min_width; state->min_height = params.min_height; state->cursor = PLATFORM_CURSOR_DEFAULT; state->cursor_handle = win32_cursor_handle(PLATFORM_CURSOR_DEFAULT); state->is_layer = params.layer; state->timer = CreateWaitableTimerExW(NULL, NULL, 0x00000002 /* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION */, TIMER_ALL_ACCESS); WNDCLASSEXW window_class = {sizeof(window_class)}; window_class.style = CS_HREDRAW | CS_VREDRAW; window_class.lpfnWndProc = win32_window_proc; window_class.hInstance = state->instance; window_class.hIcon = LoadIconW(state->instance, MAKEINTRESOURCEW(1)); // build.bat may link an icon resource if(!window_class.hIcon) window_class.hIcon = LoadIconW(NULL, MAKEINTRESOURCEW(32512)); // IDI_APPLICATION window_class.hCursor = NULL; // WM_SETCURSOR window_class.lpszClassName = WIN32_CLASS_NAME; if(!RegisterClassExW(&window_class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) { Log("RegisterClassExW failed (%lu)", GetLastError()); return NULL; } WCHAR title[256]; win32_utf8_to_wide(params.title ? params.title : params.name, -1, title, ArrayCount(title)); DWORD style = WS_OVERLAPPEDWINDOW; DWORD ex_style = WS_EX_APPWINDOW; s32 x = CW_USEDEFAULT, y = CW_USEDEFAULT, width = 0, height = 0; if(state->is_layer) { // NOTE: the closest thing to a wlr layer surface: borderless, topmost, no taskbar button style = WS_POPUP; ex_style = WS_EX_TOPMOST | WS_EX_TOOLWINDOW; RECT work = {0, 0, 1920, 1080}; SystemParametersInfoW(SPI_GETWORKAREA, 0, &work, 0); s32 margin = params.margin * (s32)state->scale; width = (s32)(state->width * state->scale); height = (s32)(state->height * state->scale); b32 left = (params.anchor & PLATFORM_ANCHOR_LEFT) != 0, right = (params.anchor & PLATFORM_ANCHOR_RIGHT) != 0; b32 top = (params.anchor & PLATFORM_ANCHOR_TOP) != 0, bottom = (params.anchor & PLATFORM_ANCHOR_BOTTOM) != 0; if(left && right) { x = work.left + margin; width = (work.right - work.left) - 2 * margin; } else if(left) { x = work.left + margin; } else if(right) { x = work.right - margin - width; } else { x = work.left + ((work.right - work.left) - width) / 2; } if(top && bottom) { y = work.top + margin; height = (work.bottom - work.top) - 2 * margin; } else if(top) { y = work.top + margin; } else if(bottom) { y = work.bottom - margin - height; } else { y = work.top + ((work.bottom - work.top) - height) / 2; } } else { win32_client_rect_for(state, state->width * state->scale, state->height * state->scale, style, ex_style, &width, &height); } state->hwnd = CreateWindowExW(ex_style, WIN32_CLASS_NAME, title, style, x, y, width, height, NULL, NULL, state->instance, state); if(!state->hwnd) { Log("CreateWindowExW failed (%lu)", GetLastError()); return NULL; } // NOTE: the default window position may have landed on a monitor with a different dpi if(win32_get_dpi_for_window) { u32 dpi = win32_get_dpi_for_window(state->hwnd); if(dpi && dpi != state->dpi && !state->is_layer) { state->dpi = dpi; win32_client_rect_for(state, state->width * win32_scale_for_dpi(dpi), state->height * win32_scale_for_dpi(dpi), style, ex_style, &width, &height); SetWindowPos(state->hwnd, NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); } if(dpi) state->dpi = dpi; } // NOTE: dark title bar to match the default scheme (DWMWA_USE_IMMERSIVE_DARK_MODE, windows 10 20h1+) HMODULE dwm = LoadLibraryW(L"dwmapi.dll"); if(dwm) { Win32DwmSetWindowAttribute *set_attribute = (Win32DwmSetWindowAttribute *)(void *)GetProcAddress(dwm, "DwmSetWindowAttribute"); BOOL dark = TRUE; if(set_attribute) set_attribute(state->hwnd, 20, &dark, sizeof(dark)); } DragAcceptFiles(state->hwnd, TRUE); ShowWindow(state->hwnd, SW_SHOWNORMAL); UpdateWindow(state->hwnd); RECT client; GetClientRect(state->hwnd, &client); state->physical_width = state->physical_height = 0; win32_update_size(state, (u32)client.right, (u32)client.bottom); state->resized = false; state->input.focused = (GetForegroundWindow() == state->hwnd); state->input.changed = true; return window; } internal b32 win32_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask) { if(ready_mask) *ready_mask = 0; Win32WindowState *state = &window->win32; //- wait for messages, one of the extra handles, or the timeout HANDLE handles[MAXIMUM_WAIT_OBJECTS - 1]; u32 which[MAXIMUM_WAIT_OBJECTS - 1]; u32 count = 0; for(u32 index = 0; index < extra_count && count + 1 < ArrayCount(handles); ++index) { if(extra_fds[index] != -1) { which[count] = index; handles[count++] = (HANDLE)(smm)extra_fds[index]; } } DWORD wait_ms = INFINITE; b32 use_timer = false; if(timeout_ns == 0) { wait_ms = 0; } else if(timeout_ns > 0) { if(state->timer) { LARGE_INTEGER due; due.QuadPart = -(LONGLONG)Max(timeout_ns / 100ll, 1ll); // relative, 100ns units use_timer = SetWaitableTimer(state->timer, &due, 0, NULL, NULL, FALSE) != 0; } if(use_timer) handles[count++] = state->timer; else wait_ms = (DWORD)((timeout_ns + 999999ll) / 1000000ll); } MsgWaitForMultipleObjectsEx(count, count ? handles : NULL, wait_ms, QS_ALLINPUT, MWMO_INPUTAVAILABLE); if(use_timer) CancelWaitableTimer(state->timer); // NOTE: both the wake event (manual reset) and change notifications stay signaled, probing is free if(ready_mask) { u32 extras = use_timer ? count - 1 : count; for(u32 slot = 0; slot < extras; ++slot) { if(WaitForSingleObject(handles[slot], 0) == WAIT_OBJECT_0) *ready_mask |= 1u << which[slot]; } } //- dispatch MSG message; while(PeekMessageW(&message, NULL, 0, 0, PM_REMOVE)) { if(message.message == WM_QUIT) { state->should_close = true; break; } TranslateMessage(&message); DispatchMessageW(&message); } *input = state->input; // NOTE: transitions, key events, text, scroll and drops are per frame, held state carries over for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) { state->input.mouse[index].half_transition_count = 0; } state->input.key_count = 0; state->input.text_size = 0; state->input.scroll_x = 0; state->input.scroll_y = 0; state->input.drop_count = 0; state->input.changed = false; return !state->should_close; } internal void win32_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale) { *width = window->win32.width; *height = window->win32.height; *scale = window->win32.scale; } internal b32 win32_window_take_resized(PlatformWindow *window) { b32 resized = window->win32.resized; window->win32.resized = false; return resized; } //- window management internal void win32_window_begin_move(PlatformWindow *window) { // NOTE: posted, so the modal move loop runs inside the next pump instead of mid frame. // SC_MOVE | HTCAPTION is the undocumented-but-universal "drag by the caption" command. Win32WindowState *state = &window->win32; if(state->is_layer) return; ReleaseCapture(); PostMessageW(state->hwnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0); } internal void win32_window_toggle_maximized(PlatformWindow *window) { Win32WindowState *state = &window->win32; if(state->is_layer) return; ShowWindow(state->hwnd, IsZoomed(state->hwnd) ? SW_RESTORE : SW_MAXIMIZE); } //- frame pacing internal void win32_window_request_frame(PlatformWindow *window) { u64 now = time_now_ns(); window->win32.frame_requested_ns = now; window->win32.last_frame_done_ns = now; } internal void win32_window_cancel_frame(PlatformWindow *window) {} internal b32 win32_window_frame_ready(PlatformWindow *window) { return true; } internal void win32_window_close(PlatformWindow *window) { Win32WindowState *state = &window->win32; if(state->hwnd) DestroyWindow(state->hwnd); if(state->timer) CloseHandle(state->timer); state->hwnd = NULL; state->timer = NULL; }