summaryrefslogtreecommitdiffstats
path: root/overdraw.c
blob: 0df1cbf127ff5b3d45c30e17292ad5e77fd2b7c3 (plain) (blame)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <X11/X.h>
#include <cairo/cairo.h>
#include <cairo/cairo-xlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>

#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))

struct rgb {
	double r, g, b;
};

struct line {
	cairo_path_t *path;
	struct rgb color;
};

static struct rgb colors[3];
static size_t color_index;

XWindowAttributes scr;
static Display *display;
static Screen *screen;
static Window root, win;
static bool grabbed;
static bool done;
static bool draw;

static cairo_surface_t *cs;
static cairo_t *cr;

static struct line *lines;
static size_t lines_cnt, lines_cap;

static bool transparent;

static void
add_line(cairo_path_t *path, struct rgb *color)
{
	if (lines_cnt == lines_cap) {
		lines_cap *= 2;
		lines = realloc(lines, lines_cap * sizeof(struct line));
		if (!lines) err(1, "realloc");
	}

	lines[lines_cnt].path = path;
	lines[lines_cnt].color = *color;

	lines_cnt++;
}

static void
draw_color_mark(void)
{
	struct rgb *c;

	c = &colors[color_index];
	cairo_save(cr);
	cairo_set_source_rgb(cr, c->r, c->g, c->b);
	cairo_rectangle(cr, 10, 10, 20, 20);
	cairo_stroke_preserve(cr);
	cairo_fill(cr);
	cairo_restore(cr);
}

static void
update_pixmap(void)
{
	Pixmap pix;
	GC gc;

	pix = XCreatePixmap(display, win, (unsigned int) scr.width,
		(unsigned int) scr.height, (unsigned int) scr.depth);
	gc = XCreateGC(display, win, 0, 0);
	XCopyArea(display, win, pix, gc, 0, 0,
		(unsigned int) scr.width, (unsigned int) scr.height, 0, 0);
	XSetWindowBackgroundPixmap(display, win, pix);
}

static void
draw_lines(void)
{
	int i;

	cairo_save(cr);
	for (i = 0; i < lines_cnt; i++) {
		cairo_new_path(cr);
		cairo_set_source_rgb(cr, lines[i].color.r,
			lines[i].color.g, lines[i].color.b);
		cairo_append_path(cr, lines[i].path);
		cairo_stroke(cr);
	}
	cairo_restore(cr);
}

static void
grab(void)
{
	Cursor cursor;

	cursor = XCreateFontCursor(display, XC_pencil);
	XGrabPointer(display, root, False,
		ButtonMotionMask | ButtonPressMask | ButtonReleaseMask,
		GrabModeAsync, GrabModeAsync, root, cursor, CurrentTime);
	XGrabKeyboard(display, root, False, GrabModeAsync,
		GrabModeAsync, CurrentTime);
	grabbed = true;
}

static void
ungrab(void)
{
	XUngrabKeyboard(display, CurrentTime);
	XUngrabPointer(display, CurrentTime);
	XGrabKey(display, XKeysymToKeycode(display, XK_Escape), Mod1Mask,
		root, False, GrabModeAsync, GrabModeAsync);
	grabbed = false;
}

static void
redraw(void)
{
	if (transparent) {
		cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
		cairo_rectangle(cr, 0, 0,
			(double) scr.width, (double) scr.height);
		cairo_fill(cr);
		cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
	} else {
		XClearWindow(display, win);
	}

	draw_color_mark();
	draw_lines();
	XFlush(display);
}

static void
keypress(XEvent ev)
{
	KeySym keysym;

	keysym = XkbKeycodeToKeysym(display, (KeyCode) ev.xkey.keycode, 0,
		ev.xkey.state & ShiftMask ? 1 : 0);
	switch (keysym) {
	case XK_Escape:
		if (!ev.xkey.state) {
			done = true;
		} else if ((ev.xkey.state & Mod1Mask)) {
			if (grabbed) {
				ungrab();
				if (!transparent) {
					XUnmapWindow(display, win);
				}
			} else {
				if (!transparent) {
					XMapWindow(display, win);
					update_pixmap();
					redraw();
				}
				grab();
			}
		}
		break;
	case ' ':
		color_index = (color_index + 1) % ARRLEN(colors);
		draw_color_mark();
		break;
	}
}

