]> git.sur5r.net Git - i3/i3/blob - libi3/dpi.c
6f58d57a409c8eaa87728513c77bbd6a745bf674
[i3/i3] / libi3 / dpi.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2014 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include "libi3.h"
9 #include <math.h>
10
11 extern xcb_screen_t *root_screen;
12
13 /*
14  * Convert a logical amount of pixels (e.g. 2 pixels on a “standard” 96 DPI
15  * screen) to a corresponding amount of physical pixels on a standard or retina
16  * screen, e.g. 5 pixels on a 227 DPI MacBook Pro 13" Retina screen.
17  *
18  */
19 int logical_px(const int logical) {
20     if (root_screen == NULL) {
21         /* Dpi info may not be available when parsing a config without an X
22          * server, such as for config file validation. */
23         return logical;
24     }
25
26     const int dpi = (double)root_screen->height_in_pixels * 25.4 /
27                     (double)root_screen->height_in_millimeters;
28     /* There are many misconfigurations out there, i.e. systems with screens
29      * whose dpi is in fact higher than 96 dpi, but not significantly higher,
30      * so software was never adapted. We could tell people to reconfigure their
31      * systems to 96 dpi in order to get the behavior they expect/are used to,
32      * but since we can easily detect this case in code, let’s do it for them.
33      */
34     if ((dpi / 96.0) < 1.25)
35         return logical;
36     return ceil((dpi / 96.0) * logical);
37 }