Changes related to dpi.

Introduced new function mfb_get_monitor_scale
Deprecated function mfb_get_monitor_dpi
This commit is contained in:
Carlos Aragones 2021-02-19 10:51:58 +01:00
parent 17df5633cd
commit fa8bf266f0
9 changed files with 265 additions and 192 deletions

View File

@ -232,6 +232,8 @@ int mfb_get_mouse_x(struct mfb_window *window); // L
int mfb_get_mouse_y(struct mfb_window *window); // Last mouse pos Y int mfb_get_mouse_y(struct mfb_window *window); // Last mouse pos Y
// Not working on Linux (X11 nor Wayland) // Not working on Linux (X11 nor Wayland)
void mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y)
// [Deprecated] Use mfb_get_monitor_scale instead
void mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) void mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y)
``` ```

View File

@ -39,7 +39,10 @@ void * mfb_get_user_data(struct mfb_window *window);
bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height); bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
// DPI // DPI
// [Deprecated]: Probably a better name will be mfb_get_monitor_scale
void mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y); void mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y);
// Use this instead
void mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y);
// Callbacks // Callbacks
void mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback); void mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback);

View File

@ -107,6 +107,13 @@ mfb_get_user_data(struct mfb_window *window) {
return 0x0; return 0x0;
} }
// [Deprecated]
//-------------------------------------
void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) {
mfb_get_monitor_scale(window, dpi_x, dpi_y);
}
//------------------------------------- //-------------------------------------
void void
mfb_close(struct mfb_window *window) { mfb_close(struct mfb_window *window) {

View File

@ -250,23 +250,23 @@ mfb_timer_init() {
//------------------------------------- //-------------------------------------
void void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) { mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y) {
(void) window; (void) window;
float scale = 1.0f; float scale = 1.0f;
scale = [[UIScreen mainScreen] scale]; scale = [[UIScreen mainScreen] scale];
if (dpi_x) { if (scale_x) {
*dpi_x = scale; *scale_x = scale;
if(*dpi_x == 0) { if(*scale_x == 0) {
*dpi_x = 1; *scale_x = 1;
} }
} }
if (dpi_y) { if (scale_y) {
*dpi_y = scale; *scale_y = scale;
if (*dpi_y == 0) { if (*scale_y == 0) {
*dpi_y = 1; *scale_y = 1;
} }
} }
} }

View File

@ -553,7 +553,7 @@ mfb_timer_init() {
//------------------------------------- //-------------------------------------
void void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) { mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y) {
float scale = 1.0f; float scale = 1.0f;
if(window != 0x0) { if(window != 0x0) {
@ -566,18 +566,17 @@ mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) {
scale = [[NSScreen mainScreen] backingScaleFactor]; scale = [[NSScreen mainScreen] backingScaleFactor];
} }
if (dpi_x) { if (scale_x) {
*dpi_x = scale; *scale_x = scale;
if(*dpi_x == 0) { if(*scale_x == 0) {
*dpi_x = 1; *scale_x = 1;
} }
} }
if (dpi_y) { if (scale_y) {
*dpi_y = scale; *scale_y = scale;
if (*dpi_y == 0) { if (*scale_y == 0) {
*dpi_y = 1; *scale_y = 1;
} }
} }
} }

View File

@ -999,7 +999,7 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) { mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y) {
float x = 96.0, y = 96.0; float x = 96.0, y = 96.0;
if(window != 0x0) { if(window != 0x0) {
@ -1009,17 +1009,17 @@ mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) {
// I cannot find a way to get dpi under VirtualBox // I cannot find a way to get dpi under VirtualBox
} }
if (dpi_x) { if (scale_x) {
*dpi_x = x / 96.0f; *scale_x = x / 96.0f;
if(*dpi_x == 0) { if(*scale_x == 0) {
*dpi_x = 1.0f; *scale_x = 1.0f;
} }
} }
if (dpi_y) { if (scale_y) {
*dpi_y = y / 96.0f; *scale_y = y / 96.0f;
if (*dpi_y == 0) { if (*scale_y == 0) {
*dpi_y = 1.0f; *scale_y = 1.0f;
} }
} }
} }

View File

