X11: list top level windows

So far I've found two approaches:

  1. For each root window (default screen, specific screen, all screens, etc), list each immediate child. Search each immediate child recursively for a window with the WM_STATE property; that window becomes the top-level application window of the immediate child and all recursion can stop. If no window within the hierarchy of the immediate child has the WM_STATE property, assume the immediate child is itself the top-level application window.

    • Use xcb_get_property, and xcb_query_tree (which despite the name lists only immediate children).

    • This is what xlsclients uses, via XCB (same algorithm as above, more or less).

    • Requirements: requires ICCCM (window manager) support for WM_STATE.

  2. For each root window (default screen, specific screen, all screens, etc), get "_NET_CLIENT_LIST" property which lists all the top-level application windows managed by the window manager.

    • Use xcb_get_property.

    • This is what wmctrl uses, via Xlib.

    • Requires the window manager to maintain the list. Some windows seem to escape the list.

    • Requirements: EWMH (window manager) support for _NET_CLIENT_LIST or _WIN_CLIENT_LIST (I have no idea what the second one is; I can't find any documentation).


  1. Are there any other approaches? Are there any approaches that do not depend on external mechanisms such as ICCCM or EWMH?

  2. Which approach is the most resilient?

    1. EWMH. I've found some reports (linked above) that certain applications don't become listed in _NET_CLIENT_LIST. Why is this?
    2. ICCCM. This approach seems more flexible, but won't otherwise windowless applications be matched by assuming the immediate child of hierarchies lacking WM_STATE is a top-level application window? Even worse, won't it break horribly under virtual root windows? For example, the virtual root window becomes the only immediate child, and recursion will stop on the first application window with WM_STATE; only a single window will be matched. Against this eventuality, xlsclients does not include any checks for virtual root windows... so why does it work with GNOME (which uses virtual root windows)?

Relevant tags