This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository enlightenment.

View the commit online.

commit 22803dde5662d551788c11323c4bb3d175d5a49f
Author: Cedric BAIL <[email protected]>
AuthorDate: Tue Nov 24 20:39:30 2026 -0600

    wl_desktop_shell - add xdg_toplevel_drag_v1
    
    When a tab is torn out of a browser window, the browser starts a drag
    and attaches a new window to it with xdg_toplevel_drag_v1, expecting
    the compositor to carry the window with the pointer. E does not offer
    the protocol, so Chromium and Firefox leave the new window where it
    was mapped.
    
    The protocol object is in wl_desktop_shell, because attach takes an
    xdg_toplevel, which the module defines. The window is carried by the
    data device, which owns the drag: e_comp_wl_data_source_toplevel_set(),
    a new E_API for the module, hands it over with a reference, and the
    window follows the drag object's moves.
    
    As the protocol asks, the offset is relative to the window geometry,
    the window is detached when it is unmapped, and it does not take part
    in choosing the drop target: it passes pointer events while it is
    carried, so the window under it gets the drag. It is raised when
    attached, so that it is seen above the window it is dragged over, and
    stays where the drop leaves it.
    
    The XML comes from wayland-protocols 1.34 or later; meson.build still
    asks for 1.12, as other staging protocols already need newer ones.
    
    Only Wayland code is touched.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bin/e_comp_wl_data.c                         |  75 ++++
 src/bin/e_comp_wl_data.h                         |   9 +
 src/modules/wl_desktop_shell/e_mod_main.c        |   2 +
 src/modules/wl_desktop_shell/e_mod_main.h        |   1 +
 src/modules/wl_desktop_shell/meson.build         |   2 +
 src/modules/wl_desktop_shell/xdg_toplevel_drag.c | 145 ++++++++
 src/tests/wayland/globals.expected               |   1 +
 src/tests/wayland/meson.build                    |   2 +
 src/tests/wayland/test_toplevel_drag.c           | 415 +++++++++++++++++++++++
 9 files changed, 652 insertions(+)

diff --git a/src/bin/e_comp_wl_data.c b/src/bin/e_comp_wl_data.c
index c90ba1c6d..a5749024c 100644
--- a/src/bin/e_comp_wl_data.c
+++ b/src/bin/e_comp_wl_data.c
@@ -360,6 +360,69 @@ data_source_set_actions(struct wl_client *client,
    source->actions_set = 1;
 }
 
