From 384bbed0dfbbc0239927b4f38565c53a86442bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20=C5=A0vagelj?= Date: Tue, 30 Apr 2024 20:59:28 +0000 Subject: [PATCH] Use _NET_VIRTUAL_ROOTS for querying virtual roots (#1875) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tin Å vagelj --- src/x11.cc | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/x11.cc b/src/x11.cc index 1be28c74..778d7e69 100644 --- a/src/x11.cc +++ b/src/x11.cc @@ -192,36 +192,35 @@ __attribute__((noreturn)) static int x11_ioerror_handler(Display *d) { /// manage workspaces. These are direct descendants of root and WMs reparent all /// children to them. /// -/// @param screen screen to get the (current) virtual root of @return the -/// virtual root window of the screen +/// @param screen screen to get the (current) virtual root of +/// @return the virtual root window of the screen static Window VRootWindowOfScreen(Screen *screen) { - Window root = screen->root; - Display *dpy = screen->display; + Window root = RootWindowOfScreen(screen); + Display *dpy = DisplayOfScreen(screen); + + /* go look for a virtual root */ + Atom _NET_VIRTUAL_ROOTS = XInternAtom(display, "_NET_VIRTUAL_ROOTS", True); + if (_NET_VIRTUAL_ROOTS == 0) return root; + + auto vroots = x11_atom_window_list(dpy, root, _NET_VIRTUAL_ROOTS); + + if (vroots.empty()) return root; + + Atom _NET_CURRENT_DESKTOP = + XInternAtom(display, "_NET_CURRENT_DESKTOP", True); + if (_NET_CURRENT_DESKTOP == 0) return root; - Window rootReturn, parentReturn, *children; - unsigned int numChildren; Atom actual_type; int actual_format; unsigned long nitems, bytesafter; + int *cardinal; - /* go look for a virtual root */ - Atom __SWM_VROOT = ATOM(__SWM_VROOT); - if (XQueryTree(dpy, root, &rootReturn, &parentReturn, &children, - &numChildren)) { - for (int i = 0; i < numChildren; i++) { - Window *newRoot = None; + XGetWindowProperty(dpy, root, _NET_CURRENT_DESKTOP, 0, 1, False, XA_CARDINAL, + &actual_type, &actual_format, &nitems, &bytesafter, + (unsigned char **)&cardinal); - if (XGetWindowProperty( - dpy, children[i], __SWM_VROOT, 0, 1, False, XA_WINDOW, - &actual_type, &actual_format, &nitems, &bytesafter, - reinterpret_cast(&newRoot)) == Success && - newRoot != None) { - root = *newRoot; - break; - } - } - if (children) XFree((char *)children); - } + if (vroots.size() > *cardinal) { root = vroots[*cardinal]; } + XFree(cardinal); return root; }