From f27e83b4674d62aef5be5a5554c6a09ad24c8f8b Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Fri, 7 Nov 2025 00:55:26 +0100 Subject: [PATCH] Use `smithay-clipboard` crate for clipboard support on Wayland Fixes #8 --- CHANGELOG.md | 1 + Cargo.lock | 199 +++++++++++++++++++++---------------------- Cargo.toml | 6 +- src/api/clipboard.rs | 13 +-- src/app.rs | 3 - src/clipboard.rs | 81 ++++++++++++++++++ src/lua.rs | 16 ++-- src/main.rs | 1 + src/window.rs | 28 +++++- 9 files changed, 220 insertions(+), 128 deletions(-) create mode 100644 src/clipboard.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c34ed3..0658821 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - Replace non-ASCII characters in text input with '?' +- Use `smithay-clipboard` crate for clipboard support on Wayland ## [0.2.7] - 2025-11-02 diff --git a/Cargo.lock b/Cargo.lock index ff09acd..1c4cc0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,7 +173,6 @@ dependencies = [ "parking_lot", "percent-encoding", "windows-sys 0.60.2", - "wl-clipboard-rs", "x11rb", ] @@ -242,7 +241,7 @@ dependencies = [ "anyhow", "arrayvec", "log", - "nom 8.0.0", + "nom", "num-rational", "v_frame", ] @@ -387,18 +386,43 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "calloop" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb9f6e1368bd4621d2c86baa7e37de77a938adf5221e5dd3d6133340101b309e" +dependencies = [ + "bitflags 2.10.0", + "polling", + "rustix 1.1.2", + "slab", + "tracing", +] + [[package]] name = "calloop-wayland-source" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20" dependencies = [ - "calloop", + "calloop 0.13.0", "rustix 0.38.44", "wayland-backend", "wayland-client", ] +[[package]] +name = "calloop-wayland-source" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138efcf0940a02ebf0cc8d1eff41a1682a46b431630f4c52450d6265876021fa" +dependencies = [ + "calloop 0.14.3", + "rustix 1.1.2", + "wayland-backend", + "wayland-client", +] + [[package]] name = "cc" version = "1.2.43" @@ -803,12 +827,6 @@ dependencies = [ "thiserror 2.0.17", ] -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - [[package]] name = "fdeflate" version = "0.3.7" @@ -836,12 +854,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127" -[[package]] -name = "fixedbitset" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" - [[package]] name = "flate2" version = "1.1.5" @@ -1397,12 +1409,6 @@ dependencies = [ "libc", ] -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - [[package]] name = "miniz_oxide" version = "0.8.9" @@ -1518,16 +1524,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - [[package]] name = "nom" version = "8.0.0" @@ -1943,16 +1939,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "os_pipe" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" -dependencies = [ - "libc", - "windows-sys 0.61.2", -] - [[package]] name = "owned_ttf_parser" version = "0.25.1" @@ -2017,16 +2003,6 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" -[[package]] -name = "petgraph" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" -dependencies = [ - "fixedbitset", - "indexmap", -] - [[package]] name = "pin-project" version = "1.1.10" @@ -2555,7 +2531,9 @@ dependencies = [ "pollster", "profiling", "puffin_http", + "raw-window-handle", "regex", + "smithay-clipboard", "swash", "tar", "ureq", @@ -2594,7 +2572,7 @@ dependencies = [ "ab_glyph", "log", "memmap2", - "smithay-client-toolkit", + "smithay-client-toolkit 0.19.2", "tiny-skia", ] @@ -2687,8 +2665,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" dependencies = [ "bitflags 2.10.0", - "calloop", - "calloop-wayland-source", + "calloop 0.13.0", + "calloop-wayland-source 0.3.0", "cursor-icon", "libc", "log", @@ -2705,6 +2683,44 @@ dependencies = [ "xkeysym", ] +[[package]] +name = "smithay-client-toolkit" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0512da38f5e2b31201a93524adb8d3136276fa4fe4aafab4e1f727a82b534cc0" +dependencies = [ + "bitflags 2.10.0", + "calloop 0.14.3", + "calloop-wayland-source 0.4.1", + "cursor-icon", + "libc", + "log", + "memmap2", + "rustix 1.1.2", + "thiserror 2.0.17", + "wayland-backend", + "wayland-client", + "wayland-csd-frame", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-experimental", + "wayland-protocols-misc", + "wayland-protocols-wlr", + "wayland-scanner", + "xkeysym", +] + +[[package]] +name = "smithay-clipboard" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71704c03f739f7745053bde45fa203a46c58d25bc5c4efba1d9a60e9dba81226" +dependencies = [ + "libc", + "smithay-client-toolkit 0.20.0", + "wayland-backend", +] + [[package]] name = "smol_str" version = "0.2.2" @@ -2799,19 +2815,6 @@ version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" -[[package]] -name = "tempfile" -version = "3.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" -dependencies = [ - "fastrand", - "getrandom 0.3.4", - "once_cell", - "rustix 1.1.2", - "windows-sys 0.61.2", -] - [[package]] name = "termcolor" version = "1.4.1" @@ -2967,6 +2970,7 @@ version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ + "log", "pin-project-lite", "tracing-core", ] @@ -2977,18 +2981,6 @@ version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9d12581f227e93f094d3af2ae690a574abb8a2b9b7a96e7cfe9647b2b617678" -[[package]] -name = "tree_magic_mini" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f943391d896cdfe8eec03a04d7110332d445be7df856db382dd96a730667562c" -dependencies = [ - "memchr", - "nom 7.1.3", - "once_cell", - "petgraph", -] - [[package]] name = "ttf-parser" version = "0.25.1" @@ -3227,6 +3219,32 @@ dependencies = [ "wayland-scanner", ] +[[package]] +name = "wayland-protocols-experimental" +version = "20250721.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40a1f863128dcaaec790d7b4b396cc9b9a7a079e878e18c47e6c2d2c5a8dcbb1" +dependencies = [ + "bitflags 2.10.0", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-misc" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfe33d551eb8bffd03ff067a8b44bb963919157841a99957151299a6307d19c" +dependencies = [ + "bitflags 2.10.0", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + [[package]] name = "wayland-protocols-plasma" version = "0.3.9" @@ -3737,7 +3755,7 @@ dependencies = [ "bitflags 2.10.0", "block2", "bytemuck", - "calloop", + "calloop 0.13.0", "cfg_aliases", "concurrent-queue", "core-foundation", @@ -3759,7 +3777,7 @@ dependencies = [ "redox_syscall 0.4.1", "rustix 0.38.44", "sctk-adwaita", - "smithay-client-toolkit", + "smithay-client-toolkit 0.19.2", "smol_str", "tracing", "unicode-segmentation", @@ -3792,25 +3810,6 @@ version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" -[[package]] -name = "wl-clipboard-rs" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5ff8d0e60065f549fafd9d6cb626203ea64a798186c80d8e7df4f8af56baeb" -dependencies = [ - "libc", - "log", - "os_pipe", - "rustix 0.38.44", - "tempfile", - "thiserror 2.0.17", - "tree_magic_mini", - "wayland-backend", - "wayland-client", - "wayland-protocols", - "wayland-protocols-wlr", -] - [[package]] name = "writeable" version = "0.6.2" diff --git a/Cargo.toml b/Cargo.toml index 8466ffa..e49311e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ publish = false [dependencies] ahash = "0.8.12" anyhow = "1.0" -arboard = { version = "3.6.1", default-features = false, features = ["wayland-data-control"] } +arboard = { version = "3.6.1", default-features = false } bytemuck = "1.23.2" clap = { version = "4.5.46", features = ["derive"] } dds = "0.1.0" @@ -32,6 +32,7 @@ parley = "0.6.0" pollster = "0.3" profiling = "1.0" puffin_http = { version = "0.16", optional = true } +raw-window-handle = "0.6.2" regex = "1.11.2" swash = "0.2.5" tar = "0.4.44" @@ -40,5 +41,8 @@ wgpu = { version = "27.0.1", default-features = false, features = ["std", "parki winit = "0.30" zstd = "0.13.3" +[target.'cfg(unix)'.dependencies] +smithay-clipboard = "0.7.3" + [features] profile-with-puffin = ["profiling/profile-with-puffin", "dep:puffin_http"] diff --git a/src/api/clipboard.rs b/src/api/clipboard.rs index a5d0b73..6937dcb 100644 --- a/src/api/clipboard.rs +++ b/src/api/clipboard.rs @@ -3,18 +3,11 @@ use mlua::{Lua, Result as LuaResult}; pub fn copy(l: &Lua, text: String) -> LuaResult<()> { let ctx = l.app_data_ref::<&'static Context>().unwrap(); - ctx - .clipboard() - .set_text(text) - .map_err(|e| anyhow::anyhow!("{}", e))?; + ctx.window().set_clipboard_text(text); Ok(()) } -pub fn paste(l: &Lua, _: ()) -> LuaResult { +pub fn paste(l: &Lua, _: ()) -> LuaResult> { let ctx = l.app_data_ref::<&'static Context>().unwrap(); - let text = ctx - .clipboard() - .get_text() - .map_err(|e| anyhow::anyhow!("{}", e))?; - Ok(text) + Ok(ctx.window().get_clipboard_text()) } diff --git a/src/app.rs b/src/app.rs index 4adf385..5b09d02 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,7 +11,6 @@ use crate::{ window::WindowState, }; use anyhow::Result; -use arboard::Clipboard; use std::sync::Arc; use winit::{ application::ApplicationHandler, event::*, event_loop::ActiveEventLoop, keyboard::PhysicalKey, @@ -27,7 +26,6 @@ pub struct AppState { pub input: InputState, pub fonts: Fonts, pub texture_manager: WrappedTextureManager, - pub clipboard: Clipboard, } impl AppState { @@ -54,7 +52,6 @@ impl App { input: InputState::default(), fonts: Fonts::new(pob_font_definitions()), texture_manager: WrappedTextureManager::new(), - clipboard: arboard::Clipboard::new()?, }, tessellator: Tessellator::default(), force_render: false, diff --git a/src/clipboard.rs b/src/clipboard.rs new file mode 100644 index 0000000..50bbd85 --- /dev/null +++ b/src/clipboard.rs @@ -0,0 +1,81 @@ +use raw_window_handle::RawDisplayHandle; + +/// Abstraction over clipboard crates +/// +/// `smithay_clipboard`: Used on wayland +/// `arboard`: All other platforms (check arboard docs for platform support) +pub struct Clipboard { + #[cfg(target_family = "unix")] + smithay: Option, + arboard: Option, + // fallback if everything else fails. only supports intra-application copy/paste + fallback: Option, +} + +impl Clipboard { + pub fn new(_raw_display_handle: Option) -> Self { + Self { + #[cfg(target_family = "unix")] + smithay: create_smithay_clipboard(_raw_display_handle), + arboard: create_arboard_clipboard(), + fallback: None, + } + } + + /// Sets the text content of clipboard + pub fn set_text(&mut self, text: String) { + #[cfg(target_family = "unix")] + if let Some(clipboard) = &mut self.smithay { + clipboard.store(text); + return; + } + + if let Some(clipboard) = &mut self.arboard { + let _ = clipboard.set_text(text); + return; + } + + self.fallback = Some(text); + } + + /// Gets the text content of clipboard + pub fn get_text(&mut self) -> Option { + #[cfg(target_family = "unix")] + if let Some(clipboard) = &mut self.smithay { + return clipboard.load().ok(); + } + + if let Some(clipboard) = &mut self.arboard { + return clipboard.get_text().ok(); + } + + if let Some(text) = &self.fallback { + return Some(text.clone()); + } + + None + } +} + +#[cfg(target_family = "unix")] +fn create_smithay_clipboard( + raw_display_handle: Option, +) -> Option { + if let Some(RawDisplayHandle::Wayland(handle)) = raw_display_handle { + log::debug!("Creating smithay clipboard..."); + Some(unsafe { smithay_clipboard::Clipboard::new(handle.display.as_ptr()) }) + } else { + None + } +} + +fn create_arboard_clipboard() -> Option { + log::debug!("Creating arboard clipboard..."); + match arboard::Clipboard::new() { + Ok(clipboard) => Some(clipboard), + Err(err) => { + log::warn!("Failed to create arboard clipboard: {err}"); + None + } + } +} diff --git a/src/lua.rs b/src/lua.rs index a97c03d..f05b123 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -12,7 +12,6 @@ use crate::{ util::change_working_directory, window::WindowState, }; -use arboard::Clipboard; use clap::Parser; use mlua::{Lua, Result as LuaResult, Table}; use std::{ @@ -45,11 +44,10 @@ macro_rules! ctx_accessor { /// Before executing any lua code, we need to "plug" the references into /// the Context and "unplug" them afterwards. pub struct Context { - window: Cell<*const WindowState>, + window: Cell<*mut WindowState>, input: Cell<*const InputState>, fonts: Cell<*mut Fonts>, texture_manager: Cell<*mut WrappedTextureManager>, - clipboard: Cell<*mut Clipboard>, current_working_dir: Cell<*mut PathBuf>, layers: Cell<*mut Layers>, needs_restart: Cell<*mut bool>, @@ -59,11 +57,10 @@ pub struct Context { impl Context { pub fn new() -> &'static Self { Box::leak(Box::new(Self { - window: Cell::new(std::ptr::null()), + window: Cell::new(std::ptr::null_mut()), input: Cell::new(std::ptr::null()), fonts: Cell::new(std::ptr::null_mut()), texture_manager: Cell::new(std::ptr::null_mut()), - clipboard: Cell::new(std::ptr::null_mut()), current_working_dir: Cell::new(std::ptr::null_mut()), layers: Cell::new(std::ptr::null_mut()), needs_restart: Cell::new(std::ptr::null_mut()), @@ -72,11 +69,10 @@ impl Context { } pub fn set(&self, ctx: &mut PoBContext) { - self.window.set(&ctx.app.window); + self.window.set(&mut ctx.app.window); self.input.set(&ctx.app.input); self.fonts.set(&mut ctx.app.fonts); self.texture_manager.set(&mut ctx.app.texture_manager); - self.clipboard.set(&mut ctx.app.clipboard); self.current_working_dir .set(&mut ctx.pob.current_working_dir); self.layers.set(&mut ctx.pob.layers); @@ -85,22 +81,20 @@ impl Context { } pub fn clear(&self) { - self.window.set(std::ptr::null()); + self.window.set(std::ptr::null_mut()); self.input.set(std::ptr::null()); self.fonts.set(std::ptr::null_mut()); self.texture_manager.set(std::ptr::null_mut()); - self.clipboard.set(std::ptr::null_mut()); self.current_working_dir.set(std::ptr::null_mut()); self.layers.set(std::ptr::null_mut()); self.needs_restart.set(std::ptr::null_mut()); self.is_dpi_aware.set(std::ptr::null_mut()); } - ctx_accessor!(window: &WindowState); + ctx_accessor!(window: &mut WindowState); ctx_accessor!(input: &InputState); ctx_accessor!(fonts: &mut Fonts); ctx_accessor!(texture_manager: &mut WrappedTextureManager); - ctx_accessor!(clipboard: &mut Clipboard); ctx_accessor!(current_working_dir: &mut PathBuf); ctx_accessor!(layers: &mut Layers); ctx_accessor!(needs_restart: &mut bool); diff --git a/src/main.rs b/src/main.rs index f8bc11b..0759043 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use winit::event_loop::EventLoop; mod api; mod app; mod args; +mod clipboard; mod color; mod dpi; mod fonts; diff --git a/src/window.rs b/src/window.rs index a8a218c..682b30f 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,10 +1,14 @@ +use crate::{ + clipboard::Clipboard, + dpi::{ConvertToLogical, LogicalSize, PhysicalSize}, +}; +use raw_window_handle::HasDisplayHandle; use std::sync::Arc; - use winit::window::Window; -use crate::dpi::{ConvertToLogical, LogicalSize, PhysicalSize}; - pub struct WindowState { + // NOTE: clipboard needs to be destroyed before window + clipboard: Option, pub window: Option>, pub size: PhysicalSize, pub scale_factor: f32, @@ -18,6 +22,7 @@ impl Default for WindowState { size: Default::default(), scale_factor: 1.0, pending_window_title: std::cell::Cell::new(None), + clipboard: None, } } } @@ -31,6 +36,9 @@ impl WindowState { let winit::dpi::PhysicalSize { width, height } = window.inner_size(); self.size = PhysicalSize::new(width, height); self.scale_factor = window.scale_factor() as f32; + + let raw_display_handle = window.display_handle().ok().map(|h| h.as_raw()); + self.clipboard = Some(Clipboard::new(raw_display_handle)); self.window = Some(window); } @@ -51,4 +59,18 @@ impl WindowState { window.focus_window(); } } + + pub fn set_clipboard_text(&mut self, text: String) { + if let Some(clipboard) = &mut self.clipboard { + clipboard.set_text(text); + } + } + + pub fn get_clipboard_text(&mut self) -> Option { + if let Some(clipboard) = &mut self.clipboard { + clipboard.get_text() + } else { + None + } + } }