+/* The carried window passes pointer events while it follows the pointer:
+ * the protocol keeps it out of the choice of the drop target, which is
+ * whatever window is under the pointer. */
+static void
+_e_comp_wl_data_source_toplevel_release(E_Comp_Wl_Data_Source *source)
+{
+   E_Client *ec = source->toplevel;
+
+   if (!ec) return;
+   source->toplevel = NULL;
+   if ((!e_object_is_del(E_OBJECT(ec))) && (ec->comp_data->mapped))
+     evas_object_pass_events_set(ec->frame, EINA_FALSE);
+   e_object_unref(E_OBJECT(ec));
+}
+
+/* An unmapped window is detached, as the protocol says. */
+static void
+_e_comp_wl_data_source_toplevel_carry(E_Comp_Wl_Data_Source *source)
+{
+   E_Client *ec = source->toplevel;
+   int x, y;
+
+   if (!ec) return;
+   if ((e_object_is_del(E_OBJECT(ec))) || (!ec->comp_data->mapped))
+     {
+        _e_comp_wl_data_source_toplevel_release(source);
+        return;
+     }
+   evas_object_pass_events_set(ec->frame, EINA_TRUE);
+   evas_pointer_canvas_xy_get(e_comp->evas, &x, &y);
+   /* The offset is relative to the window geometry, which is inside the
+    * surface that e_client_util_move_without_frame() places. */
+   e_client_util_move_without_frame(ec, x - source->toplevel_x - ec->comp_data->shell.window.x,
+                                    y - source->toplevel_y - ec->comp_data->shell.window.y);
+}
+
+static void
+_e_comp_wl_data_device_cb_drag_move(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
+{
+   if (e_comp_wl->drag_source)
+     _e_comp_wl_data_source_toplevel_carry(e_comp_wl->drag_source);
+}
+
+E_API void
+e_comp_wl_data_source_toplevel_set(E_Comp_Wl_Data_Source *source, E_Client *ec, int x, int y)
+{
+   EINA_SAFETY_ON_NULL_RETURN(source);
+   if (source->toplevel != ec)
+     {
+        _e_comp_wl_data_source_toplevel_release(source);
+        source->toplevel = ec;
+        if (ec)
+          {
+             e_object_ref(E_OBJECT(ec));
+             evas_object_raise(ec->frame);
+          }
+     }
+   source->toplevel_x = x;
+   source->toplevel_y = y;
+   if (e_comp_wl->drag_source == source)
+     _e_comp_wl_data_source_toplevel_carry(source);
+}
+
 /* called by wl_resource_destroy */
 static void
 _e_comp_wl_data_source_cb_resource_destroy(struct wl_resource *resource)
@@ -371,6 +434,7 @@ _e_comp_wl_data_source_cb_resource_destroy(struct wl_resource *resource)
 
    wl_signal_emit(&source->destroy_signal, source);
 
+   _e_comp_wl_data_source_toplevel_release(source);
    _mime_types_free(source);
 
    if (e_comp_wl->drag_source == source)
@@ -564,6 +628,7 @@ _e_comp_wl_data_device_drag_finished(E_Drag *drag, int dropped)
    if (e_comp_wl->drag != drag) return;
    e_comp_wl->drag = NULL;
    e_comp_wl->drag_client = NULL;
+   if (data_source) _e_comp_wl_data_source_toplevel_release(data_source);
    e_screensaver_inhibit_toggle(0);
    if (dropped) return;
 #ifndef HAVE_WAYLAND_ONLY
@@ -684,6 +749,9 @@ _e_comp_wl_data_device_cb_drag_start(struct wl_client *client, struct wl_resourc
    if (ec)
      e_drag_object_set(e_comp_wl->drag, ec->frame);
    e_drag_start(e_comp_wl->drag, x, y);
+   evas_object_event_callback_add(e_comp_wl->drag->comp_object, EVAS_CALLBACK_MOVE,
+                                  _e_comp_wl_data_device_cb_drag_move, NULL);
+   _e_comp_wl_data_source_toplevel_carry(source);
 #ifndef HAVE_WAYLAND_ONLY
    if (e_comp_util_has_xwayland())
      {
@@ -714,6 +782,13 @@ _e_comp_wl_data_device_cb_selection_set(struct wl_client *client EINA_UNUSED, st
                                "cannot set drag-and-drop source as selection");
         return;
      }
+   if (source->dnd_only)
+     {
+        wl_resource_post_error(source_resource,
+                               WL_DATA_SOURCE_ERROR_INVALID_SOURCE,
+                               "cannot set the source of an xdg_toplevel_drag as selection");
+        return;
+     }
 
    _e_comp_wl_data_device_selection_set(e_comp->wl_comp_data, source, serial);
 }
diff --git a/src/bin/e_comp_wl_data.h b/src/bin/e_comp_wl_data.h
index 2e996e88a..0c9b6e789 100644
--- a/src/bin/e_comp_wl_data.h
+++ b/src/bin/e_comp_wl_data.h
@@ -52,9 +52,15 @@ struct _E_Comp_Wl_Data_Source
    enum wl_data_device_manager_dnd_action current_dnd_action;
    enum wl_data_device_manager_dnd_action compositor_action;
    uint32_t serial;
+   /* xdg_toplevel_drag_v1: the window carried with this drag, holding a
+    * reference, and where it sits relative to the pointer. */
+   E_Client *toplevel;
+   int toplevel_x, toplevel_y;
 
    Eina_Bool accepted E_BITFIELD;
    Eina_Bool actions_set E_BITFIELD;
+   /* An xdg_toplevel_drag_v1 was made from it: never a selection. */
+   Eina_Bool dnd_only E_BITFIELD;
 };
 
 struct _E_Comp_Wl_Data_Offer
@@ -94,6 +100,9 @@ EINTERN void e_comp_wl_data_primary_selection_update(void);
 EINTERN Eina_Bool e_comp_wl_data_manager_init(void);
 EINTERN void e_comp_wl_data_manager_shutdown(void);
 E_API struct wl_resource *e_comp_wl_data_find_for_client(struct wl_client *client);
+/* For wl_desktop_shell, which implements xdg_toplevel_drag_v1 because it
+ * owns xdg_toplevel. A NULL ec detaches. */
+E_API void e_comp_wl_data_source_toplevel_set(E_Comp_Wl_Data_Source *source, E_Client *ec, int x, int y);
 E_API E_Comp_Wl_Data_Source *e_comp_wl_data_manager_source_create(struct wl_client *client, struct wl_resource *resource, uint32_t id);
 E_API void e_comp_wl_clipboard_source_unref(E_Comp_Wl_Clipboard_Source *source);
 E_API E_Comp_Wl_Clipboard_Source *e_comp_wl_clipboard_source_create(const char *mime_type, uint32_t serial, int fd);
