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

git pushed a commit to reference refs/pull/203/head
in repository enlightenment.

View the commit online.

commit 396635fb911133d5fa453b51fce9004504dba2c3
Author: Cedric BAIL <[email protected]>
AuthorDate: Mon Nov 23 18:19:57 2026 -0600

    e_comp_wl - add zwp_primary_selection_device_manager_v1
    
    Middle click paste does not work for Wayland clients under E, because
    the protocol that carries it is not advertised.
    
    The selection is offered only to the client with the keyboard focus,
    and a client losing the focus is told its offer is gone, as the
    protocol requires. Each device remembers what it was last told, so a
    client hears only about a real change: no empty selection is sent to
    a client that never had one, which wlcs reads as a wrong answer.
    
    An offer stops working when its client loses the focus, so a client
    in the background cannot keep one to read what the user highlights
    next. Setting the selection is not restricted the same way: the
    protocol does not ask for it and wlcs sets it from a client without
    the focus. E does not keep input serials, so the serial is ignored
    and a background client can replace the selection.
    
    The contents are not saved when the owner exits, matching PRIMARY on
    X11. gtk_primary_selection, the older protocol, is left out: GTK
    dropped it in 2020.
    
    Only Wayland code is touched.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bin/e_comp_wl.c                        |   1 +
 src/bin/e_comp_wl.h                        |   8 +
 src/bin/e_comp_wl_data.c                   | 305 +++++++++++++++++++++++++++++
 src/bin/e_comp_wl_data.h                   |   1 +
 src/bin/generated/meson.build              |   1 +
 src/tests/wayland/globals.expected         |   1 +
 src/tests/wayland/meson.build              |   2 +
 src/tests/wayland/test_primary_selection.c | 268 +++++++++++++++++++++++++
 8 files changed, 587 insertions(+)

diff --git a/src/bin/e_comp_wl.c b/src/bin/e_comp_wl.c
index cc38908b9..140e92d73 100644
--- a/src/bin/e_comp_wl.c
+++ b/src/bin/e_comp_wl.c
@@ -3063,6 +3063,7 @@ _e_comp_wl_client_cb_focus_unset(void *data EINA_UNUSED, E_Client *ec)
 
    if (e_comp_wl->kbd.focus == ec->comp_data->surface)
      e_comp_wl->kbd.focus = NULL;
+   e_comp_wl_data_primary_selection_update();
 }
 
 static void
diff --git a/src/bin/e_comp_wl.h b/src/bin/e_comp_wl.h
index 36d16ff41..1ac794975 100644
--- a/src/bin/e_comp_wl.h
+++ b/src/bin/e_comp_wl.h
@@ -267,6 +267,14 @@ struct _E_Comp_Wl_Data
         Ecore_Window xwl_owner;
      } clipboard;
 
+   struct
+     {
+        struct wl_global *global;
+        Eina_List *devices;
+        struct _E_Comp_Wl_Data_Source *source;
+        struct wl_listener source_destroy_listener;
+     } primary_selection;
+
    struct
      {
         struct wl_resource *resource;
diff --git a/src/bin/e_comp_wl_data.c b/src/bin/e_comp_wl_data.c
index bb7b9ad06..c90ba1c6d 100644
--- a/src/bin/e_comp_wl_data.c
+++ b/src/bin/e_comp_wl_data.c
@@ -26,6 +26,7 @@
 #define EXECUTIVE_MODE_ENABLED
 #define E_COMP_WL
 #include "e.h"
+#include "primary-selection-unstable-v1-server-protocol.h"
 
 #if defined(__clang__)
 # pragma clang diagnostic ignored "-Wunused-parameter"
@@ -1100,6 +1101,299 @@ e_comp_wl_data_device_send_offer(E_Client *ec)
    return offer_res;
 }
 
