Skip to content

fix(clipboard): copied templates vanish on Wayland - #23

Open
LashaKamadadze wants to merge 1 commit into
reemus-dev:mainfrom
LashaKamadadze:main
Open

fix(clipboard): copied templates vanish on Wayland#23
LashaKamadadze wants to merge 1 commit into
reemus-dev:mainfrom
LashaKamadadze:main

Conversation

@LashaKamadadze

Copy link
Copy Markdown

Problem

On Wayland compositors (I tested on Hyprland), the clipboard actions in
search (Shift+C, Shift+X) print "copied to clipboard," but nothing
ends up there. wl-paste comes back empty and no Wayland app can
paste it.

Root cause

copypasta always picks its X11 backend on Linux. From src/lib.rs:

#[cfg(feature = "x11")]
pub type ClipboardContext = x11_clipboard::X11ClipboardContext;

No runtime check for WAYLAND_DISPLAY, no fallback. If the x11
feature is on (it is by default, alongside wayland), ClipboardContext
is the X11 type, full stop. Reaching copypasta's Wayland backend means
calling wayland_clipboard::create_clipboards_from_external(display_ptr)
yourself with a raw Wayland display handle. gitnr never does that.

This isn't a gap copypasta is unaware of. The maintainer has said as
much directly:

gitnr is exactly that second case, a TUI with no window at all. So on
Wayland it opens XWayland and sets clipboard ownership on a headless,
invisible X11 window. The call succeeds, hence "Success," but the X11
selection only lives as long as the owning process answers paste
requests, and compositor XWayland-to-Wayland clipboard bridging is
generally keyed to visible or focused Xwayland surfaces. A headless
clipboard-only window often gets skipped by that bridge. The copy is
gone the moment gitnr exits, sometimes sooner.

Fix

Switched to arboard. On Linux it checks WAYLAND_DISPLAY at runtime
and, when set, uses the wlr-data-control protocol, the same one
wl-copy and cliphist use (and, notably, the same wl-clipboard-rs the
copypasta maintainer pointed people to). That protocol works with no
window, which is what a TUI needs. It falls back to X11 automatically
when Wayland isn't there.

I also had to turn on the wayland-data-control feature by hand.
arboard's default feature is image-data, not Wayland support. I
turned image-data off too, since gitnr only ever copies text. That
drops the image crate and its PNG decoding stack from the dependency
tree.

Testing

  • cargo test: 21 passed
  • cargo clippy --all-targets: no new warnings
  • cargo build --release: clean
  • Tested by hand on Hyprland: selected templates in search, copied
    with Shift+C, checked wl-paste and pasted into another app.
    Previously produced nothing.

copypasta always uses the X11 clipboard backend on Linux, even though
it also compiles in Wayland support - selecting that path requires
the caller to hand it a raw Wayland display handle, which copypasta
never does on its own. On Wayland compositors this means gitnr's
clipboard copy silently writes to an ephemeral, headless X11
selection that the native Wayland clipboard never sees, so pasted
content is just gone.

Switch to arboard, which detects WAYLAND_DISPLAY at runtime and uses
the wlr-data-control protocol (the same one wl-copy/cliphist use).
That protocol works headless with no window surface, which fits a
TUI app, and arboard falls back to X11 automatically when Wayland
isn't available. The wayland-data-control feature has to be enabled
explicitly - it's not part of arboard's defaults.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant