window_clipboard/src/platform/linux.rs
leyoda 319db02e52
Some checks failed
Test / all (windows-latest, beta) (push) Has been cancelled
Test / all (windows-latest, stable) (push) Has been cancelled
Test / all (macOS-latest, beta) (push) Has been cancelled
Test / all (macOS-latest, stable) (push) Has been cancelled
Test / all (ubuntu-latest, beta) (push) Has been cancelled
Test / all (ubuntu-latest, stable) (push) Has been cancelled
yoda: gate clipboard_x11 behind an opt-in feature
Upstream pulled clipboard_x11 unconditionally on unix, which drags
x11rb + x11rb-protocol and ~500 symbols of X11 clipboard code into
every Wayland-only iced build. Add an x11 feature (default-on for
compat) that gates:
- the clipboard_x11 workspace dep (now optional)
- `pub use clipboard_x11 as x11` re-export
- the Clipboard::X11 enum variant
- every match arm that handles it
- the fallback branch in `unsafe fn connect` (returns a helpful
  error when called with a non-Wayland display handle)

Wayland-only builds set `window_clipboard = { ..., default-features = false, features = ["wayland"] }` and skip the x11 backend entirely.
2026-04-24 06:50:48 +02:00

241 lines
6.7 KiB
Rust

use crate::{
dnd::DndProvider,
mime::{ClipboardLoadData, ClipboardStoreData},
ClipboardProvider,
};
use dnd::{DndAction, DndDestinationRectangle, DndSurface, Icon};
use mime::{AllowedMimeTypes, AsMimeTypes};
use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
use std::{borrow::Cow, error::Error, sync::Arc};
use wayland::DndSender;
pub use clipboard_wayland as wayland;
#[cfg(feature = "x11")]
pub use clipboard_x11 as x11;
pub enum Clipboard {
Wayland(wayland::Clipboard),
#[cfg(feature = "x11")]
X11(x11::Clipboard),
}
impl ClipboardProvider for Clipboard {
fn read(&self) -> Result<String, Box<dyn Error>> {
match self {
Clipboard::Wayland(c) => c.read(),
#[cfg(feature = "x11")]
Clipboard::X11(c) => c.read().map_err(Box::from),
}
}
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
match self {
Clipboard::Wayland(c) => c.write(contents),
#[cfg(feature = "x11")]
Clipboard::X11(c) => c.write(contents).map_err(Box::from),
}
}
fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
match self {
Clipboard::Wayland(c) => Some(c.read_primary()),
#[cfg(feature = "x11")]
Clipboard::X11(c) => Some(c.read_primary().map_err(Box::from)),
}
}
fn write_primary(
&mut self,
contents: String,
) -> Option<Result<(), Box<dyn Error>>> {
match self {
Clipboard::Wayland(c) => Some(c.write_primary(contents)),
#[cfg(feature = "x11")]
Clipboard::X11(c) => {
Some(c.write_primary(contents).map_err(Box::from))
}
}
}
fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
where
T: mime::AllowedMimeTypes,
{
match self {
Clipboard::Wayland(c) => {
let ret = c.read_data::<ClipboardLoadData<T>>();
Some(ret.map(|ret| ret.0))
}
#[cfg(feature = "x11")]
Clipboard::X11(_) => None,
}
}
fn write_data<T: Send + Sync + 'static>(
&mut self,
contents: ClipboardStoreData<T>,
) -> Option<Result<(), Box<dyn Error>>>
where
T: mime::AsMimeTypes,
{
match self {
Clipboard::Wayland(c) => {
Some(c.write_data::<ClipboardStoreData<T>>(contents))
}
#[cfg(feature = "x11")]
Clipboard::X11(_) => None,
}
}
fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
where
T: mime::AllowedMimeTypes,
{
match self {
Clipboard::Wayland(c) => {
let ret = c.read_primary_data::<ClipboardLoadData<T>>();
Some(ret.map(|ret| ret.0))
}
#[cfg(feature = "x11")]
Clipboard::X11(_) => None,
}
}
fn read_primary_raw(
&self,
allowed: Vec<String>,
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
match self {
Clipboard::Wayland(c) => Some(c.read_primary_raw(allowed)),
#[cfg(feature = "x11")]
Clipboard::X11(_) => None,
}
}
fn read_raw(
&self,
allowed: Vec<String>,
) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
match self {
Clipboard::Wayland(c) => Some(c.read_raw(allowed)),
#[cfg(feature = "x11")]
Clipboard::X11(_) => None,
}
}
fn write_primary_data<T: Send + Sync + 'static>(
&mut self,
contents: ClipboardStoreData<T>,
) -> Option<Result<(), Box<dyn Error>>>
where
T: mime::AsMimeTypes,
{
match self {
Clipboard::Wayland(c) => {
Some(c.write_primary_data::<ClipboardStoreData<T>>(contents))
}
#[cfg(feature = "x11")]
Clipboard::X11(_) => None,
}
}
}
impl DndProvider for Clipboard {
fn init_dnd(
&self,
tx: Box<dyn dnd::Sender<DndSurface> + Send + Sync + 'static>,
) {
match self {
Clipboard::Wayland(c) => c.init_dnd(DndSender(Arc::from(tx))),
#[cfg(feature = "x11")]
Clipboard::X11(_) => {}
}
}
fn start_dnd<D: AsMimeTypes + Send + 'static>(
&self,
internal: bool,
source_surface: DndSurface,
icon_surface: Option<Icon>,
content: D,
actions: DndAction,
) {
match self {
Clipboard::Wayland(c) => c.start_dnd(
internal,
source_surface,
icon_surface,
content,
actions,
),
#[cfg(feature = "x11")]
Clipboard::X11(_) => {}
}
}
fn end_dnd(&self) {
match self {
Clipboard::Wayland(c) => c.end_dnd(),
#[cfg(feature = "x11")]
Clipboard::X11(_) => {}
}
}
fn register_dnd_destination(
&self,
surface: DndSurface,
rectangles: Vec<DndDestinationRectangle>,
) {
match self {
Clipboard::Wayland(c) => {
c.register_dnd_destination(surface, rectangles)
}
#[cfg(feature = "x11")]
Clipboard::X11(_) => {}
}
}
fn set_action(&self, action: DndAction) {
match self {
Clipboard::Wayland(c) => c.set_action(action),
#[cfg(feature = "x11")]
Clipboard::X11(_) => {}
}
}
fn peek_offer<D: AllowedMimeTypes + 'static>(
&self,
mime_type: Option<Cow<'static, str>>,
) -> std::io::Result<D> {
match self {
Clipboard::Wayland(c) => c.peek_offer::<D>(mime_type),
#[cfg(feature = "x11")]
Clipboard::X11(_) => Err(std::io::Error::new(
std::io::ErrorKind::Other,
"DnD not supported",
)),
}
}
}
pub unsafe fn connect<W: HasDisplayHandle + ?Sized>(
window: &W,
) -> Result<Clipboard, Box<dyn Error>> {
let clipboard = match window.display_handle()?.as_raw() {
RawDisplayHandle::Wayland(handle) => Clipboard::Wayland(
wayland::Clipboard::connect(handle.display.as_ptr()),
) as _,
#[cfg(feature = "x11")]
_ => Clipboard::X11(x11::Clipboard::connect()?) as _,
#[cfg(not(feature = "x11"))]
_ => {
return Err(Box::from(
"Yoda window_clipboard: X11 feature disabled; \
non-Wayland display handles are not supported",
))
}
};
Ok(clipboard)
}