2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
12 #include <xcb/xcb_xrm.h>
16 static long init_dpi_fallback(void) {
17 return (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters;
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.
26 xcb_xrm_database_t *database = NULL;
32 database = xcb_xrm_database_from_default(conn);
33 if (database == NULL) {
34 ELOG("Failed to open the resource database.\n");
39 xcb_xrm_resource_get_string(database, "Xft.dpi", NULL, &resource);
40 if (resource == NULL) {
41 DLOG("Resource Xft.dpi not specified, skipping.\n");
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);
53 DLOG("Found Xft.dpi = %ld.\n", dpi);
56 if (database != NULL) {
57 xcb_xrm_database_free(database);
61 DLOG("Using fallback for calculating DPI.\n");
62 dpi = init_dpi_fallback();
63 DLOG("Using dpi = %ld\n", dpi);
68 * This function returns the value of the DPI setting.
71 long get_dpi_value(void) {
76 * Convert a logical amount of pixels (e.g. 2 pixels on a “standard” 96 DPI
77 * screen) to a corresponding amount of physical pixels on a standard or retina
78 * screen, e.g. 5 pixels on a 227 DPI MacBook Pro 13" Retina screen.
81 int logical_px(const int logical) {
82 if (root_screen == NULL) {
83 /* Dpi info may not be available when parsing a config without an X
84 * server, such as for config file validation. */
88 /* There are many misconfigurations out there, i.e. systems with screens
89 * whose dpi is in fact higher than 96 dpi, but not significantly higher,
90 * so software was never adapted. We could tell people to reconfigure their
91 * systems to 96 dpi in order to get the behavior they expect/are used to,
92 * but since we can easily detect this case in code, let’s do it for them.
94 if ((dpi / 96.0) < 1.25)
96 return ceil((dpi / 96.0) * logical);