
/* compile-command "cc -o query query.c -lX11" */

#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xatom.h>

Display *dpy;
int screen;
Window root;
Window rootret;
Window parent;
Window *children;
unsigned int numchildren;
Window *gchildren;
unsigned int numgchildren;

check_prop(win)
     Window win;
{
  Atom type;
  int format;
  unsigned long items;
  unsigned char *prop;
  unsigned long left;
  
  XGetWindowProperty(dpy, win, XA_WM_NAME, 0, 1000, False,
		     AnyPropertyType, &type, &format, &items, &left, &prop);
  if (format) {
    printf (" !ty=%08x fo=%d it=%d pr=%s le=%d! ",
	    type, format, items, prop, left);
  }
}

main()
{
  int n;
  dpy = XOpenDisplay("");
  screen = DefaultScreen(dpy);
  root = RootWindow(dpy, screen);
  XQueryTree (dpy, root, &rootret, &parent, &children, &numchildren);
  printf ("The root window has %d children:\n", numchildren);
  for (n = 0; n < numchildren; n++) {
    printf ("  %08x ", children[n]);
    check_prop(children[n]);
    XQueryTree (dpy, children[n], &rootret, &parent, &gchildren, &numgchildren);
    printf (" -> (%d:%08x) ", numgchildren, gchildren[0]);
    if (numgchildren)
      check_prop(gchildren[0]);
    XFree((char *) gchildren);
    printf ("\n");
  }
  XFree((char *) children);
  XCloseDisplay(dpy);
}
