diff options
| author | Federico Angelilli <code@fedang.net> | 2023-11-02 20:23:03 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-11-02 20:23:03 +0100 |
| commit | 185af2377c283f3e660cf9383018df70712e9a26 (patch) | |
| tree | 1b89278710db020ae611851002be38150abcbeb4 | |
| parent | c2ef5b291c1ef9120ac9d5c29b378317ab189212 (diff) | |
Add xrdb and dpi query
| -rw-r--r-- | x11.c | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -1,4 +1,5 @@ #include <stdbool.h> +#include <assert.h> #include <glib.h> #include <math.h> #include <cairo.h> @@ -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); } |
