From e049098733739664ffbeac6967568b2421a0cb2e Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 13 Jun 2024 15:35:11 +0100 Subject: [PATCH] Query XSETTINGS for the content scale factor For GTK-based systems using XSETTINGS it's much more likely to be available, rather than the "GDK_SCALE" environment variable, which is a debugging tool according to the GTK documentation. --- src/video/x11/SDL_x11modes.c | 9 +++++++-- src/video/x11/SDL_x11settings.c | 27 +++++++++++++++++++++++++++ src/video/x11/SDL_x11settings.h | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/video/x11/SDL_x11modes.c b/src/video/x11/SDL_x11modes.c index 52194d137..473da60f9 100644 --- a/src/video/x11/SDL_x11modes.c +++ b/src/video/x11/SDL_x11modes.c @@ -23,6 +23,7 @@ #ifdef SDL_VIDEO_DRIVER_X11 #include "SDL_x11video.h" +#include "SDL_x11settings.h" #include "edid.h" /* #define X11MODES_DEBUG */ @@ -230,9 +231,13 @@ static float GetGlobalContentScale(SDL_VideoDevice *_this) } } + /* If that failed, try the XSETTINGS key... */ + if (scale_factor <= 0.0) { + scale_factor = X11_GetXsettingsIntKey(_this, "Gdk/WindowScalingFactor", -1); + } + /* If that failed, try the GDK_SCALE envvar... */ - if (scale_factor <= 0.0) - { + if (scale_factor <= 0.0) { const char *scale_str = SDL_getenv("GDK_SCALE"); if (scale_str) { scale_factor = SDL_atoi(scale_str); diff --git a/src/video/x11/SDL_x11settings.c b/src/video/x11/SDL_x11settings.c index dc65003d1..33217b6f5 100644 --- a/src/video/x11/SDL_x11settings.c +++ b/src/video/x11/SDL_x11settings.c @@ -94,4 +94,31 @@ void X11_HandleXsettings(SDL_VideoDevice *_this, const XEvent *xevent) } } +int X11_GetXsettingsIntKey(SDL_VideoDevice *_this, const char *key, int fallback_value) { + SDL_VideoData *data = _this->driverdata; + SDLX11_SettingsData *xsettings_data = &data->xsettings_data; + XSettingsSetting *setting = NULL; + int res = fallback_value; + + + if (xsettings_data->xsettings) { + if (xsettings_client_get_setting(xsettings_data->xsettings, key, &setting) != XSETTINGS_SUCCESS) { + goto no_key; + } + + if (setting->type != XSETTINGS_TYPE_INT) { + goto no_key; + } + + res = setting->data.v_int; + } + +no_key: + if (setting) { + xsettings_setting_free(setting); + } + + return res; +} + #endif /* SDL_VIDEO_DRIVER_X11 */ diff --git a/src/video/x11/SDL_x11settings.h b/src/video/x11/SDL_x11settings.h index ad1f6a1d6..3a72cbac5 100644 --- a/src/video/x11/SDL_x11settings.h +++ b/src/video/x11/SDL_x11settings.h @@ -34,5 +34,6 @@ typedef struct X11_SettingsData { extern void X11_InitXsettings(SDL_VideoDevice *_this); extern void X11_QuitXsettings(SDL_VideoDevice *_this); extern void X11_HandleXsettings(SDL_VideoDevice *_this, const XEvent *xevent); +extern int X11_GetXsettingsIntKey(SDL_VideoDevice *_this, const char *key, int fallback_value); #endif /* SDL_x11settings_h_ */