static void
init(void)
{
	XSetWindowAttributes swa;
	XVisualInfo vinfo;
	Visual *visual;
	int depth;

	display = XOpenDisplay(NULL);
	root = DefaultRootWindow(display);
	screen = XDefaultScreenOfDisplay(display);

	XGetWindowAttributes(display, root, &scr);

	if (transparent) {
		XMatchVisualInfo(display, DefaultScreen(display),
			32, TrueColor, &vinfo);
		visual = vinfo.visual;
		depth = vinfo.depth;
	} else {
		visual = XDefaultVisualOfScreen(screen);
		depth = CopyFromParent;
	}

	swa.border_pixel = 0;
	swa.override_redirect = 1;
	swa.event_mask = ExposureMask;
	swa.colormap = XCreateColormap(display, root, visual, AllocNone);
	win = XCreateWindow(display, root, scr.x, scr.y,
		(unsigned int) scr.width, (unsigned int) scr.height,
		0, depth, InputOutput, visual, CWOverrideRedirect
		| CWBorderPixel | CWColormap | CWEventMask, &swa);
	if (!win) err(1, "XCreateWindow");
	XMapWindow(display, win);

	if (!transparent)
		update_pixmap();

	cs = cairo_xlib_surface_create(display,
		win, visual, scr.width, scr.height);
	cr = cairo_create(cs);

	cairo_surface_flush(cs);

	cairo_set_line_width(cr, 2);
	cairo_set_antialias(cr, CAIRO_ANTIALIAS_DEFAULT);

	colors[0] = (struct rgb) { 1.0, 1.0, 1.0 };
	colors[1] = (struct rgb) { 1.0, 0.0, 0.0 };
	colors[2] = (struct rgb) { 0.0, 0.0, 0.0 };
	color_index = 0;

	lines_cap = 10;
	lines = calloc(lines_cap, sizeof(struct line));
	if (!lines) exit(1);
	lines_cnt = 0;

	grab();
}

static void
update(void)
{
	struct rgb color;
	cairo_path_t *path;
	XEvent ev;

	done = 0;
	while (!done) {
		XNextEvent(display, &ev);
		switch (ev.type) {
		case KeyPress:
			keypress(ev);
			break;
		case MotionNotify:
			if (!draw) break;
			cairo_set_source_rgb(cr, color.r, color.g, color.b);
			cairo_line_to(cr, ev.xmotion.x, ev.xmotion.y);
			cairo_move_to(cr, ev.xmotion.x, ev.xmotion.y);
			cairo_stroke_preserve(cr);
			cairo_surface_flush(cs);
			path = cairo_copy_path(cr);
			if (path->num_data > 500) {
				add_line(path, &color);
				cairo_new_path(cr);
				cairo_line_to(cr, ev.xmotion.x, ev.xmotion.y);
			} else {
				cairo_path_destroy(path);
			}
			XFlush(display);
			break;
		case ButtonPress:
			cairo_new_path(cr);
			cairo_move_to(cr, ev.xmotion.x, ev.xmotion.y);
			color = colors[color_index];
			draw = 1;
			break;
		case ButtonRelease:
			draw = 0;
			path = cairo_copy_path(cr);
			add_line(path, &color);

			redraw();
			break;
		case Expose:
			redraw();
			break;
		}
	}
}

static void
deinit(void)
{
	cairo_surface_destroy(cs);

	XDestroyWindow(display, win);
	XCloseDisplay(display);

	free(lines);
}

int
main(int argc, const char **argv)
{
	const char **arg;

	transparent = false;
	for (arg = &argv[1]; *arg; arg++) {
		if (!strcmp(*arg, "-t")) {
			transparent = true;
		} else {
			errx(1, "Unknown arg: %s", *arg);
		}
	}

	init();

	update();

	deinit();
}