
#include <stdio.h>
#include <string.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>

Atom XA_WM_STATE;
Display *dpy;
int screen;
Window root;


void walk(parent, indent)
     Window parent;
     int indent;
{
  Window *children;
  unsigned int numchildren, n;
  Window rootret;
  Window parentret;
  Window retval = (Window)0;
  Atom type;
  int format, stated=0;
  unsigned long items;
  unsigned char *prop, *c;
  unsigned long left;
  XWindowAttributes attr;

  XGetWindowProperty(dpy, parent, XA_WM_STATE, 0, 1000, False,
		     AnyPropertyType, &type, &format, &items, &left, &prop);
  if(type) stated=1;
  XFree((char *)prop);

  XGetWindowProperty(dpy, parent, XA_WM_NAME, 0, 1000, False,
		     AnyPropertyType, &type, &format, &items, &left, &prop);
  printf("%*sWindow `%s' (%lx/%ld)", indent, "", prop?prop:((parent==root)?"ROOT":"?"), parent, parent);
  XFree((char *)prop);
  XGetWindowProperty(dpy, parent, XA_WM_CLASS, 0, 1000, False,
		     AnyPropertyType, &type, &format, &items, &left, &prop);
  c=prop;
  while(c<prop+items) {
    printf(" %s", c);
    c=strchr(c,0)+1;
  }
  XFree((char *)prop);

  if(stated) printf(" with WM_STATE");

  if (XGetWindowAttributes(dpy, parent, &attr))
    printf(" %dx%d%s%d%s%d", attr.width, attr.height,
	   attr.x >= 0 ? "+" : "", attr.x, attr.y >= 0 ? "+" : "", attr.y);

  printf("\n");

  XQueryTree (dpy, parent, &rootret, &parentret, &children, &numchildren);
  for (n = 0; n < numchildren; n++)
    walk(children[n], indent+2);

  free ((char *) children);
}

main(argc, argv)
     char **argv;
{
  Window win;
  if (!(dpy = XOpenDisplay(NULL))) {
    printf ("can't open display\n");
    exit(1);
  }
  screen = DefaultScreen(dpy);
  root = RootWindow(dpy, screen);
  XA_WM_STATE = XInternAtom(dpy, "WM_STATE", True);
  walk(root,0);
}

