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
|
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#define die(...) do{fprintf(stderr,__VA_ARGS__);exit(1);}while(0)
Atom pidatom;
Display *display;
Window root;
unsigned char *pidprop = 0;
Window
findwindow(unsigned int pid, Window w)
{
Atom type;
int format;
unsigned int i, nchildren;
unsigned long nitems, bytes_after;
Window *children, _root, parent;
pidprop = 0;
if (XGetWindowProperty(display, w, pidatom, 0, 1, False, XA_CARDINAL,
&type, &format, &nitems, &bytes_after, &pidprop) == Success
&& pidprop && pid == *((unsigned long *)pidprop))
return w;
if (XQueryTree(display, w, &_root, &parent, &children, &nchildren))
for (i = 0; i < nchildren; i++) {
w = findwindow(pid, children[i]);
if (w) return w;
}
return 0;
}
int
main(int argc, char *argv[])
{
char *ap;
Window w;
display = XOpenDisplay(0);
if (display == NULL)
die("could not open display\n");
root = XDefaultRootWindow(display);
pidatom = XInternAtom(display, "_NET_WM_PID", 1);
if (pidatom == None)
die("no _NET_WM_PID atom found\n");
w = findwindow(2400, root);
if (!w)
die("could not find window\n");
//XStoreName(display, w, ap);
XCloseDisplay(display);
}
|