[clutter/wip/wayland: 31/45] test-wayland-surface: Update to the new drag API



commit e846fe3a7a6e24c9db034c29ecab37a4929cd9b7
Author: Neil Roberts <neil linux intel com>
Date:   Mon Jan 16 15:21:28 2012 +0000

    test-wayland-surface: Update to the new drag API
    
    This uses the new grab API from Wayland commit 5ffe9f4708. The grab
    interface implementation has been removed so that it can just use the
    default one which is now in the wayland-server code.

 tests/interactive/test-wayland-surface.c |   12 ++
 tests/interactive/tws-input.c            |  227 ++++++++++++------------------
 tests/interactive/tws-input.h            |    5 +
 3 files changed, 107 insertions(+), 137 deletions(-)
---
diff --git a/tests/interactive/test-wayland-surface.c b/tests/interactive/test-wayland-surface.c
index acc8e48..5bf3016 100644
--- a/tests/interactive/test-wayland-surface.c
+++ b/tests/interactive/test-wayland-surface.c
@@ -380,6 +380,16 @@ const struct wl_surface_interface tws_surface_interface = {
   tws_surface_frame
 };
 
+/* This should be called whenever the window stacking changes to
+   update the current position on all of the input devices */
+void
+tws_compositor_repick (TWSCompositor *compositor)
+{
+  tws_input_device_repick (compositor->input_device,
+                           get_time (),
+                           NULL);
+}
+
 static void
 tws_surface_free (TWSSurface *surface)
 {
@@ -391,6 +401,8 @@ tws_surface_free (TWSSurface *surface)
     clutter_actor_destroy (surface->actor);
 
   g_slice_free (TWSSurface, surface);
+
+  tws_compositor_repick (compositor);
 }
 
 static void
diff --git a/tests/interactive/tws-input.c b/tests/interactive/tws-input.c
index 08d3d16..0dbe60b 100644
--- a/tests/interactive/tws-input.c
+++ b/tests/interactive/tws-input.c
@@ -14,63 +14,7 @@ struct _TwsInputDevice
 {
   struct wl_input_device parent;
 
-  /* Last position of the pointer */
-  float pointer_x, pointer_y;
-
-  /* Position of the pointer within the surface */
-  int32_t pointer_sx, pointer_sy;
-};
-
-static void
-implicit_grab_motion (struct wl_grab *grab,
-                      uint32_t time,
-                      int32_t x,
-                      int32_t y)
-{
-  struct wl_resource *resource;
-
-  resource = grab->input_device->pointer_focus_resource;
-
-  if (resource)
-    {
-      TWSSurface *surface = (TWSSurface *) grab->input_device->pointer_focus;
-      float sx, sy;
-
-      clutter_actor_transform_stage_point (surface->actor,
-                                           x,
-                                           y,
-                                           &sx,
-                                           &sy);
-
-      wl_resource_post_event (resource, WL_INPUT_DEVICE_MOTION,
-                              time, x, y, (int32_t) sx, (int32_t) sy);
-    }
-}
-
-static void
-implicit_grab_button (struct wl_grab *grab,
-                      uint32_t time,
-                      int32_t button,
-                      int32_t state)
-{
-  struct wl_resource *resource;
-
-  resource = grab->input_device->pointer_focus_resource;
-
-  if (resource)
-    wl_resource_post_event (resource, WL_INPUT_DEVICE_BUTTON,
-                            time, button, state);
-}
-
-static void
-implicit_grab_end(struct wl_grab *grab, uint32_t time)
-{
-}
-
-static const struct wl_grab_interface implicit_grab_interface = {
-  implicit_grab_motion,
-  implicit_grab_button,
-  implicit_grab_end
+  ClutterActor *current_stage;
 };
 
 static void
@@ -123,8 +67,6 @@ tws_input_device_new (struct wl_display *display)
 
   wl_input_device_init (&device->parent);
 
-  device->parent.implicit_grab.interface = &implicit_grab_interface;
-
   wl_display_add_global (display,
                          &wl_input_device_interface,
                          device,
@@ -134,73 +76,23 @@ tws_input_device_new (struct wl_display *display)
 }
 
 static void
-set_pointer_focus_for_event (TwsInputDevice *input_device,
-                             const ClutterEvent *event)
-{
-  struct wl_input_device *device =
-    (struct wl_input_device *) input_device;
-  struct wl_surface *surface;
-
-  clutter_event_get_coords (event,
-                            &input_device->pointer_x,
-                            &input_device->pointer_y);
-
-  if (CLUTTER_WAYLAND_IS_SURFACE (event->any.source))
-    {
-      ClutterWaylandSurface *surface_actor =
-        CLUTTER_WAYLAND_SURFACE (event->any.source);
-      float fsx, fsy;
-
-      surface = clutter_wayland_surface_get_surface (surface_actor);
-
-      clutter_actor_transform_stage_point (event->any.source,
-                                           input_device->pointer_x,
-                                           input_device->pointer_y,
-                                           &fsx, &fsy);
-      input_device->pointer_sx = fsx;
-      input_device->pointer_sy = fsy;
-    }
-  else
-    {
-      surface = NULL;
-      input_device->pointer_sx = input_device->pointer_x;
-      input_device->pointer_sy = input_device->pointer_y;
-    }
-
-  wl_input_device_set_pointer_focus (device,
-                                     surface,
-                                     event->any.time,
-                                     input_device->pointer_x,
-                                     input_device->pointer_y,
-                                     input_device->pointer_sx,
-                                     input_device->pointer_sy);
-}
-
-static void
 handle_motion_event (TwsInputDevice *input_device,
                      const ClutterMotionEvent *event)
 {
   struct wl_input_device *device =
     (struct wl_input_device *) input_device;
 
-  if (device->grab)
-    device->grab->interface->motion (device->grab,
-                                     event->time,
-                                     event->x,
-                                     event->y);
-  else
-    {
-      set_pointer_focus_for_event (input_device, (const ClutterEvent *) event);
-
-      if (device->pointer_focus_resource)
-        wl_resource_post_event (device->pointer_focus_resource,
-                                WL_INPUT_DEVICE_MOTION,
-                                time,
-                                (int32_t) input_device->pointer_x,
-                                (int32_t) input_device->pointer_y,
-                                input_device->pointer_sx,
-                                input_device->pointer_sy);
-    }
+  device->x = event->x;
+  device->y = event->y;
+
+  tws_input_device_repick (input_device,
+                           event->time,
+                           event->source);
+
+  device->grab->interface->motion (device->grab,
+                                   event->time,
+                                   device->grab->x,
+                                   device->grab->y);
 }
 
 static void
