summaryrefslogtreecommitdiff
path: root/text.c
blob: 17a6cbd6ed036cd4758afa08214ff47928bc411c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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>

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 * 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];
	const int res = init_x();
	if (-1 == res)
		return 1;
	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;
	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;
		case XCB_CONFIGURE_NOTIFY:
			;
			xcb_configure_notify_event_t * conf = (xcb_configure_notify_event_t *) e;
			if (width != conf->width || height != conf->height)
			{
				cairo_xcb_surface_set_size(surface, conf->width, conf->height);
				width = conf->width;
				height = conf->height;
			}
			break;
		}
		free(e);
	}
	cairo_destroy(ctx);
	cairo_surface_destroy(surface);
	xcb_disconnect(c);
}


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(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 == screen->root_visual)
				return v.data;
	}
	return NULL;
}


void draw_text(cairo_t * ctx, const char * text)
{
	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);
	pango_font_description_free(desc);
	pango_layout_set_text(layout, text, -1);
	pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
	pango_layout_set_width(layout, (width - 2 * MARGIN) * PANGO_SCALE);
	pango_cairo_update_layout(ctx, layout);
	pango_cairo_show_layout(ctx, layout);
	g_object_unref(layout);
}