]> git.sur5r.net Git - i3/i3/blob - libi3/dpi.c
Use lround instead of (long)round
[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     char *resource = NULL;
28
29     if (conn == NULL) {
30         goto init_dpi_end;
31     }
32
33     database = xcb_xrm_database_from_default(conn);
34     if (database == NULL) {
35         ELOG("Failed to open the resource database.\n");
36         goto init_dpi_end;
37     }
38
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     double in_dpi = strtod(resource, &endptr);
47     if (in_dpi == HUGE_VAL || 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     dpi = lround(in_dpi);
53
54     DLOG("Found Xft.dpi = %ld.\n", dpi);
55
56 init_dpi_end:
57     free(resource);
58
59     if (database != NULL) {
60         xcb_xrm_database_free(database);
61     }
62
63     if (dpi == 0) {
64         DLOG("Using fallback for calculating DPI.\n");
65         dpi = init_dpi_fallback();
66         DLOG("Using dpi = %ld\n", dpi);
67     }
68 }
69
70 /*
71  * This function returns the value of the DPI setting.
72  *
73  */
74 long get_dpi_value(void) {
75     return dpi;
76 }
77
78 /*
79  * Convert a logical amount of pixels (e.g. 2 pixels on a “standard” 96 DPI
80  * screen) to a corresponding amount of physical pixels on a standard or retina
81  * screen, e.g. 5 pixels on a 227 DPI MacBook Pro 13" Retina screen.
82  *
83  */
84 int logical_px(const int logical) {
85     if (root_screen == NULL) {
86         /* Dpi info may not be available when parsing a config without an X
87          * server, such as for config file validation. */
88         return logical;
89     }
90
91     /* There are many misconfigurations out there, i.e. systems with screens
92      * whose dpi is in fact higher than 96 dpi, but not significantly higher,
93      * so software was never adapted. We could tell people to reconfigure their
94      * systems to 96 dpi in order to get the behavior they expect/are used to,
95      * but since we can easily detect this case in code, let’s do it for them.
96      */
97     if ((dpi / 96.0) < 1.25)
98         return logical;
99     return ceil((dpi / 96.0) * logical);
100 }