@@ -209,7 +101,6 @@ handle_button_event (TwsInputDevice *input_device,
 {
   struct wl_input_device *device =
     (struct wl_input_device *) input_device;
-  struct wl_surface *surface = device->pointer_focus;
   gboolean state = event->type == CLUTTER_BUTTON_PRESS;
   uint32_t button;
 
@@ -230,24 +121,22 @@ handle_button_event (TwsInputDevice *input_device,
       break;
     }
 
-  if (state && surface && device->grab == NULL)
-    wl_input_device_start_grab (device,
-                                &device->implicit_grab,
-                                button,
-                                event->time);
-
-  if (device->grab)
-    device->grab->interface->button (device->grab,
-                                     event->time,
-                                     button,
-                                     state);
-
-  if (!state && device->grab && device->grab_button == button)
+  if (state)
     {
-      wl_input_device_end_grab (device, event->time);
-
-      set_pointer_focus_for_event (input_device, (const ClutterEvent *) event);
+      if (device->button_count == 0)
+        {
+          device->grab_button = button;
+          device->grab_time = event->time;
+          device->grab_x = device->x;
+          device->grab_y = device->y;
+        }
+
+      device->button_count++;
     }
+  else
+    device->button_count--;
+
+  device->grab->interface->button (device->grab, event->time, button, state);
 }
 
 void
@@ -271,3 +160,67 @@ tws_input_device_handle_event (TwsInputDevice *input_device,
       break;
     }
 }
+
+/* The actor argument can be NULL in which case a Clutter pick will be
+   performed to determine the right actor. An actor should only be
+   passed if the repick is being performed due to an event in which
+   case Clutter will have already performed a pick so we can avoid
+   redundantly doing another one */
+void
+tws_input_device_repick (TwsInputDevice *device,
+                         uint32_t time,
+                         ClutterActor *actor)
+{
+  struct wl_input_device *input_device = (struct wl_input_device *) device;
+  struct wl_surface *surface;
+  TWSSurface *focus;
+
+  if (actor == NULL && device->current_stage)
+    {
+      ClutterStage *stage = CLUTTER_STAGE (device->current_stage);
+      actor = clutter_stage_get_actor_at_pos (stage,
+                                              CLUTTER_PICK_REACTIVE,
+                                              input_device->x, input_device->y);
+    }
+
+  if (actor)
+    device->current_stage = clutter_actor_get_stage (actor);
+  else
+    device->current_stage = NULL;
+
+  if (CLUTTER_WAYLAND_IS_SURFACE (actor))
+    {
+      ClutterWaylandSurface *wl_surface = CLUTTER_WAYLAND_SURFACE (actor);
+      float ax, ay;
+
+      clutter_actor_transform_stage_point (actor,
+                                           input_device->x, input_device->y,
+                                           &ax, &ay);
+      input_device->current_x = ax;
+      input_device->current_y = ay;
+
+      surface = clutter_wayland_surface_get_surface (wl_surface);
+    }
+  else
+    surface = NULL;
+
+  if (surface != input_device->current)
+    {
+      const struct wl_grab_interface *interface = input_device->grab->interface;
+      interface->focus (input_device->grab, time, surface,
+                        input_device->current_x, input_device->current_y);
+      input_device->current = surface;
+    }
+
+  focus = (TWSSurface *) input_device->grab->focus;
+  if (focus)
+    {
+      float ax, ay;
+
+      clutter_actor_transform_stage_point (focus->actor,
+                                           input_device->x, input_device->y,
+                                           &ax, &ay);
+      input_device->grab->x = ax;
+      input_device->grab->y = ay;
+    }
+}
diff --git a/tests/interactive/tws-input.h b/tests/interactive/tws-input.h
index efe05a9..d1d6bf1 100644
--- a/tests/interactive/tws-input.h
+++ b/tests/interactive/tws-input.h
@@ -12,4 +12,9 @@ void
 tws_input_device_handle_event (TwsInputDevice *input_device,
                                const ClutterEvent *event);
 
+void
+tws_input_device_repick (TwsInputDevice *input_device,
+                         uint32_t time,
+                         ClutterActor *actor);
+
 #endif /* __TWS_INPUT_H__ */



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