Addressing the scaling factor on linux. (#107)

Use xrdb and the Xft.dpi variable to find out DPI scaling on linux.
This commit is contained in:
daubaris 2021-03-14 17:13:26 +02:00 committed by GitHub
parent 56a4584a1b
commit 27f3cb23d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <windows.h>
#elif __linux__
#include <unistd.h>
#include <stdlib.h>
#elif __APPLE__
#include <mach-o/dyld.h>
#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