summaryrefslogtreecommitdiff
path: root/x-focus-manager.c
blob: 69c9e96f1b5a305050f80cce2bab3c5e6d3e54c1 (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
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/cursorfont.h>

/* some clients fail to reset focus on exit
 * we watch for that, and fix it
 *
 * instead of this, we also could use Elliott Hughes' clock
 * to launch x-alt-tab
 */


void reset_focus(Display *d, XEvent *e);

static void (*handler[LASTEvent]) (Display *d, XEvent *) = {
	[UnmapNotify] = reset_focus,
	[DestroyNotify] = reset_focus
};

/// fun on down

void
reset_focus(Display *d, XEvent *e) {
	XSetInputFocus(d, PointerRoot , RevertToPointerRoot, CurrentTime);
}

int
interruptibleXNextEvent(Display *dpy, XEvent *event) {
	fd_set fds;
	int rc;
	int dpy_fd = ConnectionNumber(dpy);
	for (;;) {
		if (XPending(dpy)) {
			XNextEvent(dpy, event);
			return 1;
		}
		FD_ZERO(&fds);
		FD_SET(dpy_fd, &fds);
		rc = select(dpy_fd + 1, &fds, NULL, NULL, NULL);
		if (rc < 0) {
			if (errno == EINTR) {
				return 0;
			}
			//	LOG_ERROR("interruptibleXNextEvent(): select()\n");
		}
	}
}

int
main(int argc, char **argv) {
	Display *dpy;
	XEvent e;
	XSetWindowAttributes wa;

	if(!(dpy = XOpenDisplay(NULL)))
		return 1;

	wa.cursor = XCreateFontCursor(dpy, XC_left_ptr);
	wa.event_mask = SubstructureNotifyMask|StructureNotifyMask;
	XChangeWindowAttributes(dpy, DefaultRootWindow(dpy) , CWEventMask|CWCursor, &wa);

	for(;;) {
		if (! interruptibleXNextEvent(dpy, &e))
			continue;
		if(handler[e.type])
			handler[e.type](dpy, &e);
	}
}