Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PMv2 DPI awareness, add SDL_HINT_WINDOWS_DPI_AWARENESS #5393

Closed
wants to merge 8 commits into from
30 changes: 30 additions & 0 deletions include/SDL_hints.h
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,36 @@ extern "C" {
*/
#define SDL_HINT_WINDOWS_USE_D3D9EX "SDL_WINDOWS_USE_D3D9EX"

/**
* \brief Controls whether SDL will declare the process to be DPI aware.
*
* This hint must be set before initializing the video subsystem.
*
* The main purpose of declaring DPI awareness is to disable OS bitmap scaling of SDL windows on monitors with
* a DPI scale factor.
*
* This hint is equivalent to requesting DPI awareness via external means (e.g. calling SetProcessDpiAwarenessContext)
* and does not cause SDL to use a virtualized coordinate system, so it will generally give you 1 SDL coordinate = 1 pixel
* even on high-DPI displays.
*
* For more information, see:
* https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows
*
* This variable can be set to the following values:
* "" - Do not change the DPI awareness (default).
* "unaware" - Declare the process as DPI unaware. (Windows 8.1 and later).
* "system" - Request system DPI awareness. (Vista and later).
* "permonitor" - Request per-monitor DPI awareness. (Windows 8.1 and later).
* "permonitorv2" - Request per-monitor V2 DPI awareness. (Windows 10, version 1607 and later).
* The most visible difference from "permonitor" is that window title bar will be scaled
* to the visually correct size when dragging between monitors with different scale factors.
* This is the preferred DPI awareness level.
*
* If the requested DPI awareness is not available on the currently running OS, SDL will try to request the best
* available match.
*/
#define SDL_HINT_WINDOWS_DPI_AWARENESS "SDL_WINDOWS_DPI_AWARENESS"

/**
* \brief A variable controlling whether the window frame and title bar are interactive when the cursor is hidden
*
Expand Down
168 changes: 167 additions & 1 deletion src/video/windows/SDL_windowsevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "../../events/SDL_touch_c.h"
#include "../../events/scancodes_windows.h"
#include "SDL_hints.h"
#include "SDL_log.h"

/* Dropfile support */
#include <shellapi.h>
Expand All @@ -52,6 +53,8 @@
#include "wmmsg.h"
#endif

/* #define HIGHDPI_DEBUG */

/* Masks for processing the windows KEYDOWN and KEYUP messages */
#define REPEATED_KEYMASK (1<<30)
#define EXTENDED_KEYMASK (1<<24)
Expand Down Expand Up @@ -86,6 +89,12 @@
#ifndef WM_UNICHAR
#define WM_UNICHAR 0x0109
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
#ifndef WM_GETDPISCALEDSIZE
#define WM_GETDPISCALEDSIZE 0x02E4
#endif

#ifndef IS_HIGH_SURROGATE
#define IS_HIGH_SURROGATE(x) (((x) >= 0xd800) && ((x) <= 0xdbff))
Expand Down Expand Up @@ -1079,14 +1088,25 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
inside their function, so I have to do it here.
*/
BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
UINT dpi;

dpi = 96;
size.top = 0;
size.left = 0;
size.bottom = h;
size.right = w;

AdjustWindowRectEx(&size, style, menu, 0);
if (WIN_IsPerMonitorV2DPIAware(SDL_GetVideoDevice())) {
dpi = data->videodata->GetDpiForWindow(hwnd);
data->videodata->AdjustWindowRectExForDpi(&size, style, menu, 0, dpi);
} else {
AdjustWindowRectEx(&size, style, menu, 0);
}
w = size.right - size.left;
h = size.bottom - size.top;
#ifdef HIGHDPI_DEBUG
SDL_Log("WM_GETMINMAXINFO: max window size: %dx%d using dpi: %u", w, h, dpi);
#endif
}

/* Fix our size to the current size */
Expand Down Expand Up @@ -1154,6 +1174,12 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
h = rect.bottom - rect.top;
SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w, h);

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_WINDOWPOSCHANGED: Windows client rect (pixels): (%d, %d) (%d x %d)\tSDL client rect: (%d, %d) (%d x %d)\tGetDpiForWindow: %d",
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
x, y, w, h, data->videodata->GetDpiForWindow ? (int)data->videodata->GetDpiForWindow(data->hwnd) : 0);
#endif

/* Forces a WM_PAINT event */
InvalidateRect(hwnd, NULL, FALSE);

Expand Down Expand Up @@ -1399,6 +1425,146 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
}
break;

