Tuesday, December 06, 2011

Sample code to query window name.

Sample code to query window name.

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
static char *window_id_format = "0x%lx";
int main(void)
{
 unsigned int numkids, i, mapped, scrn;
 Window r, p, *kids;
 XWindowAttributes attr;
 Window root;
 Display *dispsy;
 char *win_name;
 XTextProperty text_prop;
 dispsy = XOpenDisplay(0);

 scrn = DefaultScreen(dispsy);
 root = RootWindow(dispsy, scrn);

 mapped = 0;
 XQueryTree(dispsy, root, &r, &p, &kids, &numkids);
 for(i = 0; i < numkids; ++i)
 {
  XGetWindowAttributes(dispsy, kids[i], &attr);
  if(attr.map_state == IsViewable)
  {
   ++mapped;
   printf(window_id_format, kids[i]);
   status = XGetWMName(dispsy, kids[i], &text_prop);
   if(!status || !text_prop.value || !text_prop.nitems)
   {
    printf("GetWMName error\n");
 //   return -1;
   }
   if(!XFetchName(dispsy, kids[i], &win_name))
   {
    printf("(has no name)\n");
    printf(" \n");
   }
   else if(win_name)
   {
    printf("win_name =  %s \n", win_name);
    XFree(win_name);
   }
  }
 }
}

No comments: