In GTK/Linux, what's the correct way to get the DPI scale factor? In GTK/Linux, what's the correct way to get the DPI scale factor? linux linux

In GTK/Linux, what's the correct way to get the DPI scale factor?


I haven't seen a dpi getter in GTK, but with some lines of code from open sources and some changes, I usually ask the X-server with this code-snippet to get the dpi values for the x and y direction. For the displayname you can pass the value(s) you want via argv[].

If you name it 'getdpi.c' then compile with

gcc -Wall -std=c99  -o getdpi getdpi.c -lX11

If it helps you, a vote would be appreciated. :-)

#include <X11/Xlib.h>#include <stdio.h>#include <stdlib.h>const static unsigned int FALSE = 0;const static unsigned int TRUE = 1;typedef unsigned int bool;int getDpi(Display *dpy, int scr, bool xRes){    /*     * an inch is 25.4 millimeters.     * dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))     *     = N pixels / (M inch / 25.4)     *     = N * 25.4 pixels / M inch     */    double res = xRes ? ((((double) DisplayWidth(dpy,scr)) * 25.4) / ((double) DisplayWidthMM(dpy,scr)))        : ((((double) DisplayHeight(dpy,scr)) * 25.4) / ((double) DisplayHeightMM(dpy,scr)));    return (int) (res + 0.5);} // print_Screen_infoint main(int argc, char *argv[]){    Display *dpy;           /* X connection */    char *displayname = NULL;       /* set to what you want or need */    dpy = XOpenDisplay (displayname);    if (!dpy)     {    fprintf (stderr, "xdpi:  unable to open display \"%s\".\n",         XDisplayName (displayname));    exit (1);    }    for (int i = 0; i < ScreenCount(dpy); ++i)        printf ("Xdpi: %d, Ydpi %d\n",                 getDpi(dpy, i, TRUE),                getDpi(dpy, i, FALSE));} // main


export GDK_DPI_SCALE=1.5; export GDK_SCALE=1; run_your_application_to_be_scaled

What is more you can create a dedicated launcher only for a specific applications setting their own scale factor :-)

For Qt these are the variables:

export QT_AUTO_SCREEN_SET_FACTOR=0export QT_SCALE_FACTOR=1export QT_FONT_DPI=128

For EFL / Enlightenment these are the variables:

export ELM_SCALE=1.5