Use Xft.dpi for DPI if available.

fixes #2465
This commit is contained in:
Ingo Bürk 2016-09-23 15:09:49 +02:00
parent 1437271e65
commit cb1fcfed6a
8 changed files with 75 additions and 14 deletions

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-config-wizard
i3_config_wizard_SOURCES := $(wildcard i3-config-wizard/*.c) i3_config_wizard_SOURCES := $(wildcard i3-config-wizard/*.c)
i3_config_wizard_HEADERS := $(wildcard i3-config-wizard/*.h) i3_config_wizard_HEADERS := $(wildcard i3-config-wizard/*.h)
i3_config_wizard_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(PANGO_CFLAGS) $(XKB_COMMON_CFLAGS) $(XKB_COMMON_X11_CFLAGS) i3_config_wizard_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(PANGO_CFLAGS) $(XCB_XRM_CFLAGS) $(XKB_COMMON_CFLAGS) $(XKB_COMMON_X11_CFLAGS)
i3_config_wizard_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) $(PANGO_LIBS) $(XKB_COMMON_LIBS) $(XKB_COMMON_X11_LIBS) i3_config_wizard_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) $(PANGO_LIBS) $(XCB_XRM_LIBS) $(XKB_COMMON_LIBS) $(XKB_COMMON_X11_LIBS)
i3_config_wizard_OBJECTS := $(i3_config_wizard_SOURCES:.c=.o) i3_config_wizard_OBJECTS := $(i3_config_wizard_SOURCES:.c=.o)

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-input
i3_input_SOURCES := $(wildcard i3-input/*.c) i3_input_SOURCES := $(wildcard i3-input/*.c)
i3_input_HEADERS := $(wildcard i3-input/*.h) i3_input_HEADERS := $(wildcard i3-input/*.h)
i3_input_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(PANGO_CFLAGS) i3_input_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(PANGO_CFLAGS) $(XCB_XRM_CFLAGS)
i3_input_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) $(PANGO_LIBS) i3_input_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) $(PANGO_LIBS) $(XCB_XRM_LIBS)
i3_input_OBJECTS := $(i3_input_SOURCES:.c=.o) i3_input_OBJECTS := $(i3_input_SOURCES:.c=.o)

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-nagbar
i3_nagbar_SOURCES := $(wildcard i3-nagbar/*.c) i3_nagbar_SOURCES := $(wildcard i3-nagbar/*.c)
i3_nagbar_HEADERS := $(wildcard i3-nagbar/*.h) i3_nagbar_HEADERS := $(wildcard i3-nagbar/*.h)
i3_nagbar_CFLAGS = $(XCB_CFLAGS) $(XCB_CURSOR_CFLAGS) $(XCB_WM_CFLAGS) $(PANGO_CFLAGS) i3_nagbar_CFLAGS = $(XCB_CFLAGS) $(XCB_CURSOR_CFLAGS) $(XCB_WM_CFLAGS) $(PANGO_CFLAGS) $(XCB_XRM_CFLAGS)
i3_nagbar_LIBS = $(XCB_LIBS) $(XCB_CURSOR_LIBS) $(XCB_WM_LIBS) $(PANGO_LIBS) i3_nagbar_LIBS = $(XCB_LIBS) $(XCB_CURSOR_LIBS) $(XCB_WM_LIBS) $(PANGO_LIBS) $(XCB_XRM_LIBS)
i3_nagbar_OBJECTS := $(i3_nagbar_SOURCES:.c=.o) i3_nagbar_OBJECTS := $(i3_nagbar_SOURCES:.c=.o)

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3bar
i3bar_SOURCES := $(wildcard i3bar/src/*.c) i3bar_SOURCES := $(wildcard i3bar/src/*.c)
i3bar_HEADERS := $(wildcard i3bar/include/*.h) i3bar_HEADERS := $(wildcard i3bar/include/*.h)
i3bar_CFLAGS = $(XCB_CFLAGS) $(XCB_CURSOR_CFLAGS) $(PANGO_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS) i3bar_CFLAGS = $(XCB_CFLAGS) $(XCB_CURSOR_CFLAGS) $(PANGO_CFLAGS) $(XCB_XRM_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS)
i3bar_LIBS = $(XCB_LIBS) $(XCB_CURSOR_LIBS) $(PANGO_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS) $(XCB_XKB_LIBS) i3bar_LIBS = $(XCB_LIBS) $(XCB_CURSOR_LIBS) $(PANGO_LIBS) $(XCB_XRM_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS) $(XCB_XKB_LIBS)
i3bar_OBJECTS := $(i3bar_SOURCES:.c=.o) i3bar_OBJECTS := $(i3bar_SOURCES:.c=.o)

View File

@ -470,6 +470,13 @@ char *get_process_filename(const char *prefix);
*/ */
char *get_exe_path(const char *argv0); char *get_exe_path(const char *argv0);
/**
* Initialize the DPI setting.
* This will use the 'Xft.dpi' X resource if available and fall back to
* guessing the correct value otherwise.
*/
void init_dpi(void);
/** /**
* Convert a logical amount of pixels (e.g. 2 pixels on a standard 96 DPI * Convert a logical amount of pixels (e.g. 2 pixels on a standard 96 DPI
* screen) to a corresponding amount of physical pixels on a standard or retina * screen) to a corresponding amount of physical pixels on a standard or retina

View File

@ -7,6 +7,61 @@
*/ */
#include "libi3.h" #include "libi3.h"
#include <math.h> #include <math.h>
#include <stdlib.h>
#include <xcb/xcb_xrm.h>
static long dpi;
static long init_dpi_fallback(void) {
return (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters;
}
/*
* Initialize the DPI setting.
* This will use the 'Xft.dpi' X resource if available and fall back to
* guessing the correct value otherwise.
*/
void init_dpi(void) {
xcb_xrm_database_t *database = NULL;
if (conn == NULL) {
goto init_dpi_end;
}
database = xcb_xrm_database_from_default(conn);
if (database == NULL) {
ELOG("Failed to open the resource database.\n");
goto init_dpi_end;
}
char *resource;
xcb_xrm_resource_get_string(database, "Xft.dpi", NULL, &resource);
if (resource == NULL) {
DLOG("Resource Xft.dpi not specified, skipping.\n");
goto init_dpi_end;
}
char *endptr;
dpi = strtol(resource, &endptr, 10);
if (dpi == LONG_MAX || dpi == LONG_MIN || dpi < 0 || *endptr != '\0' || endptr == resource) {
ELOG("Xft.dpi = %s is an invalid number and couldn't be parsed.\n", resource);
dpi = 0;
goto init_dpi_end;
}
DLOG("Found Xft.dpi = %ld.\n", dpi);
init_dpi_end:
if (database != NULL) {
xcb_xrm_database_free(database);
}
if (dpi == 0) {
DLOG("Using fallback for calculating DPI.\n");
dpi = init_dpi_fallback();
DLOG("Using dpi = %ld\n", dpi);
}
}
/* /*
* Convert a logical amount of pixels (e.g. 2 pixels on a standard 96 DPI * Convert a logical amount of pixels (e.g. 2 pixels on a standard 96 DPI
@ -21,8 +76,6 @@ int logical_px(const int logical) {
return logical; return logical;
} }
const int dpi = (double)root_screen->height_in_pixels * 25.4 /
(double)root_screen->height_in_millimeters;
/* There are many misconfigurations out there, i.e. systems with screens /* There are many misconfigurations out there, i.e. systems with screens
* whose dpi is in fact higher than 96 dpi, but not significantly higher, * whose dpi is in fact higher than 96 dpi, but not significantly higher,
* so software was never adapted. We could tell people to reconfigure their * so software was never adapted. We could tell people to reconfigure their

View File

@ -2,7 +2,7 @@ CLEAN_TARGETS += clean-libi3
libi3_SOURCES := $(wildcard libi3/*.c) libi3_SOURCES := $(wildcard libi3/*.c)
libi3_HEADERS := $(wildcard libi3/*.h) libi3_HEADERS := $(wildcard libi3/*.h)
libi3_CFLAGS = $(PANGO_CFLAGS) libi3_CFLAGS = $(PANGO_CFLAGS) $(XCB_XRM_CFLAGS)
libi3_LIBS = libi3_LIBS =
libi3_OBJECTS := $(libi3_SOURCES:.c=.o) libi3_OBJECTS := $(libi3_SOURCES:.c=.o)

View File

@ -503,10 +503,11 @@ int main(int argc, char *argv[]) {
visual_type = get_visualtype(root_screen); visual_type = get_visualtype(root_screen);
} }
init_dpi();
DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_type->visual_id); DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_type->visual_id);
DLOG("root_screen->height_in_pixels = %d, root_screen->height_in_millimeters = %d, dpi = %d\n", DLOG("root_screen->height_in_pixels = %d, root_screen->height_in_millimeters = %d\n",
root_screen->height_in_pixels, root_screen->height_in_millimeters, root_screen->height_in_pixels, root_screen->height_in_millimeters);
(int)((double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters));
DLOG("One logical pixel corresponds to %d physical pixels on this display.\n", logical_px(1)); DLOG("One logical pixel corresponds to %d physical pixels on this display.\n", logical_px(1));
xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root); xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);