Implement xrdb reading without popen

Avoid using the shell, awk and cut command
This commit is contained in:
Francesco Abbate
2021-03-17 16:58:44 +01:00
parent 27f3cb23d4
commit dab39d3b5c
4 changed files with 152 additions and 29 deletions
+6 -29
View File
@@ -7,7 +7,7 @@
#include <windows.h>
#elif __linux__
#include <unistd.h>
#include <stdlib.h>
#include "xrdb_parse.h"
#elif __APPLE__
#include <mach-o/dyld.h>
#endif
@@ -15,40 +15,17 @@
SDL_Window *window;
static double get_scale(void) {
#if _WIN32
float dpi;
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;
int xft_dpi = xrdb_find_dpi();
if (xft_dpi > 0) {
return xft_dpi / 96.0;
}
if (pclose(stream)) {
free(line);
return 1.0;
}
dpi = strtol(line, NULL, 10) / 96.0;
free(line);
return dpi;
return 1.0;
#else
return 1.0;
#endif