From 266f20e8f0b688ba075ee0f5e81cfd9e4b0af18a Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 31 Aug 2021 20:35:33 +0200 Subject: Changed render target to a basic X window --- Makefile | 4 ++-- text.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index b27a35c..d48d49c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS += -Wall -Wextra -Wpedantic -CFLAGS += `pkg-config --cflags cairo pango pangocairo` -LDLIBS += -lm `pkg-config --libs cairo pango pangocairo` +CFLAGS += `pkg-config --cflags cairo pango pangocairo xcb xcb-util` +LDLIBS += -lm `pkg-config --libs cairo pango pangocairo xcb xcb-util` all: text diff --git a/text.c b/text.c index 1e488a5..f0bd074 100644 --- a/text.c +++ b/text.c @@ -1,12 +1,16 @@ -#include +#include #include #include +#include #include #include #include +#include +#include void draw_text(cairo_t *, const char *); +xcb_visualtype_t * find_visual(xcb_screen_t *); static const int MARGIN = 20; static const int WIDTH = 800; @@ -16,36 +20,75 @@ static const char * FONT = "Serif 34"; int main(int argc, const char ** argv) { - if (1 >= argc) + 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, "Usage: %s OUTPUT [TEXT]\n", argv[0]); + dprintf(2, "Could not connect to X11 server"); return 1; } - const char * filename = argv[1]; - const char * text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; - if (3 == argc) - text = argv[2]; - cairo_surface_t * surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); + 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_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_t * ctx = cairo_create(surface); - cairo_scale(ctx, 1, 1); - cairo_set_source_rgb(ctx, 1., 1., 1.); - cairo_paint(ctx); - cairo_set_source_rgb(ctx, 0., 0., 0.); - draw_text(ctx, text); + xcb_flush(c); + int done = 0; + xcb_generic_event_t * e; + while (!done) + { + e = xcb_wait_for_event(c); + if (NULL == e) + break; + switch (XCB_EVENT_RESPONSE_TYPE(e)) + { + default: + break; + case XCB_KEY_PRESS: + done = 1; + break; + case XCB_EXPOSE: + cairo_set_source_rgb(ctx, 1., 1., 1.); + cairo_paint(ctx); + cairo_set_source_rgb(ctx, 0., 0., 0.); + draw_text(ctx, text); + cairo_surface_flush(surface); + xcb_flush(c); + break; + } + free(e); + } cairo_destroy(ctx); - cairo_status_t status = cairo_surface_write_to_png(surface, filename); cairo_surface_destroy(surface); - if (CAIRO_STATUS_SUCCESS != status) + xcb_disconnect(c); +} + + +xcb_visualtype_t * find_visual(xcb_screen_t * s) +{ + xcb_depth_iterator_t d = xcb_screen_allowed_depths_iterator(s); + for (; d.rem; xcb_depth_next(&d)) { - dprintf(2, "Could not save output to %s\n", filename); - return 1; + 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) + return v.data; } + return NULL; } void draw_text(cairo_t * ctx, const char * text) { - cairo_translate(ctx, MARGIN, MARGIN); + cairo_move_to(ctx, MARGIN, MARGIN); PangoLayout * layout = pango_cairo_create_layout(ctx); PangoFontDescription * desc = pango_font_description_from_string(FONT); pango_layout_set_font_description(layout, desc); -- cgit v1.1