aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.cpp
blob: 2c3bfb70f51b376a1a12e348f549c479a9f44261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "../include/Utils.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>

namespace QuickMedia {
    static float scale = 1.0f;
    static bool scale_set = false;

    static const int XFT_DPI_DEFAULT = 96;
    // Returns 96 on error
    static int xrdb_get_dpi() {
        int xft_dpi = XFT_DPI_DEFAULT;

        FILE *xrdb_query = popen("xrdb -query", "r");
        if(!xrdb_query)
            return xft_dpi;

        char line[512];
        while(fgets(line, sizeof(line), xrdb_query)) {
            int line_length = strlen(line);
            if(line_length > 0 && line[line_length - 1] == '\n') {
                line[line_length - 1] = '\0';
                line_length--;
            }

            if(line_length > 8 && memcmp(line, "Xft.dpi:", 8) == 0) {
                int xft_dpi_file = atoi(line + 8);
                if(xft_dpi_file > 0) {
                    xft_dpi = xft_dpi_file;
                    break;
                }
            }
        }

        pclose(xrdb_query);
        return xft_dpi;
    }

    float get_ui_scale() {
        if(scale_set)
            return scale;

        char *gdk_scale = getenv("GDK_SCALE");
        if(gdk_scale) {
            setlocale(LC_ALL, "C"); // Sigh... stupid C
            scale = atof(gdk_scale);
            if(scale < 0.0001f)
                scale = 1.0f;
        } else {
            scale = (float)xrdb_get_dpi() / (float)XFT_DPI_DEFAULT;
        }

        scale_set = true;
        return scale;
    }
}