summaryrefslogtreecommitdiff
path: root/xnext.c
blob: f9072bf784fe655d52a42e9dff240aa26c6496aa (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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>

#define WM_STATE_FORMAT	32

static Bool is_normal(Display *dpy, Window win)
{
	static Atom state = None;
	if (state == None) {
		state = XInternAtom(dpy, "WM_STATE", True);
		if (state == None) {
			return False;
		}
	}

	Atom type = AnyPropertyType;
	int format = WM_STATE_FORMAT;
	unsigned long nitems = 0;
	unsigned long bytes = 0;
	unsigned char *prop = NULL;

	int r = XGetWindowProperty(dpy,
		win,
		state,
		0,
		0,
		False,
		type,
		&type,
		&format,
		&nitems,
		&bytes,
		&prop);

	if (r == Success && prop != NULL) {
		XFree(prop);
		return True;
	}

	return False;
}

static int compar(const void *a, const void *b)
{
	const Window *wina = a;
	const Window *winb = b;
	if (*wina > *winb) {
		return 1;
	}
	if (*wina < *winb) {
		return -1;
	}
	return 0;
}

static Window *walk_tree(Display *dpy, Window win, Window *list, size_t *nwin)
{
	Window root = win, parent = win, *children = NULL;
	unsigned int nchildren = 0;
	if (!XQueryTree(dpy, win, &root, &parent, &children, &nchildren)) {
		return NULL;
	}

	for (unsigned int i = 0; i < nchildren; i++) {
		list = walk_tree(dpy, children[i], list, nwin);

		if (is_normal(dpy, children[i])) {
			list = realloc(list, sizeof(*list) * (*nwin + 1));
			if (list == NULL) {
				return NULL;
			}
			list[*nwin] = children[i];
			(*nwin)++;
		}
	}

	return list;
}

int switch_to(Display *dpy, Window prev, Window win)
{
	XMapRaised(dpy, win);
	XRaiseWindow(dpy, win);

	/* TODO: move pointer to the middle of the window */
	XWarpPointer(dpy, prev, win, 0, 0, 0, 0, 0, 0);
	XSetInputFocus(dpy, win, RevertToNone, CurrentTime);

	XCloseDisplay(dpy);
	return 0;
}

Window find_normal_parent(Display *dpy, Window win)
{
	if (win == PointerRoot || win == None || is_normal(dpy, win)) {
		return win;
	}

	Window root = win, parent = win, *children = NULL;
	unsigned int nchildren = 0;
	if (!XQueryTree(dpy, win, &root, &parent, &children, &nchildren)) {
		return None;
	}

	if (parent == win || is_normal(dpy, parent)) {
		return parent;
	}

	return find_normal_parent(dpy, win);
}

int main(int argc, char *argv[])
{
	int previous = 0;

	int c = 0;
	while ((c = getopt(argc, argv, "p")) != -1) {
		switch (c) {
		case 'p':
			previous = 1;
			break;
		default:
			return 1;
		}
	}

	Display *dpy = XOpenDisplay("");
	Window root = XDefaultRootWindow(dpy);

	Window focused = root;
	int revert = RevertToNone;
	XGetInputFocus(dpy, &focused, &revert);
	focused = find_normal_parent(dpy, focused);

	size_t nwin = 0;
	Window *list = walk_tree(dpy, root, NULL, &nwin);
	if (list == NULL) {
		return 1;
	}

	qsort(list, nwin, sizeof(*list), compar);
	if (focused == PointerRoot || focused == None) {
		return switch_to(dpy, root, list[previous ? nwin - 1 : 0]);
	}

	for (size_t i = 0; i < nwin; i++) {
		if (list[i] == focused) {
			return switch_to(dpy, focused, list[(i + (previous ? -1 : 1)) % nwin]);
		}
	}

	return 1;
}