FLTK logo

[master] d23dda3 - Wayland: Implement 'Primary selection' protocol (#1621)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Library      Forums      Links      Apps     Login 
[  All Forums  |  Back to fltk.commit  ]
 
[ Previous Message ] [ Next Message ]

[master] d23dda3 - Wayland: Implement 'Primary selection' protocol (#1621) "ManoloFLTK" 13:02 Oct 02  
 
commit d23dda3205405958b1ca22023ba36fc01ea0874e
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Fri Oct 2 14:50:26 2026 +0200
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Fri Oct 2 14:50:26 2026 +0200

    Wayland: Implement 'Primary selection' protocol (#1621)

 CMake/options.cmake                              |   6 ++
 config.h.in                                      |   6 ++
 documentation/src/wayland.dox                    |  22 +++--
 src/CMakeLists.txt                               |  13 +++
 src/drivers/Wayland/Fl_Wayland_Screen_Driver.H   |   8 ++
 src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx |  30 ++++++
 src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx | 118 ++++++++++++++++++++++-
 7 files changed, 194 insertions(+), 9 deletions(-)

diff --git CMake/options.cmake CMake/options.cmake
index 93cfca8..7e81e58 100644
--- CMake/options.cmake
+++ CMake/options.cmake
@@ -334,6 +334,12 @@ if(UNIX)
       set(HAVE_CURSOR_SHAPE 0)
       set(HAVE_TABLET 0)
     endif()
+    if(EXISTS ${PROTOCOLS}/unstable/primary-selection/primary-selection-unstable-v1.xml)
+      set(HAVE_PRIMARY_SELECTION 1)
+      message(STATUS "Found dev files for Wayland protocol 'Primary selection'")
+    else()
+       set(HAVE_PRIMARY_SELECTION 0)
+    endif()
     if(FLTK_BACKEND_X11)
       include(FindX11)
     endif()
diff --git config.h.in config.h.in
index 52db6ac..7e6f75c 100644
--- config.h.in
+++ config.h.in
@@ -363,3 +363,9 @@
  */
 
 #cmakedefine01 HAVE_XDG_ACTIVATION
+
+/*
+ * Can we use the "primary-selection" Wayland protocol?
+ */
+
+#cmakedefine01 HAVE_PRIMARY_SELECTION
diff --git documentation/src/wayland.dox documentation/src/wayland.dox
index 2ddcb8d..2e71e33 100644
--- documentation/src/wayland.dox
+++ documentation/src/wayland.dox
@@ -101,8 +101,8 @@ beginning with \c wl_, \c xkb_ or \c libdecor_ and belong to the \c libwayland-c
 \c libwayland-cursor.so, \c libxkbcommon.so and \c libdecor.so shared libraries.
 FLTK calls also several other functions which operate other used Wayland protocols.
 These have names beginning with \c xdg_, \c zwp_text_input_v3, \c zxdg_toplevel_decoration_,
-\c gtk_shell1_, \c gtk_surface1_, \c wp_cursor_shape_, \c xdg_activation_ and
-\c zwp_tablet_v2.
+\c gtk_shell1_, \c gtk_surface1_, \c wp_cursor_shape_, \c xdg_activation_,
+\c zwp_tablet_v2 and \c zwp_primary_selection_.
 FLTK never writes itself to the socket; this is done inside the functions just mentioned.
 - Compositor to client: the callback function \c wld_socket_callback() runs when there are
 data to read in the socket; it calls \c libdecor_dispatch() which reads available data from
@@ -166,6 +166,7 @@ Similar operations are performed for FLTK to use protocols
 <a href=https://wayland.app/protocols/xdg-decoration-unstable-v1>XDG decoration</a>,
 <a href=https://wayland.app/protocols/cursor-shape-v1>Cursor shape</a>,
 <a href=https://wayland.app/protocols/text-input-unstable-v3>Text input</a>,
+<a href=https://wayland.app/protocols/primary-selection-unstable-v1>Primary selection</a>,
 <a href=https://wayland.app/protocols/gtk-shell>GTK Shell</a>
 and <a href=https://wayland.app/protocols/tablet-v2>Tablet</a>.
 
@@ -343,6 +344,7 @@ Here is the list of the \c interface value for all protocols and system features
   zwp_text_input_manager_v3          | interface with Text Input Methods
   zxdg_decoration_manager_v1         | select between CSD and SSD modes
   zwp_tablet_manager_v2              | support of graphics tablets
+  zwp_primary_selection_device_manager_v1 | middle-click paste of current selection
 
 Wayland compositors typically support several other protocols (e.g., \c zxdg_output_manager_v1)
 that FLTK does not use.
@@ -1269,20 +1271,28 @@ given in \c libdecor-gtk.c or \c libdecor-cairo.c such as type <tt>struct border
 \section wayland-clipboard Copy/Paste/Drag-n-Drop
 
 FLTK follows the procedure that is very well described in item "Wayland clipboard and drag &
-drop" of the \ref wayland-doc. All corresponding source code is in file
-\c src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx.
+drop" of the \ref wayland-doc.
+FLTK also implements the 'Primary selection' Wayland protocol to allow middle-click pasting
+of the current text selection.
+All corresponding source code is in file \c src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx.
 
 This part of the \ref wayland-seat "Fl_Wayland_Screen_Driver::seat" record stores pointers to
-Wayland objects used for clipboard and D-n-D operations:
+Wayland objects used for clipboard, Drag-n-Drop and middle-click-paste operations:
 \code
   struct wl_data_device_manager *data_device_manager;
   struct wl_data_device *data_device;
   struct wl_data_source *data_source;
+#if HAVE_PRIMARY_SELECTION
+  struct zwp_primary_selection_device_manager_v1 *primary_selection_device_manager;
+  struct zwp_primary_selection_device_v1 *primary_selection_device;
+  struct zwp_primary_selection_source_v1 *primary_selection_source;
+#endif
 \endcode
 
 FLTK can copy or paste plain UTF-8 text or image data to/from the clipboard. Images are copied to the
 clipboard as \c image/bmp mime type. Images in \c image/bmp or \c image/png mime types from the
-clipboard can be pasted to FLTK apps.
+clipboard can be pasted to FLTK apps. It can also middle-click paste the current text selection
+of the current app or any other running app.
 
 Files dropped are received one pathname per line, with no \c '\\n' after the last pathname.
 
diff --git src/CMakeLists.txt src/CMakeLists.txt
index e46f6ef..b5fc0f9 100644
--- src/CMakeLists.txt
+++ src/CMakeLists.txt
@@ -905,6 +905,19 @@ if(UNIX AND FLTK_USE_WAYLAND)
     list(APPEND SHARED_FILES "tablet-protocol.c")
   endif()
 
+  if(HAVE_PRIMARY_SELECTION)
+    set(INFILE ${PROTOCOLS}/unstable/primary-selection/primary-selection-unstable-v1.xml)
+    add_custom_command(
+      OUTPUT primary-selection-protocol.c primary-selection-client-protocol.h
+      COMMAND wayland-scanner private-code  ${INFILE} primary-selection-protocol.c
+      COMMAND wayland-scanner client-header ${INFILE} primary-selection-client-protocol.h
+      DEPENDS ${INFILE}
+      VERBATIM
+    )
+    list(APPEND STATIC_FILES "primary-selection-protocol.c")
+    list(APPEND SHARED_FILES "primary-selection-protocol.c")
+  endif()
+
   if(STOP_REQUIRED)
     message(FATAL_ERROR "*** Terminating: one or more required file(s) were not found. ***")
   endif()
diff --git src/drivers/Wayland/Fl_Wayland_Screen_Driver.H src/drivers/Wayland/Fl_Wayland_Screen_Driver.H
index 3068ef6..5cf735b 100644
--- src/drivers/Wayland/Fl_Wayland_Screen_Driver.H
+++ src/drivers/Wayland/Fl_Wayland_Screen_Driver.H
@@ -57,6 +57,11 @@ public:
     struct wl_data_device_manager *data_device_manager;
     struct wl_data_device *data_device;
     struct wl_data_source *data_source;
+#if HAVE_PRIMARY_SELECTION
+    struct zwp_primary_selection_device_manager_v1 *primary_selection_device_manager;
+    struct zwp_primary_selection_device_v1 *primary_selection_device;
+    struct zwp_primary_selection_source_v1 *primary_selection_source;
+#endif
     struct xkb_state *xkb_state;
     struct xkb_context *xkb_context;
     struct xkb_keymap *xkb_keymap;
@@ -190,6 +195,9 @@ public:
   struct wl_seat *get_wl_seat();
   char  *get_seat_name();
   struct xkb_keymap *get_xkb_keymap();
+#if HAVE_PRIMARY_SELECTION
+  void primary_selection_init();
+#endif
 };
 
 
diff --git src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx
index 0ceae06..fa7d5cf 100644
--- src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx
+++ src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx
@@ -47,6 +47,9 @@
 #  include "tablet-client-protocol.h"
 #  include "Fl_Wayland_Pen_Driver.H"
 #endif
+#if HAVE_PRIMARY_SELECTION
+#  include "primary-selection-client-protocol.h"
+#endif
 #include <assert.h>
 #include <sys/mman.h>
 #include <poll.h>
@@ -1321,6 +1324,9 @@ static void registry_handle_global(void *user_data, struct wl_registry *wl_regis
       wl_data_device_add_listener(scr_driver->seat->data_device,
                                   Fl_Wayland_Screen_Driver::p_data_device_listener, NULL);
     }
+#if HAVE_PRIMARY_SELECTION
+    if (scr_driver->seat->primary_selection_device_manager) scr_driver->primary_selection_init();
+#endif
 
   } else if (strcmp(interface, wl_data_device_manager_interface.name) == 0) {
     if (!scr_driver->seat) scr_driver->seat =
@@ -1431,6 +1437,15 @@ static void registry_handle_global(void *user_data, struct wl_registry *wl_regis
               wl_registry, id, &zwp_tablet_manager_v2_interface, 1);
       fl_wayland_tablet_set_manager(tm);
 #endif
+#if HAVE_PRIMARY_SELECTION
+  } else if (strcmp(interface, zwp_primary_selection_device_manager_v1_interface.name) == 0) {
+    bool seat_known = (scr_driver->seat && scr_driver->seat->wl_seat);
+    if (!scr_driver->seat) scr_driver->seat = (struct Fl_Wayland_Screen_Driver::seat*)calloc(1,
+                                              sizeof(struct Fl_Wayland_Screen_Driver::seat));
+    scr_driver->seat->primary_selection_device_manager = (struct zwp_primary_selection_device_manager_v1 *)
+      wl_registry_bind(wl_registry, id, &zwp_primary_selection_device_manager_v1_interface, 1);
+    if (seat_known) scr_driver->primary_selection_init();
+#endif // HAVE_PRIMARY_SELECTION
   }
 }
 