+/* zwp_primary_selection_device_manager_v1, the middle click paste buffer.
+ *
+ * A device's user data is the source it was last told about, so that a
+ * client only hears about a change: the selection is offered to the client
+ * with the keyboard focus, and every other device holds NULL, as the
+ * protocol requires across focus changes. Unlike the clipboard, the contents
+ * are not saved: as with X11's PRIMARY, the selection dies with its owner. */
+
+static struct wl_client *
+_e_comp_wl_primary_selection_focus_client_get(void)
+{
+   if ((!e_comp_wl->kbd.enabled) || (!e_comp_wl->kbd.focus)) return NULL;
+   return wl_resource_get_client(e_comp_wl->kbd.focus);
+}
+
+static void
+_e_comp_wl_primary_selection_offer_cb_receive(struct wl_client *client, struct wl_resource *resource, const char *mime_type, int32_t fd)
+{
+   E_Comp_Wl_Data_Offer *offer = wl_resource_get_user_data(resource);
+
+   /* An offer stops being valid when its client loses the focus, and a
+    * client in the background must not read what the user selects next. */
+   if ((offer->source) &&
+       (offer->source == e_comp_wl->primary_selection.source) &&
+       (client == _e_comp_wl_primary_selection_focus_client_get()))
+     offer->source->send(offer->source, mime_type, fd);
+   else
+     close(fd);
+}
+
+static void
+_e_comp_wl_primary_selection_offer_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   wl_resource_destroy(resource);
+}
+
+static const struct zwp_primary_selection_offer_v1_interface _e_primary_selection_offer_interface =
+{
+   _e_comp_wl_primary_selection_offer_cb_receive,
+   _e_comp_wl_primary_selection_offer_cb_destroy,
+};
+
+static void
+_e_comp_wl_primary_selection_offer_cb_resource_destroy(struct wl_resource *resource)
+{
+   E_Comp_Wl_Data_Offer *offer = wl_resource_get_user_data(resource);
+
+   if (offer->source)
+     wl_list_remove(&offer->source_destroy_listener.link);
+   free(offer);
+}
+
+static void
+_e_comp_wl_primary_selection_offer_cb_source_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
+{
+   E_Comp_Wl_Data_Offer *offer;
+
+   offer = container_of(listener, E_Comp_Wl_Data_Offer, source_destroy_listener);
+   wl_list_remove(&offer->source_destroy_listener.link);
+   offer->source = NULL;
+}
+
+static void
+_e_comp_wl_primary_selection_source_send_send(E_Comp_Wl_Data_Source *source, const char *mime_type, int32_t fd)
+{
+   zwp_primary_selection_source_v1_send_send(source->resource, mime_type, fd);
+   close(fd);
+}
+
+static void
+_e_comp_wl_primary_selection_source_cancelled_send(E_Comp_Wl_Data_Source *source)
+{
+   zwp_primary_selection_source_v1_send_cancelled(source->resource);
+}
+
+static const struct zwp_primary_selection_source_v1_interface _e_primary_selection_source_interface =
+{
+   _e_comp_wl_data_source_cb_offer,
+   _e_comp_wl_data_source_cb_destroy,
+};
+
+static struct wl_resource *
+_e_comp_wl_primary_selection_offer_create(E_Comp_Wl_Data_Source *source, struct wl_resource *device)
+{
+   E_Comp_Wl_Data_Offer *offer;
+   Eina_Array_Iterator it;
+   unsigned int i;
+   char *t;
+
+   offer = E_NEW(E_Comp_Wl_Data_Offer, 1);
+   if (!offer)
+     {
+        wl_client_post_no_memory(wl_resource_get_client(device));
+        return NULL;
+     }
+
+   offer->resource =
+     wl_resource_create(wl_resource_get_client(device),
+                        &zwp_primary_selection_offer_v1_interface,
+                        wl_resource_get_version(device), 0);
+   if (!offer->resource)
+     {
+        free(offer);
+        wl_client_post_no_memory(wl_resource_get_client(device));
+        return NULL;
+     }
+
+   wl_resource_set_implementation(offer->resource,
+                                  &_e_primary_selection_offer_interface, offer,
+                                  _e_comp_wl_primary_selection_offer_cb_resource_destroy);
+   offer->source = source;
+   offer->source_destroy_listener.notify =
+     _e_comp_wl_primary_selection_offer_cb_source_destroy;
+   wl_signal_add(&source->destroy_signal, &offer->source_destroy_listener);
+
+   zwp_primary_selection_device_v1_send_data_offer(device, offer->resource);
+   if (source->mime_types)
+     EINA_ARRAY_ITER_NEXT(source->mime_types, i, t, it)
+       zwp_primary_selection_offer_v1_send_offer(offer->resource, t);
+
+   return offer->resource;
+}
+
+EINTERN void
+e_comp_wl_data_primary_selection_update(void)
+{
+   E_Comp_Wl_Data_Source *wanted;
+   struct wl_client *focus;
+   struct wl_resource *device, *offer;
+   Eina_List *l;
+
+   focus = _e_comp_wl_primary_selection_focus_client_get();
+   EINA_LIST_FOREACH(e_comp_wl->primary_selection.devices, l, device)
+     {
+        wanted = NULL;
+        if (wl_resource_get_client(device) == focus)
+          wanted = e_comp_wl->primary_selection.source;
+        if (wl_resource_get_user_data(device) == wanted) continue;
+
+        wl_resource_set_user_data(device, wanted);
+        offer = NULL;
+        if (wanted)
+          offer = _e_comp_wl_primary_selection_offer_create(wanted, device);
+        zwp_primary_selection_device_v1_send_selection(device, offer);
+     }
+}
+
+static void
+_e_comp_wl_primary_selection_cb_source_destroy(struct wl_listener *listener EINA_UNUSED, void *data EINA_UNUSED)
+{
+   wl_list_remove(&e_comp_wl->primary_selection.source_destroy_listener.link);
+   e_comp_wl->primary_selection.source = NULL;
+   e_comp_wl_data_primary_selection_update();
+}
+
+static void
+_e_comp_wl_primary_selection_set(E_Comp_Wl_Data_Source *source)
+{
+   E_Comp_Wl_Data_Source *prev = e_comp_wl->primary_selection.source;
+
+   if (prev == source) return;
+
+   if (prev)
+     {
+        wl_list_remove(&e_comp_wl->primary_selection.source_destroy_listener.link);
+        prev->cancelled(prev);
+     }
+
+   e_comp_wl->primary_selection.source = source;
+   if (source)
+     {
+        e_comp_wl->primary_selection.source_destroy_listener.notify =
+          _e_comp_wl_primary_selection_cb_source_destroy;
+        wl_signal_add(&source->destroy_signal,
+                      &e_comp_wl->primary_selection.source_destroy_listener);
+     }
+
+   e_comp_wl_data_primary_selection_update();
+}
+
+/* The serial is not checked: E does not keep the serials of past input
+ * events. */
+static void
+_e_comp_wl_primary_selection_device_cb_selection_set(struct wl_client *client EINA_UNUSED, struct wl_resource *resource EINA_UNUSED, struct wl_resource *source_resource, uint32_t serial EINA_UNUSED)
+{
+   E_Comp_Wl_Data_Source *source = NULL;
+
+   if (source_resource)
+     source = wl_resource_get_user_data(source_resource);
+   _e_comp_wl_primary_selection_set(source);
+}
+
+static void
+_e_comp_wl_primary_selection_device_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   wl_resource_destroy(resource);
+}
+
+static const struct zwp_primary_selection_device_v1_interface _e_primary_selection_device_interface =
+{
+   _e_comp_wl_primary_selection_device_cb_selection_set,
+   _e_comp_wl_primary_selection_device_cb_destroy,
+};
+
+static void
+_e_comp_wl_primary_selection_device_cb_unbind(struct wl_resource *resource)
+{
+   e_comp_wl->primary_selection.devices =
+     eina_list_remove(e_comp_wl->primary_selection.devices, resource);
+}
+
+static void
+_e_comp_wl_primary_selection_manager_cb_source_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
+{
+   E_Comp_Wl_Data_Source *source;
+
+   source = E_NEW(E_Comp_Wl_Data_Source, 1);
+   if (!source)
+     {
+        wl_resource_post_no_memory(resource);
+        return;
+     }
+
+   wl_signal_init(&source->destroy_signal);
+   source->send = _e_comp_wl_primary_selection_source_send_send;
+   source->cancelled = _e_comp_wl_primary_selection_source_cancelled_send;
+
+   source->resource =
+     wl_resource_create(client, &zwp_primary_selection_source_v1_interface,
+                        wl_resource_get_version(resource), id);
+   if (!source->resource)
+     {
+        free(source);
+        wl_resource_post_no_memory(resource);
+        return;
+     }
+
+   wl_resource_set_implementation(source->resource,
+                                  &_e_primary_selection_source_interface, source,
+                                  _e_comp_wl_data_source_cb_resource_destroy);
+}
+
+static void
+_e_comp_wl_primary_selection_manager_cb_device_get(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *seat_resource EINA_UNUSED)
+{
+   struct wl_resource *device;
+
+   device = wl_resource_create(client, &zwp_primary_selection_device_v1_interface,
+                               wl_resource_get_version(resource), id);
+   if (!device)
+     {
+        wl_resource_post_no_memory(resource);
+        return;
+     }
+
+   wl_resource_set_implementation(device, &_e_primary_selection_device_interface,
+                                  NULL, _e_comp_wl_primary_selection_device_cb_unbind);
+   e_comp_wl->primary_selection.devices =
+     eina_list_append(e_comp_wl->primary_selection.devices, device);
+   e_comp_wl_data_primary_selection_update();
+}
+
+static void
+_e_comp_wl_primary_selection_manager_cb_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
+{
+   wl_resource_destroy(resource);
+}
+
+static const struct zwp_primary_selection_device_manager_v1_interface _e_primary_selection_manager_interface =
+{
+   _e_comp_wl_primary_selection_manager_cb_source_create,
+   _e_comp_wl_primary_selection_manager_cb_device_get,
+   _e_comp_wl_primary_selection_manager_cb_destroy,
+};
+
+static void
+_e_comp_wl_primary_selection_cb_bind_manager(struct wl_client *client, void *data EINA_UNUSED, uint32_t version, uint32_t id)
+{
+   struct wl_resource *res;
+
+   res = wl_resource_create(client,
+                            &zwp_primary_selection_device_manager_v1_interface,
+                            version, id);
+   if (!res)
+     {
+        wl_client_post_no_memory(client);
+        return;
+     }
+
+   wl_resource_set_implementation(res, &_e_primary_selection_manager_interface,
+                                  NULL, NULL);
+}
+
 E_API void
 e_comp_wl_data_device_keyboard_focus_set(void)
 {
@@ -1107,6 +1401,8 @@ e_comp_wl_data_device_keyboard_focus_set(void)
    E_Comp_Wl_Data_Source *source;
    E_Client *focused;
 
+   e_comp_wl_data_primary_selection_update();
+
    if (!e_comp_wl->kbd.enabled)
      {
         ERR("Keyboard not enabled");
@@ -1172,6 +1468,13 @@ e_comp_wl_data_manager_init(void)
    _e_comp_wl_clipboard_create();
    e_comp_wl->mgr.data_resources = eina_hash_pointer_new(NULL);
 
+   e_comp_wl->primary_selection.global =
+     wl_global_create(e_comp_wl->wl.disp,
+                      &zwp_primary_selection_device_manager_v1_interface, 1,
+                      NULL, _e_comp_wl_primary_selection_cb_bind_manager);
+   if (!e_comp_wl->primary_selection.global)
+     ERR("Could not create global for primary selection device manager");
+
    return EINA_TRUE;
 }
 
@@ -1183,6 +1486,8 @@ e_comp_wl_data_manager_shutdown(void)
 
    wl_list_remove(&e_comp_wl->clipboard.listener.link);
    E_FREE_FUNC(e_comp_wl->mgr.data_resources, eina_hash_free);
+   e_comp_wl->primary_selection.devices =
+     eina_list_free(e_comp_wl->primary_selection.devices);
 }
 
 E_API struct wl_resource *
