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

git pushed a commit to branch wl-clipboard-seam
in repository enlightenment.

View the commit online.

commit e7417cbc1eaa81e8dd146d6538b614b8008fa3b4
Author: Cedric BAIL <[email protected]>
AuthorDate: Tue Dec 1 21:25:43 2026 -0600

    xwayland - take and let go of the clipboard like a client does
    
    When an X11 client took the clipboard, xwayland replaced
    selection.data_source by hand. The Wayland owner was cancelled, but its
    destruction listener stayed linked, nothing told selection.signal, and
    E's saved copy of that owner kept the reference the clipboard manager
    holds on it for ever. Two things followed. When the X11 owner let go,
    E went on offering its types to Wayland clients, and a paste of them
    never got an answer. And an X11 client that set the clipboard twice
    before E had read the types of the first (copying twice quickly) left
    E with an X11 owner it did not know it had: the next copy by a Wayland
    client, or a module, crashed E, the owner letting go was ignored, and
    focusing an X11 window stole the clipboard from its owner.
    
    Add e_comp_wl_data_selection_release() and
    e_comp_wl_data_selection_adopt() to E_API, for xwayland. It is the only
    owner of the clipboard that is not a Wayland client, and it has to do
    what set_selection does (cancel the owner, unlink its listener, drop
    E's copy, emit selection.signal), which nothing exports. They are the
    only new API.
    
    The clipboard role, e_comp_wl->clipboard.source, now holds exactly one
    reference on its source, taken where the role is assigned and dropped
    where it is released, never by the source's own cancelled callback.
    adopt links the destruction listener as set_selection does, so a
    selection always has one linked and set_selection no longer guesses it
    from xwl_owner, and it sets xwl_owner from the owner xwayland names.
    The save of E's copy reads from its own source, not from whatever the
    clipboard is by then: a paste in flight keeps the copy alive after the
    clipboard moved on, and when its owner ended its data the reader read
    the X11 source, failed, and dropped the X11 clipboard. xwayland drops
    the answer to its question about the types when the owner has let go,
    or a Wayland client or E took the clipboard, since it asked.
    
    Two more things an X11 copy now does as a client's would. release leaves
    the serial that clients copy against as it was, so a Wayland client
    copying with the serial of a key from before the X11 copy is still
    obeyed. And adopt offers the new clipboard to the Wayland client that
    has the keyboard focus: until now it was told there was none, by
    release, and kept that until the focus moved. xwayland also lets go of
    the clipboard when its module is unloaded, as the source of an X11 owner
    points into it.
    
    What does not change: copy in an X11 application, close it, paste
    elsewhere did not work before and still does not. E never kept a copy
    of an X11 owner's contents, only its types, and once the owner is gone
    nothing answers a paste. Saving the contents of an X11 owner as E does
    for a Wayland one is possible later work.
    
    Tests, all of which fail before this change except double-copy, which
    only guards that a Wayland copy after an X11 client set the clipboard
    twice works: an X11 client taking the clipboard over a Wayland copy, the
    Wayland client destroying the source it was cancelled with, and the X11
    client letting go (keep-owner). The same after a double set (double-
    leave: the clipboard stayed). A Wayland copy before E has the types of
    the X11 owner (copy-before-answer: the X11 owner took it back). An X11
    client taking over while E saves a Wayland copy (take-while-saving), and
    while a paste of that copy is under way (paste-while-saving). A copy
    with an older serial (old-serial), and an X11 copy while a Wayland
    client has the focus (copy-while-focused).
    
    Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 src/bin/e_comp_wl_data.c          |  64 ++++--
 src/bin/e_comp_wl_data.h          |  19 ++
 src/modules/xwayland/dnd.c        |  30 ++-
 src/tests/wayland/meson.build     |   5 +-
 src/tests/wayland/test_xwayland.c | 426 +++++++++++++++++++++++++++++++++++++-
 5 files changed, 512 insertions(+), 32 deletions(-)

diff --git a/src/bin/e_comp_wl_data.c b/src/bin/e_comp_wl_data.c
index 29b725740..4afb6fab7 100644
--- a/src/bin/e_comp_wl_data.c
+++ b/src/bin/e_comp_wl_data.c
@@ -567,8 +567,7 @@ _e_comp_wl_data_device_selection_set(void *data EINA_UNUSED, E_Comp_Wl_Data_Sour
 
    if (sel_source)
      {
-        if (!e_comp_wl->clipboard.xwl_owner)
-          wl_list_remove(&e_comp_wl->selection.data_source_listener.link);
+        wl_list_remove(&e_comp_wl->selection.data_source_listener.link);
         if (sel_source->cancelled)
           sel_source->cancelled(sel_source);
         e_comp_wl->selection.data_source = NULL;
@@ -929,15 +928,12 @@ _e_comp_wl_clipboard_offer_create(E_Comp_Wl_Clipboard_Source *source, int fd)
 }
 
 static Eina_Bool
-_e_comp_wl_clipboard_source_save(void *data EINA_UNUSED, Ecore_Fd_Handler *handler)
+_e_comp_wl_clipboard_source_save(void *data, Ecore_Fd_Handler *handler)
 {
-   E_Comp_Wl_Clipboard_Source *source;
+   E_Comp_Wl_Clipboard_Source *source = data;
    char *p;
    int len, size;
 
-   if (!(source = (E_Comp_Wl_Clipboard_Source *)e_comp_wl->clipboard.source))
-     return ECORE_CALLBACK_CANCEL;
-
    /* extend contents buffer */
    if ((source->contents.alloc - source->contents.size) < CLIPBOARD_CHUNK)
      {
@@ -949,16 +945,18 @@ _e_comp_wl_clipboard_source_save(void *data EINA_UNUSED, Ecore_Fd_Handler *handl
    size = source->contents.alloc - source->contents.size;
    len = read(source->fd, p, size);
 
-   if (len == 0)
+   if (len <= 0)
      {
         ecore_main_fd_handler_del(handler);
         close(source->fd);
         source->fd_handler = NULL;
-     }
-   else if (len < 0)
-     {
-        e_comp_wl_clipboard_source_unref(source);
-        e_comp_wl->clipboard.source = NULL;
+        /* A failed read drops the copy, unless it is no longer the one E
+         * keeps: a paste still under way is all that holds it then. */
+        if ((len < 0) && (source == e_comp_wl->clipboard.source))
+          {
+             e_comp_wl->clipboard.source = NULL;
+             e_comp_wl_clipboard_source_unref(source);
+          }
      }
    else
      {
@@ -1644,7 +1642,7 @@ e_comp_wl_clipboard_source_create(const char *mime_type, uint32_t serial, int fd
         source->fd_handler =
           ecore_main_fd_handler_file_add(fd, ECORE_FD_READ | ECORE_FD_ERROR,
                                          _e_comp_wl_clipboard_source_save,
-                                         e_comp->wl_comp_data, NULL, NULL);
+                                         source, NULL, NULL);
         if (!source->fd_handler)
           {
              _mime_types_free(&source->data_source);
@@ -1682,3 +1680,41 @@ e_comp_wl_clipboard_source_unref(E_Comp_Wl_Clipboard_Source *source)
    free(source);
 }
 
+E_API void
+e_comp_wl_data_selection_release(void)
+{
+   E_Comp_Wl_Clipboard_Source *saved = e_comp_wl->clipboard.source;
+   uint32_t serial = e_comp_wl->selection.serial;
+
+   /* Taken off first, so that the signal of the owner going does not offer
+    * the saved copy again. */
+   e_comp_wl->clipboard.source = NULL;
+   if (e_comp_wl->selection.data_source)
+     {
+        _e_comp_wl_data_device_selection_set(e_comp->wl_comp_data, NULL,
+                                             wl_display_next_serial(e_comp_wl->wl.disp));
+        /* Clients copy with the serial of an input event: an owner that is
+         * not a client does not make theirs older. */
+        e_comp_wl->selection.serial = serial;
+     }
+   if (saved) e_comp_wl_clipboard_source_unref(saved);
+}
+
+E_API void
+e_comp_wl_data_selection_adopt(E_Comp_Wl_Clipboard_Source *source, Ecore_Window owner)
+{
+   EINA_SAFETY_ON_NULL_RETURN(source);
+
+   e_comp_wl_data_selection_release();
+   e_comp_wl->clipboard.source = source;
+   e_comp_wl->clipboard.xwl_owner = owner;
+   e_comp_wl->selection.data_source = &source->data_source;
+   e_comp_wl->selection.data_source_listener.notify =
+     _e_comp_wl_data_device_destroy_selection_data_source;
+   wl_signal_add(&source->data_source.destroy_signal,
+                 &e_comp_wl->selection.data_source_listener);
+   wl_signal_emit(&e_comp_wl->selection.signal, e_comp->wl_comp_data);
+   /* release told the client that has the keyboard focus there is nothing. */
+   if (e_comp_wl->kbd.enabled && e_comp_wl->kbd.focus)
+     e_comp_wl_data_device_keyboard_focus_set();
+}
diff --git a/src/bin/e_comp_wl_data.h b/src/bin/e_comp_wl_data.h
index 0c9b6e789..6e56b4244 100644
--- a/src/bin/e_comp_wl_data.h
+++ b/src/bin/e_comp_wl_data.h
@@ -105,6 +105,25 @@ E_API struct wl_resource *e_comp_wl_data_find_for_client(struct wl_client *clien
 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);
+/* For an owner of the clipboard that is not a Wayland client, as xwayland's
+ * X11 clients are. The clipboard role (e_comp_wl->clipboard.source) holds
+ * one reference on its source, and E drops it when the source stops being
+ * the clipboard.
+ *
+ * release empties the clipboard: the owner is cancelled, E's saved copy of
+ * a Wayland owner is dropped, selection.signal is emitted and the client
+ * that has the keyboard focus is told there is no selection. The serial
+ * clients must beat to copy is left as it was. adopt releases whatever is
+ * there, then makes source the selection and the clipboard role, emits the
+ * signal, and offers it to the client that has the keyboard focus. source
+ * comes from e_comp_wl_clipboard_source_create(), with the send and
+ * cancelled of its owner, and is not the source that already is the
+ * clipboard: adopt takes the caller's reference, and cancelled is told the
+ * source is no longer the selection, which does not end it. owner is the
+ * X11 window that owns the clipboard, and stays
+ * e_comp_wl->clipboard.xwl_owner for as long as source is the selection. */
+E_API void e_comp_wl_data_selection_release(void);
+E_API void e_comp_wl_data_selection_adopt(E_Comp_Wl_Clipboard_Source *source, Ecore_Window owner);
 E_API E_Comp_Wl_Clipboard_Source *e_comp_wl_clipboard_source_create(const char *mime_type, uint32_t serial, int fd);
 # endif
 #endif
diff --git a/src/modules/xwayland/dnd.c b/src/modules/xwayland/dnd.c
index 0469e7fbb..ed1333a12 100644
--- a/src/modules/xwayland/dnd.c
+++ b/src/modules/xwayland/dnd.c
@@ -146,9 +146,8 @@ _xwayland_send_send(E_Comp_Wl_Data_Source *source EINA_UNUSED, const char* mime_
 }
 
 static void
-_xwayland_send_cancelled(E_Comp_Wl_Data_Source *source)
+_xwayland_send_cancelled(E_Comp_Wl_Data_Source *source EINA_UNUSED)
 {
-   e_comp_wl_clipboard_source_unref((void*)source);
 }
 
 static void
@@ -252,20 +251,16 @@ _xwl_fixes_selection_notify(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_X_Even
      {
         if (ev->owner)
           {
-             if (e_comp_wl->selection.data_source)
-               {
-                  E_Comp_Wl_Data_Source *psource = e_comp_wl->selection.data_source;
-                  if (psource->cancelled)
-                    psource->cancelled(psource);
-               }
-             e_comp_wl->clipboard.source = NULL;
-             e_comp_wl->selection.data_source = NULL;
+             e_comp_wl_data_selection_release();
              e_comp_wl->clipboard.xwl_owner = ev->owner;
              xconvertselection(ecore_x_display_get(), ECORE_X_ATOM_SELECTION_CLIPBOARD,
                ECORE_X_ATOM_SELECTION_TARGETS, xwl_dnd_atom, e_comp->cm_selection, 0);
           }
-        else
-          e_comp_wl->clipboard.xwl_owner = 0;
+        else if (e_comp_wl->clipboard.xwl_owner)
+          {
+             e_comp_wl_data_selection_release();
+             e_comp_wl->clipboard.xwl_owner = 0;
+          }
      }
    return ECORE_CALLBACK_RENEW;
 }
@@ -322,6 +317,10 @@ _xwl_selection_notify(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_X_Event_Sele
              E_Comp_Wl_Data_Source *dsource;
              int i;
 
+             /* The owner let go, or a Wayland client or E took the clipboard
+              * since the question was asked. */
+             if (!e_comp_wl->clipboard.xwl_owner) return ECORE_CALLBACK_RENEW;
+
              source = e_comp_wl_clipboard_source_create(NULL, 0, -1);
              dsource = e_comp_wl_data_manager_source_create(e_comp_wl->xwl_client,
                e_comp_wl->mgr.resource, 0);
@@ -335,13 +334,10 @@ _xwl_selection_notify(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_X_Event_Sele
                     eina_array_push(source->data_source.mime_types, eina_stringshare_add(tgs->targets[i]));
                  }
 
-             e_comp_wl->clipboard.source = source;
-             e_comp_wl->selection.data_source = &source->data_source;
              source->data_source.resource = dsource->resource;
              source->data_source.send = _xwayland_send_send;
              source->data_source.cancelled = _xwayland_send_cancelled;
-             if (e_client_has_xwindow(e_client_focused_get()))
-               e_comp_wl_data_device_keyboard_focus_set();
+             e_comp_wl_data_selection_adopt(source, e_comp_wl->clipboard.xwl_owner);
              return ECORE_CALLBACK_RENEW;
           }
      }
@@ -529,6 +525,8 @@ dnd_shutdown(void)
    Xfer_Data *xd;
 
    E_FREE_LIST(handlers, ecore_event_handler_del);
+   /* The source of an X11 owner calls into this module. */
+   if (e_comp_wl->clipboard.xwl_owner) e_comp_wl_data_selection_release();
    E_FREE_FUNC(pipes, eina_hash_free);
    while (xfer_data_list)
      {
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index 1ba335bba..cd1c7e886 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -188,7 +188,10 @@ if get_option('xwayland') and not config_h.has('HAVE_WAYLAND_ONLY')
     ['test_xwayland.c', tk_src, test_proto_src],
     dependencies: [dependency('wayland-client'), dep_ecore_x, dep_ecore],
   )
-  foreach scenario: ['close', 'restore', 'early-map', 'paste-saved']
+  foreach scenario: ['close', 'restore', 'early-map', 'paste-saved', 'keep-owner',
+                     'double-copy', 'double-leave', 'copy-before-answer',
+                     'take-while-saving', 'paste-while-saving', 'old-serial',
+                     'copy-while-focused']
     test('wl-xwayland-' + scenario,
       run_nested,
       args: [test_xwayland, scenario],
diff --git a/src/tests/wayland/test_xwayland.c b/src/tests/wayland/test_xwayland.c
index 02a4b5782..ffa0e9d98 100644
--- a/src/tests/wayland/test_xwayland.c
+++ b/src/tests/wayland/test_xwayland.c
@@ -17,6 +17,26 @@
  *   paste-saved  A Wayland client copies and destroys its source. E keeps
  *              the copy, which has no wl_data_source of its own, and an X
  *              client pastes it.
+ *   keep-owner   An X client takes the clipboard over a Wayland client's
+ *              copy. The Wayland client then destroys the source it was
+ *              cancelled with, which must not take the clipboard from the
+ *              X client. When the X client lets go of the clipboard, it
+ *              is empty.
+ *   take-while-saving
+ *              An X client takes the clipboard while E is still reading a
+ *              Wayland client's copy.
+ *   paste-while-saving
+ *              The same, with a paste of E's copy under way.
+ *   old-serial A Wayland client copies, after an X client did, with the
+ *              serial of a key from before.
+ *   copy-while-focused
+ *              An X client copies while a Wayland client has the focus.
+ *   double-copy  An X client sets the clipboard twice in a row, and a
+ *              Wayland client copies over it.
+ *   double-leave Likewise, and the X client then lets go of it.
+ *   copy-before-answer
+ *              A Wayland client copies after an X client has taken the
+ *              clipboard and before E has the answer to what it offers.
  *
  * Every scenario ends with the client closing, so each also checks that E
  * still finds the window from its X window id.
@@ -29,6 +49,7 @@
 #include <time.h>
 #include <Ecore.h>
 #include <Ecore_X.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include "e_wl_clipboardkit.h"
@@ -194,8 +215,11 @@ _restore(Tk *tk)
 }
 
 #define APP_WL "E.test.xwayland.wl"
+#define APP_WL2 "E.test.xwayland.wl2"
 #define MIME "text/plain"
 #define COPY_TEXT "copied by a client"
+/* What E offers of an X client's clipboard, from the targets it answers. */
+#define X_TYPE "UTF8_STRING"
 
 static char x_got[256];
 static int x_got_count;
@@ -264,6 +288,390 @@ _paste_saved(Tk *tk)
    tk_disconnect(a.tk);
 }
 
+static Eina_Bool
+_x_targets(char *target EINA_UNUSED, void *data EINA_UNUSED, int size EINA_UNUSED,
+           void **data_ret, int *size_ret, Ecore_X_Atom *type, int *tsize)
+{
+   Ecore_X_Atom *atoms = malloc(2 * sizeof(Ecore_X_Atom));
+
+   atoms[0] = ECORE_X_ATOM_UTF8_STRING;
+   atoms[1] = ecore_x_atom_get("TARGETS");
+   *data_ret = atoms;
+   *size_ret = 2;
+   *type = ECORE_X_ATOM_ATOM;
+   *tsize = 32;
+   return EINA_TRUE;
+}
+
+/* The X client takes the clipboard. It answers E's questions about it only
+ * when the test lets the main loop run. */
+static Ecore_X_Window
+_x_clipboard_take(void)
+{
+   Ecore_X_Window win = ecore_x_window_new(0, 0, 0, 10, 10);
+
+   ecore_x_selection_converter_add("TARGETS", _x_targets);
+   ecore_x_selection_clipboard_set(win, "from X", 7);
+   return win;
+}
+
+/* Which Wayland client has the focus decides who hears of the selection:
+ * the one gaining it is offered what E holds. Hand the focus from a to b,
+ * and back, until b is offered a clipboard that has type among its types,
+ * or, with no type, no clipboard. b has the focus on return. */
+static void
+_offer_wait(Cb_Peer *a, Cb_Peer *b, const char *type, const char *what)
+{
+   double deadline = _now() + TK_WAIT_SECONDS;
+
+   for (;;)
+     {
+        cb_focus(a, b);
+        if (type ? (b->offer && strstr(b->offer->types, type)) : !b->offer) return;
+        if (_now() > deadline) tk_fail(b->tk, "%s", what);
+        cb_focus(b, a);
+        ecore_main_loop_iterate();
+     }
+}
+
+static void
+_keep_owner(Tk *tk)
+{
+   static const char *const types[] = { MIME };
+   Cb_Peer a, b;
+   Cb_Source src;
+   Ecore_X_Window win;
+   int i;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&b, &a);
+   cb_copy(&a, &src, types, 1, COPY_TEXT, strlen(COPY_TEXT), 0);
+
+   win = _x_clipboard_take();
+   ecore_x_sync();
+
+   /* Until E has the X client's types, the focus going to b offers it
+    * nothing. */
+   _offer_wait(&a, &b, X_TYPE, "the X client's clipboard never reached b");
+   cb_focus(&b, &a);
+   if (src.cancels != 1)
+     tk_fail(a.tk, "a's source was cancelled %d times when X took over", src.cancels);
+
+   /* a's source was cancelled when X took over. Destroying it is not a
+    * change of the clipboard. */
+   wl_data_source_destroy(src.source);
+   tk_settle(a.tk);
+   for (i = 0; i < 3; i++) ecore_main_loop_iterate();
+   cb_focus(&a, &b);
+   if (!b.offer)
+     tk_fail(b.tk, "a Wayland client destroying its old source cleared the X client's clipboard");
+   cb_focus(&b, &a);
+
+   /* The X client lets go: nothing is left to offer. */
+   ecore_x_window_free(win);
+   ecore_x_sync();
+   _offer_wait(&a, &b, NULL, "b is still offered the clipboard of an X client that left");
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
+/* An X client that sets the clipboard twice in a row is asked twice what it
+ * offers, and E hears of the second answer with the first's source in place.
+ * The test lets E take in both questions before the X client answers. A
+ * guard: the clipboard is the Wayland client's after it copies, and E is
+ * still there. */
+static Ecore_X_Window
+_x_clipboard_take_twice(Cb_Peer *a)
+{
+   Ecore_X_Window win = _x_clipboard_take();
+   int i;
+
+   ecore_x_selection_clipboard_set(win, "from X again", 13);
+   ecore_x_sync();
+   for (i = 0; i < 3; i++) tk_settle(a->tk);
+   return win;
+}
+
+/* Answer the X client's questions until b is offered X's types, then a
+ * little longer for the second answer. */
+static void
+_x_answers_wait(Cb_Peer *a, Cb_Peer *b)
+{
+   int i;
+
+   _offer_wait(a, b, X_TYPE, "the X client's clipboard never reached b");
+   cb_focus(b, a);
+   for (i = 0; i < 20; i++)
+     {
+        ecore_main_loop_iterate();
+        tk_settle(a->tk);
+     }
+}
+
+static void
+_double_copy(Tk *tk, Eina_Bool leave)
+{
+   static const char *const types[] = { "text/x-copied" };
+   Cb_Peer a, b;
+   Cb_Source src;
+   Ecore_X_Window win;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&b, &a);
+   win = _x_clipboard_take_twice(&a);
+   _x_answers_wait(&a, &b);
+
+   if (leave)
+     {
+        /* The X client lets go: nothing is left to offer. */
+        ecore_x_window_free(win);
+        ecore_x_sync();
+        _offer_wait(&a, &b, NULL, "b is still offered the clipboard of an X client that left");
+     }
+   else
+     {
+        /* A Wayland client copies over it. */
+        cb_copy(&a, &src, types, 1, COPY_TEXT, strlen(COPY_TEXT), 0);
+        cb_focus(&a, &b);
+        if ((!b.offer) || strcmp(b.offer->types, types[0]))
+          tk_fail(b.tk, "b is offered '%s', not what a copied", b.offer ? b.offer->types : "nothing");
+     }
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
+/* A Wayland client copies while the X client's answer is still on its way:
+ * the copy is the newer clipboard, and the answer, which is about a
+ * clipboard that is not E's any more, must not take it back. */
+static void
+_copy_before_answer(Tk *tk)
+{
+   static const char *const types[] = { "text/x-copied" };
+   Cb_Peer a, b;
+   Cb_Source first, second;
+   Ecore_X_Window win;
+   int i;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&b, &a);
+   cb_copy(&a, &first, types, 1, COPY_TEXT, strlen(COPY_TEXT), 0);
+
+   win = _x_clipboard_take();
+   ecore_x_sync();
+   cb_wait_int(&a, &first.cancels, 1, "E never took the clipboard for the X client");
+   cb_copy(&a, &second, types, 1, COPY_TEXT, strlen(COPY_TEXT), 0);
+
+   /* Now the X client answers. */
+   for (i = 0; i < 20; i++)
+     {
+        ecore_main_loop_iterate();
+        tk_settle(a.tk);
+     }
+   cb_focus(&a, &b);
+   if ((!b.offer) || strcmp(b.offer->types, types[0]))
+     tk_fail(b.tk, "b is offered '%s', not what a copied", b.offer ? b.offer->types : "nothing");
+   ecore_x_window_free(win);
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
+/* An X client takes the clipboard while E is still reading what a Wayland
+ * client copied: E drops its copy, and the copy's reader goes with it.
+ * Before, the reader went on and, when the Wayland client ended its data,
+ * read the X client's source, failed, and dropped the X clipboard. */
+static void
+_take_while_saving(Tk *tk)
+{
+   static const char *const types[] = { MIME };
+   Cb_Peer a, b;
+   Cb_Source src;
+   Ecore_X_Window win;
+   int i;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&b, &a);
+   cb_copy(&a, &src, types, 1, NULL, 0, 1);
+   cb_wait_int(&a, &src.sends, 1, "E never asked for the data to keep");
+
+   win = _x_clipboard_take();
+   ecore_x_sync();
+   _offer_wait(&a, &b, X_TYPE, "the X client's clipboard never reached b");
+   cb_focus(&b, &a);
+
+   close(src.held_fds[0]);
+   tk_settle(a.tk);
+   for (i = 0; i < 3; i++) ecore_main_loop_iterate();
+   cb_focus(&a, &b);
+   if (!b.offer)
+     tk_fail(b.tk, "the end of a Wayland client's data cleared the X client's clipboard");
+   ecore_x_window_free(win);
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
+/* The reader of E's copy of a Wayland client's clipboard is held by a
+ * paste in flight when an X client takes the clipboard. The paste still
+ * gets the whole copy, and the Wayland client ending its data does not
+ * touch the X client's clipboard (the reader read it instead). */
+static void
+_paste_while_saving(Tk *tk)
+{
+   static const char *const types[] = { MIME };
+   Cb_Peer a, b;
+   Cb_Source src;
+   Ecore_X_Window win;
+   char *chunk = cb_pattern(4096), *got;
+   size_t written = 0, length, i;
+   int fd, j;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&b, &a);
+   cb_copy(&a, &src, types, 1, NULL, 0, 1);
+   cb_wait_int(&a, &src.sends, 1, "E never asked for the data to keep");
+   fcntl(src.held_fds[0], F_SETFL, O_NONBLOCK);
+   while (written < 300000)
+     {
+        ssize_t n = write(src.held_fds[0], chunk, 4096);
+
+        if (n > 0) written += n;
+        else tk_sync(a.tk);
+     }
+   for (j = 0; j < 5; j++) tk_sync(a.tk);
+
+   /* The owner goes and E offers its copy, which it is still saving. */
+   wl_data_source_destroy(src.source);
+   tk_settle(a.tk);
+   cb_focus(&a, &b);
+   if ((!b.offer) || strcmp(b.offer->types, MIME))
+     tk_fail(b.tk, "b is offered '%s', not E's copy", b.offer ? b.offer->types : "nothing");
+   fd = cb_receive_start(&b, b.offer->offer, MIME);
+   tk_sync(b.tk);
+   cb_focus(&b, &a);
+
+   win = _x_clipboard_take();
+   ecore_x_sync();
+   _offer_wait(&a, &b, X_TYPE, "the X client's clipboard never reached b");
+
+   /* The owner ends its data. */
+   for (j = 0; j < 10; j++)
+     {
+        ssize_t n = write(src.held_fds[0], chunk, 4096);
+
+        if (n > 0) written += n;
+        tk_sync(a.tk);
+     }
+   close(src.held_fds[0]);
+   for (j = 0; j < 5; j++)
+     {
+        tk_sync(a.tk);
+        ecore_main_loop_iterate();
+     }
+   got = cb_receive_finish(&b, fd, &length);
+   if (length != written)
+     tk_fail(b.tk, "the paste got %zu bytes of %zu", length, written);
+   for (i = 0; i + 4096 <= length; i += 4096)
+     if (memcmp(got + i, chunk, 4096)) tk_fail(b.tk, "the paste is damaged at %zu", i);
+   free(got);
+
+   cb_focus(&b, &a);
+   _offer_wait(&a, &b, X_TYPE, "the X client's clipboard was dropped");
+   free(chunk);
+   ecore_x_window_free(win);
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
+/* A Wayland client copies with the serial of a key it got before the X
+ * client copied: as ever, the serial only has to be newer than the one of
+ * the last Wayland copy. */
+static void
+_old_serial(Tk *tk)
+{
+   static const char *const types[] = { "text/x-late" };
+   Cb_Peer a, b;
+   Cb_Source first;
+   struct wl_data_source *late;
+   Ecore_X_Window win;
+   uint32_t serial;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&b, &a);
+   cb_copy(&a, &first, types, 1, COPY_TEXT, strlen(COPY_TEXT), 0);
+   serial = cb_serial_fresh(&a);
+
+   win = _x_clipboard_take();
+   ecore_x_sync();
+   _offer_wait(&a, &b, X_TYPE, "the X client's clipboard never reached b");
+   cb_focus(&b, &a);
+
+   late = wl_data_device_manager_create_data_source(a.manager);
+   wl_data_source_offer(late, types[0]);
+   wl_data_device_set_selection(a.device, late, serial);
+   tk_sync(a.tk);
+   cb_focus(&a, &b);
+   if ((!b.offer) || strcmp(b.offer->types, types[0]))
+     tk_fail(b.tk, "b is offered '%s': the copy was refused", b.offer ? b.offer->types : "nothing");
+   ecore_x_window_free(win);
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
+/* An X client copies while a Wayland client has the focus: the client is
+ * offered it there and then, without the focus changing, and can paste it. */
+static void
+_copy_while_focused(Tk *tk)
+{
+   Cb_Peer a, b;
+   Ecore_X_Window win;
+   char *got;
+   size_t length;
+   double deadline = _now() + TK_WAIT_SECONDS;
+   int fd;
+
+   _x_window_manager_wait(tk);
+   cb_peer_new(&a, PROG, APP_WL);
+   cb_peer_new(&b, PROG, APP_WL2);
+   cb_focus(&a, &b);
+
+   win = _x_clipboard_take();
+   ecore_x_sync();
+   while ((!b.offer) || (!strstr(b.offer->types, X_TYPE)))
+     {
+        if (_now() > deadline) tk_fail(b.tk, "the focused client was never offered the X clipboard");
+        ecore_main_loop_iterate();
+        tk_sync(b.tk);
+     }
+
+   fd = cb_receive_start(&b, b.offer->offer, X_TYPE);
+   for (;;)
+     {
+        struct pollfd pfd = { fd, POLLIN, 0 };
+
+        if (_now() > deadline) tk_fail(b.tk, "the X client's data never came");
+        if (poll(&pfd, 1, 0) > 0) break;
+        ecore_main_loop_iterate();
+        tk_sync(b.tk);
+     }
+   got = cb_receive_finish(&b, fd, &length);
+   if (strcmp(got, "from X")) tk_fail(b.tk, "pasted '%s', not 'from X'", got);
+   free(got);
+   ecore_x_window_free(win);
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+}
+
 static void
 _early_map(Tk *tk)
 {
@@ -312,7 +720,7 @@ main(int argc, char **argv)
 
    if (argc != 2)
      {
-        fprintf(stderr, "usage: %s close|restore|early-map|paste-saved\n", argv[0]);
+        fprintf(stderr, "usage: %s close|restore|early-map|paste-saved|keep-owner|double-copy|double-leave|copy-before-answer|take-while-saving|paste-while-saving|old-serial|copy-while-focused\n", argv[0]);
         return 2;
      }
 
@@ -330,6 +738,22 @@ main(int argc, char **argv)
      _restore(tk);
    else if (!strcmp(argv[1], "paste-saved"))
      _paste_saved(tk);
+   else if (!strcmp(argv[1], "keep-owner"))
+     _keep_owner(tk);
+   else if (!strcmp(argv[1], "take-while-saving"))
+     _take_while_saving(tk);
+   else if (!strcmp(argv[1], "paste-while-saving"))
+     _paste_while_saving(tk);
+   else if (!strcmp(argv[1], "old-serial"))
+     _old_serial(tk);
+   else if (!strcmp(argv[1], "copy-while-focused"))
+     _copy_while_focused(tk);
+   else if (!strcmp(argv[1], "double-copy"))
+     _double_copy(tk, EINA_FALSE);
+   else if (!strcmp(argv[1], "double-leave"))
+     _double_copy(tk, EINA_TRUE);
+   else if (!strcmp(argv[1], "copy-before-answer"))
+     _copy_before_answer(tk);
    else if (!strcmp(argv[1], "early-map"))
      _early_map(tk);
    else

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

Reply via email to