From 27f3cb23d4ca5fef65477205e69e5b7a33b4e380 Mon Sep 17 00:00:00 2001 From: daubaris Date: Sun, 14 Mar 2021 17:13:26 +0200 Subject: [PATCH] Addressing the scaling factor on linux. (#107) Use xrdb and the Xft.dpi variable to find out DPI scaling on linux. --- src/main.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main.c b/src/main.c index 47998e5d..daf3dc3f 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ #include #elif __linux__ #include + #include #elif __APPLE__ #include #endif @@ -20,6 +21,34 @@ static double get_scale(void) { SDL_GetDisplayDPI(0, NULL, &dpi, NULL); #if _WIN32 return dpi / 96.0; +#elif __linux__ + FILE *stream; + char *xrdb = "xrdb -query | grep dpi | cut -f 2"; + + // In case something goes wrong in popen (e.g., fork or exec calls) or any + // other calls listed below we return the dpi equal to 1. + if ((stream = popen(xrdb, "r")) == NULL) + return 1.0; + + char *line = NULL; + size_t buffer = 0; + ssize_t length = getline(&line, &buffer, stream); + + if (length == -1) { + free(line); + pclose(stream); + return 1.0; + } + + if (pclose(stream)) { + free(line); + return 1.0; + } + + dpi = strtol(line, NULL, 10) / 96.0; + free(line); + + return dpi; #else return 1.0; #endif