0

I am trying to get the number of top level windows aka children of the root window in Xlib using XQuerytree. It is just working fine when only experimenting with basic X utilities such as Xterm or Xeyes but the num_top_level_windows contains a completely wrong number for apps using GTK or QT frameworks (e.g. with only KeepassXC open, i apparently have 5 top level windows). Here is the relevant code snippet

Window returned_root, returned_parent;
Window* top_level_windows;
unsigned int num_top_level_windows;
XQueryTree(
        display_,
        root_,
        &returned_root,
        &returned_parent,
        &top_level_windows,
        &num_top_level_windows);  
printf("\n\n N: %d\n", num_top_level_windows);

root_ and display_ are references to the root window and the display (obtained via XOpenDisplay and DefaultRootWindow(display_) and should be working fine. I am using a nested Xephyr X server session on gentoo linux. I really wonder why this "error" occurs and how can i get the true number of children windows. Thank you in advance!

1 Answer 1

0

XQueryTree() returns any child window from the specified 'w' field, created by XWindows, including override-redirect windows and Unmapped/Unviewable windows.

To filter out these windows you need to query their properties using XGetWindowAttributes(), checking if the window is override_redirect or if the map_state is not set to IsViewable.

That should be sufficient, but you may also need to query the WM_STATE of a window to check if its IconicState or WithdrawnState, depending on how your environment is set up.

You can refer to the dwm source code, specifically "dwm.c, scan()" to see how they implemented their way of managing windows, or in this case seeing which are "top level".

2
  • 1
    XQueryTree cannot return information about windows not created by X (such as Wayland).
    – gsm
    Commented Jul 8 at 5:22
  • Thank you for commenting on that oversight, I have updated the post to specify X Windows.
    – xcb_2976
    Commented Jul 8 at 7:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.