From 55ad9c74c62e896c67d585e6ec0f58745fe0dd52 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 6 Sep 2021 18:21:54 +0200 Subject: Moved x state to static variables --- text.c | 58 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'text.c') diff --git a/text.c b/text.c index 956385b..17a6cbd 100644 --- a/text.c +++ b/text.c @@ -9,37 +9,33 @@ #include #include +int init_x(void); void draw_text(cairo_t *, const char *); xcb_visualtype_t * find_visual(xcb_screen_t *); static const int MARGIN = 20; -static const char * FONT = "Serif 34"; +static const char * const FONT = "Serif 34"; + static int width = 800; static int height = 600; +static xcb_connection_t * c; +static xcb_screen_t * s; +static xcb_visualtype_t * v; +static xcb_window_t w; + int main(int argc, const char ** argv) { const char * text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; if (2 == argc) text = argv[2]; - xcb_connection_t * c = xcb_connect(NULL, NULL); - if (xcb_connection_has_error(c)) - { - dprintf(2, "Could not connect to X11 server"); + const int res = init_x(); + if (-1 == res) return 1; - } - xcb_screen_t * s = xcb_setup_roots_iterator(xcb_get_setup(c)).data; - xcb_window_t w = xcb_generate_id(c); - const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; - const uint32_t values[] = {s->white_pixel, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_STRUCTURE_NOTIFY}; - xcb_visualtype_t * visual = find_visual(s); - xcb_create_window( - c, XCB_COPY_FROM_PARENT, w, s->root, 0, 0, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, - s->root_visual, mask, values); - xcb_map_window(c, w); - cairo_surface_t * surface = cairo_xcb_surface_create(c, w, visual, width, height); + cairo_surface_t * surface = cairo_xcb_surface_create(c, w, v, width, height); cairo_t * ctx = cairo_create(surface); + xcb_map_window(c, w); xcb_flush(c); int done = 0; xcb_generic_event_t * e; @@ -82,14 +78,38 @@ int main(int argc, const char ** argv) } -xcb_visualtype_t * find_visual(xcb_screen_t * s) +int init_x(void) +{ + c = xcb_connect(NULL, NULL); + if (xcb_connection_has_error(c)) + { + dprintf(2, "Could not connect to X11 server"); + return -1; + } + s = xcb_setup_roots_iterator(xcb_get_setup(c)).data; + w = xcb_generate_id(c); + v = find_visual(s); + const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + const uint32_t values[] = { + s->white_pixel, + XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_STRUCTURE_NOTIFY, + }; + xcb_create_window( + c, XCB_COPY_FROM_PARENT, w, s->root, 0, 0, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, + s->root_visual, mask, values); + xcb_flush(c); + return 0; +} + + +xcb_visualtype_t * find_visual(xcb_screen_t * screen) { - xcb_depth_iterator_t d = xcb_screen_allowed_depths_iterator(s); + xcb_depth_iterator_t d = xcb_screen_allowed_depths_iterator(screen); for (; d.rem; xcb_depth_next(&d)) { xcb_visualtype_iterator_t v = xcb_depth_visuals_iterator(d.data); for (; v.rem; xcb_visualtype_next(&v)) - if (v.data->visual_id == s->root_visual) + if (v.data->visual_id == screen->root_visual) return v.data; } return NULL; -- cgit v1.1