diff --git a/src/modules/wl_desktop_shell/e_mod_main.c b/src/modules/wl_desktop_shell/e_mod_main.c
index 0a5d42293..787945bc9 100644
--- a/src/modules/wl_desktop_shell/e_mod_main.c
+++ b/src/modules/wl_desktop_shell/e_mod_main.c
@@ -173,6 +173,8 @@ e_modapi_init(E_Module *m)
 
    have_shell = e_xdg_shell_v6_init() | e_xdg_shell_init();
    if (!have_shell) return NULL;
+   if (!e_xdg_toplevel_drag_init())
+     ERR("Could not create global for xdg_toplevel_drag_manager_v1");
 
 #ifdef HAVE_WL_TEXT_INPUT
    if (!e_input_panel_init())
diff --git a/src/modules/wl_desktop_shell/e_mod_main.h b/src/modules/wl_desktop_shell/e_mod_main.h
index f169be3fe..85ec09add 100644
--- a/src/modules/wl_desktop_shell/e_mod_main.h
+++ b/src/modules/wl_desktop_shell/e_mod_main.h
@@ -17,6 +17,7 @@ EINTERN E_Shell_Data *e_shell_data_new(unsigned int version);
 
 EINTERN Eina_Bool e_xdg_shell_v6_init(void);
 EINTERN Eina_Bool e_xdg_shell_init(void);
+EINTERN Eina_Bool e_xdg_toplevel_drag_init(void);
 EINTERN void e_xdg_shell_shutdown(void);
 EINTERN void wl_shell_cb_bind(struct wl_client *client, void *data EINA_UNUSED, uint32_t version, uint32_t id);
 
diff --git a/src/modules/wl_desktop_shell/meson.build b/src/modules/wl_desktop_shell/meson.build
index 83803afaf..bf15a85fc 100644
--- a/src/modules/wl_desktop_shell/meson.build
+++ b/src/modules/wl_desktop_shell/meson.build
@@ -7,6 +7,7 @@ else
     'wl_shell.c',
     'xdg6.c',
     'xdg.c',
+    'xdg_toplevel_drag.c',
     'e_mod_main.h'
    )
 
@@ -15,6 +16,7 @@ else
     '@0@/stable/xdg-shell/xdg-shell.xml'.format(dir_wayland_protocols),
     '@0@/unstable/input-method/input-method-unstable-v1.xml'.format(dir_wayland_protocols),
     '@0@/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml'.format(dir_wayland_protocols),
+    '@0@/staging/xdg-toplevel-drag/xdg-toplevel-drag-v1.xml'.format(dir_wayland_protocols),
   ]
     src += gen_scanner_server.process(p)
     src += gen_scanner_impl.process(p)
