[mutter/wip/dnd-actions] WIP: Wayland DnD actions



commit 522598c3444ed956998594295943dc8ad9d1bbf1
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Feb 18 13:07:00 2015 +0100

    WIP: Wayland DnD actions

 src/wayland/meta-wayland-data-device.c |  201 +++++++++++++++++++++++++++++++-
 src/wayland/meta-wayland-keyboard.c    |   60 +++++++++-
 src/wayland/meta-wayland-keyboard.h    |   22 ++++
 src/wayland/meta-wayland-pointer.h     |    2 +
 src/wayland/meta-wayland-private.h     |    1 +
 src/wayland/meta-wayland-types.h       |    2 +
 src/wayland/meta-wayland-versions.h    |    2 +-
 7 files changed, 281 insertions(+), 9 deletions(-)
---
diff --git a/src/wayland/meta-wayland-data-device.c b/src/wayland/meta-wayland-data-device.c
index 16f626d..2b2a697 100644
--- a/src/wayland/meta-wayland-data-device.c
+++ b/src/wayland/meta-wayland-data-device.c
@@ -41,6 +41,8 @@ typedef struct
   struct wl_resource *resource;
   MetaWaylandDataSource *source;
   struct wl_listener source_destroy_listener;
+  uint32_t dnd_actions;
+  uint32_t preferred_dnd_action;
 } MetaWaylandDataOffer;
 
 struct _MetaWaylandDataSource
@@ -48,6 +50,10 @@ struct _MetaWaylandDataSource
   struct wl_resource *resource;
   struct wl_array mime_types;
   gboolean has_target;
+  uint32_t dnd_actions;
+  uint32_t user_dnd_action;
+  uint32_t current_dnd_action;
+  MetaWaylandDataOffer *offer;
 };
 
 static void
@@ -56,6 +62,60 @@ unbind_resource (struct wl_resource *resource)
   wl_list_remove (wl_resource_get_link (resource));
 }
 
+static uint32_t
+data_offer_choose_action (MetaWaylandDataOffer *offer)
+{
+  MetaWaylandDataSource *source = offer->source;
+  uint32_t available_actions;
+
+  available_actions = source->dnd_actions & offer->dnd_actions;
+
+  if (!available_actions)
+    return 0;
+
+  /* If the user is forcing an action, go for it */
+  if ((source->user_dnd_action & available_actions) != 0)
+    return source->user_dnd_action;
+
+  /* If the dest side has a preferred DnD action, use it */
+  if ((offer->preferred_dnd_action & available_actions) != 0)
+    return offer->preferred_dnd_action;
+
+  /* Use the first found action, in bit order */
+  return 1 << (ffs (available_actions) - 1);
+}
+
+static void
+data_source_update_actions (MetaWaylandDataSource *source,
+                            uint32_t               dnd_actions)
+{
+  source->dnd_actions = dnd_actions;
+
+  if (source->offer)
+    {
+      wl_data_offer_send_source_actions (source->offer->resource,
+                                         source->dnd_actions);
+    }
+}
+
+static void
+data_offer_update_action (MetaWaylandDataOffer *offer)
+{
+  uint32_t action;
+
+  if (!offer->source)
+    return;
+
+  action = data_offer_choose_action (offer);
+
+  if (offer->source->current_dnd_action == action)
+    return;
+
+  offer->source->current_dnd_action = action;
+  wl_data_source_send_action (offer->source->resource, action);
+  wl_data_offer_send_action (offer->resource, action);
+}
+
 static void
 data_offer_accept (struct wl_client *client,
                    struct wl_resource *resource,
@@ -93,10 +153,25 @@ data_offer_destroy (struct wl_client *client, struct wl_resource *resource)
   wl_resource_destroy (resource);
 }
 
