Files
magpie/internal/gui/testdata/clipboard_probe.c
T
yetone 558a313325 gui: a copy on KDE Plasma Wayland reaches the clipboard, and one that doesn't isn't called copied (#1881, BabyLy233)
On KDE Plasma (KWin 6.7.2) Wayland the GUI said "copied" and the clipboard
stayed empty, for wl-paste and Klipper too; GDK_BACKEND=x11 copied.

The reporter's WAYLAND_DEBUG log: KWin took magpie's selection
(set_selection(wl_data_source#43, 17983), then selection(wl_data_offer)),
and every paste asked it (wl_data_source#43.send("text/plain;charset=utf-8",
fd 74), fd 74 again, fd 77 ...) with the same fd number coming back each
time: GTK closed each pipe without writing. Their registry has no
zwp_primary_selection_device_manager_v1, which is KWin with
[Wayland] EnablePrimarySelection=false (middle-click paste off).

Wails v3's clipboardSet sets CLIPBOARD, then PRIMARY. GTK 3's Wayland
selection code (gdkselection-wayland.c, every 3.24 release up to .52) keeps
one source_targets list for both: setting PRIMARY clears it, and with no
primary manager gdk_wayland_selection_add_targets returns before adding
any, so CLIPBOARD's source then answers every send() with a closed pipe.
Wails' SetText answers true whatever happened.

- clipboard_gtk3.h/.go (linux, gtk3): set PRIMARY first, then CLIPBOARD.
  On Wayland, read the clipboard back through the compositor on a
  wl_data_device of its own (own event queue; GTK's own clipboard API
  short-cuts to the local copy and would never see the bug). Copy is true
  only when the text reads back whole (copyHeld); an empty, other, short
  or unknown read is false, so /api/copy answers 501 and the page tries
  its own ways. X11 keeps GTK's word, as before.
- clipboard_other.go: macOS, Windows and non-gtk3 builds keep Wails'
  SetText unchanged.

Run:
- go test -tags nogui ./internal/gui (pass); TestCopyHeld red with
  copyHeld taking an empty read as held.
- gofmt -l; go vet for darwin (gui and nogui), GOOS=linux and
  GOOS=windows -tags nogui.
- Fedora 44 container (GTK 3.24.52, KWin 6.7.5 kwin_wayland --virtual,
  Go 1.26.9): go vet -tags gtk3 ./internal/gui; TestClipboardReadBack
  (builds testdata/clipboard_probe.c, a GTK 3 window copying with
  clipboard_gtk3.h) with EnablePrimarySelection=false: Wails' order reads
  back len=0 and wl-paste prints nothing, magpie's reads back the text and
  wl-paste prints it; with it true both read back whole (wails subtest
  skips). With the order swapped back the "magpie" subtest goes red.
- Same container, the real app (go build -tags "gtk3 dev", sandbox HOME,
  shell role's control /copy calling host.Copy): "ok" and wl-paste prints
  each text; rebuilt with Wails' order: not ok, wl-paste empty.

Not tried: a real KDE Plasma desktop, Klipper, or GDK_BACKEND=x11 after
the change (that path is Wails' as before).
2026-10-13 11:13:24 +08:00

89 lines
3.0 KiB
C

// clipboard_probe: a GTK 3 window that copies a text once it has the
// focus, then reads the clipboard back through the compositor with
// magpie_clipboard_read and prints what it read (TestClipboardReadBack).
// "wails" copies as Wails v3 does (CLIPBOARD, then PRIMARY), "magpie" as
// magpie does (magpie_clipboard_set).
#include "clipboard_gtk3.h"
#include <stdio.h>
static const char *mode;
static const char *text;
static void *display, *seat;
static int done;
static gpointer reader(gpointer data) {
char *out = NULL;
size_t n = 0;
int r = magpie_clipboard_read(display, seat, 3000, &out, &n);
printf("read=%d len=%zu text=%s\n", r, n, out ? out : "");
fflush(stdout);
free(out);
// PROBE_HOLD=<seconds> keeps the window, and its copy, for a paste
// from outside (wl-paste) by hand
const char *hold = getenv("PROBE_HOLD");
if (hold)
g_usleep(atoi(hold) * G_USEC_PER_SEC);
g_idle_add((GSourceFunc)gtk_main_quit, NULL);
return NULL;
}
static gboolean copy(gpointer data) {
if (done++)
return G_SOURCE_REMOVE;
if (strcmp(mode, "wails") == 0) {
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), text, -1);
} else {
magpie_clipboard_set(text);
}
if (!magpie_clipboard_wayland(&display, &seat)) {
printf("read=-1 len=0 text= (not wayland)\n");
gtk_main_quit();
return G_SOURCE_REMOVE;
}
g_thread_unref(g_thread_new("reader", reader, NULL));
return G_SOURCE_REMOVE;
}
// primary says whether the compositor has a primary selection protocol,
// from a connection of its own
static void primary_global(void *data, struct wl_registry *r, uint32_t name, const char *iface, uint32_t v) {
if (strcmp(iface, "zwp_primary_selection_device_manager_v1") == 0 || strcmp(iface, "gtk_primary_selection_device_manager") == 0)
*(int *)data = 1;
}
static void primary_remove(void *data, struct wl_registry *r, uint32_t name) {}
static const struct wl_registry_listener primary_listener = {primary_global, primary_remove};
static int primary(void) {
int has = 0;
struct wl_display *d = wl_display_connect(NULL);
if (!d)
return -1;
struct wl_registry *r = wl_display_get_registry(d);
wl_registry_add_listener(r, &primary_listener, &has);
wl_display_roundtrip(d);
wl_registry_destroy(r);
wl_display_disconnect(d);
return has;
}
static gboolean focused(GtkWidget *w, GdkEvent *e, gpointer data) {
// the copy goes out from the main loop, as the app's does
g_idle_add(copy, NULL);
return FALSE;
}
int main(int argc, char **argv) {
gtk_init(&argc, &argv);
printf("primary=%d\n", primary());
mode = argc > 1 ? argv[1] : "magpie";
text = argc > 2 ? argv[2] : "magpie probe text";
GtkWidget *w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(w), 200, 100);
g_signal_connect(w, "focus-in-event", G_CALLBACK(focused), NULL);
gtk_widget_show_all(w);
g_timeout_add_seconds(5, copy, NULL);
g_timeout_add_seconds(getenv("PROBE_HOLD") ? 60 : 20, (GSourceFunc)gtk_main_quit, NULL);
gtk_main();
return 0;
}