diff --git a/Cargo.toml b/Cargo.toml index 2aacaa0..4d13334 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,16 @@ readme = "README.md" keywords = ["clipboard", "window", "ui", "gui", "raw-window-handle"] categories = ["gui"] +[features] +# Yoda: put the unix clipboard backends behind opt-in features. Upstream +# pulled both X11 + Wayland unconditionally on unix — pure bloat for +# Wayland-only builds (clipboard_x11 pulls x11rb + its protocol machinery). +# Default keeps both enabled to preserve upstream behaviour; yoda consumers +# pass default-features=false + "wayland" at the dep declaration. +default = ["x11", "wayland"] +x11 = ["dep:clipboard_x11"] +wayland = ["dep:clipboard_wayland"] + [dependencies] raw-window-handle = { version = "0.6", features = ["std"] } thiserror = "1.0" @@ -24,8 +34,8 @@ clipboard-win = { version = "5.0", features = ["std"] } clipboard_macos = { version = "0.1", path = "./macos" } [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies] -clipboard_x11 = { version = "0.4.2", path = "./x11" } -clipboard_wayland = { version = "0.2.2", path = "./wayland" } +clipboard_x11 = { version = "0.4.2", path = "./x11", optional = true } +clipboard_wayland = { version = "0.2.2", path = "./wayland", optional = true } [dev-dependencies] rand = "0.8" diff --git a/dnd/Cargo.toml b/dnd/Cargo.toml index f977386..0ddb926 100644 --- a/dnd/Cargo.toml +++ b/dnd/Cargo.toml @@ -9,9 +9,9 @@ bitflags = "2.5.0" raw-window-handle = "0.6" [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies] -smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd-3", features = [ +smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "sctk-0.20", features = [ "dnd", ] } -sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", default-features = false, features = [ +sctk = { package = "smithay-client-toolkit", version = "0.20", default-features = false, features = [ "calloop", -], rev = "3bed072" } +] } diff --git a/dnd/src/lib.rs b/dnd/src/lib.rs index 16c99f9..66ccd8c 100644 --- a/dnd/src/lib.rs +++ b/dnd/src/lib.rs @@ -189,9 +189,7 @@ pub enum Icon { } #[derive(Clone)] -pub struct DndSurface( - pub Arc>, -); +pub struct DndSurface(pub Arc); impl Debug for DndSurface { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/dnd/src/platform/linux.rs b/dnd/src/platform/linux.rs index abafb4b..31a1879 100644 --- a/dnd/src/platform/linux.rs +++ b/dnd/src/platform/linux.rs @@ -45,13 +45,7 @@ impl AsMimeTypes for DataWrapper { impl smithay_clipboard::dnd::RawSurface for DndSurface { unsafe fn get_ptr(&mut self) -> *mut c_void { - // XXX won't panic because this is only called once before it could be - // cloned - Arc::get_mut(&mut self.0) - .unwrap() - .window_handle() - .unwrap() - .get_ptr() + self.0.window_handle().unwrap().get_ptr() } } diff --git a/mime/Cargo.toml b/mime/Cargo.toml index 61643a0..adacdb5 100644 --- a/mime/Cargo.toml +++ b/mime/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies] -smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd-3" } +smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "sctk-0.20" } diff --git a/src/lib.rs b/src/lib.rs index e671882..54d330f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ pub struct PlatformClipboard { impl PlatformClipboard { /// Safety: the display handle must be valid for the lifetime of `Clipboard` - pub unsafe fn connect( + pub unsafe fn connect( window: &W, ) -> Result> { Ok(PlatformClipboard { diff --git a/src/platform/android.rs b/src/platform/android.rs index c2d460a..885f80d 100644 --- a/src/platform/android.rs +++ b/src/platform/android.rs @@ -1,12 +1,15 @@ use crate::ClipboardProvider; +use crate::dnd::DndProvider; +use dnd::{DndAction, DndDestinationRectangle, DndSurface, Icon}; +use mime::{AllowedMimeTypes, AsMimeTypes}; use raw_window_handle::HasDisplayHandle; -use std::error::Error; +use std::{borrow::Cow, error::Error}; pub fn connect( _window: &W, ) -> Result> { - Ok(Clipboard::new()) + Clipboard::new() } pub struct Clipboard; @@ -36,7 +39,46 @@ impl ClipboardProvider for Clipboard { Err(Box::new(AndroidClipboardError::Unimplemented)) } - fn write(&mut self, contents: String) -> Result<(), Box> { + fn write(&mut self, _contents: String) -> Result<(), Box> { Err(Box::new(AndroidClipboardError::Unimplemented)) } } + +impl DndProvider for Clipboard { + fn init_dnd( + &self, + _tx: Box + Send + Sync + 'static>, + ) { + } + + fn start_dnd( + &self, + _internal: bool, + _source_surface: DndSurface, + _icon_surface: Option, + _content: D, + _actions: DndAction, + ) { + } + + fn end_dnd(&self) {} + + fn register_dnd_destination( + &self, + _surface: DndSurface, + _rectangles: Vec, + ) { + } + + fn set_action(&self, _action: DndAction) {} + + fn peek_offer( + &self, + _mime_type: Option>, + ) -> std::io::Result { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + "DnD not supported", + )) + } +} diff --git a/src/platform/dummy.rs b/src/platform/dummy.rs index 3fc3779..c86bcbf 100644 --- a/src/platform/dummy.rs +++ b/src/platform/dummy.rs @@ -7,7 +7,7 @@ use std::borrow::Cow; pub struct Clipboard; -pub fn connect( +pub fn connect( _window: &W, ) -> Result> { Ok(Clipboard) diff --git a/src/platform/ios.rs b/src/platform/ios.rs index 1ab6e84..fd961c7 100644 --- a/src/platform/ios.rs +++ b/src/platform/ios.rs @@ -3,7 +3,7 @@ use crate::ClipboardProvider; use raw_window_handle::HasDisplayHandle; use std::error::Error; -pub fn connect( +pub fn connect( _window: &W, ) -> Result> { Clipboard::new() diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 734f8f6..755dfb9 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -11,10 +11,12 @@ 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), } @@ -22,6 +24,7 @@ impl ClipboardProvider for Clipboard { fn read(&self) -> Result> { match self { Clipboard::Wayland(c) => c.read(), + #[cfg(feature = "x11")] Clipboard::X11(c) => c.read().map_err(Box::from), } } @@ -29,6 +32,7 @@ impl ClipboardProvider for Clipboard { fn write(&mut self, contents: String) -> Result<(), Box> { match self { Clipboard::Wayland(c) => c.write(contents), + #[cfg(feature = "x11")] Clipboard::X11(c) => c.write(contents).map_err(Box::from), } } @@ -36,6 +40,7 @@ impl ClipboardProvider for Clipboard { fn read_primary(&self) -> Option>> { match self { Clipboard::Wayland(c) => Some(c.read_primary()), + #[cfg(feature = "x11")] Clipboard::X11(c) => Some(c.read_primary().map_err(Box::from)), } } @@ -46,6 +51,7 @@ impl ClipboardProvider for Clipboard { ) -> Option>> { 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)) } @@ -61,6 +67,7 @@ impl ClipboardProvider for Clipboard { let ret = c.read_data::>(); Some(ret.map(|ret| ret.0)) } + #[cfg(feature = "x11")] Clipboard::X11(_) => None, } } @@ -76,6 +83,7 @@ impl ClipboardProvider for Clipboard { Clipboard::Wayland(c) => { Some(c.write_data::>(contents)) } + #[cfg(feature = "x11")] Clipboard::X11(_) => None, } } @@ -89,6 +97,7 @@ impl ClipboardProvider for Clipboard { let ret = c.read_primary_data::>(); Some(ret.map(|ret| ret.0)) } + #[cfg(feature = "x11")] Clipboard::X11(_) => None, } } @@ -99,6 +108,7 @@ impl ClipboardProvider for Clipboard { ) -> Option, String), Box>> { match self { Clipboard::Wayland(c) => Some(c.read_primary_raw(allowed)), + #[cfg(feature = "x11")] Clipboard::X11(_) => None, } } @@ -109,6 +119,7 @@ impl ClipboardProvider for Clipboard { ) -> Option, String), Box>> { match self { Clipboard::Wayland(c) => Some(c.read_raw(allowed)), + #[cfg(feature = "x11")] Clipboard::X11(_) => None, } } @@ -124,6 +135,7 @@ impl ClipboardProvider for Clipboard { Clipboard::Wayland(c) => { Some(c.write_primary_data::>(contents)) } + #[cfg(feature = "x11")] Clipboard::X11(_) => None, } } @@ -135,7 +147,8 @@ impl DndProvider for Clipboard { tx: Box + Send + Sync + 'static>, ) { match self { - Clipboard::Wayland(c) => c.init_dnd(DndSender(Arc::new(tx))), + Clipboard::Wayland(c) => c.init_dnd(DndSender(Arc::from(tx))), + #[cfg(feature = "x11")] Clipboard::X11(_) => {} } } @@ -156,6 +169,7 @@ impl DndProvider for Clipboard { content, actions, ), + #[cfg(feature = "x11")] Clipboard::X11(_) => {} } } @@ -163,6 +177,7 @@ impl DndProvider for Clipboard { fn end_dnd(&self) { match self { Clipboard::Wayland(c) => c.end_dnd(), + #[cfg(feature = "x11")] Clipboard::X11(_) => {} } } @@ -176,6 +191,7 @@ impl DndProvider for Clipboard { Clipboard::Wayland(c) => { c.register_dnd_destination(surface, rectangles) } + #[cfg(feature = "x11")] Clipboard::X11(_) => {} } } @@ -183,6 +199,7 @@ impl DndProvider for Clipboard { fn set_action(&self, action: DndAction) { match self { Clipboard::Wayland(c) => c.set_action(action), + #[cfg(feature = "x11")] Clipboard::X11(_) => {} } } @@ -193,6 +210,7 @@ impl DndProvider for Clipboard { ) -> std::io::Result { match self { Clipboard::Wayland(c) => c.peek_offer::(mime_type), + #[cfg(feature = "x11")] Clipboard::X11(_) => Err(std::io::Error::new( std::io::ErrorKind::Other, "DnD not supported", @@ -201,14 +219,22 @@ impl DndProvider for Clipboard { } } -pub unsafe fn connect( +pub unsafe fn connect( window: &W, ) -> Result> { 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) diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 084ace8..d89c446 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -1,15 +1,57 @@ use crate::ClipboardProvider; +use crate::dnd::DndProvider; pub(crate) use clipboard_macos::Clipboard; +use dnd::{DndAction, DndDestinationRectangle, DndSurface, Icon}; +use mime::{AllowedMimeTypes, AsMimeTypes}; use raw_window_handle::HasDisplayHandle; -use std::error::Error; +use std::{borrow::Cow, error::Error}; -pub fn connect( +pub fn connect( _window: &W, ) -> Result> { Clipboard::new() } +impl DndProvider for Clipboard { + fn init_dnd( + &self, + _tx: Box + Send + Sync + 'static>, + ) { + } + + fn start_dnd( + &self, + _internal: bool, + _source_surface: DndSurface, + _icon_surface: Option, + _content: D, + _actions: DndAction, + ) { + } + + fn end_dnd(&self) {} + + fn register_dnd_destination( + &self, + _surface: DndSurface, + _rectangles: Vec, + ) { + } + + fn set_action(&self, _action: DndAction) {} + + fn peek_offer( + &self, + _mime_type: Option>, + ) -> std::io::Result { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + "DnD not supported", + )) + } +} + impl ClipboardProvider for Clipboard { fn read(&self) -> Result> { self.read() diff --git a/src/platform/windows.rs b/src/platform/windows.rs index f317b10..81d1e73 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -1,15 +1,55 @@ use crate::ClipboardProvider; +use crate::dnd::DndProvider; use clipboard_win::{get_clipboard_string, set_clipboard_string}; +use dnd::{DndAction, DndDestinationRectangle, DndSurface, Icon}; +use mime::{AllowedMimeTypes, AsMimeTypes}; use raw_window_handle::HasDisplayHandle; +use std::{borrow::Cow, error::Error}; -use std::error::Error; - -pub fn connect( +pub fn connect( _window: &W, ) -> Result> { Ok(Clipboard) } +impl DndProvider for Clipboard { + fn init_dnd( + &self, + _tx: Box + Send + Sync + 'static>, + ) { + } + + fn start_dnd( + &self, + _internal: bool, + _source_surface: DndSurface, + _icon_surface: Option, + _content: D, + _actions: DndAction, + ) { + } + + fn end_dnd(&self) {} + + fn register_dnd_destination( + &self, + _surface: DndSurface, + _rectangles: Vec, + ) { + } + + fn set_action(&self, _action: DndAction) {} + + fn peek_offer( + &self, + _mime_type: Option>, + ) -> std::io::Result { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + "DnD not supported", + )) + } +} pub struct Clipboard; diff --git a/wayland/Cargo.toml b/wayland/Cargo.toml index eafd122..fd1d4fc 100644 --- a/wayland/Cargo.toml +++ b/wayland/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["clipboard", "wayland"] [dependencies] -smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd-3", features = [ +smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "sctk-0.20", features = [ "dnd", ] } mime = { path = "../mime" } diff --git a/wayland/src/lib.rs b/wayland/src/lib.rs index deb59d9..298e87c 100644 --- a/wayland/src/lib.rs +++ b/wayland/src/lib.rs @@ -27,9 +27,7 @@ use smithay_clipboard::dnd::{Icon, Rectangle}; pub use smithay_clipboard::mime::{AllowedMimeTypes, AsMimeTypes, MimeType}; #[derive(Clone)] -pub struct DndSender( - pub Arc + 'static + Send + Sync>>, -); +pub struct DndSender(pub Arc + 'static + Send + Sync>); impl smithay_clipboard::dnd::Sender for DndSender { fn send(