+/* Whether the given window is:
+ - the real root window;
+ - the virtual root window;
+ - a direct child of the root window;
+ - a direct child of the window manager's decorations.
+ */
+Bool
+top_level_window_p (Screen *screen, Window window)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ Window root, parent, *kids;
+ Window vroot = VirtualRootWindowOfScreen(screen);
+ unsigned int nkids;
+
+ if (window == vroot)
+ return True;
+
+ if (!XQueryTree (dpy, window, &root, &parent, &kids, &nkids))
+ return False;
+
+ if (window == root)
+ return True;
+
+ /* If our direct parent is the root (or *a* root), then yes. */
+ if (parent == root || parent == vroot)
+ return True;
+ else
+ {
+ Atom type = None;
+ int format;
+ unsigned long nitems, bytesafter;
+ unsigned char *data;
+
+ /* If our direct parent has the WM_STATE property, then it is a
+ window manager decoration -- yes.
+ */
+ if (XGetWindowProperty (dpy, window,
+ XInternAtom (dpy, "WM_STATE", True),
+ 0, 0, False, AnyPropertyType,
+ &type, &format, &nitems, &bytesafter,
+ (unsigned char **) &data)
+ == Success
+ && type != None)
+ return True;
+ }
+
+ /* Else, no. We're deep in a tree somewhere.
+ */
+ return False;
+}
+
+
+