[mutter/wip/tablet-protocol-v2: 9/14] wayland: Implement wl_tablet_tool.set_cursor



commit 42541ecc624ec4d252e557f652d37bb96c523a3d
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Oct 28 12:55:58 2015 +0100

    wayland: Implement wl_tablet_tool.set_cursor
    
    Each tool has its own MetaCursorRenderer instance, which is created/destroyed
    upon proximity, and possibly updated through focus and set_cursor calls in
    between.

 src/wayland/meta-wayland-tablet-tool.c |  110 ++++++++++++++++++++++++++++++++
 src/wayland/meta-wayland-tablet-tool.h |    4 +
 2 files changed, 114 insertions(+), 0 deletions(-)
---
diff --git a/src/wayland/meta-wayland-tablet-tool.c b/src/wayland/meta-wayland-tablet-tool.c
index f6fe504..28a7dad 100644
--- a/src/wayland/meta-wayland-tablet-tool.c
+++ b/src/wayland/meta-wayland-tablet-tool.c
@@ -30,6 +30,7 @@
 #include <wayland-server.h>
 #include "wayland-tablet-server-protocol.h"
 #include "meta-wayland-private.h"
+#include "meta-wayland-surface-role-cursor.h"
 #include "meta-surface-actor-wayland.h"
 #include "meta-wayland-tablet.h"
 #include "meta-wayland-tablet-seat.h"
@@ -69,6 +70,53 @@ move_resources_for_client (struct wl_list   *destination,
 }
 
 static void
+meta_wayland_tablet_tool_update_cursor_surface (MetaWaylandTabletTool *tool)
+{
+  MetaCursorSprite *cursor = NULL;
+
+  if (tool->cursor_renderer == NULL)
+    return;
+
+  if (tool->current && tool->current_tablet)
+    {
+      if (tool->cursor_surface && tool->cursor_surface->buffer)
+        {
+          MetaWaylandSurfaceRoleCursor *cursor_role =
+            META_WAYLAND_SURFACE_ROLE_CURSOR (tool->cursor_surface->role);
+
+          cursor = meta_wayland_surface_role_cursor_get_sprite (cursor_role);
+        }
+      else
+        cursor = NULL;
+    }
+  else if (tool->current_tablet)
+    cursor = meta_cursor_sprite_from_theme (META_CURSOR_CROSSHAIR);
+  else
+    cursor = NULL;
+
+  meta_cursor_renderer_set_cursor (tool->cursor_renderer, cursor);
+}
+
+static void
+meta_wayland_tablet_tool_set_cursor_surface (MetaWaylandTabletTool *tool,
+                                             MetaWaylandSurface    *surface)
+{
+  if (tool->cursor_surface == surface)
+    return;
+
+  if (tool->cursor_surface)
+    wl_list_remove (&tool->cursor_surface_destroy_listener.link);
+
+  tool->cursor_surface = surface;
+
+  if (tool->cursor_surface)
+    wl_resource_add_destroy_listener (tool->cursor_surface->resource,
+                                      &tool->cursor_surface_destroy_listener);
+
+  meta_wayland_tablet_tool_update_cursor_surface (tool);
+}
+
+static void
 meta_wayland_tablet_tool_set_focus (MetaWaylandTabletTool *tool,
                                     MetaWaylandSurface    *surface)
 {
@@ -131,6 +179,8 @@ meta_wayland_tablet_tool_set_focus (MetaWaylandTabletTool *tool,
             }
         }
     }
+
+  meta_wayland_tablet_tool_update_cursor_surface (tool);
 }
 
 static guint32
@@ -205,6 +255,16 @@ tablet_tool_handle_focus_surface_destroy (struct wl_listener *listener,
   meta_wayland_tablet_tool_set_focus (tool, NULL);
 }
 