+static void
+data_offer_notify_actions (struct wl_client   *client,
+                           struct wl_resource *resource,
+                           uint32_t            dnd_actions,
+                           uint32_t            preferred_action)
+{
+  MetaWaylandDataOffer *offer = wl_resource_get_user_data (resource);
+
+  offer->dnd_actions = dnd_actions;
+  offer->preferred_dnd_action = preferred_action;
+
+  data_offer_update_action (offer);
+}
+
 static const struct wl_data_offer_interface data_offer_interface = {
   data_offer_accept,
   data_offer_receive,
   data_offer_destroy,
+  data_offer_notify_actions
 };
 
 static void
@@ -105,7 +180,15 @@ destroy_data_offer (struct wl_resource *resource)
   MetaWaylandDataOffer *offer = wl_resource_get_user_data (resource);
 
   if (offer->source)
-    wl_list_remove (&offer->source_destroy_listener.link);
+    {
+      if (offer == offer->source->offer)
+        {
+          wl_data_source_send_drag_finished (offer->source->resource);
+          offer->source->offer = NULL;
+        }
+
+      wl_list_remove (&offer->source_destroy_listener.link);
+    }
 
   g_slice_free (MetaWaylandDataOffer, offer);
 }
@@ -128,7 +211,10 @@ meta_wayland_data_source_send_offer (MetaWaylandDataSource *source,
   char **p;
 
   offer->source = source;
+
   offer->source_destroy_listener.notify = destroy_offer_data_source;
+  offer->dnd_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
+    WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
 
   offer->resource = wl_resource_create (wl_resource_get_client (target), &wl_data_offer_interface, 
wl_resource_get_version (target), 0);
   wl_resource_set_implementation (offer->resource, &data_offer_interface, offer, destroy_data_offer);
@@ -139,6 +225,9 @@ meta_wayland_data_source_send_offer (MetaWaylandDataSource *source,
   wl_array_for_each (p, &source->mime_types)
     wl_data_offer_send_offer (offer->resource, *p);
 
+  data_offer_update_action (offer);
+  source->offer = offer;
+
   return offer->resource;
 }
 
@@ -162,14 +251,38 @@ data_source_destroy (struct wl_client *client, struct wl_resource *resource)
   wl_resource_destroy (resource);
 }
 
+static void
+data_source_notify_actions (struct wl_client   *client,
+                            struct wl_resource *resource,
+                            uint32_t            dnd_actions)
+{
+  MetaWaylandDataSource *source = wl_resource_get_user_data (resource);
+
+  if (source->dnd_actions == dnd_actions)
+    return;
+
+  data_source_update_actions (source, dnd_actions);
+
+  if (source->offer)
+    data_offer_update_action (source->offer);
+}
+
 static struct wl_data_source_interface data_source_interface = {
   data_source_offer,
-  data_source_destroy
+  data_source_destroy,
+  data_source_notify_actions
 };
 
