Files
magpie/internal/gui/clipboard_gtk3_test.go
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

85 lines
2.8 KiB
Go

//go:build linux && cgo && gtk3 && !nogui
package gui
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
// TestClipboardReadBack builds testdata/clipboard_probe.c with the copy and
// the read back the app runs (clipboard_gtk3.h) and copies in a real GTK 3
// window on the Wayland compositor the test runs under. magpie's copy is
// read back whole. Wails' copy (CLIPBOARD, then PRIMARY) is read back empty
// where the compositor has no primary selection, as on #1881's KDE Plasma
// with middle-click paste off, and the read back then says no copy was
// made. It needs a Wayland session that gives a new window the focus
// (KWin, with kwinrc [Wayland] EnablePrimarySelection=false for the
// second case).
func TestClipboardReadBack(t *testing.T) {
if os.Getenv("WAYLAND_DISPLAY") == "" {
t.Skip("needs a Wayland session")
}
flags, err := exec.Command("pkg-config", "--cflags", "--libs", "gtk+-3.0", "wayland-client").Output()
if err != nil {
t.Skip("no GTK 3 / wayland-client headers:", err)
}
here, _ := filepath.Abs(".")
bin := filepath.Join(t.TempDir(), "probe")
args := append([]string{"testdata/clipboard_probe.c", "-I" + here, "-o", bin}, strings.Fields(string(flags))...)
if out, err := exec.Command("cc", args...).CombinedOutput(); err != nil {
t.Fatalf("build probe: %v\n%s", err, out)
}
run := func(t *testing.T, mode, text string) (primary, read int, held string) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, bin, mode, text)
cmd.Env = append(os.Environ(), "GDK_BACKEND=wayland")
out, err := cmd.Output()
t.Logf("%s: %s", mode, out)
if err != nil {
t.Fatalf("probe: %v", err)
}
primary, read = -2, -2
for _, l := range strings.Split(string(out), "\n") {
fmt.Sscanf(l, "primary=%d", &primary)
if strings.HasPrefix(l, "read=") {
var n int
fmt.Sscanf(l, "read=%d len=%d", &read, &n)
if i := strings.Index(l, " text="); i >= 0 {
held = l[i+len(" text="):]
}
}
}
if read == -1 {
t.Skip("the compositor sent the probe no selection (no focus?)")
}
return primary, read, held
}
text := "claude --resume 1881 «剪贴板»"
t.Run("magpie", func(t *testing.T) {
_, read, held := run(t, "magpie", text)
if !copyHeld(read, held, text) {
t.Errorf("read back %d %q, want %q", read, held, text)
}
})
t.Run("wails", func(t *testing.T) {
primary, read, held := run(t, "wails", text)
if primary != 0 {
t.Skip("the compositor has a primary selection: Wails' copy works there")
}
if read != 1 || held != "" {
t.Errorf("read back %d %q, want 1 and an empty text (GTK closing the pipe)", read, held)
}
if copyHeld(read, held, text) {
t.Error("an empty read back counts as a copy made")
}
})
}