From 94a88b752a9373656bb0f62897513c8f5e552127 Mon Sep 17 00:00:00 2001
From: Carlos Garnacho <carlosg@gnome.org>
Date: Fri, 4 Nov 2016 19:36:10 +0100
Subject: [PATCH xserver 07/12] xwayland: Refactor cursor management into
 xwl_cursor

This struct takes away the cursor info in xwl_seat, and has
an update function so we can share the frame handling code
across several xwl_cursors.

Signed-off-by: Carlos Garnacho <carlosg@gnome.org>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Acked-by: Ping Cheng <ping.cheng@wacom.com>
(cherry picked from commit 6d1ad39fe6c18220dd39b0653fd1e4145140e2dc)
---
 hw/xwayland/xwayland-cursor.c | 39 ++++++++++++++++++++-------------------
 hw/xwayland/xwayland-input.c  | 38 +++++++++++++++++++++++++++++++-------
 hw/xwayland/xwayland.h        | 11 ++++++++---
 3 files changed, 59 insertions(+), 29 deletions(-)

diff --git a/hw/xwayland/xwayland-cursor.c b/hw/xwayland/xwayland-cursor.c
index f334f1ca5..fdae3ce85 100644
--- a/hw/xwayland/xwayland-cursor.c
+++ b/hw/xwayland/xwayland-cursor.c
@@ -96,11 +96,11 @@ xwl_unrealize_cursor(DeviceIntPtr device, ScreenPtr screen, CursorPtr cursor)
 }
 
 static void
