dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Other / vroot.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*****************************************************************************/
24 /**                   Copyright 1991 by Andreas Stolcke                     **/
25 /**               Copyright 1990 by Solbourne Computer Inc.                 **/
26 /**                          Longmont, Colorado                             **/
27 /**                                                                         **/
28 /**                           All Rights Reserved                           **/
29 /**                                                                         **/
30 /**    Permission to use, copy, modify, and distribute this software and    **/
31 /**    its documentation  for  any  purpose  and  without  fee is hereby    **/
32 /**    granted, provided that the above copyright notice appear  in  all    **/
33 /**    copies and that both  that  copyright  notice  and  this  permis-    **/
34 /**    sion  notice appear in supporting  documentation,  and  that  the    **/
35 /**    name of Solbourne not be used in advertising                         **/
36 /**    in publicity pertaining to distribution of the  software  without    **/
37 /**    specific, written prior permission.                                  **/
38 /**                                                                         **/
39 /**    ANDREAS STOLCKE AND SOLBOURNE COMPUTER INC. DISCLAIMS ALL WARRANTIES **/
40 /**    WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF    **/
41 /**    MERCHANTABILITY  AND  FITNESS,  IN  NO  EVENT SHALL ANDREAS STOLCKE  **/
42 /**    OR SOLBOURNE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL    **/
43 /**    DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA   **/
44 /**    OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER    **/
45 /**    TORTIOUS ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE    **/
46 /**    OR PERFORMANCE OF THIS SOFTWARE.                                     **/
47 /*****************************************************************************/
48 /*
49  * vroot.h -- Virtual Root Window handling header file
50  *
51  * This header file redefines the X11 macros RootWindow and DefaultRootWindow,
52  * making them look for a virtual root window as provided by certain `virtual'
53  * window managers like swm and tvtwm. If none is found, the ordinary root
54  * window is returned, thus retaining backward compatibility with standard
55  * window managers.
56  * The function implementing the virtual root lookup remembers the result of
57  * its last invocation to avoid overhead in the case of repeated calls
58  * on the same display and screen arguments. 
59  * The lookup code itself is taken from Tom LaStrange's ssetroot program.
60  *
61  * Most simple root window changing X programs can be converted to using
62  * virtual roots by just including
63  *
64  * #include <X11/vroot.h>
65  *
66  * after all the X11 header files.  It has been tested on such popular
67  * X clients as xphoon, xfroot, xloadimage, and xaqua.
68  * It also works with the core clients xprop, xwininfo, xwd, and editres
69  * (and is necessary to get those clients working under tvtwm).
70  * It does NOT work with xsetroot; get the xsetroot replacement included in
71  * the tvtwm distribution instead.
72  *
73  * Andreas Stolcke <stolcke@ICSI.Berkeley.EDU>, 9/7/90
74  * - replaced all NULL's with properly cast 0's, 5/6/91
75  * - free children list (suggested by Mark Martin <mmm@cetia.fr>), 5/16/91
76  * - include X11/Xlib.h and support RootWindowOfScreen, too 9/17/91
77  */
78
79 #ifndef _VROOT_H_
80 #define _VROOT_H_
81
82 #if !defined(lint) && !defined(SABER)
83 static char vroot_rcsid[] = "$XConsortium: vroot.h /main/3 1996/06/11 16:30:45 cde-hal $";
84 #endif
85
86 #include <X11/X.h>
87 #include <X11/Xatom.h>
88 #include <X11/Xlib.h>
89
90 static Window
91 VirtualRootWindowOfScreen(Screen *screen)
92 {
93   static Screen *save_screen = NULL;
94   static Window root = None;
95
96   if (screen != save_screen)
97     {
98       Display *dpy = DisplayOfScreen(screen);
99       Atom __SWM_VROOT = None;
100       int i;
101       Window rootReturn, parentReturn, *children;
102       unsigned int numChildren;
103
104       root = RootWindowOfScreen(screen);
105
106       /* go look for a virtual root */
107       /* we're in deep shit if a window dies during this loop */
108       __SWM_VROOT = XInternAtom(dpy, "__SWM_VROOT", False);
109       if (XQueryTree(dpy, root, &rootReturn, &parentReturn,
110                      &children, &numChildren))
111         {
112           for (i = 0; i < numChildren; i++)
113             {
114               Atom actual_type;
115               int actual_format;
116               unsigned long nitems, bytesafter;
117               Window *newRoot = NULL;
118               int status;
119
120               status =
121                 XGetWindowProperty(dpy, children[i],
122                                    __SWM_VROOT, 0, 1, False, XA_WINDOW,
123                                    &actual_type, &actual_format,
124                                    &nitems, &bytesafter,
125                                    (unsigned char **) &newRoot);
126
127               if (status == Success && newRoot != NULL)
128                 {
129                   root = *newRoot;
130                   break;
131                 }
132             }
133           if (children != NULL)
134             XFree((char *) children);
135         }
136
137       save_screen = screen;
138     }
139
140   return (root);
141 }
142
143
144 /* Need original macros to figure out if there is a virtual root. */
145   
146 #if 0
147 #undef RootWindowOfScreen
148 #define RootWindowOfScreen(s) VirtualRootWindowOfScreen(s)
149
150 #undef RootWindow
151 #define RootWindow(dpy,screen) VirtualRootWindowOfScreen(ScreenOfDisplay(dpy,screen))
152
153 #undef DefaultRootWindow
154 #define DefaultRootWindow(dpy) VirtualRootWindowOfScreen(DefaultScreenOfDisplay(dpy))
155
156 #else
157
158 #define VRootWindowOfScreen(s) VirtualRootWindowOfScreen(s)
159
160 #define VRootWindow(dpy,screen) VirtualRootWindowOfScreen(ScreenOfDisplay(dpy,screen))
161
162 #define VDefaultRootWindow(dpy) VirtualRootWindowOfScreen(DefaultScreenOfDisplay(dpy))
163 #endif
164
165 #endif /* _VROOT_H_ */