1 /*
2  * Article 12523 of comp.windows.x:
3  * From: stolcke@ICSI.Berkeley.EDU (Andreas Stolcke)
4  * Subject: Converting programs to virtual root
5  * Date: 8 Sep 90 02:26:14 GMT
6  *
7  * Andreas Stolcke
8  * International Computer Science Institute	stolcke@icsi.Berkeley.EDU
9  * 1957 Center St., Suite 600, Berkeley, CA 94704	(415) 642-4274 ext. 126
10  *
11  * vroot.h -- Virtual Root Window handling header file
12  *
13  * This header file redefines the X11 macros RootWindow and DefaultRootWindow,
14  * making them look for a virtual root window as provided by certain `virtual'
15  * window managers like swm and tvtwm. If none is found, the ordinary root
16  * window is returned, thus retaining backward compatibility with standard
17  * window managers.
18  * The function implementing the virtual root lookup remembers the result of
19  * its last invocation to avoid overhead in the case of repeated calls
20  * on the same display and screen arguments.
21  * The lookup code itself is taken from Tom LaStrange's ssetroot program.
22  *
23  * Most simple root window changing X programs can be converted to using
24  * virtual roots by just including
25  *
26  * #include "vroot.h"
27  *
28  * after all the X11 header files.  It has been tested on such popular
29  * X clients as xphoon, xfroot, xloadimage, and xaqua.
30  *
31  * Andreas Stolcke <stolcke@ICSI.Berkeley.EDU>, 9/7/90
32  * - replaced all NULL's with properly cast 0's, 5/6/91
33  * - free children list (suggested by Mark Martin <mmm@cetia.fr>), 5/16/91
34  */
35 #define DONTOPTIMISE	/* unclutter needs to search from scratch each time */
36 
37 #include <X11/X.h>
38 #include <X11/Xatom.h>
39 
40 static Window
VirtualRootWindow(dpy,screen)41 VirtualRootWindow(dpy, screen)
42 Display *dpy;
43 {
44 	static Display *save_dpy = (Display *)0;
45 	static int save_screen = -1;
46 	static Window root = (Window)0;
47 
48 #ifdef DONTOPTIMISE
49 	{
50 #else
51 	if (dpy != save_dpy || screen != save_screen) {
52 #endif
53 		Atom __SWM_VROOT = None;
54 		int i;
55 		Window rootReturn, parentReturn, *children;
56 		unsigned int numChildren;
57 
58 		root = RootWindow(dpy, screen);
59 
60 		/* go look for a virtual root */
61 		__SWM_VROOT = XInternAtom(dpy, "__SWM_VROOT", False);
62 		if (XQueryTree(dpy, root, &rootReturn, &parentReturn,
63 				 &children, &numChildren)) {
64 			for (i = 0; i < numChildren; i++) {
65 				Atom actual_type;
66 				int actual_format;
67 				unsigned long nitems, bytesafter;
68 				Window *newRoot = (Window *)0;
69 
70 				if (XGetWindowProperty(dpy, children[i],
71 					__SWM_VROOT, 0, 1, False, XA_WINDOW,
72 					&actual_type, &actual_format,
73 					&nitems, &bytesafter,
74 					(unsigned char **) &newRoot) == Success
75 				    && newRoot) {
76 				    root = *newRoot;
77 				    break;
78 				}
79 			}
80 			if (children)
81 				XFree((char *)children);
82 		}
83 
84 		save_dpy = dpy;
85 		save_screen = screen;
86 	}
87 
88 	return root;
89 }
90 
91 #undef DefaultRootWindow
92 #define DefaultRootWindow(dpy) RootWindow(dpy, DefaultScreen(dpy))
93 
94 #undef RootWindow
95 #define RootWindow(dpy,screen) VirtualRootWindow(dpy,screen)
96