diff --git a/runtime/dpi_probe.c b/runtime/dpi_probe.c new file mode 100644 index 00000000..a2ddcf66 --- /dev/null +++ b/runtime/dpi_probe.c @@ -0,0 +1,33 @@ +// --probe-dpi: the process DPI awareness that totk.manifest declares, and what the primary monitor reports under it. +// A DPI-unaware process gets every window and monitor size scaled down by the display scale and its output stretched +// back up by Windows; the probe fails unless the process is per-monitor aware (v2). Needs no game files and no GPU. +#include +#include + +#include "host.h" + +static const char *awareness_name(DPI_AWARENESS_CONTEXT ctx) { + if (AreDpiAwarenessContextsEqual(ctx, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) return "per-monitor aware v2"; + switch (GetAwarenessFromDpiAwarenessContext(ctx)) { + case DPI_AWARENESS_UNAWARE: return "unaware"; + case DPI_AWARENESS_SYSTEM_AWARE: return "system aware"; + case DPI_AWARENESS_PER_MONITOR_AWARE: return "per-monitor aware v1"; + default: return "invalid"; + } +} + +int probe_dpi(void) { + DPI_AWARENESS_CONTEXT ctx = GetThreadDpiAwarenessContext(); + MONITORINFO mi = {sizeof mi}; + HMONITOR mon = MonitorFromPoint((POINT){0, 0}, MONITOR_DEFAULTTOPRIMARY); + int have = GetMonitorInfoW(mon, &mi); + printf("dpi: awareness %s\n", awareness_name(ctx)); + printf("dpi: system dpi %u (scale %u%%)\n", GetDpiForSystem(), GetDpiForSystem() * 100 / 96); + if (have) + printf("dpi: primary monitor %ldx%ld at %ld,%ld; screen metrics %dx%d\n", mi.rcMonitor.right - mi.rcMonitor.left, + mi.rcMonitor.bottom - mi.rcMonitor.top, mi.rcMonitor.left, mi.rcMonitor.top, GetSystemMetrics(SM_CXSCREEN), + GetSystemMetrics(SM_CYSCREEN)); + int ok = AreDpiAwarenessContextsEqual(ctx, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) && have; + printf("probe-dpi: %s\n", ok ? "PASS" : "FAIL (the process is not per-monitor aware v2)"); + return ok ? 0 : 1; +} diff --git a/runtime/gfx_window.c b/runtime/gfx_window.c index f62de225..5d4bd485 100644 --- a/runtime/gfx_window.c +++ b/runtime/gfx_window.c @@ -12,6 +12,10 @@ // The in-game menu (ui/menu.h) draws over the game image in the present's command buffer, after the blit; its keyboard // and mouse come from the window procedure (ui/menu_win.cpp). The window's size and fullscreen state are the settings // TOTK_WINDOW_SIZE and TOTK_FULLSCREEN, applied at start and whenever a cfg_set changes them (window_settings_live). +// +// The process is per-monitor DPI aware (totk.manifest): every size here is physical pixels, TOTK_WINDOW_SIZE included, +// and the swapchain matches the screen pixel for pixel. A window moved to a monitor at another display scale keeps its +// client size (WM_GETDPISCALEDSIZE); the menu takes its text size floor from the window's DPI (gfx_window_dpi). #include #include #include @@ -48,6 +52,7 @@ static CRITICAL_SECTION g_start_lock; // gfx_window_start / vsync start static int g_started, g_wnd_failed; static int g_background; // TOTK_BACKGROUND_WINDOW: the window stays behind the user's windows static int g_fs_pending; // window thread: TOTK_FULLSCREEN waits for the first activation +static volatile LONG g_dpi = USER_DEFAULT_SCREEN_DPI; // the window's DPI, set by the window thread // ---- swapchain and present state (g_pl) static CRITICAL_SECTION g_pl; @@ -189,6 +194,18 @@ static WINDOWPLACEMENT g_saved_place = {sizeof g_saved_place}; // the window b static int is_fullscreen(HWND h) { return !(GetWindowLongW(h, GWL_STYLE) & WS_OVERLAPPEDWINDOW); } +unsigned gfx_window_dpi(void) { return (unsigned)InterlockedCompareExchange(&g_dpi, 0, 0); } + +// The outer size of a framed window whose client area is cw x ch. The frame is measured at the window's DPI, or the +// system DPI before the window exists: plain AdjustWindowRect always takes the system DPI, which is wrong on a monitor +// at another display scale. +static void outer_size(HWND h, int cw, int ch, int *ow, int *oh) { + RECT r = {0, 0, cw, ch}; + AdjustWindowRectExForDpi(&r, WND_STYLE, FALSE, 0, h ? GetDpiForWindow(h) : GetDpiForSystem()); + *ow = r.right - r.left; + *oh = r.bottom - r.top; +} + // TOTK_FULLSCREEN=1: borderless window over the monitor. TOTK_WINDOW_SIZE=x: client area of the window (the // swapchain follows the window; the game image is scaled to it). static int cfg_fullscreen(void) { @@ -242,20 +259,18 @@ static void apply_settings(HWND h) { int size_changed = strcmp(cfg_value("TOTK_WINDOW_SIZE"), g_applied_size) != 0, fs = cfg_fullscreen(), fs_changed = fs != g_applied_fs; note_applied(); if (size_changed) { - int cw, ch; + int cw, ch, ow, oh; cfg_window_size(&cw, &ch); - RECT r = {0, 0, cw, ch}; - AdjustWindowRect(&r, WND_STYLE, FALSE); + outer_size(h, cw, ch, &ow, &oh); if (is_fullscreen(h)) { RECT *n = &g_saved_place.rcNormalPosition; - n->right = n->left + (r.right - r.left); - n->bottom = n->top + (r.bottom - r.top); + n->right = n->left + ow; + n->bottom = n->top + oh; } else { RECT have; if (GetClientRect(h, &have) && (have.right != cw || have.bottom != ch)) { if (IsZoomed(h)) ShowWindow(h, SW_RESTORE); - SetWindowPos(h, NULL, 0, 0, r.right - r.left, r.bottom - r.top, - SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); + SetWindowPos(h, NULL, 0, 0, ow, oh, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); } } } @@ -381,6 +396,32 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) { case WM_DISPLAYCHANGE: refresh_rate_update(h, 1); break; + case WM_GETDPISCALEDSIZE: // a framed window keeps its client size in pixels on a monitor at another scale + if (!is_fullscreen(h) && !IsZoomed(h)) { + RECT c, r; + if (GetClientRect(h, &c)) { + r = (RECT){0, 0, c.right, c.bottom}; + if (AdjustWindowRectExForDpi(&r, WND_STYLE, FALSE, 0, LOWORD(w))) { + *(SIZE *)l = (SIZE){r.right - r.left, r.bottom - r.top}; + return TRUE; + } + } + } + break; // DefWindowProc: Windows scales the size with the DPI + case WM_DPICHANGED: { // display scale changed, or the window reached a monitor at another scale + const RECT *s = (const RECT *)l; + InterlockedExchange(&g_dpi, LOWORD(w)); + rt_log("gfx: window DPI %u (%u%%)", LOWORD(w), LOWORD(w) * 100u / USER_DEFAULT_SCREEN_DPI); + if (is_fullscreen(h)) { // keep covering the monitor the suggested rect lies on + MONITORINFO mi = {sizeof mi}; + if (GetMonitorInfoW(MonitorFromRect(s, MONITOR_DEFAULTTONEAREST), &mi)) + SetWindowPos(h, NULL, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left, + mi.rcMonitor.bottom - mi.rcMonitor.top, SWP_NOZORDER | SWP_NOACTIVATE); + } else { + SetWindowPos(h, NULL, s->left, s->top, s->right - s->left, s->bottom - s->top, SWP_NOZORDER | SWP_NOACTIVATE); + } + return 0; + } case WM_ACTIVATEAPP: { int in = w != 0; InterlockedExchange(&g_focused, in); @@ -417,23 +458,25 @@ static DWORD WINAPI window_main(void *arg) { GetSystemMetrics(SM_CYSMICON), LR_SHARED), .hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW), .lpszClassName = L"TotKRecompWindow"}; RegisterClassExW(&wc); - int cw, ch; + int cw, ch, ow, oh; cfg_window_size(&cw, &ch); - RECT r = {0, 0, cw, ch}; - AdjustWindowRect(&r, WND_STYLE, FALSE); + outer_size(NULL, cw, ch, &ow, &oh); // The window opens behind the user's windows without taking focus: it shows the game's blank loading frames for a // while, and must not cover what the player works on meanwhile. The game still counts itself in focus until the // first WM_ACTIVATEAPP, so loading goes on, and keyboard input waits for the foreground (input_win.c). Its taskbar // button flashes until it is brought up, and TOTK_FULLSCREEN waits for that first activation. // TOTK_BACKGROUND_WINDOW=1 (test runs): no flashing, never fullscreen at start, and F11 keeps the window's place. int background = g_background = cfg_get("TOTK_BACKGROUND_WINDOW") && atoi(cfg_get("TOTK_BACKGROUND_WINDOW")); - g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"TotK Recomp", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, - r.right - r.left, r.bottom - r.top, NULL, NULL, inst, NULL); + g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"TotK Recomp", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, ow, oh, NULL, + NULL, inst, NULL); if (!g_hwnd) { rt_log("gfx: CreateWindowEx failed (%lu)", GetLastError()); g_wnd_failed = 1; } else { - SetWindowPos(g_hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW); + // The frame was measured at the system DPI; the window may have opened on a monitor at another scale. + InterlockedExchange(&g_dpi, (LONG)GetDpiForWindow(g_hwnd)); + outer_size(g_hwnd, cw, ch, &ow, &oh); + SetWindowPos(g_hwnd, HWND_BOTTOM, 0, 0, ow, oh, SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW); if (!background) { FLASHWINFO fw = {.cbSize = sizeof fw, .hwnd = g_hwnd, .dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG}; FlashWindowEx(&fw); @@ -901,9 +944,9 @@ void vk_present_flip(void) { void gfx_window_resize(int cw, int chh) { gfx_window_start(); - RECT r = {0, 0, cw, chh}; - AdjustWindowRect(&r, WND_STYLE, FALSE); - SetWindowPos(g_hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); + int ow, oh; + outer_size(g_hwnd, cw, chh, &ow, &oh); + SetWindowPos(g_hwnd, NULL, 0, 0, ow, oh, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); EnterCriticalSection(&g_pl); create_swapchain(); LeaveCriticalSection(&g_pl); @@ -979,8 +1022,8 @@ void gfx_window_start(void) { LeaveCriticalSection(&g_pl); RECT rc = {0}; GetClientRect(g_hwnd, &rc); - rt_log("gfx: window %ldx%ld, swapchain %ux%u format %d", rc.right, rc.bottom, g_sc_ext.width, g_sc_ext.height, - (int)g_sc_fmt); + rt_log("gfx: window %ldx%ld at DPI %u, swapchain %ux%u format %d", rc.right, rc.bottom, gfx_window_dpi(), + g_sc_ext.width, g_sc_ext.height, (int)g_sc_fmt); } LeaveCriticalSection(&g_start_lock); vsync_ensure(); diff --git a/runtime/host.h b/runtime/host.h index 87b55007..262b7b98 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -281,6 +281,7 @@ void rt_boot(void); // boot.c // modules, registers its host functions, then seals (hostfn_seal, or implicitly at the first rt_call). Return the // process exit code. int probe_cfg(void); // cfg_probe.c +int probe_dpi(void); // dpi_probe.c int probe_loader(void), probe_kernel(void), probe_ipc(void), probe_services(void), probe_fs(void); int probe_gfx_abi(void); // gfx_abi_probe.c int probe_vkdevice(void); // vk_device.c diff --git a/runtime/main.c b/runtime/main.c index 80e80ae6..901804d6 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -4,6 +4,7 @@ // --selftest layout, dispatch table and host-function registry; needs no game files // --probe-loader load and relocate the modules, check them against the measured 1.2.1 values // --probe-kernel, --probe-ipc, --probe-services, --probe-fs host probes +// --probe-dpi the process DPI awareness (per-monitor v2) and the primary monitor's size; no game files // --probe-gfx-abi NVN object layouts against nvn.h's reserved sizes; needs no game files // --probe-nvn, --probe-nvn-tex, --probe-window, --probe-shader, --probe-vkmem, --probe-vkexec graphics probes // --probe-astc the compute ASTC decoder against astc_decode_ldr; needs no game files @@ -472,7 +473,7 @@ static int selftest(void) { static int usage(const char *argv0) { fprintf(stderr, "usage: %s [--exefs DIR] [--romfs DIR] [--profile DIR] [--selftest | --probe-loader | " - "--probe-cfg | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " + "--probe-cfg | --probe-dpi | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " "--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | " "--probe-vkexec | --probe-astc | --probe-movie | --prepare-shaders | --warm-shaders | --boot |--seconds S [--shot PNG]]\n", argv0); return 2; @@ -559,6 +560,7 @@ int main(int argc, char **argv) { if (!strcmp(mode, "--selftest")) return selftest(); if (!strcmp(mode, "--probe-loader")) return probe_loader(); if (!strcmp(mode, "--probe-cfg")) return probe_cfg(); + if (!strcmp(mode, "--probe-dpi")) return probe_dpi(); if (!strcmp(mode, "--probe-kernel")) return probe_kernel(); if (!strcmp(mode, "--probe-ipc")) return probe_ipc(); if (!strcmp(mode, "--probe-services")) return probe_services(); diff --git a/runtime/swkbd_win.c b/runtime/swkbd_win.c index e319fc32..9d266978 100644 --- a/runtime/swkbd_win.c +++ b/runtime/swkbd_win.c @@ -217,8 +217,12 @@ static int text_entry_run(const SwkbdRequest *req, uint16_t *out, size_t cap, si Entry e = {.req = req, .armed = 0}; const char *test = cfg_get("TOTK_SWKBD_TEST"); if (test && *test) e.test = !strcmp(test, "cancel") ? -1 : 1; + // The process is per-monitor DPI aware: the font and the layout below are measured at the game window's DPI, the + // monitor the dialog opens on, not at the system DPI that plain SystemParametersInfo and AdjustWindowRectEx use. + HWND owner = gfx_window_handle(); + UINT dpi = owner ? GetDpiForWindow(owner) : GetDpiForSystem(); NONCLIENTMETRICSW ncm = {.cbSize = sizeof ncm}; - SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof ncm, &ncm, 0); + SystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, sizeof ncm, &ncm, 0, dpi); HFONT font = CreateFontIndirectW(&ncm.lfMessageFont); HDC dc = GetDC(NULL); HGDIOBJ old = SelectObject(dc, font); @@ -230,10 +234,9 @@ static int text_entry_run(const SwkbdRequest *req, uint16_t *out, size_t cap, si int y_prompt = pad, h_prompt = u * 5 / 2, y_edit = y_prompt + h_prompt + u / 4, h_edit = u * 9 / 5; int y_hint = y_edit + h_edit + u / 2, y_btn = y_hint + u * 3 / 2, ch = y_btn + bh + pad; - HWND owner = gfx_window_handle(); RECT wr = {0, 0, cw + 2 * pad, ch}; DWORD style = WS_POPUP | WS_CAPTION | WS_SYSMENU, ex = WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT; - AdjustWindowRectEx(&wr, style, FALSE, ex); + AdjustWindowRectExForDpi(&wr, style, FALSE, ex, dpi); int ww = wr.right - wr.left, wh = wr.bottom - wr.top, x = CW_USEDEFAULT, y = CW_USEDEFAULT; RECT orc; if (owner && GetWindowRect(owner, &orc)) { diff --git a/runtime/totk.manifest b/runtime/totk.manifest index 8fe37fa1..a0da154b 100644 --- a/runtime/totk.manifest +++ b/runtime/totk.manifest @@ -1,7 +1,11 @@ + and fs_host.c convert with, so an install folder or user profile with a non-ASCII name works. + dpiAwareness PerMonitorV2 (dpiAware true/pm on Windows 10 before 1607): window, monitor and cursor sizes are physical + pixels and the swapchain matches the screen. A DPI-unaware process gets them divided by the display scale and its + image stretched back up by Windows, so 4K at 150% presented a 2560x1440 image, blurred. gfx_window.c follows a + change of the window's DPI (WM_GETDPISCALEDSIZE, WM_DPICHANGED). --> @@ -13,6 +17,8 @@ UTF-8 + true/pm + PerMonitorV2, PerMonitor diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 803c4810..073c57e3 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -171,8 +171,13 @@ static ImGuiStyle g_base_style; // the menu's style at 1080 lines static uint64_t g_last_ns; static uint64_t g_cost_ns, g_cost_frames; // CPU time of layout and recording while open, logged per 600 frames -// Sizes are tuned for 1080 lines and follow the window's height; under 720 lines they stay at the 720 size. -static float menu_scale(uint32_t h) { return (h >= 720 ? h : 720) / 1080.0f; } +// Sizes are tuned for 1080 lines and follow the window's height. Under 720 lines at 100% display scale they stay at the +// 720 size; that floor grows with the window's DPI, so a small window on a 150% or 200% display (sizes are physical +// pixels) still gets text as large as the display scale asks for. +static float menu_scale(uint32_t h) { + float least = 720.0f * (float)gfx_window_dpi() / 96.0f; + return ((float)h >= least ? (float)h : least) / 1080.0f; +} // SavageWindRecomp's colours: dark green panels, gold accents. static void style_init(ImGuiStyle &s) { diff --git a/runtime/ui/menu.h b/runtime/ui/menu.h index 0bf7b6b7..1dffc8ff 100644 --- a/runtime/ui/menu.h +++ b/runtime/ui/menu.h @@ -59,6 +59,7 @@ void gfx_window_apply_settings(void); // TOTK_FULLSCREEN or TOTK_WINDOW_SIZE void gfx_window_request_close(void); // as the window's close button: the process ends int gfx_window_monitor_size(int *w, int *h); // pixels of the monitor the window is on; 0 when unknown double gfx_window_refresh_hz(void); // its refresh rate (TOTK_FPS_LIMIT=display); 0 when unknown +unsigned gfx_window_dpi(void); // the window's DPI (96 at 100% display scale); any thread // The present mode the swapchain got, as a TOTK_VK_PRESENT value ("fifo" when mailbox is missing); "" before the first // swapchain. Any thread. const char *gfx_window_present_mode(void); diff --git a/totk.ini.example b/totk.ini.example index d55d52e9..abccd570 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -95,7 +95,8 @@ [display] # Presentation mode: mailbox (default when the driver offers it, vsync otherwise), fifo (vsync) or immediate. # vk_present = mailbox -# Window client area in pixels (640x360 to 7680x4320), and borderless fullscreen. F11 switches fullscreen and saves it +# Window client area in screen pixels (640x360 to 7680x4320), and borderless fullscreen. Windows display scaling does +# not apply: at 150% scale, 1920x1080 covers the area 1280x720 covers at 100%. F11 switches fullscreen and saves it # here. Changes while the game runs take effect at once. # window_size = 1920x1080 # fullscreen = 0