]> git.sur5r.net Git - i3/i3/blob - libi3/dpi.c
Use Xft.dpi for DPI if available.
[i3/i3] / libi3 / dpi.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include "libi3.h"
9 #include <math.h>
10 #include <stdlib.h>
11 #include <xcb/xcb_xrm.h>
12
13 static long dpi;
14
15 static long init_dpi_fallback(void) {
16     return (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters;
17 }
18
19 /*
20  * Initialize the DPI setting.
21  * This will use the 'Xft.dpi' X resource if available and fall back to
22  * guessing the correct value otherwise.
23  */
24 void init_dpi(void) {
25     xcb_xrm_database_t *database = NULL;
26
27     if (conn == NULL) {
28         goto init_dpi_end;
29     }
30
31     database = xcb_xrm_database_from_default(conn);
32     if (database == NULL) {
33         ELOG("Failed to open the resource database.\n");
34         goto init_dpi_end;
35     }
36
37     char *resource;
38     xcb_xrm_resource_get_string(database, "Xft.dpi", NULL, &resource);
39     if (resource == NULL) {
40         DLOG("Resource Xft.dpi not specified, skipping.\n");
41         goto init_dpi_end;
42     }
43
44     char *endptr;
45     dpi = strtol(resource, &endptr, 10);
46     if (dpi == LONG_MAX || dpi == LONG_MIN || dpi < 0 || *endptr != '\0' || endptr == resource) {
47         ELOG("Xft.dpi = %s is an invalid number and couldn't be parsed.\n", resource);
48         dpi = 0;
49         goto init_dpi_end;
50     }
51
52     DLOG("Found Xft.dpi = %ld.\n", dpi);
53
54 init_dpi_end:
55     if (database != NULL) {
56         xcb_xrm_database_free(database);
57     }
58
59     if (dpi == 0) {
60         DLOG("Using fallback for calculating DPI.\n");
61         dpi = init_dpi_fallback();
62         DLOG("Using dpi = %ld\n", dpi);
63     }
64 }
65
66 /*
67  * Convert a logical amount of pixels (e.g. 2 pixels on a “standard” 96 DPI
68  * screen) to a corresponding amount of physical pixels on a standard or retina
69  * screen, e.g. 5 pixels on a 227 DPI MacBook Pro 13" Retina screen.
70  *
71  */
72 int logical_px(const int logical) {
73     if (root_screen == NULL) {
74         /* Dpi info may not be available when parsing a config without an X
75          * server, such as for config file validation. */
76         return logical;
77     }
78
79     /* There are many misconfigurations out there, i.e. systems with screens
80      * whose dpi is in fact higher than 96 dpi, but not significantly higher,
81      * so software was never adapted. We could tell people to reconfigure their
82      * systems to 96 dpi in order to get the behavior they expect/are used to,
83      * but since we can easily detect this case in code, let’s do it for them.
84      */
85     if ((dpi / 96.0) < 1.25)
86         return logical;
87     return ceil((dpi / 96.0) * logical);
88 }