diff --git a/src/modules/wl_desktop_shell/xdg_toplevel_drag.c b/src/modules/wl_desktop_shell/xdg_toplevel_drag.c
new file mode 100644
index 000000000..68da6f775
--- /dev/null
+++ b/src/modules/wl_desktop_shell/xdg_toplevel_drag.c
@@ -0,0 +1,145 @@
+/* xdg_toplevel_drag_v1 carries a window with a drag, as a browser does
+ * when a tab is torn off. It is here because attach takes an
+ * xdg_toplevel, which this module defines; the drag and the window it
+ * carries are in e_comp_wl_data.c. */
+#define E_COMP_WL
+#include "e.h"
+#include "e_mod_main.h"
+#include "xdg-toplevel-drag-v1-server-protocol.h"
+
+typedef struct Toplevel_Drag
+{
+   E_Comp_Wl_Data_Source *source;
+   struct wl_listener source_destroy;
+} Toplevel_Drag;
+
+static void
+_e_xdg_toplevel_drag_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   Toplevel_Drag *td = wl_resource_get_user_data(resource);
+
+   if ((td->source) && (e_comp_wl->drag_source == td->source))
+     {
+        wl_resource_post_error(resource, XDG_TOPLEVEL_DRAG_V1_ERROR_ONGOING_DRAG,
+                               "the drag is still running");
+        return;
+     }
+   wl_resource_destroy(resource);
+}
+
+static void
+_e_xdg_toplevel_drag_cb_attach(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *toplevel, int32_t x_offset, int32_t y_offset)
+{
+   Toplevel_Drag *td = wl_resource_get_user_data(resource);
+   E_Client *ec = wl_resource_get_user_data(toplevel), *attached;
+
+   if ((!td->source) || (!ec) || (e_object_is_del(E_OBJECT(ec)))) return;
+   attached = td->source->toplevel;
+   if ((attached) && (attached != ec) && (!e_object_is_del(E_OBJECT(attached))) &&
+       (attached->comp_data->mapped))
+     {
+        wl_resource_post_error(resource, XDG_TOPLEVEL_DRAG_V1_ERROR_TOPLEVEL_ATTACHED,
+                               "a mapped toplevel is already attached");
+        return;
+     }
+   e_comp_wl_data_source_toplevel_set(td->source, ec, x_offset, y_offset);
+}
+
+static const struct xdg_toplevel_drag_v1_interface _e_xdg_toplevel_drag_interface =
+{
+   _e_xdg_toplevel_drag_cb_destroy,
+   _e_xdg_toplevel_drag_cb_attach,
+};
+
+static void
+_e_xdg_toplevel_drag_cb_source_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
+{
+   Toplevel_Drag *td = container_of(listener, Toplevel_Drag, source_destroy);
+
+   wl_list_remove(&td->source_destroy.link);
+   td->source = NULL;
+}
+
+static void
+_e_xdg_toplevel_drag_cb_resource_destroy(struct wl_resource *resource)
+{
+   Toplevel_Drag *td = wl_resource_get_user_data(resource);
+
+   if (td->source)
+     {
+        wl_list_remove(&td->source_destroy.link);
+        e_comp_wl_data_source_toplevel_set(td->source, NULL, 0, 0);
+     }
+   free(td);
+}
+
+static void
+_e_xdg_toplevel_drag_manager_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   wl_resource_destroy(resource);
+}
+
+/* The source must not have been used yet: the protocol wants this before
+ * start_drag, and the source can never become a selection. */
+static void
+_e_xdg_toplevel_drag_manager_cb_get(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *source_resource)
+{
+   E_Comp_Wl_Data_Source *source = wl_resource_get_user_data(source_resource);
+   struct wl_resource *res;
+   Toplevel_Drag *td;
+
+   if ((e_comp_wl->drag_source == source) || (e_comp_wl->selection.data_source == source))
+     {
+        wl_resource_post_error(resource, XDG_TOPLEVEL_DRAG_MANAGER_V1_ERROR_INVALID_SOURCE,
+                               "the source is already in use");
+        return;
+     }
+
+   td = E_NEW(Toplevel_Drag, 1);
+   if (!td)
+     {
+        wl_resource_post_no_memory(resource);
+        return;
+     }
+   res = wl_resource_create(client, &xdg_toplevel_drag_v1_interface,
+                            wl_resource_get_version(resource), id);
+   if (!res)
+     {
+        free(td);
+        wl_resource_post_no_memory(resource);
+        return;
+     }
+   td->source = source;
+   td->source_destroy.notify = _e_xdg_toplevel_drag_cb_source_destroy;
+   wl_signal_add(&source->destroy_signal, &td->source_destroy);
+   wl_resource_set_implementation(res, &_e_xdg_toplevel_drag_interface, td,
+                                  _e_xdg_toplevel_drag_cb_resource_destroy);
+   source->dnd_only = EINA_TRUE;
+}
+
+static const struct xdg_toplevel_drag_manager_v1_interface _e_xdg_toplevel_drag_manager_interface =
+{
+   _e_xdg_toplevel_drag_manager_cb_destroy,
+   _e_xdg_toplevel_drag_manager_cb_get,
+};
+
+static void
+_e_xdg_toplevel_drag_manager_cb_bind(struct wl_client *client, void *data EINA_UNUSED, uint32_t version, uint32_t id)
+{
+   struct wl_resource *res;
+
+   res = wl_resource_create(client, &xdg_toplevel_drag_manager_v1_interface, version, id);
+   if (!res)
+     {
+        wl_client_post_no_memory(client);
+        return;
+     }
+   wl_resource_set_implementation(res, &_e_xdg_toplevel_drag_manager_interface, NULL, NULL);
+}
+
+EINTERN Eina_Bool
+e_xdg_toplevel_drag_init(void)
+{
+   return !!wl_global_create(e_comp_wl->wl.disp, &xdg_toplevel_drag_manager_v1_interface,
+                             1, NULL, _e_xdg_toplevel_drag_manager_cb_bind);
+}
diff --git a/src/tests/wayland/globals.expected b/src/tests/wayland/globals.expected
index f349ecb19..549db773d 100644
--- a/src/tests/wayland/globals.expected
+++ b/src/tests/wayland/globals.expected
@@ -9,6 +9,7 @@ wl_shm	>=1
 wl_subcompositor	1
 wp_presentation	1
 xdg_activation_v1	1