-clear_cursor_frame_callback(struct xwl_seat *xwl_seat)
+clear_cursor_frame_callback(struct xwl_cursor *xwl_cursor)
 {
-   if (xwl_seat->cursor_frame_cb) {
-       wl_callback_destroy (xwl_seat->cursor_frame_cb);
-       xwl_seat->cursor_frame_cb = NULL;
+   if (xwl_cursor->frame_cb) {
+       wl_callback_destroy (xwl_cursor->frame_cb);
+       xwl_cursor->frame_cb = NULL;
    }
 }
 
@@ -109,12 +109,12 @@ frame_callback(void *data,
                struct wl_callback *callback,
                uint32_t time)
 {
-    struct xwl_seat *xwl_seat = data;
+    struct xwl_cursor *xwl_cursor = data;
 
-    clear_cursor_frame_callback(xwl_seat);
-    if (xwl_seat->cursor_needs_update) {
-        xwl_seat->cursor_needs_update = FALSE;
-        xwl_seat_set_cursor(xwl_seat);
+    clear_cursor_frame_callback(xwl_cursor);
+    if (xwl_cursor->needs_update) {
+        xwl_cursor->needs_update = FALSE;
+        xwl_cursor->update_proc(xwl_cursor);
     }
 }
 
@@ -125,6 +125,7 @@ static const struct wl_callback_listener frame_listener = {
 void
 xwl_seat_set_cursor(struct xwl_seat *xwl_seat)
 {
+    struct xwl_cursor *xwl_cursor = &xwl_seat->cursor;
     PixmapPtr pixmap;
     CursorPtr cursor;
     int stride;
@@ -135,13 +136,13 @@ xwl_seat_set_cursor(struct xwl_seat *xwl_seat)
     if (!xwl_seat->x_cursor) {
         wl_pointer_set_cursor(xwl_seat->wl_pointer,
                               xwl_seat->pointer_enter_serial, NULL, 0, 0);
-        clear_cursor_frame_callback(xwl_seat);
-        xwl_seat->cursor_needs_update = FALSE;
+        clear_cursor_frame_callback(xwl_cursor);
+        xwl_cursor->needs_update = FALSE;
         return;
     }
 
-    if (xwl_seat->cursor_frame_cb) {
-        xwl_seat->cursor_needs_update = TRUE;
+    if (xwl_cursor->frame_cb) {
+        xwl_cursor->needs_update = TRUE;
         return;
     }
 
@@ -159,19 +160,19 @@ xwl_seat_set_cursor(struct xwl_seat *xwl_seat)
 
     wl_pointer_set_cursor(xwl_seat->wl_pointer,
                           xwl_seat->pointer_enter_serial,
-                          xwl_seat->cursor,
+                          xwl_cursor->surface,
                           xwl_seat->x_cursor->bits->xhot,
                           xwl_seat->x_cursor->bits->yhot);
-    wl_surface_attach(xwl_seat->cursor,
+    wl_surface_attach(xwl_cursor->surface,
                       xwl_shm_pixmap_get_wl_buffer(pixmap), 0, 0);
-    wl_surface_damage(xwl_seat->cursor, 0, 0,
+    wl_surface_damage(xwl_cursor->surface, 0, 0,
                       xwl_seat->x_cursor->bits->width,
                       xwl_seat->x_cursor->bits->height);
 
-    xwl_seat->cursor_frame_cb = wl_surface_frame(xwl_seat->cursor);
-    wl_callback_add_listener(xwl_seat->cursor_frame_cb, &frame_listener, xwl_seat);
+    xwl_cursor->frame_cb = wl_surface_frame(xwl_cursor->surface);
+    wl_callback_add_listener(xwl_cursor->frame_cb, &frame_listener, xwl_cursor);
 
-    wl_surface_commit(xwl_seat->cursor);
+    wl_surface_commit(xwl_cursor->surface);
 }
 
 static void
diff --git a/hw/xwayland/xwayland-input.c b/hw/xwayland/xwayland-input.c
index 50da10839..bb520e891 100644
--- a/hw/xwayland/xwayland-input.c
+++ b/hw/xwayland/xwayland-input.c
@@ -424,9 +424,9 @@ pointer_handle_enter(void *data, struct wl_pointer *pointer,
      * of our surfaces might not have been shown. In that case we'll
      * have a cursor surface frame callback pending which we need to
      * clear so that we can continue submitting new cursor frames. */
-    if (xwl_seat->cursor_frame_cb) {
-        wl_callback_destroy(xwl_seat->cursor_frame_cb);
-        xwl_seat->cursor_frame_cb = NULL;
+    if (xwl_seat->cursor.frame_cb) {
+        wl_callback_destroy(xwl_seat->cursor.frame_cb);
+        xwl_seat->cursor.frame_cb = NULL;
         xwl_seat_set_cursor(xwl_seat);
     }
 
@@ -1203,6 +1203,31 @@ static const struct wl_seat_listener seat_listener = {
 };
 
 static void
+xwl_cursor_init(struct xwl_cursor *xwl_cursor, struct xwl_screen *xwl_screen,
+                void (* update_proc)(struct xwl_cursor *))
+{
+    xwl_cursor->surface = wl_compositor_create_surface(xwl_screen->compositor);
+    xwl_cursor->update_proc = update_proc;
+    xwl_cursor->frame_cb = NULL;
+    xwl_cursor->needs_update = FALSE;
+}
+
+static void
+xwl_cursor_release(struct xwl_cursor *xwl_cursor)
+{
+    wl_surface_destroy(xwl_cursor->surface);
+    if (xwl_cursor->frame_cb)
+        wl_callback_destroy(xwl_cursor->frame_cb);
+}
+
+static void
+xwl_seat_update_cursor(struct xwl_cursor *xwl_cursor)
+{
+    struct xwl_seat *xwl_seat = wl_container_of(xwl_cursor, xwl_seat, cursor);
+    xwl_seat_set_cursor(xwl_seat);
+}
+
+static void
 create_input_device(struct xwl_screen *xwl_screen, uint32_t id, uint32_t version)
 {
     struct xwl_seat *xwl_seat;
@@ -1221,7 +1246,8 @@ create_input_device(struct xwl_screen *xwl_screen, uint32_t id, uint32_t version
                          &wl_seat_interface, min(version, 5));
     xwl_seat->id = id;
 
-    xwl_seat->cursor = wl_compositor_create_surface(xwl_screen->compositor);
+    xwl_cursor_init(&xwl_seat->cursor, xwl_seat->xwl_screen,
+                    xwl_seat_update_cursor);
     wl_seat_add_listener(xwl_seat->seat, &seat_listener, xwl_seat);
 
     init_tablet_manager_seat(xwl_screen, xwl_seat);
@@ -1252,9 +1278,7 @@ xwl_seat_destroy(struct xwl_seat *xwl_seat)
     release_tablet_manager_seat(xwl_seat);
 
     wl_seat_destroy(xwl_seat->seat);
-    wl_surface_destroy(xwl_seat->cursor);
-    if (xwl_seat->cursor_frame_cb)
-        wl_callback_destroy(xwl_seat->cursor_frame_cb);
+    xwl_cursor_release(&xwl_seat->cursor);
     wl_array_release(&xwl_seat->keys);
     free(xwl_seat);
 }
diff --git a/hw/xwayland/xwayland.h b/hw/xwayland/xwayland.h
index bb119dad7..bfa5f47c7 100644
--- a/hw/xwayland/xwayland.h
+++ b/hw/xwayland/xwayland.h
@@ -127,6 +127,13 @@ struct xwl_pointer_warp_emulator {
     struct zwp_locked_pointer_v1 *locked_pointer;
 };
 
+struct xwl_cursor {
+    void (* update_proc) (struct xwl_cursor *);
+    struct wl_surface *surface;
+    struct wl_callback *frame_cb;
+    Bool needs_update;
+};
+
 struct xwl_seat {
     DeviceIntPtr pointer;
     DeviceIntPtr relative_pointer;
@@ -148,9 +155,7 @@ struct xwl_seat {
     uint32_t pointer_enter_serial;
     struct xorg_list link;
     CursorPtr x_cursor;
-    struct wl_surface *cursor;
-    struct wl_callback *cursor_frame_cb;
-    Bool cursor_needs_update;
+    struct xwl_cursor cursor;
     WindowPtr last_xwindow;
 
     struct xorg_list touches;
-- 
2.13.5

