summaryrefslogtreecommitdiff
path: root/text.c
blob: 24cfb3ea94653674472b4e294f8d0eb3224abe48 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdint.h>
#include <stdio.h>
#include <stdlib.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>

enum CommandType
{
	COMMAND_NOTHING,
	COMMAND_DONE,
	COMMAND_BLOCK,
	COMMAND_SPAN,
};

union Command
{
	int type;
	struct {
		int type;
		int length;
		const char * data;
	} block;
	struct {
		int type;
		int position;
		int length;
	} span;
};

struct State
{
	int width;
	int height;
	int pointer_x;
	int pointer_y;
	xcb_connection_t * c;
	xcb_screen_t * s;
	xcb_visualtype_t * v;
	xcb_window_t w;
	cairo_t * ctx;  // TODO-maybe: separate drawing context, application state and xcb state?
	cairo_surface_t * surface;
};

enum Actions
{
	ACTION_REDRAW = 1 << 0,
	ACTION_ARRANGE = 1 << 1,
	ACTION_CLOSE = 1 << 2,
	ACTION_POINTER = 1 << 3,
};

struct Layout
{
	PangoFontDescription * desc;
	int size;
	PangoLayout ** v;
};

int setup(struct State *);
void finalize(struct State *, struct Layout *);
int handle(struct State *, xcb_generic_event_t *);
void arrange(struct State *, struct Layout *, union Command[]);
void draw(struct State *, struct Layout *);
xcb_visualtype_t * find_visual(xcb_screen_t *);

static const int MARGIN = 20;
static const char * const FONT = "Serif 16";

#define _BLOCK(_txt) {.block = {.type = COMMAND_BLOCK, .length = sizeof(_txt) - 1, .data = _txt}}
union Command body[] = {
	{.span = {.type = COMMAND_SPAN, .position = 28, .length = 11}},
	_BLOCK(
		"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam auctor porta eros at tempus. Aliquam "
		"elementum lectus id mi fermentum, non consectetur urna lobortis."
	),
	_BLOCK("Phasellus dignissim rhoncus magna at imperdiet."),
	{.type = COMMAND_DONE},
};
#undef _BLOCK


int main(int argc, const char ** argv)
{
	(void) argc;
	(void) argv;
	struct State state;
	if (-1 == setup(&state))
	{
		dprintf(2, "Could not connect to X server\n");  // TODO: Helpful (specific) errors.
		return 1;
	}
	struct Layout layout = {.desc = NULL, .size = 0, .v = NULL};
	arrange(&state, &layout, body);
	for (;;)
	{
		xcb_generic_event_t * e = xcb_wait_for_event(state.c);  // TODO: All events first, then flagged actions.
		int actions = handle(&state, e);  // Obviously, right now this may have at most one action.
		if (ACTION_CLOSE & actions)
			break;
		if ((ACTION_ARRANGE | ACTION_POINTER) & actions)  // TODO: Separate implementation for pointer.
			arrange(&state, &layout, body);
		if ((ACTION_REDRAW | ACTION_POINTER) & actions)
			draw(&state, &layout);
	}
	finalize(&state, &layout);
}


int setup(struct State * state)
{
	static const int initial_width = 800;
	static const int initial_height = 600;
	state->c = xcb_connect(NULL, NULL);
	if (xcb_connection_has_error(state->c))
		return -1;
	state->s = xcb_setup_roots_iterator(xcb_get_setup(state->c)).data;
	state->w = xcb_generate_id(state->c);
	state->v = find_visual(state->s);
	const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
	const uint32_t values[] = {
		state->s->white_pixel,
		XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_STRUCTURE_NOTIFY |
			XCB_EVENT_MASK_POINTER_MOTION,
	};
	xcb_create_window(
		state->c, XCB_COPY_FROM_PARENT, state->w, state->s->root, 0, 0, initial_width, initial_height, 0,
		XCB_WINDOW_CLASS_INPUT_OUTPUT, state->s->root_visual, mask, values);
	state->surface = cairo_xcb_surface_create(state->c, state->w, state->v, initial_width, initial_height);
	state->ctx = cairo_create(state->surface);
	xcb_map_window(state->c, state->w);
	xcb_flush(state->c);
	state->pointer_x = 0;
	state->pointer_y = 0;
	return 0;
}


void finalize(struct State * state, struct Layout * layout)
{
	pango_font_description_free(layout->desc);
	for (int i = 0; i < layout->size; ++i)
		g_object_unref(layout->v[i]);
	free(layout->v);
	cairo_destroy(state->ctx);
	cairo_surface_destroy(state->surface);
	xcb_disconnect(state->c);
}