+xdg_toplevel_drag_manager_v1	1
 xdg_wm_base	6
 zwp_e_session_recovery	1
 zwp_idle_inhibit_manager_v1	1
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index e0e204502..7f02d9cce 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -32,6 +32,7 @@ foreach p: [
   '@0@/unstable/xdg-output/xdg-output-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/staging/xdg-activation/xdg-activation-v1.xml'.format(dir_wayland_protocols),
   '@0@/stable/presentation-time/presentation-time.xml'.format(dir_wayland_protocols),
+  '@0@/staging/xdg-toplevel-drag/xdg-toplevel-drag-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'.format(dir_wayland_protocols),
   '@0@/unstable/primary-selection/primary-selection-unstable-v1.xml'.format(dir_wayland_protocols),
@@ -62,6 +63,7 @@ wl_protocol_tests = [
   ['xdg-foreign-v2', 'test_xdg_foreign_v2.c'],
   ['presentation-time', 'test_presentation_time.c'],
   ['shortcuts-inhibit', 'test_shortcuts_inhibit.c'],
+  ['toplevel-drag', 'test_toplevel_drag.c'],
   ['seat-capabilities', 'test_seat_capabilities.c'],
   ['dialog-focus', 'test_dialog_focus.c'],
   ['buffer-release', 'test_buffer_release.c'],
diff --git a/src/tests/wayland/test_toplevel_drag.c b/src/tests/wayland/test_toplevel_drag.c
new file mode 100644
index 000000000..08f71bb53
--- /dev/null
+++ b/src/tests/wayland/test_toplevel_drag.c
@@ -0,0 +1,415 @@
+/* xdg_toplevel_drag_v1: a window attached to a drag follows the pointer at
+ * the offset the client gave, relative to its window geometry. It is not a
+ * drop target while it is carried, it stays where the drop leaves it, and
+ * it is detached when it is unmapped. */
+#include <stdio.h>
+#include <string.h>
+#include <linux/input-event-codes.h>
+
+#include "e_wl_testkit.h"
+#include "xdg-toplevel-drag-v1-client-protocol.h"
+
+#define PROG "test-toplevel-drag"
+#define SHADOW 10
+
+typedef struct
+{
+   Tk *tk;
+   struct xdg_toplevel_drag_manager_v1 *manager;
+   struct wl_data_device_manager *ddm;
+   struct wl_data_device *device;
+   struct wl_seat *seat;
+   struct wl_surface *drag_target, *pointer_focus;
+} Session;
+
+static void
+_dd_data_offer(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)),
+               struct wl_data_offer *o __attribute__((unused)))
+{
+}
+
+static void
+_dd_enter(void *data, struct wl_data_device *dd __attribute__((unused)),
+          uint32_t serial __attribute__((unused)), struct wl_surface *surface,
+          wl_fixed_t x __attribute__((unused)), wl_fixed_t y __attribute__((unused)),
+          struct wl_data_offer *o __attribute__((unused)))
+{
+   Session *s = data;
+
+   s->drag_target = surface;
+}
+
+static void
+_dd_leave(void *data, struct wl_data_device *dd __attribute__((unused)))
+{
+   Session *s = data;
+
+   s->drag_target = NULL;
+}
+
+static void
+_dd_motion(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)),
+           uint32_t t __attribute__((unused)), wl_fixed_t x __attribute__((unused)),
+           wl_fixed_t y __attribute__((unused)))
+{
+}
+
+static void
+_dd_drop(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)))
+{
+}
+
+static void
+_dd_selection(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)),
+              struct wl_data_offer *o __attribute__((unused)))
+{
+}
+
+static const struct wl_data_device_listener _device_listener =
+{
+   _dd_data_offer, _dd_enter, _dd_leave, _dd_motion, _dd_drop, _dd_selection
+};
+
+static void
+_ptr_enter(void *data, struct wl_pointer *p __attribute__((unused)),
+           uint32_t serial __attribute__((unused)), struct wl_surface *surface,
+           wl_fixed_t x __attribute__((unused)), wl_fixed_t y __attribute__((unused)))
+{
+   Session *s = data;
+
+   s->pointer_focus = surface;
+}
+
+static void
+_ptr_leave(void *data, struct wl_pointer *p __attribute__((unused)),
+           uint32_t serial __attribute__((unused)), struct wl_surface *surface __attribute__((unused)))
+{
+   Session *s = data;
+
+   s->pointer_focus = NULL;
+}
+
+static void
+_ptr_motion(void *d __attribute__((unused)), struct wl_pointer *p __attribute__((unused)),
+            uint32_t t __attribute__((unused)), wl_fixed_t x __attribute__((unused)),
+            wl_fixed_t y __attribute__((unused)))
+{
+}
+
+static void
+_ptr_button(void *d __attribute__((unused)), struct wl_pointer *p __attribute__((unused)),
+            uint32_t serial __attribute__((unused)), uint32_t t __attribute__((unused)),
+            uint32_t b __attribute__((unused)), uint32_t st __attribute__((unused)))
+{
+}
+
+static void
+_ptr_axis(void *d __attribute__((unused)), struct wl_pointer *p __attribute__((unused)),
+          uint32_t t __attribute__((unused)), uint32_t a __attribute__((unused)),
+          wl_fixed_t v __attribute__((unused)))
+{
+}
+
+static const struct wl_pointer_listener _pointer_listener =
+{
+   _ptr_enter, _ptr_leave, _ptr_motion, _ptr_button, _ptr_axis
+};
+
+static void
+_session_new(Session *s)
+{
+   memset(s, 0, sizeof(*s));
+   s->tk = tk_connect(PROG);
+   s->manager = tk_bind(s->tk, &xdg_toplevel_drag_manager_v1_interface, 1);
+   s->ddm = tk_bind(s->tk, &wl_data_device_manager_interface, 3);
+   s->seat = tk_bind(s->tk, &wl_seat_interface, 1);
+   if ((!s->manager) || (!s->ddm) || (!s->seat)) tk_fail(s->tk, "missing global");
+   s->device = wl_data_device_manager_get_data_device(s->ddm, s->seat);
+   wl_data_device_add_listener(s->device, &_device_listener, s);
+   wl_pointer_add_listener(wl_seat_get_pointer(s->seat), &_pointer_listener, s);
+}
+
+static struct wl_data_source *
+_source_new(Session *s)
+{
+   struct wl_data_source *src = ""
+
+   wl_data_source_offer(src, "text/plain");
+   wl_data_source_set_actions(src, WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
+   return src;
+}
+
+/* A press in the origin window, which has the keyboard focus, and a drag
+ * started from it. */
+static void
+_drag_start(Session *s, struct wl_data_source *src, Tk_Toplevel *origin, int x, int y)
+{
+   tk_pointer_warp(s->tk, x, y);
+   tk_pointer_button(s->tk, BTN_LEFT, 1);
+   tk_sync(s->tk);
+   wl_data_device_start_drag(s->device, src, tk_toplevel_surface(origin), NULL, 0);
+   tk_sync(s->tk);
+}
+
+/* Where the window geometry is: for a window with a shadow, E's geometry
+ * is the window geometry, inside the surface. */
+static void
+_expect_at(Session *s, Tk_Toplevel *top, int x, int y, const char *what)
+{
+   int wx, wy;
+
+   tk_surface_geometry(s->tk, tk_toplevel_surface(top), &wx, &wy, NULL, NULL);
+   if ((wx != x) || (wy != y))
+     tk_fail(s->tk, "%s: the window is at %d,%d, not %d,%d", what, wx, wy, x, y);
+}
+
+static void
+_expect_error(Session *s, const struct wl_interface *want, int code, const char *what)
+{
+   const struct wl_interface *iface = NULL;
+   int got;
+
+   got = tk_protocol_error(s->tk, &iface);
+   if ((got != code) || (iface != want))
+     tk_fail(s->tk, "%s is not the protocol's error: %d on %s", what, got,
+             iface ? iface->name : "nothing");
+   tk_disconnect(s->tk);
+}
+
+/* Carried at an offset, over a window of the same client that must be the
+ * drop target although the carried window is under the pointer; left where
+ * the drop puts it, and back to taking pointer events. */
+static void
+_test_carry(void)
+{
+   struct xdg_toplevel_drag_v1 *drag;
+   struct wl_data_source *src;
+   Tk_Toplevel *dragged, *origin;
+   Session s;
+   int ox, oy;
+
+   _session_new(&s);
+   dragged = tk_toplevel_new(s.tk, "e.test.drag.dragged", "dragged", 100, 80);
+   origin = tk_toplevel_new(s.tk, "e.test.drag.origin", "origin", 400, 300);
+   tk_surface_geometry(s.tk, tk_toplevel_surface(origin), &ox, &oy, NULL, NULL);
+
+   src = ""
+   drag = xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   _drag_start(&s, src, origin, ox + 20, oy + 20);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(dragged), 10, 20);
+   tk_settle(s.tk);
+   _expect_at(&s, dragged, ox + 10, oy, "attached");
+
+   tk_pointer_warp(s.tk, ox + 200, oy + 150);
+   tk_settle(s.tk);
+   _expect_at(&s, dragged, ox + 190, oy + 130, "first move");
+   if (s.drag_target != tk_toplevel_surface(origin))
+     tk_fail(s.tk, "the carried window, not the one under it, is the drop target");
+
+   tk_pointer_warp(s.tk, ox + 100, oy + 60);
+   tk_settle(s.tk);
+   _expect_at(&s, dragged, ox + 90, oy + 40, "second move");
+   if (s.drag_target != tk_toplevel_surface(origin))
+     tk_fail(s.tk, "the drop target changed to the carried window");
+
+   tk_pointer_button(s.tk, BTN_LEFT, 0);
+   tk_settle(s.tk);
+   tk_pointer_warp(s.tk, ox + 300, oy + 250);
+   tk_settle(s.tk);
+   _expect_at(&s, dragged, ox + 90, oy + 40, "after the drop");
+   tk_pointer_warp(s.tk, ox + 120, oy + 70);
+   tk_settle(s.tk);
+   if (s.pointer_focus != tk_toplevel_surface(dragged))
+     tk_fail(s.tk, "after the drop the window does not get the pointer");
+
+   xdg_toplevel_drag_v1_destroy(drag);
+   wl_data_source_destroy(src);
+   tk_sync(s.tk);
+   tk_disconnect(s.tk);
+}
+
+/* The offset is relative to the window geometry, not to the surface,
+ * which is SHADOW larger on every side. */
+static void
+_test_window_geometry(void)
+{
+   struct xdg_toplevel_drag_v1 *drag;
+   struct wl_data_source *src;
+   Tk_Toplevel *dragged, *origin;
+   Session s;
+   int ox, oy;
+
+   _session_new(&s);
+   dragged = tk_toplevel_new_shadowed(s.tk, "e.test.drag.shadowed", "shadowed", 100, 80, SHADOW);
+   origin = tk_toplevel_new(s.tk, "e.test.drag.origin", "origin", 400, 300);
+   tk_surface_geometry(s.tk, tk_toplevel_surface(origin), &ox, &oy, NULL, NULL);
+
+   /* Attached before the drag starts, as for a window that already
+    * exists: it goes to the pointer when the drag starts. */
+   src = ""
+   drag = xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(dragged), 5, 5);
+   _drag_start(&s, src, origin, ox + 20, oy + 20);
+   tk_settle(s.tk);
+   _expect_at(&s, dragged, ox + 15, oy + 15, "when the drag starts");
+   tk_pointer_warp(s.tk, ox + 200, oy + 150);
+   tk_settle(s.tk);
+   _expect_at(&s, dragged, ox + 195, oy + 145, "with a shadow");
+
+   tk_pointer_button(s.tk, BTN_LEFT, 0);
+   tk_settle(s.tk);
+   tk_disconnect(s.tk);
+}
+
+/* Unmapped while carried, a window is detached: it stays where it is,
+ * and another one can be attached in its place, before or after the
+ * pointer moves again. */
+static void
+_test_unmap(void)
+{
+   struct xdg_toplevel_drag_v1 *drag;
+   struct wl_data_source *src;
+   Tk_Toplevel *first, *second, *third, *origin;
+   Session s;
+   int ox, oy;
+
+   _session_new(&s);
+   first = tk_toplevel_new(s.tk, "e.test.drag.first", "first", 100, 80);
+   second = tk_toplevel_new(s.tk, "e.test.drag.second", "second", 100, 80);
+   third = tk_toplevel_new(s.tk, "e.test.drag.third", "third", 100, 80);
+   origin = tk_toplevel_new(s.tk, "e.test.drag.origin", "origin", 400, 300);
+   tk_surface_geometry(s.tk, tk_toplevel_surface(origin), &ox, &oy, NULL, NULL);
+
+   src = ""
+   drag = xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   _drag_start(&s, src, origin, ox + 20, oy + 20);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(first), 0, 0);
+   tk_pointer_warp(s.tk, ox + 200, oy + 150);
+   tk_settle(s.tk);
+   _expect_at(&s, first, ox + 200, oy + 150, "the first window");
+
+   wl_surface_attach(tk_toplevel_surface(first), NULL, 0, 0);
+   wl_surface_commit(tk_toplevel_surface(first));
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(second), 0, 0);
+   tk_pointer_warp(s.tk, ox + 100, oy + 60);
+   tk_settle(s.tk);
+   _expect_at(&s, second, ox + 100, oy + 60, "attached right after an unmap");
+   _expect_at(&s, first, ox + 200, oy + 150, "the first window, unmapped");
+
+   wl_surface_attach(tk_toplevel_surface(second), NULL, 0, 0);
+   wl_surface_commit(tk_toplevel_surface(second));
+   tk_settle(s.tk);
+   tk_pointer_warp(s.tk, ox + 150, oy + 100);
+   tk_settle(s.tk);
+   _expect_at(&s, second, ox + 100, oy + 60, "the second window, unmapped");
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(third), 0, 0);
+   tk_pointer_warp(s.tk, ox + 120, oy + 80);
+   tk_settle(s.tk);
+   _expect_at(&s, third, ox + 120, oy + 80, "attached after a move");
+
+   tk_pointer_button(s.tk, BTN_LEFT, 0);
+   tk_settle(s.tk);
+   tk_disconnect(s.tk);
+}
+
+/* The window or the source going away mid-drag leaves nothing dangling. */
+static void
+_test_gone_mid_drag(void)
+{
+   struct xdg_toplevel_drag_v1 *drag;
+   struct wl_data_source *src;
+   Tk_Toplevel *dragged, *origin;
+   Session s;
+   int ox, oy;
+
+   _session_new(&s);
+   dragged = tk_toplevel_new(s.tk, "e.test.drag.dragged", "dragged", 100, 80);
+   origin = tk_toplevel_new(s.tk, "e.test.drag.origin", "origin", 400, 300);
+   tk_surface_geometry(s.tk, tk_toplevel_surface(origin), &ox, &oy, NULL, NULL);
+
+   src = ""
+   drag = xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   _drag_start(&s, src, origin, ox + 20, oy + 20);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(dragged), 0, 0);
+   tk_settle(s.tk);
+   tk_toplevel_del(dragged);
+   tk_pointer_warp(s.tk, ox + 200, oy + 150);
+   tk_settle(s.tk);
+   wl_data_source_destroy(src);
+   tk_pointer_warp(s.tk, ox + 100, oy + 60);
+   tk_settle(s.tk);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(origin), 0, 0);
+   xdg_toplevel_drag_v1_destroy(drag);
+   tk_pointer_button(s.tk, BTN_LEFT, 0);
+   tk_settle(s.tk);
+   tk_expect(s.tk, "e.test.drag.origin");
+   tk_disconnect(s.tk);
+}
+
+static void
+_drag_start_in(Session *s, struct wl_data_source *src, Tk_Toplevel *origin)
+{
+   int ox, oy;
+
+   tk_surface_geometry(s->tk, tk_toplevel_surface(origin), &ox, &oy, NULL, NULL);
+   _drag_start(s, src, origin, ox + 20, oy + 20);
+}
+
+static void
+_test_errors(void)
+{
+   struct xdg_toplevel_drag_v1 *drag;
+   struct wl_data_source *src;
+   Tk_Toplevel *origin, *a, *b;
+   Session s;
+
+   _session_new(&s);
+   origin = tk_toplevel_new(s.tk, "e.test.drag.origin", "origin", 400, 300);
+   src = ""
+   drag = xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   _drag_start_in(&s, src, origin);
+   /* The proxy is gone once destroy is sent, so the error names no
+    * interface. */
+   xdg_toplevel_drag_v1_destroy(drag);
+   _expect_error(&s, NULL, XDG_TOPLEVEL_DRAG_V1_ERROR_ONGOING_DRAG,
+                 "destroying during the drag");
+
+   _session_new(&s);
+   src = ""
+   wl_data_source_offer(src, "text/plain");
+   xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   wl_data_device_set_selection(s.device, src, 0);
+   _expect_error(&s, &wl_data_source_interface,
+                 WL_DATA_SOURCE_ERROR_INVALID_SOURCE, "a toplevel drag source as selection");
+
+   _session_new(&s);
+   origin = tk_toplevel_new(s.tk, "e.test.drag.origin", "origin", 400, 300);
+   src = ""
+   _drag_start_in(&s, src, origin);
+   xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, src);
+   _expect_error(&s, &xdg_toplevel_drag_manager_v1_interface,
+                 XDG_TOPLEVEL_DRAG_MANAGER_V1_ERROR_INVALID_SOURCE, "a toplevel drag after start_drag");
+
+   _session_new(&s);
+   a = tk_toplevel_new(s.tk, "e.test.drag.a", "a", 100, 80);
+   b = tk_toplevel_new(s.tk, "e.test.drag.b", "b", 100, 80);
+   drag = xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(s.manager, _source_new(&s));
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(a), 0, 0);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(a), 5, 5);
+   tk_sync(s.tk);
+   xdg_toplevel_drag_v1_attach(drag, tk_toplevel_xdg(b), 0, 0);
+   _expect_error(&s, &xdg_toplevel_drag_v1_interface,
+                 XDG_TOPLEVEL_DRAG_V1_ERROR_TOPLEVEL_ATTACHED, "attaching a second mapped toplevel");
+}
+
+int
+main(void)
+{
+   _test_carry();
+   _test_window_geometry();
+   _test_unmap();
+   _test_gone_mid_drag();
+   _test_errors();
+   printf(PROG ": ok\n");
+   return 0;
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to