+typedef struct {
+  MetaWaylandKeyboardGrab generic;
+  MetaWaylandDataSource  *data_source;
+} MetaWaylandKeyboardDragGrab;
+
 struct _MetaWaylandDragGrab {
   MetaWaylandPointerGrab  generic;
 
+  MetaWaylandKeyboardDragGrab keyboard_grab;
+
   MetaWaylandSeat        *seat;
   struct wl_client       *drag_client;
 
@@ -189,6 +302,7 @@ struct _MetaWaylandDragGrab {
   struct wl_listener      drag_origin_listener;
 
   int                     drag_start_x, drag_start_y;
+  ClutterModifierType     buttons;
 };
 
 static void
@@ -254,6 +368,28 @@ drag_grab_focus (MetaWaylandPointerGrab *grab,
 }
 
 static void
+data_source_update_user_dnd_action (MetaWaylandDataSource *source,
+                                    ClutterModifierType    modifiers)
+{
+  uint32_t user_dnd_action = 0;
+
+  if (modifiers & CLUTTER_SHIFT_MASK)
+    user_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
+  else if (modifiers & CLUTTER_CONTROL_MASK)
+    user_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
+  else if (modifiers & (CLUTTER_MOD1_MASK | CLUTTER_BUTTON2_MASK))
+    user_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
+
+  if (source->user_dnd_action == user_dnd_action)
+    return;
+
+  source->user_dnd_action = user_dnd_action;
+
+  if (source->offer)
+    data_offer_update_action (source->offer);
+}
+
+static void
 drag_grab_motion (MetaWaylandPointerGrab *grab,
                  const ClutterEvent     *event)
 {
@@ -307,6 +443,7 @@ data_device_end_drag_grab (MetaWaylandDragGrab *drag_grab)
   drag_grab_focus (&drag_grab->generic, NULL);
 
   meta_wayland_pointer_end_grab (drag_grab->generic.pointer);
+  meta_wayland_keyboard_end_grab (drag_grab->keyboard_grab.generic.keyboard);
   g_slice_free (MetaWaylandDragGrab, drag_grab);
 }
 
@@ -324,11 +461,17 @@ drag_grab_button (MetaWaylandPointerGrab *grab,
       gboolean success = FALSE;
 
       if (drag_grab->drag_focus_data_device &&
-          drag_grab->drag_data_source->has_target)
+          drag_grab->drag_data_source->has_target &&
+          drag_grab->drag_data_source->current_dnd_action)
         {
           wl_data_device_send_drop (drag_grab->drag_focus_data_device);
+          wl_data_source_send_drop_performed (drag_grab->drag_data_source->resource);
           success = TRUE;
         }
+      else
+        {
+          wl_data_source_send_cancelled (drag_grab->drag_data_source->resource);
+        }
 
       /* Finish drag and let actor self-destruct */
       meta_dnd_actor_drag_finish (META_DND_ACTOR (drag_grab->feedback_actor),
@@ -344,7 +487,36 @@ drag_grab_button (MetaWaylandPointerGrab *grab,
 static const MetaWaylandPointerGrabInterface drag_grab_interface = {
   drag_grab_focus,
   drag_grab_motion,
-  drag_grab_button,
+  drag_grab_button
+};
+
+static gboolean
+keyboard_drag_grab_key (MetaWaylandKeyboardGrab *grab,
+                        const ClutterEvent      *event)
+{
+  return FALSE;
+}
+
+static void
+keyboard_drag_grab_modifiers (MetaWaylandKeyboardGrab *grab,
+                              ClutterModifierType      modifiers)
+{
+  MetaWaylandDragGrab *drag_grab;
+
+  drag_grab = wl_container_of (grab, drag_grab, keyboard_grab);
+
+  /* The modifiers here just contain keyboard modifiers, mix it with the
+   * mouse button modifiers we got when starting the drag operation.
+   */
+  modifiers |= drag_grab->buttons;
+
+  if (drag_grab->drag_data_source)
+    data_source_update_user_dnd_action (drag_grab->drag_data_source, modifiers);
+}
+
+static const MetaWaylandKeyboardGrabInterface keyboard_drag_grab_interface = {
+  keyboard_drag_grab_key,
+  keyboard_drag_grab_modifiers
 };
 
 static void
@@ -371,7 +543,7 @@ static void
 destroy_data_device_icon (struct wl_listener *listener, void *data)
 {
   MetaWaylandDragGrab *drag_grab =
-    wl_container_of (listener, drag_grab, drag_data_source_listener);
+    wl_container_of (listener, drag_grab, drag_icon_listener);
 
   drag_grab->drag_surface = NULL;
 
@@ -390,6 +562,8 @@ data_device_start_drag (struct wl_client *client,
   MetaWaylandSeat *seat = wl_container_of (data_device, seat, data_device);
   MetaWaylandSurface *surface = NULL;
   MetaWaylandDragGrab *drag_grab;
+  MetaWaylandKeyboardDragGrab *keyboard_grab;
+  ClutterModifierType modifiers;
   ClutterPoint pos;
 
   if (origin_resource)
@@ -418,10 +592,14 @@ data_device_start_drag (struct wl_client *client,
     return;
 
   data_device->current_grab = drag_grab = g_slice_new0 (MetaWaylandDragGrab);
+  keyboard_grab = &drag_grab->keyboard_grab;
 
   drag_grab->generic.interface = &drag_grab_interface;
   drag_grab->generic.pointer = &seat->pointer;
 
+  keyboard_grab->generic.interface = &keyboard_drag_grab_interface;
+  keyboard_grab->generic.keyboard = &seat->keyboard;
+
   drag_grab->drag_client = client;
   drag_grab->seat = seat;
 
@@ -436,12 +614,18 @@ data_device_start_drag (struct wl_client *client,
   drag_grab->drag_start_x = pos.x;
   drag_grab->drag_start_y = pos.y;
 
+  modifiers = clutter_input_device_get_modifier_state (seat->pointer.device);
+  drag_grab->buttons = modifiers &
+    (CLUTTER_BUTTON1_MASK | CLUTTER_BUTTON2_MASK | CLUTTER_BUTTON3_MASK |
+     CLUTTER_BUTTON4_MASK | CLUTTER_BUTTON5_MASK);
+
   if (source_resource)
     {
-      drag_grab->drag_data_source = wl_resource_get_user_data (source_resource);
+      drag_grab->drag_data_source = keyboard_grab->data_source = wl_resource_get_user_data (source_resource);
       drag_grab->drag_data_source_listener.notify = destroy_data_device_source;
       wl_resource_add_destroy_listener (source_resource,
                                         &drag_grab->drag_data_source_listener);
+      data_source_update_user_dnd_action (drag_grab->drag_data_source, modifiers);
     }
 
   if (icon_resource)
@@ -467,6 +651,9 @@ data_device_start_drag (struct wl_client *client,
 
   meta_wayland_pointer_set_focus (&seat->pointer, NULL);
   meta_wayland_pointer_start_grab (&seat->pointer, (MetaWaylandPointerGrab*)drag_grab);
+
+  meta_wayland_keyboard_set_focus (&seat->keyboard, NULL);
+  meta_wayland_keyboard_start_grab (&seat->keyboard, (MetaWaylandKeyboardGrab*) keyboard_grab);
 }
 
 static void
@@ -589,6 +776,8 @@ create_data_source (struct wl_client *client,
   wl_resource_set_implementation (source->resource, &data_source_interface, source, destroy_data_source);
 
   wl_array_init (&source->mime_types);
+  source->dnd_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
+    WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
 }
 
 static void
diff --git a/src/wayland/meta-wayland-keyboard.c b/src/wayland/meta-wayland-keyboard.c
index e4d4f22..99f63bd 100644
--- a/src/wayland/meta-wayland-keyboard.c
+++ b/src/wayland/meta-wayland-keyboard.c
@@ -63,6 +63,7 @@
 
 static void meta_wayland_keyboard_update_xkb_state (MetaWaylandKeyboard *keyboard);
 static void notify_modifiers (MetaWaylandKeyboard *keyboard);
+static guint evdev_code (const ClutterKeyEvent *event);
 
 static void
 unbind_resource (struct wl_resource *resource)
@@ -264,7 +265,7 @@ notify_key (MetaWaylandKeyboard *keyboard,
 }
 
 static void
-notify_modifiers (MetaWaylandKeyboard *keyboard)
+apply_modifiers (MetaWaylandKeyboard *keyboard)
 {
   struct xkb_state *state;
   struct wl_resource *resource;
@@ -290,6 +291,16 @@ notify_modifiers (MetaWaylandKeyboard *keyboard)
 }
 
 static void
+notify_modifiers (MetaWaylandKeyboard *keyboard)
+{
+  struct xkb_state *state;
+
+  state = keyboard->xkb_info.state;
+  keyboard->grab->interface->modifiers (keyboard->grab,
+                                        xkb_state_serialize_mods (state, XKB_STATE_MODS_EFFECTIVE));
+}
+
+static void
 meta_wayland_keyboard_update_xkb_state (MetaWaylandKeyboard *keyboard)
 {
   MetaWaylandXkbInfo *xkb_info = &keyboard->xkb_info;
@@ -368,6 +379,33 @@ settings_changed (GSettings           *settings,
   notify_key_repeat (keyboard);
 }
 
+static gboolean
+default_grab_key (MetaWaylandKeyboardGrab *grab,
+                  const ClutterEvent      *event)
+{
+  MetaWaylandKeyboard *keyboard = grab->keyboard;
+  gboolean is_press = event->type == CLUTTER_KEY_PRESS;
+
+  /* Synthetic key events are for autorepeat. Ignore those, as
+   * autorepeat in Wayland is done on the client side. */
+  if (event->key.flags & CLUTTER_EVENT_FLAG_SYNTHETIC)
+    return FALSE;
+
+  return notify_key (keyboard, event->key.time, evdev_code (&event->key), is_press);
+}
+
+static void
+default_grab_modifiers (MetaWaylandKeyboardGrab *grab,
+                        ClutterModifierType      modifiers)
+{
+  apply_modifiers (grab->keyboard);
+}
+
+static const MetaWaylandKeyboardGrabInterface default_keyboard_grab_interface = {
+  default_grab_key,
+  default_grab_modifiers
+};
+
 void
 meta_wayland_keyboard_init (MetaWaylandKeyboard *keyboard,
                             struct wl_display   *display)
@@ -385,6 +423,10 @@ meta_wayland_keyboard_init (MetaWaylandKeyboard *keyboard,
 
   keyboard->xkb_info.keymap_fd = -1;
 
+  keyboard->default_grab.interface = &default_keyboard_grab_interface;
+  keyboard->default_grab.keyboard = keyboard;
+  keyboard->grab = &keyboard->default_grab;
+
   keyboard->settings = g_settings_new ("org.gnome.desktop.peripherals.keyboard");
   g_signal_connect (keyboard->settings, "changed",
                     G_CALLBACK (settings_changed), keyboard);
@@ -461,7 +503,7 @@ meta_wayland_keyboard_handle_event (MetaWaylandKeyboard *keyboard,
                is_press ? "press" : "release",
                event->hardware_keycode);
 
-  handled = notify_key (keyboard, event->time, evdev_code (event), is_press);
+  handled = keyboard->grab->interface->key (keyboard->grab, (const ClutterEvent *) event);
 
   if (handled)
     meta_verbose ("Sent event to wayland client\n");
@@ -644,3 +686,17 @@ meta_wayland_keyboard_create_new_resource (MetaWaylandKeyboard *keyboard,
       wl_list_insert (&keyboard->resource_list, wl_resource_get_link (cr));
     }
 }
+
+void
+meta_wayland_keyboard_start_grab (MetaWaylandKeyboard     *keyboard,
+                                  MetaWaylandKeyboardGrab *grab)
+{
+  keyboard->grab = grab;
+  grab->keyboard = keyboard;
+}
+
+void
+meta_wayland_keyboard_end_grab (MetaWaylandKeyboard *keyboard)
+{
+  keyboard->grab = &keyboard->default_grab;
+}
diff --git a/src/wayland/meta-wayland-keyboard.h b/src/wayland/meta-wayland-keyboard.h
index b4a8c52..9eeedac 100644
--- a/src/wayland/meta-wayland-keyboard.h
+++ b/src/wayland/meta-wayland-keyboard.h
@@ -49,6 +49,20 @@
 #include <wayland-server.h>
 #include <xkbcommon/xkbcommon.h>
 
+struct _MetaWaylandKeyboardGrabInterface
+{
+  gboolean (* key)      (MetaWaylandKeyboardGrab *grab,
+                         const ClutterEvent      *event);
+  void     (*modifiers) (MetaWaylandKeyboardGrab *grab,
+                         ClutterModifierType      modifiers);
+};
+
+struct _MetaWaylandKeyboardGrab
+{
+  const MetaWaylandKeyboardGrabInterface *interface;
+  MetaWaylandKeyboard *keyboard;
+};
+
 typedef struct
 {
   struct xkb_keymap *keymap;
@@ -72,6 +86,9 @@ struct _MetaWaylandKeyboard
   MetaWaylandXkbInfo xkb_info;
   enum xkb_state_component mods_changed;
 
+  MetaWaylandKeyboardGrab *grab;
+  MetaWaylandKeyboardGrab default_grab;
+
   GSettings *settings;
 };
 
@@ -96,4 +113,9 @@ void meta_wayland_keyboard_create_new_resource (MetaWaylandKeyboard *keyboard,
                                                 struct wl_resource  *seat_resource,
                                                 uint32_t id);
 
+void meta_wayland_keyboard_start_grab (MetaWaylandKeyboard     *keyboard,
+                                       MetaWaylandKeyboardGrab *grab);
+void meta_wayland_keyboard_end_grab   (MetaWaylandKeyboard     *keyboard);
+
+
 #endif /* META_WAYLAND_KEYBOARD_H */
diff --git a/src/wayland/meta-wayland-pointer.h b/src/wayland/meta-wayland-pointer.h
index 70b7b42..b725681 100644
--- a/src/wayland/meta-wayland-pointer.h
+++ b/src/wayland/meta-wayland-pointer.h
@@ -36,6 +36,8 @@ struct _MetaWaylandPointerGrabInterface
                  const ClutterEvent     *event);
   void (*button) (MetaWaylandPointerGrab *grab,
                  const ClutterEvent     *event);
+  void (*modifiers) (MetaWaylandPointerGrab *grab,
+                     ClutterModifierType     modifiers);
 };
 
 struct _MetaWaylandPointerGrab
diff --git a/src/wayland/meta-wayland-private.h b/src/wayland/meta-wayland-private.h
index 245d496..a21206e 100644
--- a/src/wayland/meta-wayland-private.h
+++ b/src/wayland/meta-wayland-private.h
@@ -51,6 +51,7 @@ typedef struct
   struct wl_resource *xserver_resource;
   char *display_name;
 
+  GMainContext *main_context;
   GMainLoop *init_loop;
 } MetaXWaylandManager;
 
diff --git a/src/wayland/meta-wayland-types.h b/src/wayland/meta-wayland-types.h
index 9713f1e..5165a76 100644
--- a/src/wayland/meta-wayland-types.h
+++ b/src/wayland/meta-wayland-types.h
@@ -29,6 +29,8 @@ typedef struct _MetaWaylandPointerGrabInterface MetaWaylandPointerGrabInterface;
 typedef struct _MetaWaylandPopupGrab MetaWaylandPopupGrab;
 typedef struct _MetaWaylandPopup MetaWaylandPopup;
 typedef struct _MetaWaylandKeyboard MetaWaylandKeyboard;
+typedef struct _MetaWaylandKeyboardGrab MetaWaylandKeyboardGrab;
+typedef struct _MetaWaylandKeyboardGrabInterface MetaWaylandKeyboardGrabInterface;
 typedef struct _MetaWaylandTouch MetaWaylandTouch;
 typedef struct _MetaWaylandDataSource MetaWaylandDataSource;
 typedef struct _MetaWaylandDataDevice MetaWaylandDataDevice;
diff --git a/src/wayland/meta-wayland-versions.h b/src/wayland/meta-wayland-versions.h
index 3944956..d601148 100644
--- a/src/wayland/meta-wayland-versions.h
+++ b/src/wayland/meta-wayland-versions.h
@@ -36,7 +36,7 @@
 
 /* Global/master objects (version exported by wl_registry and negotiated through bind) */
 #define META_WL_COMPOSITOR_VERSION          3
-#define META_WL_DATA_DEVICE_MANAGER_VERSION 2
+#define META_WL_DATA_DEVICE_MANAGER_VERSION 3
 #define META_XDG_SHELL_VERSION              1
 #define META_WL_SHELL_VERSION               1
 #define META_WL_SEAT_VERSION                4


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