@@ -1653,6 +1668,21 @@ void Fl_Wayland_Screen_Driver::close_display() {
 #if FLTK_HAVE_PEN_SUPPORT
   fl_wayland_tablet_cleanup();
 #endif
+#if HAVE_PRIMARY_SELECTION
+  if (seat->primary_selection_device_manager) {
+    if (seat->primary_selection_source)
+      zwp_primary_selection_source_v1_destroy(seat->primary_selection_source);
+    if (seat->primary_selection_device) {
+      struct zwp_primary_selection_offer_v1 *primary_selection_offer =
+        *(struct zwp_primary_selection_offer_v1 **)
+        zwp_primary_selection_device_v1_get_user_data(seat->primary_selection_device);
+      if (primary_selection_offer)
+        zwp_primary_selection_offer_v1_destroy(primary_selection_offer);
+      zwp_primary_selection_device_v1_destroy(seat->primary_selection_device);
+    }
+    zwp_primary_selection_device_manager_v1_destroy(seat->primary_selection_device_manager);
+  }
+#endif // HAVE_PRIMARY_SELECTION
   wl_seat_destroy(seat->wl_seat); seat->wl_seat = NULL;
   if (seat->name) free(seat->name);
   free(seat); seat = NULL;
diff --git src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx
index e9927d5..659be12 100644
--- src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx
+++ src/drivers/Wayland/fl_wayland_clipboard_dnd.cxx
@@ -25,6 +25,9 @@
 #  include "Fl_Wayland_Window_Driver.H"
 #  include "../Unix/Fl_Unix_System_Driver.H"
 #  include "Fl_Wayland_Graphics_Driver.H"
+#if HAVE_PRIMARY_SELECTION
+#  include "primary-selection-client-protocol.h"
+#endif
 
 #  include <errno.h>
 #  include <stdio.h>
@@ -55,7 +58,7 @@ struct data_source_write_struct {
   const char *from;
 };
 
-void write_data_source_cb(FL_SOCKET fd, data_source_write_struct *data) {
+static void write_data_source_cb(FL_SOCKET fd, data_source_write_struct *data) {
   while (data->rest) {
     ssize_t n = write(fd, data->from, data->rest);
     if (n == -1) {
@@ -581,9 +584,101 @@ static int get_clipboard_image(struct wl_data_offer *offer) {
 }
 
 
+#if HAVE_PRIMARY_SELECTION
+
+static struct zwp_primary_selection_offer_v1 *primary_selection_offer = NULL;
+
+static void primary_selection_source_handle_send(void *data,
+                                                 struct zwp_primary_selection_source_v1 *source,
+                                                 const char *mime_type, int fd) {
+  data_source_write_struct *write_data = new data_source_write_struct;
+  write_data->rest = selection_string[0].length();
+  write_data->from = selection_string[0].c_str();
+  Fl::add_fd(fd, FL_WRITE, (Fl_FD_Handler)write_data_source_cb, write_data);
+}
+
+static void primary_selection_source_handle_cancelled(void *, struct zwp_primary_selection_source_v1 *);
+
+static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
+  .send = primary_selection_source_handle_send,
+  .cancelled = primary_selection_source_handle_cancelled
+};
+
+static void primary_selection_source_handle_cancelled(void *data,
+                                                      struct zwp_primary_selection_source_v1 *source) {
+}
+
+
+static void primary_selection_offer_handle_offer(void *data,
+                                                 struct zwp_primary_selection_offer_v1 *offer,
+                                                 const char *mime) {
+}
+
+static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
+  .offer = primary_selection_offer_handle_offer
+};
+
+
+static void primary_selection_device_handle_data_offer(void *data,
+                                                 struct zwp_primary_selection_device_v1 *device,
+                                                 struct zwp_primary_selection_offer_v1 *offer) {
+  zwp_primary_selection_offer_v1_add_listener(offer, &primary_selection_offer_listener, NULL);
+}
+
+static void primary_selection_device_handle_selection(void *data,
+                                                      struct zwp_primary_selection_device_v1 *device,
+                                                      struct zwp_primary_selection_offer_v1 *offer) {
+  if (primary_selection_offer) zwp_primary_selection_offer_v1_destroy(primary_selection_offer);
+  primary_selection_offer = offer;
+}
+
+static const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
+  .data_offer  = primary_selection_device_handle_data_offer,
+  .selection = primary_selection_device_handle_selection
+};
+
+static std::string tmp_selection_string;
+
+static void read_fd_cb(FL_SOCKET fd, bool *p_finished) {
+  char buffer[1000];
+  int l = read(fd, buffer, sizeof(buffer));
+  if (l > 0) tmp_selection_string.insert(tmp_selection_string.end(), buffer, buffer + l);
+  else {
+    close(fd);
+    Fl::remove_fd(fd, FL_READ);
+    *p_finished = true;
+  }
+}
+
+
+static void paste_primary_selection_offer() {
+  int fds[2];
+  pipe(fds);
+  zwp_primary_selection_offer_v1_receive(primary_selection_offer, wld_plain_text_clipboard, fds[1]);
+  close(fds[1]);
+  tmp_selection_string.clear();
+  bool finished = false;
+  Fl::add_fd(fds[0], FL_READ, (Fl_FD_Handler)read_fd_cb, &finished);
+  while (!finished) Fl::wait();
+  selection_string[0] = tmp_selection_string;
+  tmp_selection_string.clear();
+}
+
+
+/* called after both the primary-selection protocol and the seat have been received */
+void Fl_Wayland_Screen_Driver::primary_selection_init() {
+  seat->primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(seat->primary_selection_device_manager, seat->wl_seat);
+  zwp_primary_selection_device_v1_add_listener(seat->primary_selection_device, &primary_selection_device_listener, &primary_selection_offer);
+  seat->primary_selection_source = zwp_primary_selection_device_manager_v1_create_source(seat->primary_selection_device_manager);
+  zwp_primary_selection_source_v1_add_listener(seat->primary_selection_source, &primary_selection_source_listener, seat);
+}
+
+#endif // HAVE_PRIMARY_SELECTION
+
+
 void Fl_Wayland_Screen_Driver::paste(Fl_Widget &receiver, int clipboard, const char *type) {
-  if (clipboard != 1) return;
-  if (fl_i_own_selection[1]) {
+  if (clipboard > 1) return;
+  if (clipboard == 1 && fl_i_own_selection[1]) {
     // We already have it, do it quickly without compositor.
     if (type == Fl::clipboard_plain_text && fl_selection_type[1] == type) {
       Fl::e_text = (char*)selection_string[1].c_str();
@@ -596,6 +691,18 @@ void Fl_Wayland_Screen_Driver::paste(Fl_Widget &receiver, int clipboard, const c
     return;
   }
   // otherwise get the compositor to return it:
+  if (clipboard == 0) {
+#if HAVE_PRIMARY_SELECTION
+    if (primary_selection_offer) {
+      paste_primary_selection_offer();
+      Fl::e_text = (char*)selection_string[0].c_str();
+      Fl::e_length = selection_string[0].length();
+      fl_selection_type[0] = Fl::clipboard_plain_text;
+      receiver.handle(FL_PASTE);
+    }
+#endif
+    return;
+  }
   if (!fl_selection_offer) return;
   if (type == Fl::clipboard_plain_text && clipboard_contains(Fl::clipboard_plain_text)) {
     get_clipboard_or_dragged_text(fl_selection_offer, wld_plain_text_clipboard);
@@ -643,6 +750,11 @@ void Fl_Wayland_Screen_Driver::copy(const char *stuff, int len, int clipboard,
                                  seat->data_source,
                                  seat->keyboard_enter_serial);
 //fprintf(stderr, "wl_data_device_set_selection len=%d to %d\n", len, clipboard);
+#if HAVE_PRIMARY_SELECTION
+  } else if (seat->primary_selection_device_manager) {
+    zwp_primary_selection_source_v1_offer(seat->primary_selection_source, wld_plain_text_clipboard);
+    zwp_primary_selection_device_v1_set_selection(seat->primary_selection_device, seat->primary_selection_source, seat->serial);
+#endif // HAVE_PRIMARY_SELECTION
   }
 }
 
[ Direct Link to Message ]
 
     
[ Previous Message ] [ Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2026 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.