From 185af2377c283f3e660cf9383018df70712e9a26 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Thu, 2 Nov 2023 20:23:03 +0100 Subject: Add xrdb and dpi query --- x11.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/x11.c b/x11.c index 1cf4393..f95d4f5 100644 --- a/x11.c +++ b/x11.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -14,10 +15,12 @@ struct Window { xcb_screen_t *screen; xcb_connection_t *connection; + xcb_xrm_database_t *database; xcb_drawable_t window; xcb_visualtype_t *visual_type; cairo_surface_t *surface; cairo_t *cr; + double dpi; }; Window *window_create(void) @@ -26,6 +29,19 @@ Window *window_create(void) int preferred_screen = 0; win->connection = xcb_connect(NULL, &preferred_screen); + assert(win->connection != NULL && !xcb_connection_has_error(win->connection)); + + win->database = xcb_xrm_database_from_default(win->connection); + assert(win->database != NULL); + + char *dpi_value; + if (xcb_xrm_resource_get_string(win->database, "Xft.dpi", "Xft.dpi", &dpi_value) >= 0) { + win->dpi = strtod(dpi_value, NULL); + g_free(dpi_value); + } else { + // TODO: Calculate automatically somehow + win->dpi = 96.0; + } xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(win->connection)); @@ -35,7 +51,6 @@ Window *window_create(void) } win->screen = iter.data; - win->window = xcb_generate_id(win->connection); int y = 0, x = 0, h = 300, w = 400; @@ -84,8 +99,7 @@ cairo_t *window_get_context(Window *win) double window_get_scale(Window *win) { - // TODO - return 1; + return MAX(1, win->dpi/96.); } void window_paint_surface(Window *win, cairo_surface_t *surface, int width, int height) @@ -107,6 +121,7 @@ void window_destroy(Window *win) { cairo_destroy(win->cr); cairo_surface_destroy(win->surface); + xcb_xrm_database_free(win->database); xcb_disconnect(win->connection); g_free(win); } -- cgit v1.2.3