diff options
author | Aki <please@ignore.pl> | 2021-08-31 20:35:33 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-08-31 21:57:52 +0200 |
commit | 266f20e8f0b688ba075ee0f5e81cfd9e4b0af18a (patch) | |
tree | d3ec4d1fd23c3d95680d07aaff526aadb8a23545 | |
parent | 74a89676841f37d83c1ec02c9813fec95d773c55 (diff) | |
download | text-266f20e8f0b688ba075ee0f5e81cfd9e4b0af18a.zip text-266f20e8f0b688ba075ee0f5e81cfd9e4b0af18a.tar.gz text-266f20e8f0b688ba075ee0f5e81cfd9e4b0af18a.tar.bz2 |
Changed render target to a basic X window
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | text.c | 79 |
2 files changed, 63 insertions, 20 deletions
@@ -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 @@ -1,12 +1,16 @@ -#include <math.h> +#include <stdint.h> #include <stdio.h> #include <cairo.h> +#include <cairo-xcb.h> #include <glib-object.h> #include <pango/pango.h> #include <pango/pangocairo.h> +#include <xcb/xcb.h> +#include <xcb/xcb_util.h> 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); |