From aa0f9ba1aa3f12f7e65ed64ac03114f27c8bff1f Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Mon, 27 Jan 2020 11:08:00 +0100
Subject: [PATCH xserver 14/24] xwayland: Cache client-id for the
 window-manager client

Instead of iterating over all clients which are listening for events on the
root window and checking if the client we are dealing with is the one
listening for SubstructureRedirectMask | ResizeRedirectMask events and thus
is the window-manager, cache the client-id of the window-manager in
xwl_screen and use that when checking if a client is the window-manager.

Note that we cache and compare the client-id rather then the ClienPtr,
this saves reading the ClientPtr from the global clients array when doing
the comparison.

Suggested-by: Olivier Fourdan <ofourdan@redhat.com>
Acked-by: Olivier Fourdan <ofourdan@redhat.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
(cherry picked from commit ded89300c1dd541f59fe6e93c5c69d7fe7088244)
---
 hw/xwayland/xwayland.c | 48 ++++++++++++++++++++++++++++--------------
 hw/xwayland/xwayland.h |  2 ++
 2 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/hw/xwayland/xwayland.c b/hw/xwayland/xwayland.c
index e43f3de96..f4d433bca 100644
--- a/hw/xwayland/xwayland.c
+++ b/hw/xwayland/xwayland.c
@@ -576,20 +576,11 @@ xwl_window_enable_viewport(struct xwl_window *xwl_window,
 }
 
 static Bool
-xwl_screen_client_is_window_manager(struct xwl_screen *xwl_screen,
-                                    ClientPtr client)
+window_is_wm_window(WindowPtr window)
 {
-    WindowPtr root = xwl_screen->screen->root;
-    OtherClients *others;
-
-    for (others = wOtherClients(root); others; others = others->next) {
-        if (SameClient(others, client)) {
-            if (others->mask & (SubstructureRedirectMask | ResizeRedirectMask))
-                return TRUE;
-        }
-    }
+    struct xwl_screen *xwl_screen = xwl_screen_get(window->drawable.pScreen);
 
-    return FALSE;
+    return CLIENT_ID(window->drawable.id) == xwl_screen->wm_client_id;
 }
 
 static ClientPtr
@@ -603,7 +594,7 @@ xwl_window_get_owner(struct xwl_window *xwl_window)
      * decoration window. In that case return the client of the
      * first *and only* child of the toplevel (decoration) window.
      */
-    if (xwl_screen_client_is_window_manager(xwl_window->xwl_screen, client)) {
+    if (window_is_wm_window(window)) {
         if (window->firstChild && window->firstChild == window->lastChild)
             return wClient(window->firstChild);
         else
@@ -698,9 +689,7 @@ xwl_screen_check_resolution_change_emulation(struct xwl_screen *xwl_screen)
 Bool
 xwl_window_is_toplevel(WindowPtr window)
 {
-    struct xwl_screen *xwl_screen = xwl_screen_get(window->drawable.pScreen);
-
-    if (xwl_screen_client_is_window_manager(xwl_screen, wClient(window)))
+    if (window_is_wm_window(window))
         return FALSE;
 
     /* CSD and override-redirect toplevel windows */
@@ -975,6 +964,30 @@ xwl_set_window_pixmap(WindowPtr window,
     ensure_surface_for_window(window);
 }
 
+static Bool
+xwl_change_window_attributes(WindowPtr window, unsigned long mask)
+{
+    ScreenPtr screen = window->drawable.pScreen;
+    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
+    OtherClients *others;
+    Bool ret;
+
+    screen->ChangeWindowAttributes = xwl_screen->ChangeWindowAttributes;
+    ret = (*screen->ChangeWindowAttributes) (window, mask);
+    xwl_screen->ChangeWindowAttributes = screen->ChangeWindowAttributes;
+    screen->ChangeWindowAttributes = xwl_change_window_attributes;
+
+    if (window != screen->root || !(mask & CWEventMask))
+        return ret;
+
+    for (others = wOtherClients(window); others; others = others->next) {
+        if (others->mask & (SubstructureRedirectMask | ResizeRedirectMask))
+            xwl_screen->wm_client_id = CLIENT_ID(others->resource);
+    }
+
+    return ret;
+}
+
 static void
 xwl_resize_window(WindowPtr window,
                   int x, int y,
@@ -1552,6 +1565,9 @@ xwl_screen_init(ScreenPtr pScreen, int argc, char **argv)
     xwl_screen->CloseScreen = pScreen->CloseScreen;
     pScreen->CloseScreen = xwl_close_screen;
 
+    xwl_screen->ChangeWindowAttributes = pScreen->ChangeWindowAttributes;
+    pScreen->ChangeWindowAttributes = xwl_change_window_attributes;
+
     xwl_screen->ResizeWindow = pScreen->ResizeWindow;
     pScreen->ResizeWindow = xwl_resize_window;
 
diff --git a/hw/xwayland/xwayland.h b/hw/xwayland/xwayland.h
index eb8c4bd70..126af8823 100644
--- a/hw/xwayland/xwayland.h
+++ b/hw/xwayland/xwayland.h
@@ -118,6 +118,7 @@ struct xwl_screen {
     int height;
     int depth;
     ScreenPtr screen;
+    int wm_client_id;
     int expecting_event;
     enum RootClipMode root_clip_mode;
 
@@ -135,6 +136,7 @@ struct xwl_screen {
     DestroyWindowProcPtr DestroyWindow;
     XYToWindowProcPtr XYToWindow;
     SetWindowPixmapProcPtr SetWindowPixmap;
+    ChangeWindowAttributesProcPtr ChangeWindowAttributes;
     ResizeWindowProcPtr ResizeWindow;
 
     struct xorg_list output_list;
-- 
2.25.2

