summaryrefslogtreecommitdiff
path: root/test.c
blob: f4b39907e9e82b569aed151c7a9c747668fbbe46 (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
#include <stdlib.h>
#include <stdio.h>

#include <X11/Xlib.h>

static void
print_event(XEvent *e)
{
	switch (e->type) {
	case MapNotify:
		printf("got MapNotify event: window %lu event %lu\n",
		       e->xmap.window, e->xmap.event);
		break;
	case ReparentNotify:
		printf("got ReparentNotify event: window %lu\n",
		       e->xreparent.window);
		break;
	case MotionNotify:
		printf("got MotionNotify event: window %lu (%d,%d)\n",
		       e->xmotion.window, e->xmotion.x, e->xmotion.y);
		break;
	case ButtonPress:
		printf("got ButtonPress event: window %lu (%d,%d)\n",
		       e->xbutton.window, e->xbutton.x, e->xbutton.y);
		break;
	case ButtonRelease:
		printf("got ButtonRelease event: window %lu (%d,%d)\n",
		       e->xbutton.window, e->xbutton.x, e->xbutton.y);
		break;
	case EnterNotify:
		printf("got EnterNotify event: window %lu (%d,%d)\n",
		       e->xcrossing.window, e->xcrossing.x, e->xcrossing.y);
		break;
	case LeaveNotify:
		printf("got LeaveaNotify event: window %lu\n",
		       e->xcrossing.window);
		break;
	case CreateNotify:
		printf("got CreateNotify event: window %lu, parent %lu\n",
		       e->xcreatewindow.window, e->xcreatewindow.parent);
		break;
	case Expose:
		printf("got Expose event: window %lu at %d,%d size %dx%d\n",
		       e->xexpose.window,
		       e->xexpose.x, e->xexpose.y,
		       e->xexpose.width, e->xexpose.height);
		break;
	case ConfigureNotify:
		printf("got ConfigureNotify event: window %lu event %lu at %d,%d size %dx%d\n",
		       e->xconfigure.window, e->xconfigure.event,
		       e->xconfigure.x, e->xconfigure.y,
		       e->xconfigure.width, e->xconfigure.height);
		break;
	default:
		printf("got event type %d\n", e->type);
		break;
	}
}

static void
run(Display *display, int event)
{
	XEvent e;

	while (1) {
		XNextEvent(display, &e);
		print_event(&e);

		if (e.type == event)
			break;
	}
}

int main(int argc, char *argv[])
{
	Display *display;
	Window win, sub;
	XID root;
	XSetWindowAttributes attributes;
	Atom atom;

	display = XOpenDisplay(NULL);
	root = XRootWindow(display, 0);
	printf("opened display %p, root window is %lu\n", display, root);
	attributes.event_mask =
		ExposureMask |
		LeaveWindowMask |
		EnterWindowMask |
		Button1MotionMask |
		ButtonPressMask |
		ButtonReleaseMask |
		SubstructureNotifyMask |
		StructureNotifyMask;
	win = XCreateWindow(display, root,
			    100, 100, 200, 200, 0, 24,
			    InputOutput, NULL, CWEventMask, &attributes);

	sub = XCreateWindow(display, win,
			    50, 50, 100, 100, 0, 24,
			    InputOutput, NULL, CWEventMask, &attributes);

	printf("created window %lu and subwindow %lu\n", win, sub);

	atom = XInternAtom(display, "FOO", False);
	printf("FOO atom is %lu\n", atom);
	atom = XInternAtom(display, "BAR", False);
	printf("BAR atom is %lu\n", atom);
	atom = XInternAtom(display, "WINDOW", False);
	printf("WINDOW atom is %lu\n", atom);

	XMapWindow(display, win);

	/* Wait for ReparentNotify to get more similar behavior
	   between csx and xlib: run(display, ReparentNotify); */

	XMapWindow(display, sub);

	XMoveWindow(display, sub, 60, 60);

	XFlush(display);

	run(display, ButtonPress);

	XCloseDisplay(display);

	return 0;
}