case WM_GETDPISCALEDSIZE:
/* Windows 10 Creators Update+ */
/* Documented as only being sent to windows that are per-monitor V2 DPI aware.

Experimentation shows it's only sent during interactive dragging, not in response to
SetWindowPos. */
if (data->videodata->GetDpiForWindow && data->videodata->AdjustWindowRectExForDpi) {
/* Windows expects applications to scale their window rects linearly
when dragging between monitors with different DPI's.
e.g. a 100x100 window dragged to a 200% scaled monitor
becomes 200x200.

For SDL, we instead want the client size to scale linearly.
This is not the same as the window rect scaling linearly,
because Windows doesn't scale the non-client area (titlebar etc.)
linearly. So, we need to handle this message to request custom
scaling. */

const int nextDPI = (int)wParam;
const int prevDPI = (int)data->videodata->GetDpiForWindow(hwnd);
SIZE *sizeInOut = (SIZE *)lParam;

int frame_w, frame_h;
int query_client_w_win, query_client_h_win;

const DWORD style = GetWindowLong(hwnd, GWL_STYLE);
const BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_GETDPISCALEDSIZE: current DPI: %d potential DPI: %d input size: (%dx%d)",
prevDPI, nextDPI, sizeInOut->cx, sizeInOut->cy);
#endif

/* Subtract the window frame size that would have been used at prevDPI */
{
RECT rect = {0};

if (!(data->window->flags & SDL_WINDOW_BORDERLESS)) {
data->videodata->AdjustWindowRectExForDpi(&rect, style, menu, 0, prevDPI);
}

frame_w = -rect.left + rect.right;
frame_h = -rect.top + rect.bottom;

query_client_w_win = sizeInOut->cx - frame_w;
query_client_h_win = sizeInOut->cy - frame_h;
}

/* Add the window frame size that would be used at nextDPI */
{
RECT rect = {0};
rect.right = query_client_w_win;
rect.bottom = query_client_h_win;

if (!(data->window->flags & SDL_WINDOW_BORDERLESS)) {
data->videodata->AdjustWindowRectExForDpi(&rect, style, menu, 0, nextDPI);
}

/* This is supposed to control the suggested rect param of WM_DPICHANGED */
sizeInOut->cx = rect.right - rect.left;
sizeInOut->cy = rect.bottom - rect.top;
}

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_GETDPISCALEDSIZE: output size: (%dx%d)", sizeInOut->cx, sizeInOut->cy);
#endif
return TRUE;
}
break;

case WM_DPICHANGED:
/* Windows 8.1+ */
{
const int newDPI = HIWORD(wParam);
RECT* const suggestedRect = (RECT*)lParam;
int w, h;

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_DPICHANGED: to %d\tsuggested rect: (%d, %d), (%dx%d)\n", newDPI,
suggestedRect->left, suggestedRect->top, suggestedRect->right - suggestedRect->left, suggestedRect->bottom - suggestedRect->top);
#endif

if (data->expected_resize) {
/* This DPI change is coming from an explicit SetWindowPos call within SDL.
Assume all call sites are calculating the DPI-aware frame correctly, so
we don't need to do any further adjustment. */
#ifdef HIGHDPI_DEBUG
SDL_Log("WM_DPICHANGED: Doing nothing, assuming window is already sized correctly");
#endif
return 0;
}

/* Interactive user-initiated resizing/movement */

if (WIN_IsPerMonitorV2DPIAware(SDL_GetVideoDevice())) {
/* WM_GETDPISCALEDSIZE should have been called prior, so we can trust the given
suggestedRect. */
w = suggestedRect->right - suggestedRect->left;
h = suggestedRect->bottom - suggestedRect->top;

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_DPICHANGED: using suggestedRect");
#endif
} else {
/* permonitor and earlier DPI awareness: calculate the new frame w/h such that
the client area size is maintained. */
const DWORD style = GetWindowLong(hwnd, GWL_STYLE);
const BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);

RECT rect = {0};
rect.right = data->window->w;
rect.bottom = data->window->h;

if (!(data->window->flags & SDL_WINDOW_BORDERLESS)) {
AdjustWindowRectEx(&rect, style, menu, 0);
}

w = rect.right - rect.left;
h = rect.bottom - rect.top;
}

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_DPICHANGED: current SDL window size: (%dx%d)\tcalling SetWindowPos: (%d, %d), (%dx%d)\n",
data->window->w, data->window->h,
suggestedRect->left, suggestedRect->top, w, h);
#endif

data->expected_resize = SDL_TRUE;
SetWindowPos(hwnd,
NULL,
suggestedRect->left,
suggestedRect->top,
w,
h,
SWP_NOZORDER | SWP_NOACTIVATE);
data->expected_resize = SDL_FALSE;
return 0;
}
break;
}

/* If there's a window proc, assume it's going to handle messages */
Expand Down
Loading