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