首页 文章

如何在X11中获得系统比例因子

提问于
浏览
2

我想制作我的应用程序,它纯粹是在X11中,具有高DPI感知能力 . 为此,我需要一种方法来找出在显示设置中配置的系统比例因子 . 有没有办法从X11应用程序获得这个系统比例因子而不诉诸GTK等更高级别的API?

FWIW,我检查了GTK源代码,看看 gdk_window_get_scale_factor() 是如何做到的,它似乎读取了一个名为 GDK_SCALE 的环境变量 . 但是,此环境变量在我的4K显示器上未设置为1.75 .

那么如何以编程方式检索系统缩放因子呢?

1 回答

  • 0

    为了回答我自己的问题,我现在尝试了三种方法:

    • XRandR

    • X11的 DisplayWidth/HeightDisplayWidthMM/HeightMM

    • 查看 xdpyinfo 输出

    都没有返回正确的DPI . 相反, Xft.dpi Xresource似乎是这个问题的关键 . Xft.dpi 似乎总是带有正确的DPI,因此我们只需读取它即可获得系统比例因子 .

    以下是here的一些来源:

    #include <X11/Xlib.h>
    #include <X11/Xatom.h>
    #include <X11/Xresource.h>
    
    double _glfwPlatformGetMonitorDPI(_GLFWmonitor* monitor)
    {
        char *resourceString = XResourceManagerString(_glfw.x11.display);
        XrmDatabase db;
        XrmValue value;
        char *type = NULL;
        double dpi = 0.0;
    
        XrmInitialize(); /* Need to initialize the DB before calling Xrm* functions */
    
        db = XrmGetStringDatabase(resourceString);
    
        if (resourceString) {
            printf("Entire DB:\n%s\n", resourceString);
            if (XrmGetResource(db, "Xft.dpi", "String", &type, &value) == True) {
                if (value.addr) {
                    dpi = atof(value.addr);
                }
            }
        }
    
        printf("DPI: %f\n", dpi);
        return dpi;
    }
    

    这对我来说很有用 .

相关问题