int handle(struct State * state, xcb_generic_event_t * e)
{
	int actions = 0;
	switch (XCB_EVENT_RESPONSE_TYPE(e))
	{
	case XCB_KEY_PRESS:
		actions = ACTION_CLOSE;
		break;
	case XCB_EXPOSE:
		actions |= ACTION_REDRAW;
		break;
	case XCB_MOTION_NOTIFY:
		;
		actions |= ACTION_POINTER;
		xcb_motion_notify_event_t * motion = (xcb_motion_notify_event_t *) e;
		state->pointer_x = motion->event_x;
		state->pointer_y = motion->event_y;
		break;
	case XCB_CONFIGURE_NOTIFY:
		;
		actions |= ACTION_ARRANGE;
		xcb_configure_notify_event_t * conf = (xcb_configure_notify_event_t *) e;
		if (state->width != conf->width || state->height != conf->height)
		{
			cairo_xcb_surface_set_size(state->surface, conf->width, conf->height);
			state->width = conf->width;
			state->height = conf->height;
		}
		break;
	default:
		break;
	}
	free(e);
	return actions;
}


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;
}


PangoAttribute * find_link_under(PangoLayout * layout, PangoAttrList * attrs, int x, int y)
{
	int index;
	int trailing;
	int found = pango_layout_xy_to_index(layout, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
	PangoAttribute * attr = NULL;
	if (!found)
		return NULL;
	PangoAttrIterator * iter = pango_attr_list_get_iterator(attrs);
	for (int next = TRUE; next; next = pango_attr_iterator_next(iter))
	{
		attr = pango_attr_iterator_get(iter, PANGO_ATTR_FOREGROUND);
		if (NULL == attr)
			continue;
		int start, end;
		pango_attr_iterator_range(iter, &start, &end);
		if (start > index || index > end)
		{
			attr = NULL;
			continue;
		}
		attr = pango_attr_foreground_new(0xffff, 0x0000, 0x0000);
		attr->start_index = start;
		attr->end_index = end;
		goto done;
	}
done:
	pango_attr_iterator_destroy(iter);
	return attr;
}


void arrange(struct State * state, struct Layout * layout, union Command body[])
{
	if (NULL == layout->desc)
		layout->desc = pango_font_description_from_string(FONT);
	int size = 0;
	for (union Command * c = body; COMMAND_DONE != c->type; ++c)
		if (COMMAND_BLOCK == c->type)
			size++;
	if (size != layout->size)
	{
		for (int i = 0; i < layout->size; ++i)
			g_object_unref(layout->v[i]);  /// TODO-maybe: Reuse already existing layouts of blocks.
		layout->v = malloc(sizeof(PangoLayout *) * size);
		if (NULL == layout->v)
			abort();  // TODO: Be more graceful?
		for (int i = 0; i < size; ++i)
			layout->v[i] = pango_cairo_create_layout(state->ctx);
		layout->size = size;
	}
	PangoAttrList * attrs = pango_attr_list_new();
	int i = 0;
	for (union Command * c = body; COMMAND_DONE != c->type; ++c)
	{
		switch (c->type)
		{
		case COMMAND_SPAN:
			;
			PangoAttribute * attr = pango_attr_foreground_new(0x0000, 0x0000, 0xffff);
			attr->start_index = c->span.position;
			attr->end_index = c->span.position + c->span.length;
			pango_attr_list_change(attrs, attr);
			break;
		case COMMAND_BLOCK:
			;
			attr = find_link_under(layout->v[i], attrs, state->pointer_x - MARGIN, state->pointer_y - MARGIN);
			if (NULL != attr)
				pango_attr_list_change(attrs, attr);
			pango_layout_set_attributes(layout->v[i], attrs);  // TODO-maybe: Don't reset everything?
			pango_attr_list_unref(attrs);
			attrs = pango_attr_list_new();
			pango_layout_set_font_description(layout->v[i], layout->desc);
			pango_layout_set_wrap(layout->v[i], PANGO_WRAP_WORD);
			pango_layout_set_width(layout->v[i], (state->width - 2 * MARGIN) * PANGO_SCALE);
			pango_layout_set_text(layout->v[i], c->block.data, c->block.length);
			i++;
			break;
		case COMMAND_NOTHING:
		default:
			break;
		}
	}
	pango_attr_list_unref(attrs);
}


void draw(struct State * state, struct Layout * layout)
{
	cairo_set_source_rgb(state->ctx, 1., 1., 1.);
	cairo_paint(state->ctx);
	cairo_set_source_rgb(state->ctx, 0., 0., 0.);
	int y = MARGIN;
	for (int i = 0; i < layout->size; ++i)
	{
		cairo_move_to(state->ctx, MARGIN, y);
		pango_cairo_update_layout(state->ctx, layout->v[i]);
		pango_cairo_show_layout(state->ctx, layout->v[i]);
		int height;
		pango_layout_get_pixel_size(layout->v[i], NULL, &height);
		y += height + MARGIN;
	}
	cairo_surface_flush(state->surface);
	xcb_flush(state->c);
}