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 (resource != NULL) {
60 if (database != NULL) {
61 xcb_xrm_database_free(database);
65 DLOG("Using fallback for calculating DPI.\n");
66 dpi = init_dpi_fallback();
67 DLOG("Using dpi = %ld\n", dpi);
72 * This function returns the value of the DPI setting.
75 long get_dpi_value(void) {
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.
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. */
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.
98 if ((dpi / 96.0) < 1.25)
100 return ceil((dpi / 96.0) * logical);