views:

853

answers:

2

I want to monitor all the open windows under X11. Currently, I'm doing this as follows:

  1. Initially walking the whole tree by recursively calling XQueryTree from the root window
  2. Listening for substructure changes on the whole desktop: XSelectInput( display, root_window, SubstructureNotifyMask | PropertyChangeMask )
  3. Handling all MapNotify, UnmapNotify and DestroyNotify events, updating my own list of windows in the process

I'm mainly worried about point 1. During the recursion, XQueryTree will be called multiple times. Is there any way to ensure that the tree does not change in the meantime? In other words, to get a 'snapshot' of the whole tree at one point in time?

Also, I've noticed that under some X11 systems, not all events arrive correctly. For example, when opening a new window on the desktop, MapNotify for that window may never arrive at my monitoring application. How can this be? Is it possible that it is thrown away before arriving?

Update:

I've written a small program that will monitor X events on the root window (see below). Now, when I run this program and start and quit xcalc, I get the following output:

Reparented: 0x4a0005b to 0x1001e40
Mapped    : 0x1001e40
Destroyed : 0x1001e40

That's it. I'm never notified of the real window (0x4a0005b) being destroyed. Not even of it being mapped! Can anyone tell me why not? Does SubStructureNotifyMask only cause events of direct subwindows to be sent instead of the whole subtree?

By the way, this apparently does not happen when Compiz is running. Then no reparenting is done:

Mapped    : 0x4a0005b
Mapped    : 0x4e00233
Destroyed : 0x4a0005b
Destroyed : 0x4e00233

Monitoring program source:

#include <X11/Xlib.h>
#include <cstdio>

int main()
{
    Display *display;
    Window rootwin;

    display = XOpenDisplay( NULL );
    rootwin = DefaultRootWindow( display );
    XSelectInput( display, rootwin, SubstructureNotifyMask );

    XEvent event;

    while ( 1 ) {
        XNextEvent( display, &event );
        if ( event.type == MapNotify ) {
            XMapEvent *mapevent = (XMapEvent *)&event;
            printf( "Mapped    : 0x%x\n", (unsigned int)(mapevent->window) );
        }
        if ( event.type == DestroyNotify ) {
            XDestroyWindowEvent *destroywindowevent = (XDestroyWindowEvent *)&event;
            printf( "Destroyed : 0x%x\n", (unsigned int)(destroywindowevent->window) );
        }
        if ( event.type == ReparentNotify ) {
            XReparentEvent *reparentevent = (XReparentEvent *)&event;
            printf( "Reparented: 0x%x to 0x%x\n", (unsigned int)(reparentevent->window), (unsigned int)(reparentevent->parent) );
        }
    }

    return 0;
}
+2  A: 

G'day,

Have a look at xwininfo.

You might also like xprop and xspy for getting more info.

Update: Yep. Try using xwininfo and -root with either -tree or -children to get all windows involved.

And changes can be tracked with xprop -spy.

HTH

cheers,

Rob Wells
Thanks! I took a look at the source code for xwininfo and it seems to do the tree traversal the same way I do: without protecting constructs around it. So if there is the possibility of the tree changing between XQueryTree calls, xwininfo will also be affected and not give the right results I guess...
Marten
@Marten, yes. xwininfo is a snapshot, but it will give you the heirarchy. Then you can open several windows and use them to run xprop -spy to check for updates. There's quite a few of these tools. Have a look at the man page list at http://www.x.org/archive/X11R6.9.0/doc/html/manindex1.html
Rob Wells
+1  A: 

I believe that grabbing the X server (XGrabServer(3)) will prevent changes to the window hierarchy. It is a bit heavy handed though, so you should probably only do it if you really need it.

For an example of code that walks the window hierarchy, builds up a tree, uses window events to keep it up to date, and ignores X protocol errors which are unavoidable due to races, see the file src/VBox/Additions/x11/VBoxClient/seamless-x11.cpp in the source code for VirtualBox.

michaeljt