-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnthwindow.c
67 lines (55 loc) · 1.1 KB
/
nthwindow.c
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
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
Display *Dpy;
Window Root;
int
is_viewable (Window w)
{
XWindowAttributes wattr;
XGetWindowAttributes (Dpy, w, &wattr);
return wattr.map_state == IsViewable &&
wattr.override_redirect == False;
}
Window
find_nth_window (int n)
{
Window r_root, r_parent, *r_ch, w = 0;
unsigned int n_ch;
int i, j = 0;
if (!XQueryTree (Dpy, Root, &r_root, &r_parent, &r_ch, &n_ch))
return 0;
for (i = n_ch; i; i--) {
//printf ("#%d window %x\n", j, r_ch[i]);
if (is_viewable (r_ch[i]) && j++ == n) {
//puts ("found");
w = r_ch[i];
break;
}
}
XFree (r_ch);
return w;
}
int
xerror (Display *dpy, XErrorEvent *e)
{
return 0;
}
int
main (int argc, char *argv[])
{
int n;
Window w;
if (argc < 2)
return 2;
n = atoi (argv[1]);
Dpy = XOpenDisplay (NULL);
if (!Dpy)
return 1;
Root = DefaultRootWindow (Dpy);
XSetErrorHandler (xerror);
if ((w = find_nth_window (n)))
printf ("0x%x\n", w);
XCloseDisplay (Dpy);
return w ? 0 : 1;
}