@ -5,11 +5,8 @@
#if defined(USE_OPENGL_API) #if defined(USE_OPENGL_API)
#include "gl/MiniFB_GL.h" #include "gl/MiniFB_GL.h"
#endif #endif
#if defined(_DEBUG) || defined(DEBUG)
#include <stdio.h> #include <stdio.h>
#endif
#include <stdlib.h> #include <stdlib.h>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copied (and modified) from Windows Kit 10 to avoid setting _WIN32_WINNT to a higher version // Copied (and modified) from Windows Kit 10 to avoid setting _WIN32_WINNT to a higher version
@ -35,10 +32,14 @@ typedef enum mfb_MONITOR_DPI_TYPE {
// user32.dll // user32.dll
typedef BOOL(WINAPI *PFN_SetProcessDPIAware)(void); typedef BOOL(WINAPI *PFN_SetProcessDPIAware)(void);
typedef BOOL(WINAPI *PFN_SetProcessDpiAwarenessContext)(HANDLE); typedef BOOL(WINAPI *PFN_SetProcessDpiAwarenessContext)(HANDLE);
typedef UINT(WINAPI *PFN_GetDpiForWindow)(HWND);
typedef BOOL(WINAPI *PFN_EnableNonClientDpiScaling)(HWND);
HMODULE mfb_user32_dll = 0x0; HMODULE mfb_user32_dll = 0x0;
PFN_SetProcessDPIAware mfb_SetProcessDPIAware = 0x0; PFN_SetProcessDPIAware mfb_SetProcessDPIAware = 0x0;
PFN_SetProcessDpiAwarenessContext mfb_SetProcessDpiAwarenessContext = 0x0; PFN_SetProcessDpiAwarenessContext mfb_SetProcessDpiAwarenessContext = 0x0;
PFN_GetDpiForWindow mfb_GetDpiForWindow = 0x0;
PFN_EnableNonClientDpiScaling mfb_EnableNonClientDpiScaling = 0x0;
// shcore.dll // shcore.dll
typedef HRESULT(WINAPI *PFN_SetProcessDpiAwareness)(mfb_PROCESS_DPI_AWARENESS); typedef HRESULT(WINAPI *PFN_SetProcessDpiAwareness)(mfb_PROCESS_DPI_AWARENESS);
@ -56,6 +57,8 @@ load_functions() {
if (mfb_user32_dll != 0x0) { if (mfb_user32_dll != 0x0) {
mfb_SetProcessDPIAware = (PFN_SetProcessDPIAware) GetProcAddress(mfb_user32_dll, "SetProcessDPIAware"); mfb_SetProcessDPIAware = (PFN_SetProcessDPIAware) GetProcAddress(mfb_user32_dll, "SetProcessDPIAware");
mfb_SetProcessDpiAwarenessContext = (PFN_SetProcessDpiAwarenessContext) GetProcAddress(mfb_user32_dll, "SetProcessDpiAwarenessContext"); mfb_SetProcessDpiAwarenessContext = (PFN_SetProcessDpiAwarenessContext) GetProcAddress(mfb_user32_dll, "SetProcessDpiAwarenessContext");
mfb_GetDpiForWindow = (PFN_GetDpiForWindow) GetProcAddress(mfb_user32_dll, "GetDpiForWindow");
mfb_EnableNonClientDpiScaling = (PFN_EnableNonClientDpiScaling) GetProcAddress(mfb_user32_dll, "EnableNonClientDpiScaling");
} }
} }
@ -68,23 +71,56 @@ load_functions() {
} }
} }
//--
// NOT Thread safe. Just convenient (Don't do this at home guys)
char *
GetErrorMessage() {
static char buffer[256];
buffer[0] = 0;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, // Not used with FORMAT_MESSAGE_FROM_SYSTEM
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buffer,
sizeof(buffer),
NULL);
return buffer;
}
//-- //--
void void
dpi_aware() { dpi_aware() {
if (mfb_SetProcessDpiAwarenessContext != 0x0) { if (mfb_SetProcessDpiAwarenessContext != 0x0) {
mfb_SetProcessDpiAwarenessContext(mfb_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); if(mfb_SetProcessDpiAwarenessContext(mfb_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) == false) {
uint32_t error = GetLastError();
if(error == ERROR_INVALID_PARAMETER) {
error = NO_ERROR;
if(mfb_SetProcessDpiAwarenessContext(mfb_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE) == false) {
error = GetLastError();
}
}
if(error != NO_ERROR) {
fprintf(stderr, "Error (SetProcessDpiAwarenessContext): %s\n", GetErrorMessage());
}
}
} }
else if (mfb_SetProcessDpiAwareness != 0x0) { else if (mfb_SetProcessDpiAwareness != 0x0) {
mfb_SetProcessDpiAwareness(mfb_PROCESS_PER_MONITOR_DPI_AWARE); if(mfb_SetProcessDpiAwareness(mfb_PROCESS_PER_MONITOR_DPI_AWARE) != S_OK) {
fprintf(stderr, "Error (SetProcessDpiAwareness): %s\n", GetErrorMessage());
}
} }
else if (mfb_SetProcessDPIAware != 0x0) { else if (mfb_SetProcessDPIAware != 0x0) {
mfb_SetProcessDPIAware(); if(mfb_SetProcessDPIAware() == false) {
fprintf(stderr, "Error (SetProcessDPIAware): %s\n", GetErrorMessage());
}
} }
} }
//-- //--
void void
get_monitor_dpi(HWND hWnd, float *dpi_x, float *dpi_y) { get_monitor_scale(HWND hWnd, float *scale_x, float *scale_y) {
UINT x, y; UINT x, y;
if(mfb_GetDpiForMonitor != 0x0) { if(mfb_GetDpiForMonitor != 0x0) {
@ -98,17 +134,17 @@ get_monitor_dpi(HWND hWnd, float *dpi_x, float *dpi_y) {
ReleaseDC(NULL, dc); ReleaseDC(NULL, dc);
} }
if (dpi_x) { if (scale_x) {
*dpi_x = x / (float) USER_DEFAULT_SCREEN_DPI; *scale_x = x / (float) USER_DEFAULT_SCREEN_DPI;
if(*dpi_x == 0) { if(*scale_x == 0) {
*dpi_x = 1; *scale_x = 1;
} }
} }
if (dpi_y) { if (scale_y) {
*dpi_y = y / (float) USER_DEFAULT_SCREEN_DPI; *scale_y = y / (float) USER_DEFAULT_SCREEN_DPI;
if (*dpi_y == 0) { if (*scale_y == 0) {
*dpi_y = 1; *scale_y = 1;
} }
} }
} }
@ -116,7 +152,7 @@ get_monitor_dpi(HWND hWnd, float *dpi_x, float *dpi_y) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) { mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y) {
HWND hWnd = 0x0; HWND hWnd = 0x0;
if(window != 0x0) { if(window != 0x0) {
@ -124,7 +160,7 @@ mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) {
SWindowData_Win *window_data_win = (SWindowData_Win *) window_data->specific; SWindowData_Win *window_data_win = (SWindowData_Win *) window_data->specific;
hWnd = window_data_win->window; hWnd = window_data_win->window;
} }
get_monitor_dpi(hWnd, dpi_x, dpi_y); get_monitor_scale(hWnd, scale_x, scale_y);
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -151,6 +187,31 @@ WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) switch (message)
{ {
case WM_NCCREATE:
{
if(mfb_EnableNonClientDpiScaling)
mfb_EnableNonClientDpiScaling(hWnd);
return DefWindowProc(hWnd, message, wParam, lParam);;
}
// TODO
//case 0x02E4://WM_GETDPISCALEDSIZE:
//{
// SIZE* size = (SIZE*) lParam;
// WORD dpi = LOWORD(wParam);
// return true;
// break;
//}
// TODO
//case WM_DPICHANGED:
//{
// const float xscale = HIWORD(wParam);
// const float yscale = LOWORD(wParam);
// break;
//}
#if !defined(USE_OPENGL_API) #if !defined(USE_OPENGL_API)
case WM_PAINT: case WM_PAINT:
{ {
@ -292,14 +353,14 @@ WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
case WM_SIZE: case WM_SIZE:
if (window_data) { if (window_data) {
float scaleX, scaleY; float scale_x, scale_y;
uint32_t width, height; uint32_t width, height;
if(wParam == SIZE_MINIMIZED) { if(wParam == SIZE_MINIMIZED) {
return res; return res;
} }
get_monitor_dpi(hWnd, &scaleX, &scaleY); get_monitor_scale(hWnd, &scale_x, &scale_y);
window_data->window_width = LOWORD(lParam); window_data->window_width = LOWORD(lParam);
window_data->window_height = HIWORD(lParam); window_data->window_height = HIWORD(lParam);
resize_dst(window_data, window_data->window_width, window_data->window_height); resize_dst(window_data, window_data->window_width, window_data->window_height);
@ -310,8 +371,8 @@ WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
resize_GL(window_data); resize_GL(window_data);
#endif #endif
if(window_data->window_width != 0 && window_data->window_height != 0) { if(window_data->window_width != 0 && window_data->window_height != 0) {
width = (uint32_t) (window_data->window_width / scaleX); width = (uint32_t) (window_data->window_width / scale_x);
height = (uint32_t) (window_data->window_height / scaleY); height = (uint32_t) (window_data->window_height / scale_y);
kCall(resize_func, width, height); kCall(resize_func, width, height);
} }
} }
@ -417,11 +478,12 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
} }
} }
else if (!(flags & WF_FULLSCREEN)) { else if (!(flags & WF_FULLSCREEN)) {
float dpi_x, dpi_y; float scale_x, scale_y;
get_monitor_dpi(0, &dpi_x, &dpi_y);
rect.right = (LONG) (width * dpi_x); get_monitor_scale(0, &scale_x, &scale_y);
rect.bottom = (LONG) (height * dpi_y);
rect.right = (LONG) (width * scale_x);
rect.bottom = (LONG) (height * scale_y);
AdjustWindowRect(&rect, s_window_style, 0); AdjustWindowRect(&rect, s_window_style, 0);
@ -847,7 +909,7 @@ bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) { mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window; SWindowData *window_data = (SWindowData *) window;
SWindowData_Win *window_data_win = 0x0; SWindowData_Win *window_data_win = 0x0;
float scaleX, scaleY; float scale_x, scale_y;
if(window_data == 0x0) { if(window_data == 0x0) {
return false; return false;
@ -862,12 +924,12 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
window_data_win = (SWindowData_Win *) window_data->specific; window_data_win = (SWindowData_Win *) window_data->specific;
get_monitor_dpi(window_data_win->window, &scaleX, &scaleY); get_monitor_scale(window_data_win->window, &scale_x, &scale_y);
window_data->dst_offset_x = (uint32_t) (offset_x * scaleX); window_data->dst_offset_x = (uint32_t) (offset_x * scale_x);
window_data->dst_offset_y = (uint32_t) (offset_y * scaleY); window_data->dst_offset_y = (uint32_t) (offset_y * scale_y);
window_data->dst_width = (uint32_t) (width * scaleX); window_data->dst_width = (uint32_t) (width * scale_x);
window_data->dst_height = (uint32_t) (height * scaleY); window_data->dst_height = (uint32_t) (height * scale_y);
calc_dst_factor(window_data, window_data->window_width, window_data->window_height); calc_dst_factor(window_data, window_data->window_width, window_data->window_height);

View File

@ -798,7 +798,7 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) { mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y) {
float x = 96.0, y = 96.0; float x = 96.0, y = 96.0;
if(window != 0x0) { if(window != 0x0) {
@ -812,17 +812,17 @@ mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) {
// All returning invalid values or 0 // All returning invalid values or 0
} }
if (dpi_x) { if (scale_x) {
*dpi_x = x / 96.0f; *scale_x = x / 96.0f;
if(*dpi_x == 0) { if(*scale_x == 0) {
*dpi_x = 1.0f; *scale_x = 1.0f;
} }
} }
if (dpi_y) { if (scale_y) {
*dpi_y = y / 96.0f; *scale_y = y / 96.0f;
if (*dpi_y == 0) { if (*scale_y == 0) {
*dpi_y = 1.0f; *scale_y = 1.0f;
} }
} }
} }

View File

@ -91,7 +91,7 @@ resize(struct mfb_window *window, int width, int height) {
kUnused(launchOptions); kUnused(launchOptions);
if(g_window == 0x0) { if(g_window == 0x0) {
mfb_get_monitor_dpi(0x0, &g_scale, 0x0); mfb_get_monitor_scale(0x0, &g_scale, 0x0);
//g_scale = [UIScreen mainScreen].scale; //g_scale = [UIScreen mainScreen].scale;
g_width = [UIScreen mainScreen].bounds.size.width * g_scale; g_width = [UIScreen mainScreen].bounds.size.width * g_scale;
g_height = [UIScreen mainScreen].bounds.size.height * g_scale; g_height = [UIScreen mainScreen].bounds.size.height * g_scale;