diff --git a/rklipd/Cargo.lock b/rklipd/Cargo.lock index dddd55f..c77e831 100644 --- a/rklipd/Cargo.lock +++ b/rklipd/Cargo.lock @@ -292,6 +292,12 @@ dependencies = [ "objc2", ] +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + [[package]] name = "either" version = "1.15.0" @@ -948,6 +954,16 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[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 = "parking_lot" version = "0.12.5" @@ -1076,6 +1092,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" +[[package]] +name = "quick-xml" +version = "0.39.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "958f21e8e7ceb5a1aa7fa87fab28e7c75976e0bfe7e23ff069e0a260f894067d" +dependencies = [ + "memchr", +] + [[package]] name = "quote" version = "1.0.45" @@ -1220,6 +1245,7 @@ dependencies = [ "image", "rusqlite", "uuid", + "wayland-clipboard-listener", ] [[package]] @@ -1548,6 +1574,76 @@ dependencies = [ "semver", ] +[[package]] +name = "wayland-backend" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa75f400b7f719bcd68b3f47cd939ba654cedeef690f486db71331eec4c6a406" +dependencies = [ + "cc", + "downcast-rs", + "rustix", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab51d9f7c071abeee76007e2b742499e535148035bb835f97aaed1338cf516c3" +dependencies = [ + "bitflags", + "rustix", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-clipboard-listener" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e27b4464dcbc041268b018226dceda1ce94ca9d1a020fe47c251fa3de88e7706" +dependencies = [ + "log", + "os_pipe", + "thiserror", + "wayland-client", + "wayland-protocols", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b23b5df31ceff1328f06ac607591d5ba360cf58f90c8fad4ac8d3a55a3c4aec7" +dependencies = [ + "bitflags", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c86287151a309799b821ca709b7345a048a2956af05957c89cb824ab919fa4e3" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", +] + +[[package]] +name = "wayland-sys" +version = "0.31.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374f6b70e8e0d6bf9461a32988fd553b59ff630964924dad6e4a4eb6bd538d17" +dependencies = [ + "pkg-config", +] + [[package]] name = "weezl" version = "0.1.12" diff --git a/rklipd/Cargo.toml b/rklipd/Cargo.toml index 304f1ec..0b8ada4 100644 --- a/rklipd/Cargo.toml +++ b/rklipd/Cargo.toml @@ -9,6 +9,7 @@ image = "0.25.9" clipboard-master = "4.0.0" uuid = {version = "1.22.0", features = ["v4", "serde"]} rusqlite = "0.38.0" +wayland-clipboard-listener = "0.6.0" [features] x11 = [] diff --git a/rklipd/src/clipboard.rs b/rklipd/src/clipboard.rs index ed4910a..121da97 100644 --- a/rklipd/src/clipboard.rs +++ b/rklipd/src/clipboard.rs @@ -6,22 +6,6 @@ use uuid::Uuid; use crate::models::{ClipboardData, ClipboardEntry, Image}; -use clipboard_master::{CallbackResult, ClipboardHandler}; -use std::sync::mpsc::Sender; - -pub struct Handler { - pub clipboard_tx: Sender<()>, -} - -impl ClipboardHandler for Handler { - fn on_clipboard_change(&mut self) -> CallbackResult { - if let Err(e) = self.clipboard_tx.send(()) { - eprintln!("{}", e); - } - CallbackResult::Next - } -} - pub trait ImageDataExt { fn to_png(&self) -> Result, Box>; } diff --git a/rklipd/src/main.rs b/rklipd/src/main.rs index ad94010..d57ef35 100644 --- a/rklipd/src/main.rs +++ b/rklipd/src/main.rs @@ -1,11 +1,11 @@ -use arboard::Clipboard; - use crate::database::Database; +use arboard::Clipboard; mod clipboard; mod database; mod models; mod monitor; +mod ws; fn main() -> Result<(), Box> { let clipboard = Clipboard::new()?; @@ -14,7 +14,7 @@ fn main() -> Result<(), Box> { let db = Database::init(dir_path)?; - println!("{:#?}", db.read_history()); + // println!("{:#?}", db.read_history()); monitor::start(db, clipboard)?; diff --git a/rklipd/src/monitor.rs b/rklipd/src/monitor.rs index a8e1ffd..8bd973f 100644 --- a/rklipd/src/monitor.rs +++ b/rklipd/src/monitor.rs @@ -1,32 +1,24 @@ -use std::{error::Error, sync::mpsc::channel}; - -use arboard::Clipboard; -use clipboard_master::Master; - -use crate::clipboard::Handler; use crate::database::Database; -use crate::models::ClipboardEntry; - -pub fn start(db: Database, mut clipboard: Clipboard) -> Result<(), Box> { - let (tx, rx) = channel(); - - let mut master = Master::new(Handler { clipboard_tx: tx })?; - std::thread::spawn(move || { - if let Err(e) = master.run() { - eprintln!("Clipboard monitor error : {}", e); - } - }); - - for _ in rx { - print!("Clipboard changed!"); - if let Ok(entry) = ClipboardEntry::new(&mut clipboard) { - if let Err(e) = db.append(entry) { - eprintln!("SQLite writing error: {}", e); - } else { - println!("SQLite edited!"); - } - } - } +use arboard::Clipboard; +use std::error::Error; +#[cfg(feature = "x11")] +use crate::ws; +#[cfg(feature = "x11")] +pub fn start(db: Database, clipboard: Clipboard) -> Result<(), Box> { + ws::x11::start(db, clipboard)?; Ok(()) } + +#[cfg(feature = "wayland")] +use crate::ws; +#[cfg(feature = "wayland")] +pub fn start(db: Database, clipboard: Clipboard) -> Result<(), Box> { + ws::wayland::start(db, clipboard)?; + Ok(()) +} + +#[cfg(not(any(feature = "x11", feature = "wayland")))] +pub fn start(_db: Database, _clipboard: Clipboard) -> Result<(), Box> { + Err("No window system feature enabled".into()) +} diff --git a/rklipd/src/ws.rs b/rklipd/src/ws.rs new file mode 100644 index 0000000..515d9e2 --- /dev/null +++ b/rklipd/src/ws.rs @@ -0,0 +1,2 @@ +pub mod wayland; +pub mod x11; diff --git a/rklipd/src/ws/wayland.rs b/rklipd/src/ws/wayland.rs new file mode 100644 index 0000000..9fe77ee --- /dev/null +++ b/rklipd/src/ws/wayland.rs @@ -0,0 +1,7 @@ +use crate::database::Database; +use arboard::Clipboard; +use std::error::Error; + +pub fn start(db: Database, mut clipboard: Clipboard) -> Result<(), Box> { + Ok(()) +} diff --git a/rklipd/src/ws/x11.rs b/rklipd/src/ws/x11.rs new file mode 100644 index 0000000..8db4a21 --- /dev/null +++ b/rklipd/src/ws/x11.rs @@ -0,0 +1,45 @@ +use arboard::Clipboard; +use clipboard_master::{CallbackResult, ClipboardHandler, Master}; +use std::{ + error::Error, + sync::mpsc::{Sender, channel}, +}; + +use crate::{database::Database, models::ClipboardEntry}; + +pub struct Handler { + pub clipboard_tx: Sender<()>, +} + +impl ClipboardHandler for Handler { + fn on_clipboard_change(&mut self) -> CallbackResult { + if let Err(e) = self.clipboard_tx.send(()) { + eprintln!("{}", e); + } + CallbackResult::Next + } +} + +pub fn start(db: Database, mut clipboard: Clipboard) -> Result<(), Box> { + let (tx, rx) = channel(); + + let mut master = Master::new(Handler { clipboard_tx: tx })?; + std::thread::spawn(move || { + if let Err(e) = master.run() { + eprintln!("Clipboard monitor error : {}", e); + } + }); + + for _ in rx { + println!("Clipboard update!"); + if let Ok(entry) = ClipboardEntry::new(&mut clipboard) { + if let Err(e) = db.append(entry) { + eprintln!("SQLite writing error: {}", e); + } else { + // println!("SQLite edited!"); + } + } + } + + Ok(()) +}