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 f30ce4ba0c28222719e7e46bb885b848476879b1
Author: Cedric BAIL <[email protected]>
AuthorDate: Tue Dec 1 20:52:23 2026 -0600

    e_comp_wl_data - write the saved clipboard without blocking E
    
    E keeps a copy of the clipboard so that it outlives the client that
    made it, and writes it to whichever client pastes with one blocking
    write(). When the copy is larger than a pipe (64 KiB) and the client
    reads slowly, or has asked and not yet started to read, that write
    stops E's main loop: no input, no frames, no answer to any client until
    the paster reads. A client that never reads freezes the session for
    good. Anyone pasting a large selection from an application that has
    since closed is exposed to it.
    
    Make the fd non-blocking and keep the handler when the pipe is full, so
    the write goes on as the reader makes room. The fd is the pasting
    client's, and the O_NONBLOCK is on its write end only.
    
    The test copies 200 KiB, lets the owner go, has a second client ask for
    it without reading and checks that a third connection is still
    answered, then that the data arrives whole. Without the change E stops
    answering.
    
    Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 src/bin/e_comp_wl_data.c                 |   5 +
 src/tests/wayland/e_wl_clipboardkit.h    | 423 +++++++++++++++++++++++++++++++
 src/tests/wayland/meson.build            |   1 +
 src/tests/wayland/test_clipboard_saved.c |  70 +++++
 4 files changed, 499 insertions(+)

diff --git a/src/bin/e_comp_wl_data.c b/src/bin/e_comp_wl_data.c
index 0d58a5b7d..29b725740 100644
--- a/src/bin/e_comp_wl_data.c
+++ b/src/bin/e_comp_wl_data.c
@@ -895,6 +895,9 @@ _e_comp_wl_clipboard_offer_load(void *data, Ecore_Fd_Handler *handler)
    p = (char *)offer->source->contents.data;
    len = write(fd, p + offer->offset, size - offer->offset);
    if (len > 0) offer->offset += len;
