aboutsummaryrefslogtreecommitdiff
path: root/src/x11.c
blob: 4779c0828524c71a19b2070b3dd70a0713007640 (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
#ifdef X11
#include <stdio.h>
#include <time.h>
#include <X11/Xlib.h>

#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/shape.h>

#include <cairo.h>
#include <cairo-xlib.h>

Display *d;
Window overlay;
Window root;
int width, height;

void allow_input_passthrough(Window w)
{
	XserverRegion region = XFixesCreateRegion(d, NULL, 0);

	XFixesSetWindowShapeRegion(d, w, ShapeBounding, 0, 0, 0);
	XFixesSetWindowShapeRegion(d, w, ShapeInput, 0, 0, region);

	XFixesDestroyRegion(d, region);
}

void prep_overlay(void)
{
	overlay = XCompositeGetOverlayWindow(d, root);
	allow_input_passthrough(overlay);
}

void draw(cairo_t *cr)
{
	int quarter_w = width / 4;
	int quarter_h = height / 4;
	cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
	cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2);
	cairo_fill(cr);
}

int main()
{
	printf("X11\n");

	struct timespec ts = { 0, 5000 };

	d = XOpenDisplay(NULL);

	int s = DefaultScreen(d);
	root = RootWindow(d, s);

	XCompositeRedirectSubwindows(d, root, CompositeRedirectAutomatic);
	XSelectInput(d, root, SubstructureNotifyMask);

	width = DisplayWidth(d, s);
	height = DisplayHeight(d, s);

	prep_overlay();

	cairo_surface_t *surf = cairo_xlib_surface_create(d, overlay, DefaultVisual(d, s), width, height);
	cairo_t *cr = cairo_create(surf);

	XSelectInput(d, overlay, ExposureMask);

	XEvent ev;
	while (1) {
		overlay = XCompositeGetOverlayWindow(d, root);
		draw(cr);
		XCompositeReleaseOverlayWindow(d, root);
		nanosleep(&ts, NULL);
	}

	cairo_destroy(cr);
	cairo_surface_destroy(surf);
	XCloseDisplay(d);

	return 0;
}
#endif