How do I obtain, and synchronize, a complete list of all X11 windows?

10,659

Solution 1

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.

Solution 2

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.

Solution 3

X11 is a remote protocol. This means when you query the X server for any information, you always get your own copy. Your copy never changes when the X server updates its internal data structures.

This means the tree won't suddenly change while you traverse it but when you use the information in it (like examining a window), that information might be stale (someone might have closed the window). That is why you need to do proper error handling.

Share:
10,659

Related videos on Youtube

Marten
Author by

Marten

Updated on April 17, 2022

Comments

  • Marten
    Marten about 2 years

    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;
    }
    
  • Marten
    Marten almost 15 years
    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...
  • hunt
    hunt almost 15 years
    @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 x.org/archive/X11R6.9.0/doc/html/manindex1.html
  • Dan D.
    Dan D. about 13 years
    XGrabServer will give you complete attention but it also disconnects all the other clients which makes it useless for this
  • alanc
    alanc over 10 years
    XGrabServer doesn't close the other clients connections, it just stops the X server from processing any requests over them, freezing them all out until you release the grab. If your code ever hangs during the grab though, you've basically frozen the users session, leaving them quite upset with you.
  • Iyad Ahmed
    Iyad Ahmed over 3 years