diff --git a/src/bin/e_comp_wl_data.h b/src/bin/e_comp_wl_data.h
index bc76b5c75..2e996e88a 100644
--- a/src/bin/e_comp_wl_data.h
+++ b/src/bin/e_comp_wl_data.h
@@ -90,6 +90,7 @@ E_API void e_comp_wl_data_device_send_enter(E_Client *ec);
 E_API void e_comp_wl_data_device_send_leave(E_Client *ec);
 EINTERN void *e_comp_wl_data_device_send_offer(E_Client *ec);
 E_API void e_comp_wl_data_device_keyboard_focus_set(void);
+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);
diff --git a/src/bin/generated/meson.build b/src/bin/generated/meson.build
index f291b057b..4bc09f12c 100644
--- a/src/bin/generated/meson.build
+++ b/src/bin/generated/meson.build
@@ -8,6 +8,7 @@ protos = [
   '@0@/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'.format(dir_wayland_protocols),
   '@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@/unstable/primary-selection/primary-selection-unstable-v1.xml'.format(dir_wayland_protocols),
 ]
 
 proto_c = []
diff --git a/src/tests/wayland/globals.expected b/src/tests/wayland/globals.expected
index 72a18fa9d..22740208e 100644
--- a/src/tests/wayland/globals.expected
+++ b/src/tests/wayland/globals.expected
@@ -11,6 +11,7 @@ xdg_activation_v1	1
 xdg_wm_base	6
 zwp_e_session_recovery	1
 zwp_pointer_constraints_v1	1
