/*
	Traverse the window tree, printing window information
	to the standard output.
*/
#include <stdio.h>
#include <X11/Xlib.h>

static void ShowWindowTree();   /* recursive */

main(argc,argv)
   unsigned int argc;
   char *argv[];
{
        Display *display;
	int windows = 0,generations = 0;

        if (!(display = XOpenDisplay(NULL))) {
           fprintf(stderr,"Can't open display\n");
           exit(0);
        }

        ShowWindowTree(display,
                       RootWindow(display,DefaultScreen(display)),
                       &windows,&generations);

        printf("Total of %d windows.\n",windows);

        exit;
}

static void ShowWindowTree(display,window,windows,generations)
   Display *display;
   Window window;
   int *windows,*generations;
{
   Window current,root,parent,*children;
   int nchildren,x,y,width,height,border_width,depth;
   register int i;
   char *name;

   XQueryTree(display,window,&root,&parent,&children,&nchildren);
   XGetGeometry(display,window,&root,&x,&y,&width,&height,
                &border_width,&depth);
   XFetchName(display,window,&name);

   ++(*windows);
   for (i = 0; i < *generations; i++) printf(". ");
   printf("w=%d, p=%d, nc=%d, x=%d, y=%d, w=%d, h=%d, n=%s\n",
          window,parent,nchildren,x,y,width,height,
          name ? name : "None");
   if (name) XFree(name);

   if (nchildren) {
      register Window *child;

      ++(*generations);

      for (i = 0, child = children; i < nchildren; ++i, ++child)
         ShowWindowTree(display,*child,windows,generations);

      --(*generations);
      XFree(children);
   }

   return;
}