+   /* The pipe is full: wait for the reader to make room. */
+   else if ((len < 0) && ((errno == EAGAIN) || (errno == EINTR)))
+     return ECORE_CALLBACK_RENEW;
 
    if ((offer->offset == size) || (len <= 0))
      {
@@ -914,6 +917,8 @@ _e_comp_wl_clipboard_offer_create(E_Comp_Wl_Clipboard_Source *source, int fd)
 
    offer = E_NEW(E_Comp_Wl_Clipboard_Offer, 1);
 
+   /* A reader that is slow, or never reads, must not stall E. */
+   fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
    offer->offset = 0;
    offer->source = source;
    source->ref++;
diff --git a/src/tests/wayland/e_wl_clipboardkit.h b/src/tests/wayland/e_wl_clipboardkit.h
new file mode 100644
index 000000000..d67a2c4cb
--- /dev/null
+++ b/src/tests/wayland/e_wl_clipboardkit.h
@@ -0,0 +1,423 @@
+/* What the tests of the clipboard share: a client with a data device and a
+ * keyboard, and its sources and offers. Nothing waits on a timer: a wait is a
+ * round trip through the compositor's main loop, repeated until the state
+ * wanted is reached, and fails after TK_WAIT_SECONDS. */
+#ifndef E_WL_CLIPBOARDKIT_H
+#define E_WL_CLIPBOARDKIT_H
+
+#include <errno.h>
+#include <poll.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/input-event-codes.h>
+
+#include "e_wl_testkit.h"
+#include "xdg-activation-v1-client-protocol.h"
+
+#define CB_WL_TEST_VERSION 14
+#define CB_FOCUS WL_TEST_CLIENT_STATE_FOCUSED
+#define CB_TYPES_MAX 256
+
+/* A wl_data_offer and the types it announced, in order, space separated. */
+typedef struct
+{
+   struct wl_data_offer *offer;
+   char types[CB_TYPES_MAX];
+} Cb_Offer;
+
+typedef struct
+{
+   Tk *tk;
+   struct wl_test *tester;
+   /* Only for a client with a window. */
+   Tk_Toplevel *top;
+   const char *app_id;
+   struct xdg_activation_v1 *activation;
+   struct wl_data_device_manager *manager;
+   struct wl_data_device *device;
+   Cb_Offer *offer; /* the selection, NULL if none */
+   int selections;
+   uint32_t key_serial;
+} Cb_Peer;
+
+/* A client's own source. It writes its data when asked, or, held, keeps
+ * every fd it is given open and never writes. */
+typedef struct
+{
+   struct wl_data_source *source;
+   const char *data;
+   size_t length;
+   int sends, cancels;
+   int held;
+   int held_fds[4];
+   int held_count;
+} Cb_Source;
+
+static inline unsigned int
+cb_fnv1a(const void *data, size_t length)
+{
+   const unsigned char *p = data;
+   unsigned int hash = 2166136261u;
+   size_t i;
+
+   for (i = 0; i < length; i++)
+     hash = (hash ^ p[i]) * 16777619u;
+   return hash;
+}
+
+/* A payload that shows where a byte went missing or came twice. */
+static inline char *
+cb_pattern(size_t length)
+{
+   char *data = "" + 1);
+   size_t i;
+
+   for (i = 0; i < length; i++)
+     data[i] = 'a' + ((i * 7 + (i / 26)) % 26);
+   data[length] = 0;
+   return data;
+}
+
+static inline void
+_cb_data_offer_offer(void *data, struct wl_data_offer *o __attribute__((unused)), const char *mime)
+{
+   Cb_Offer *offer = data;
+   size_t len = strlen(offer->types);
+
+   snprintf(offer->types + len, sizeof(offer->types) - len, "%s%s", len ? " " : "", mime);
+}
+
+static inline void
+_cb_data_offer_source_actions(void *d __attribute__((unused)), struct wl_data_offer *o __attribute__((unused)), uint32_t a __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_data_offer_action(void *d __attribute__((unused)), struct wl_data_offer *o __attribute__((unused)), uint32_t a __attribute__((unused)))
+{
+}
+
+static const struct wl_data_offer_listener _cb_data_offer_listener =
+{
+   _cb_data_offer_offer,
+   _cb_data_offer_source_actions,
+   _cb_data_offer_action
+};
+
+static inline void
+_cb_device_data_offer(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)), struct wl_data_offer *offer)
+{
+   Cb_Offer *o = calloc(1, sizeof(*o));
+
+   o->offer = offer;
+   wl_data_offer_add_listener(offer, &_cb_data_offer_listener, o);
+}
+
+static inline void
+_cb_device_enter(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)), uint32_t serial __attribute__((unused)), struct wl_surface *s __attribute__((unused)), wl_fixed_t x __attribute__((unused)), wl_fixed_t y __attribute__((unused)), struct wl_data_offer *o __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_device_leave(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_device_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 inline void
+_cb_device_drop(void *d __attribute__((unused)), struct wl_data_device *dd __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_device_selection(void *data, struct wl_data_device *dd __attribute__((unused)), struct wl_data_offer *offer)
+{
+   Cb_Peer *p = data;
+
+   /* An offer that is replaced is kept: a test may read through it. */
+   p->offer = offer ? wl_data_offer_get_user_data(offer) : NULL;
+   p->selections++;
+}
+
+static const struct wl_data_device_listener _cb_device_listener =
+{
+   _cb_device_data_offer,
+   _cb_device_enter,
+   _cb_device_leave,
+   _cb_device_motion,
+   _cb_device_drop,
+   _cb_device_selection
+};
+
+static inline void
+_cb_keyboard_keymap(void *d __attribute__((unused)), struct wl_keyboard *k __attribute__((unused)), uint32_t format __attribute__((unused)), int32_t fd, uint32_t size __attribute__((unused)))
+{
+   close(fd);
+}
+
+static inline void
+_cb_keyboard_enter(void *d __attribute__((unused)), struct wl_keyboard *k __attribute__((unused)), uint32_t serial __attribute__((unused)), struct wl_surface *s __attribute__((unused)), struct wl_array *keys __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_keyboard_leave(void *d __attribute__((unused)), struct wl_keyboard *k __attribute__((unused)), uint32_t serial __attribute__((unused)), struct wl_surface *s __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_keyboard_key(void *data, struct wl_keyboard *k __attribute__((unused)), uint32_t serial, uint32_t time __attribute__((unused)), uint32_t key __attribute__((unused)), uint32_t state __attribute__((unused)))
+{
+   Cb_Peer *p = data;
+
+   p->key_serial = serial;
+}
+
+static inline void
+_cb_keyboard_modifiers(void *d __attribute__((unused)), struct wl_keyboard *k __attribute__((unused)), uint32_t serial __attribute__((unused)), uint32_t dep __attribute__((unused)), uint32_t lat __attribute__((unused)), uint32_t lock __attribute__((unused)), uint32_t group __attribute__((unused)))
+{
+}
+
+static const struct wl_keyboard_listener _cb_keyboard_listener =
+{
+   _cb_keyboard_keymap,
+   _cb_keyboard_enter,
+   _cb_keyboard_leave,
+   _cb_keyboard_key,
+   _cb_keyboard_modifiers
+};
+
+/* A client with a window, a keyboard (E only tells a client it has the
+ * focus through its wl_keyboard) and a data device. */
+static inline void
+cb_peer_new(Cb_Peer *p, const char *prog, const char *app_id)
+{
+   struct wl_seat *seat;
+
+   memset(p, 0, sizeof(*p));
+   p->tk = tk_connect(prog);
+   p->app_id = app_id;
+   p->top = tk_toplevel_new(p->tk, app_id, app_id, 200, 150);
+   p->activation = tk_bind(p->tk, &xdg_activation_v1_interface, 1);
+   p->manager = tk_bind(p->tk, &wl_data_device_manager_interface, 3);
+   seat = tk_bind(p->tk, &wl_seat_interface, 1);
+   if ((!p->activation) || (!p->manager) || (!seat)) tk_fail(p->tk, "missing global");
+   wl_keyboard_add_listener(wl_seat_get_keyboard(seat), &_cb_keyboard_listener, p);
+   p->device = wl_data_device_manager_get_data_device(p->manager, seat);
+   wl_data_device_add_listener(p->device, &_cb_device_listener, p);
+   p->tester = tk_bind(p->tk, &wl_test_interface, CB_WL_TEST_VERSION);
+   if (!p->tester) tk_fail(p->tk, "no wl_test version %d", CB_WL_TEST_VERSION);
+   tk_sync(p->tk);
+}
+
+/* Round trips until *value reaches want. */
+static inline void
+cb_wait_int(Cb_Peer *p, const int *value, int want, const char *what)
+{
+   double deadline = tk_now() + tk_wait_seconds();
+
+   while (*value < want)
+     {
+        if (tk_now() > deadline) tk_fail(p->tk, "%s: %d, wanted %d", what, *value, want);
+        tk_sync(p->tk);
+     }
+}
+
+/* A key press and release, which gives the focused client a serial newer
+ * than anything E has handed out so far, as a user copying would. */
+static inline uint32_t
+cb_serial_fresh(Cb_Peer *p)
+{
+   uint32_t before = p->key_serial;
+   double deadline = tk_now() + tk_wait_seconds();
+
+   wl_test_key_code(p->tester, KEY_LEFTSHIFT, 1);
+   wl_test_key_code(p->tester, KEY_LEFTSHIFT, 0);
+   while (p->key_serial == before)
+     {
+        if (tk_now() > deadline) tk_fail(p->tk, "no key event, is the client focused?");
+        tk_sync(p->tk);
+     }
+   tk_settle(p->tk);
+   return p->key_serial;
+}
+
+static inline void
+_cb_source_target(void *d __attribute__((unused)), struct wl_data_source *s __attribute__((unused)), const char *m __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_source_send(void *data, struct wl_data_source *s __attribute__((unused)), const char *mime __attribute__((unused)), int32_t fd)
+{
+   Cb_Source *src = ""
+   size_t done = 0;
+   ssize_t n;
+
+   src->sends++;
+   if (src->held)
+     {
+        if (src->held_count < 4) src->held_fds[src->held_count++] = fd;
+        else close(fd);
+        return;
+     }
+   while (done < src->length)
+     {
+        n = write(fd, src->data + done, src->length - done);
+        if (n < 0)
+          {
+             if (errno == EINTR) continue;
+             break;
+          }
+        done += n;
+     }
+   close(fd);
+}
+
+static inline void
+_cb_source_cancelled(void *data, struct wl_data_source *s __attribute__((unused)))
+{
+   Cb_Source *src = ""
+
+   src->cancels++;
+}
+
+static inline void
+_cb_source_dnd_drop_performed(void *d __attribute__((unused)), struct wl_data_source *s __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_source_dnd_finished(void *d __attribute__((unused)), struct wl_data_source *s __attribute__((unused)))
+{
+}
+
+static inline void
+_cb_source_action(void *d __attribute__((unused)), struct wl_data_source *s __attribute__((unused)), uint32_t a __attribute__((unused)))
+{
+}
+
+static const struct wl_data_source_listener _cb_source_listener =
+{
+   _cb_source_target,
+   _cb_source_send,
+   _cb_source_cancelled,
+   _cb_source_dnd_drop_performed,
+   _cb_source_dnd_finished,
+   _cb_source_action
+};
+
+/* The client copies: a source offering these types, set as the selection
+ * with a serial from a key it just got. */
+static inline void
+cb_copy(Cb_Peer *p, Cb_Source *src, const char *const *types, int count, const char *data, size_t length, int held)
+{
+   uint32_t serial = cb_serial_fresh(p);
+   int i;
+
+   memset(src, 0, sizeof(*src));
+   src->data = ""
+   src->length = length;
+   src->held = held;
+   src->source = wl_data_device_manager_create_data_source(p->manager);
+   wl_data_source_add_listener(src->source, &_cb_source_listener, src);
+   for (i = 0; i < count; i++)
+     wl_data_source_offer(src->source, types[i]);
+   wl_data_device_set_selection(p->device, src->source, serial);
+   tk_sync(p->tk);
+}
+
+/* Ask for the data through an offer, without reading it: the read end. */
+static inline int
+cb_receive_start(Cb_Peer *p, struct wl_data_offer *offer, const char *mime)
+{
+   int fds[2];
+
+   if (pipe(fds)) tk_fail(p->tk, "pipe");
+   wl_data_offer_receive(offer, mime, fds[1]);
+   wl_display_flush(tk_display(p->tk));
+   close(fds[1]);
+   return fds[0];
+}
+
+/* Read until the owner closes its end. Returns what came, malloc'd and NUL
+ * terminated. */
+static inline char *
+cb_receive_finish(Cb_Peer *p, int fd, size_t *length)
+{
+   double deadline = tk_now() + tk_wait_seconds();
+   size_t size = 0, alloc = 4096;
+   char *data = ""
+   ssize_t n;
+
+   for (;;)
+     {
+        struct pollfd pfd = { fd, POLLIN, 0 };
+
+        if (tk_now() > deadline) tk_fail(p->tk, "the data did not end");
+        if (poll(&pfd, 1, 100) <= 0) continue;
+        if (size + 4096 + 1 > alloc)
+          {
+             alloc *= 2;
+             data = "" alloc);
+          }
+        n = read(fd, data + size, alloc - size - 1);
+        if (n < 0)
+          {
+             if (errno == EINTR) continue;
+             tk_fail(p->tk, "read: %s", strerror(errno));
+          }
+        if (!n) break;
+        size += n;
+     }
+   close(fd);
+   data[size] = 0;
+   *length = size;
+   return data;
+}
+
+static inline char *
+cb_receive(Cb_Peer *p, struct wl_data_offer *offer, const char *mime, size_t *length)
+{
+   return cb_receive_finish(p, cb_receive_start(p, offer, mime), length);
+}
+
+static inline void
+_cb_token_done(void *data, struct xdg_activation_token_v1 *o __attribute__((unused)), const char *token)
+{
+   snprintf(data, 64, "%s", token);
+}
+
+static const struct xdg_activation_token_v1_listener _cb_token_listener =
+{
+   _cb_token_done
+};
+
+/* Hand the focus from one client to the other, and let both hear of it. */
+static inline void
+cb_focus(Cb_Peer *from, Cb_Peer *to)
+{
+   struct xdg_activation_token_v1 *obj;
+   char token[64] = "";
+
+   obj = xdg_activation_v1_get_activation_token(from->activation);
+   xdg_activation_token_v1_add_listener(obj, &_cb_token_listener, token);
+   xdg_activation_token_v1_commit(obj);
+   tk_sync(from->tk);
+   xdg_activation_token_v1_destroy(obj);
+   xdg_activation_v1_activate(to->activation, token, tk_toplevel_surface(to->top));
+   tk_settle(to->tk);
+   tk_wait_states(to->tk, to->app_id, CB_FOCUS, CB_FOCUS, "the focus moved");
+   tk_wait_states(from->tk, from->app_id, CB_FOCUS, 0, "the focus left");
+   tk_sync(from->tk);
+   tk_sync(to->tk);
+}
+
+#endif
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index a6125ce20..afd4bf7f5 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -110,6 +110,7 @@ wl_protocol_tests = [
   ['activation-expiry', 'test_activation.c',
    {'E_TEST_ACTIVATION_TOKEN_LIFETIME': '1'}],
   ['primary-selection', 'test_primary_selection.c'],
+  ['clipboard-saved', 'test_clipboard_saved.c'],
   ['idle-inhibit', 'test_idle_inhibit.c'],
   ['text-input', 'test_text_input.c'],
   ['input-method-keyboard', 'test_input_method_keyboard.c'],
diff --git a/src/tests/wayland/test_clipboard_saved.c b/src/tests/wayland/test_clipboard_saved.c
new file mode 100644
index 000000000..953328465
--- /dev/null
+++ b/src/tests/wayland/test_clipboard_saved.c
@@ -0,0 +1,70 @@
+/* E keeps a copy of the clipboard for when its owner goes. Pasting it must
+ * not stall E, however slow the client pasting is. */
+#include <signal.h>
+
+#include "e_wl_clipboardkit.h"
+
+#define PROG "test-clipboard-saved"
+#define APP_A "e.test.clipboard.saved.a"
+#define APP_B "e.test.clipboard.saved.b"
+#define TEXT "text/plain"
+#define BIG (200 * 1024)
+
+static Tk *observer;
+
+/* tk_fail asks E for its windows, which is what is stuck. */
+static void
+_stuck(int sig __attribute__((unused)))
+{
+   static const char msg[] = PROG ": E stopped answering while a client pasted\n";
+
+   if (write(2, msg, sizeof(msg) - 1) < 0) _exit(2);
+   _exit(1);
+}
+
+int
+main(void)
+{
+   static const char *const types[] = { TEXT };
+   Cb_Peer a, b;
+   Cb_Source src;
+   char *big, *data;
+   size_t length;
+   int fd, i;
+
+   signal(SIGPIPE, SIG_IGN);
+   cb_peer_new(&a, PROG, APP_A);
+   cb_peer_new(&b, PROG, APP_B);
+   observer = tk_connect(PROG);
+   cb_focus(&b, &a);
+
+   /* More than a pipe holds, copied and left behind by its owner. */
+   big = cb_pattern(BIG);
+   cb_copy(&a, &src, types, 1, big, BIG, 0);
+   wl_data_source_destroy(src.source);
+   for (i = 0; i < 20; i++) tk_settle(a.tk);
+
+   /* The client gaining the focus asks for it and does not read. */
+   cb_focus(&a, &b);
+   if (!b.offer) tk_fail(b.tk, "the saved copy was not offered");
+   fd = cb_receive_start(&b, b.offer->offer, TEXT);
+   tk_sync(b.tk);
+
+   /* Everyone else is still served. */
+   signal(SIGALRM, _stuck);
+   alarm(tk_wait_seconds());
+   for (i = 0; i < 5; i++) tk_settle(observer);
+   alarm(0);
+
+   data = "" fd, &length);
+   if ((length != BIG) || memcmp(data, big, BIG))
+     tk_fail(b.tk, "pasted %zu bytes, wanted %d", length, BIG);
+   free(data);
+
+   free(big);
+   tk_disconnect(observer);
+   tk_disconnect(b.tk);
+   tk_disconnect(a.tk);
+   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