Files
magpie/internal/gui/clipboard_gtk3.go
T
yetone 64487d458e gui: copying on KDE Wayland no longer breaks the connection to the compositor and quits magpie (#2039, dannisun0412)
dannisun0412 runs magpie on KDE Plasma 6.6.6 (kwin_wayland, libwayland
1.24) and it quit three times in twelve minutes with "Error 22 (Invalid
argument) dispatching to Wayland display" / "Error reading events from
display: Invalid argument".

The cause is the clipboard read-back probe (clipboard_gtk3.h). It made
its own wl_data_device and, when done, called wl_data_device_destroy,
which only frees the client side. The compositor still thinks the
device is live and keeps sending it a fresh wl_data_offer on every
later copy. libwayland drops those events for the dead proxy without
reserving their new ids, so the client's id map falls behind the
server's; the next server-created object whose id lands past the gap
makes wl_map_reserve_new return EINVAL and GDK exits.

The probe now binds the data device manager at version 3 at most and
releases the device with wl_data_device_release (version 2+), so the
compositor stops sending to it. On a version-1 manager it doesn't read
back at all (returns -2, which the Go side takes as "copy held").

The probe test (testdata/clipboard_probe.c) gets an "again" mode: copy
and read back eight times, then make the compositor hand out four
offers at once (the "burst" that needs ids past the leak), then check
the process is still alive.

Run:
- Fedora 44 container with kwin_wayland --virtual (SYS_NICE),
  gtk3/webkit2gtk-4.1, go 1.26.9: with origin/main's header the probe
  died with EINVAL 6 of 6 times (primary selection on and off) and
  TestClipboardReadBack failed 3 of 3 with "Error reading events from
  display: Invalid argument"; with this change the probe stayed alive
  6 of 6 and TestClipboardReadBack passed 3 of 3. GOOS=linux go vet
  with the gtk3 build in the container was clean.
- macOS: gofmt -l clean; go vet darwin, linux nogui, windows nogui;
  go test -tags nogui ./internal/gui passed.
Not tried: a real Plasma desktop or the full app over Selkies.
2026-10-14 09:15:21 +08:00

52 lines
1.4 KiB
Go

//go:build linux && cgo && gtk3 && !nogui
package gui
/*
#cgo pkg-config: gtk+-3.0 wayland-client
#include "clipboard_gtk3.h"
*/
import "C"
import (
"unsafe"
"github.com/wailsapp/wails/v3/pkg/application"
)
// copyText puts text on the clipboard and says whether it is there. Wails'
// own copy sets CLIPBOARD and then PRIMARY, which GTK 3 serves empty where
// the compositor has no primary selection (#1881, see clipboard_gtk3.h),
// and its answer is always true. On Wayland the clipboard is read back
// through the compositor, as a paste in another program would read it; a
// copy that can't be read back is not reported as made, so the page tries
// its own ways. X11 (GDK_BACKEND=x11) keeps GTK's word, as before.
func copyText(_ *application.App, text string) bool {
ct := C.CString(text)
defer C.free(unsafe.Pointer(ct))
var display, seat unsafe.Pointer
wayland := false
application.InvokeSync(func() {
C.magpie_clipboard_set(ct)
wayland = C.magpie_clipboard_wayland(&display, &seat) != 0
})
if !wayland {
return true
}
// off the main thread: GTK answers the read from it when the copy is
// magpie's own
var out *C.char
var n C.size_t
got := C.magpie_clipboard_read(display, seat, 2000, &out, &n)
if got == -2 {
// a compositor too old to read back from without harm (#2039)
return true
}
var held string
if out != nil {
held = C.GoStringN(out, C.int(n))
C.free(unsafe.Pointer(out))
}
return copyHeld(int(got), held, text)
}