+static void
+tablet_tool_handle_cursor_surface_destroy (struct wl_listener *listener,
+                                           void               *data)
+{
+  MetaWaylandTabletTool *tool;
+
+  tool = wl_container_of (listener, tool, cursor_surface_destroy_listener);
+  meta_wayland_tablet_tool_set_cursor_surface (tool, NULL);
+}
+
 MetaWaylandTabletTool *
 meta_wayland_tablet_tool_new (MetaWaylandTabletSeat  *seat,
                               ClutterInputDevice     *device,
@@ -220,6 +280,7 @@ meta_wayland_tablet_tool_new (MetaWaylandTabletSeat  *seat,
   wl_list_init (&tool->focus_resource_list);
 
   tool->focus_surface_destroy_listener.notify = tablet_tool_handle_focus_surface_destroy;
+  tool->cursor_surface_destroy_listener.notify = tablet_tool_handle_cursor_surface_destroy;
 
   return tool;
 }
@@ -230,6 +291,8 @@ meta_wayland_tablet_tool_free (MetaWaylandTabletTool *tool)
   struct wl_resource *resource, *next;
 
   meta_wayland_tablet_tool_set_focus (tool, NULL);
+  meta_wayland_tablet_tool_set_cursor_surface (tool, NULL);
+  g_clear_object (&tool->cursor_renderer);
 
   wl_resource_for_each_safe (resource, next, &tool->resource_list)
     {
@@ -249,6 +312,45 @@ tool_set_cursor (struct wl_client   *client,
                  int32_t             hotspot_y,
                  struct wl_resource *tablet_resource)
 {
+  MetaWaylandTabletTool *tool = wl_resource_get_user_data (resource);
+  MetaWaylandTablet *tablet = wl_resource_get_user_data (tablet_resource);
+  MetaWaylandSurface *surface;
+
+  surface = (surface_resource ? wl_resource_get_user_data (surface_resource) : NULL);
+
+  if (tool->focus_surface == NULL)
+    return;
+  if (tool->cursor_renderer == NULL)
+    return;
+  if (wl_resource_get_client (tool->focus_surface->resource) != client)
+    return;
+  if (tool->proximity_serial - serial > G_MAXUINT32 / 2)
+    return;
+  if (tablet != tool->current_tablet)
+    return;
+
+  if (surface &&
+      !meta_wayland_surface_assign_role (surface,
+                                         META_TYPE_WAYLAND_SURFACE_ROLE_CURSOR))
+    {
+      wl_resource_post_error (resource, WL_POINTER_ERROR_ROLE,
+                              "wl_surface %d already has a different role",
+                              wl_resource_get_id (surface_resource));
+      return;
+    }
+
+  if (surface)
+    {
+      MetaWaylandSurfaceRoleCursor *cursor_role;
+
+      cursor_role = META_WAYLAND_SURFACE_ROLE_CURSOR (surface->role);
+      meta_wayland_surface_role_cursor_set_renderer (cursor_role,
+                                                     tool->cursor_renderer);
+      meta_wayland_surface_role_cursor_set_hotspot (cursor_role,
+                                                    hotspot_x, hotspot_y);
+    }
+
+  meta_wayland_tablet_tool_set_cursor_surface (tool, surface);
 }
 
 static void
@@ -397,6 +499,7 @@ repick_for_event (MetaWaylandTabletTool *tool,
     tool->current = NULL;
 
   sync_focus_surface (tool);
+  meta_wayland_tablet_tool_update_cursor_surface (tool);
 }
 
 static void
@@ -619,10 +722,17 @@ meta_wayland_tablet_tool_update (MetaWaylandTabletTool *tool,
         repick_for_event (tool, event);
       break;
     case CLUTTER_PROXIMITY_IN:
+      if (!tool->cursor_renderer)
+        tool->cursor_renderer = meta_cursor_renderer_new ();
       tool->current_tablet =
         meta_wayland_tablet_seat_lookup_tablet (tool->seat,
                                                 clutter_event_get_source_device (event));
       break;
+    case CLUTTER_PROXIMITY_OUT:
+      tool->current_tablet = NULL;
+      meta_wayland_tablet_tool_update_cursor_surface (tool);
+      g_clear_object (&tool->cursor_renderer);
+      break;
     default:
       break;
     }
diff --git a/src/wayland/meta-wayland-tablet-tool.h b/src/wayland/meta-wayland-tablet-tool.h
index bcdd07a..2d7031a 100644
--- a/src/wayland/meta-wayland-tablet-tool.h
+++ b/src/wayland/meta-wayland-tablet-tool.h
@@ -41,6 +41,10 @@ struct _MetaWaylandTabletTool
   MetaWaylandSurface *focus_surface;
   struct wl_listener focus_surface_destroy_listener;
 
+  MetaWaylandSurface *cursor_surface;
+  struct wl_listener cursor_surface_destroy_listener;
+  MetaCursorRenderer *cursor_renderer;
+
   MetaWaylandSurface *current;
   guint32 pressed_buttons;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]