+zwp_primary_selection_device_manager_v1	1
 zwp_relative_pointer_manager_v1	1
 zxdg_decoration_manager_v1	1
 zxdg_exporter_v1	1
diff --git a/src/tests/wayland/meson.build b/src/tests/wayland/meson.build
index 72bce326a..9f16a9d29 100644
--- a/src/tests/wayland/meson.build
+++ b/src/tests/wayland/meson.build
@@ -30,6 +30,7 @@ foreach p: [
   '@0@/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml'.format(dir_wayland_protocols),
   '@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@/unstable/primary-selection/primary-selection-unstable-v1.xml'.format(dir_wayland_protocols),
 ]
   test_proto_src += gen_scanner_client.process(p)
   test_proto_src += gen_scanner_impl.process(p)
@@ -76,6 +77,7 @@ wl_protocol_tests = [
   ['activation', 'test_activation.c'],
   ['activation-expiry', 'test_activation.c',
    {'E_TEST_ACTIVATION_TOKEN_LIFETIME': '1'}],
+  ['primary-selection', 'test_primary_selection.c'],
   ['client-iconify', 'test_client_iconify.c',
    {'ELM_TRANSITION_DURATION_FACTOR': '4'}],
 ]
diff --git a/src/tests/wayland/test_primary_selection.c b/src/tests/wayland/test_primary_selection.c
new file mode 100644
index 000000000..0c72ad4c2
--- /dev/null
+++ b/src/tests/wayland/test_primary_selection.c
@@ -0,0 +1,268 @@
+/* The primary selection between two clients: it follows the keyboard focus,
+ * and a client without the focus cannot read it. */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "e_wl_testkit.h"
+#include "primary-selection-unstable-v1-client-protocol.h"
+#include "xdg-activation-v1-client-protocol.h"
+
+#define PROG "test-primary-selection"
+#define APP_A "e.test.primary.a"
+#define APP_B "e.test.primary.b"
+#define APP_C "e.test.primary.c"
+#define MIME "text/plain"
+#define FOCUS WL_TEST_CLIENT_STATE_FOCUSED
+
+typedef struct
+{
+   struct zwp_primary_selection_source_v1 *obj;
+   const char *text;
+   int sends, cancels;
+} Source;
+
+typedef struct
+{
+   Tk *tk;
+   Tk_Toplevel *top;
+   const char *app_id;
+   struct xdg_activation_v1 *activation;
+   struct wl_seat *seat;
+   struct zwp_primary_selection_device_manager_v1 *manager;
+   struct zwp_primary_selection_device_v1 *device;
+   struct zwp_primary_selection_offer_v1 *offer;
+   int selections, mimes;
+} Peer;
+
+static void
+_source_send(void *data, struct zwp_primary_selection_source_v1 *o __attribute__((unused)),
+             const char *mime __attribute__((unused)), int32_t fd)
+{
+   Source *s = data;
+
+   s->sends++;
+   if (write(fd, s->text, strlen(s->text)) < 0) perror("write");
+   close(fd);
+}
+
+static void
+_source_cancelled(void *data, struct zwp_primary_selection_source_v1 *o __attribute__((unused)))
+{
+   Source *s = data;
+
+   s->cancels++;
+}
+
+static const struct zwp_primary_selection_source_v1_listener _source_listener =
+{
+   _source_send,
+   _source_cancelled
+};
+
+static void
+_offer_mime(void *data, struct zwp_primary_selection_offer_v1 *o __attribute__((unused)),
+            const char *mime)
+{
+   Peer *p = data;
+
+   if (!strcmp(mime, MIME)) p->mimes++;
+}
+
+static const struct zwp_primary_selection_offer_v1_listener _offer_listener =
+{
+   _offer_mime
+};
+
+static void
+_device_data_offer(void *data, struct zwp_primary_selection_device_v1 *o __attribute__((unused)),
+                   struct zwp_primary_selection_offer_v1 *offer)
+{
+   Peer *p = data;
+
+   p->mimes = 0;
+   zwp_primary_selection_offer_v1_add_listener(offer, &_offer_listener, p);
+}
+
+static void
+_device_selection(void *data, struct zwp_primary_selection_device_v1 *o __attribute__((unused)),
+                  struct zwp_primary_selection_offer_v1 *offer)
+{
+   Peer *p = data;
+
+   p->selections++;
+   p->offer = offer;
+}
+
+static const struct zwp_primary_selection_device_v1_listener _device_listener =
+{
+   _device_data_offer,
+   _device_selection
+};
+
+static void
+_peer_new(Peer *p, const char *app_id)
+{
+   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, &zwp_primary_selection_device_manager_v1_interface, 1);
+   if ((!p->activation) || (!p->manager)) tk_fail(p->tk, "missing global");
+   p->seat = tk_bind(p->tk, &wl_seat_interface, 1);
+   /* E only tells a client it has the focus through its wl_keyboard. */
+   wl_seat_get_keyboard(p->seat);
+   p->device = zwp_primary_selection_device_manager_v1_get_device(p->manager, p->seat);
+   zwp_primary_selection_device_v1_add_listener(p->device, &_device_listener, p);
+   tk_sync(p->tk);
+}
+
+static void
+_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 _token_listener =
+{
+   _token_done
+};
+
+/* Hand the focus from one client to the other, and let both hear of it. */
+static void
+_focus(Peer *from, 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, &_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, FOCUS, FOCUS, "the focus moved");
+   tk_wait_states(from->tk, from->app_id, FOCUS, 0, "the focus left");
+   tk_sync(from->tk);
+   tk_sync(to->tk);
+}
+
+static void
+_source_set(Peer *p, Source *s, const char *text, int with_mime)
+{
+   memset(s, 0, sizeof(*s));
+   s->text = text;
+   s->obj = zwp_primary_selection_device_manager_v1_create_source(p->manager);
+   zwp_primary_selection_source_v1_add_listener(s->obj, &_source_listener, s);
+   if (with_mime) zwp_primary_selection_source_v1_offer(s->obj, MIME);
+   zwp_primary_selection_device_v1_set_selection(p->device, s->obj, 0);
+   tk_sync(p->tk);
+}
+
+/* Ask for the data through this offer and return what came back. */
+static const char *
+_read(Peer *reader, struct zwp_primary_selection_offer_v1 *offer, Peer *owner)
+{
+   static char buf[64];
+   int fds[2];
+   ssize_t n;
+
+   if (pipe(fds)) tk_fail(reader->tk, "pipe");
+   zwp_primary_selection_offer_v1_receive(offer, MIME, fds[1]);
+   tk_sync(reader->tk);
+   close(fds[1]);
+   tk_sync(owner->tk);
+   n = read(fds[0], buf, sizeof(buf) - 1);
+   close(fds[0]);
+   buf[(n > 0) ? n : 0] = 0;
+   return buf;
+}
+
+int
+main(void)
+{
+   struct zwp_primary_selection_offer_v1 *stale;
+   Source s, s2, s3;
+   Peer a, b;
+   Tk *c;
+   int count;
+
+   _peer_new(&a, APP_A);
+   _peer_new(&b, APP_B);
+   tk_wait_states(b.tk, APP_B, FOCUS, FOCUS, "the newest window has the focus");
+
+   /* No selection yet: nothing to say to either client, focused or not. */
+   _focus(&b, &a);
+   _focus(&a, &b);
+   if (a.selections || b.selections)
+     tk_fail(a.tk, "an empty selection was announced (%d, %d)", a.selections, b.selections);
+
+   /* The focused client owns it, and only it hears about it. */
+   _source_set(&b, &s, "from b", 1);
+   tk_sync(a.tk);
+   if ((b.selections != 1) || (!b.offer)) tk_fail(b.tk, "the owner got no offer");
+   if (a.selections) tk_fail(a.tk, "a client without the focus heard of the selection");
+
+   /* The focus moves it over, with its types, and the old client is told. */
+   stale = b.offer;
+   count = b.selections;
+   _focus(&b, &a);
+   if ((a.selections != 1) || (!a.offer) || (a.mimes != 1))
+     tk_fail(a.tk, "the focused client got no usable offer");
+   if ((b.selections != count + 1) || b.offer)
+     tk_fail(b.tk, "the client losing the focus was not told");
+   if (strcmp(_read(&a, a.offer, &b), "from b") || (s.sends != 1))
+     tk_fail(a.tk, "the focused client could not read the selection");
+
+   /* An offer kept past the focus reads nothing. */
+   if (_read(&b, stale, &b)[0] || (s.sends != 1))
+     tk_fail(b.tk, "a client without the focus read the selection");
+
+   /* A new selection cancels the old owner. */
+   _source_set(&a, &s2, "from a", 1);
+   tk_sync(b.tk);
+   if ((s.cancels != 1) || s2.cancels) tk_fail(a.tk, "replacing did not cancel the old owner");
+   zwp_primary_selection_source_v1_destroy(s.obj);
+
+   /* The owner can clear its own, even without the focus. */
+   _focus(&a, &b);
+   if (!b.offer) tk_fail(b.tk, "no offer after the focus came back");
+   count = b.selections;
+   zwp_primary_selection_device_v1_set_selection(a.device, NULL, 0);
+   tk_sync(a.tk);
+   tk_sync(b.tk);
+   if ((s2.cancels != 1) || (b.selections != count + 1) || b.offer)
+     tk_fail(b.tk, "the owner could not clear its selection");
+   zwp_primary_selection_source_v1_destroy(s2.obj);
+
+   /* A source with no types still moves with the focus, and when its owner
+    * destroys it the focused client is told it is gone. */
+   _source_set(&b, &s3, "empty", 0);
+   _focus(&b, &a);
+   if ((!a.offer) || a.mimes) tk_fail(a.tk, "a source with no types was not offered");
+
+   /* The focus going to a client with no wl_keyboard still ends the offer. */
+   c = tk_connect(PROG);
+   tk_toplevel_new(c, APP_C, APP_C, 200, 150);
+   tk_wait_states(c, APP_C, FOCUS, FOCUS, "the new window has the focus");
+   tk_sync(a.tk);
+   if (a.offer) tk_fail(a.tk, "focus went to a client with no keyboard, the offer lives on");
+   tk_disconnect(c);
+   tk_wait_states(a.tk, APP_A, FOCUS, FOCUS, "the focus came back");
+   tk_sync(a.tk);
+   if (!a.offer) tk_fail(a.tk, "no offer after the focus came back");
+   count = a.selections;
+   zwp_primary_selection_source_v1_destroy(s3.obj);
+   tk_sync(b.tk);
+   tk_sync(a.tk);
+   if ((a.selections != count + 1) || a.offer)
+     tk_fail(a.tk, "the end of the source was not announced");
+
+   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