From 05ced3b7eaf126035c8e6ad26699a2c874490be6 Mon Sep 17 00:00:00 2001 From: zeefaad Date: Sun, 8 Mar 2026 14:45:26 +0100 Subject: [PATCH] optimisations --- Cargo.lock | 196 +++++++++++++++++++++++++-------------- rklipd/src/database.rs | 33 ++----- rklipd/src/models.rs | 10 +- rklipd/src/ws.rs | 2 + rklipd/src/ws/wayland.rs | 41 +++++++- 5 files changed, 182 insertions(+), 100 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 158c06d..1153fb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,12 +133,6 @@ dependencies = [ "arrayvec", ] -[[package]] -name = "base64" -version = "0.22.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" - [[package]] name = "bit_field" version = "0.10.3" @@ -211,17 +205,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" -[[package]] -name = "chacha20" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" -dependencies = [ - "cfg-if", - "cpufeatures", - "rand_core 0.10.0", -] - [[package]] name = "clipboard-master" version = "4.0.0" @@ -259,15 +242,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "cpufeatures" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" -dependencies = [ - "libc", -] - [[package]] name = "crc32fast" version = "1.5.0" @@ -381,6 +355,18 @@ dependencies = [ "zune-inflate", ] +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fax" version = "0.2.6" @@ -432,6 +418,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "gethostname" version = "1.1.0" @@ -463,7 +455,6 @@ dependencies = [ "cfg-if", "libc", "r-efi 6.0.0", - "rand_core 0.10.0", "wasip2", "wasip3", ] @@ -495,7 +486,7 @@ version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ - "foldhash", + "foldhash 0.1.5", ] [[package]] @@ -503,6 +494,18 @@ name = "hashbrown" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash 0.2.0", +] + +[[package]] +name = "hashlink" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0b22561a9c04a7cb1a302c013e0259cd3b4bb619f145b32f72b8b4bcbed230" +dependencies = [ + "hashbrown 0.16.1", +] [[package]] name = "heck" @@ -604,6 +607,16 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "leb128fmt" version = "0.1.0" @@ -632,6 +645,16 @@ dependencies = [ "cc", ] +[[package]] +name = "libsqlite3-sys" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b4103cffefa72eb8428cb6b47d6627161e51c2739fc5e3b734584157bc642a" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -966,6 +989,12 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + [[package]] name = "png" version = "0.18.1" @@ -1075,18 +1104,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" dependencies = [ "rand_chacha", - "rand_core 0.9.5", -] - -[[package]] -name = "rand" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc266eb313df6c5c09c1c7b1fbe2510961e5bcd3add930c1e31f7ed9da0feff8" -dependencies = [ - "chacha20", - "getrandom 0.4.2", - "rand_core 0.10.0", + "rand_core", ] [[package]] @@ -1096,7 +1114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", - "rand_core 0.9.5", + "rand_core", ] [[package]] @@ -1108,12 +1126,6 @@ dependencies = [ "getrandom 0.3.4", ] -[[package]] -name = "rand_core" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" - [[package]] name = "rav1e" version = "0.8.1" @@ -1141,7 +1153,7 @@ dependencies = [ "num-traits", "paste", "profiling", - "rand 0.9.2", + "rand", "rand_chacha", "simd_helpers", "thiserror", @@ -1204,7 +1216,6 @@ name = "rklip" version = "0.1.0" dependencies = [ "arboard", - "rand 0.10.0", "rklipd", ] @@ -1213,12 +1224,35 @@ name = "rklipd" version = "0.1.0" dependencies = [ "arboard", - "base64", "clipboard-master", "image", - "serde", - "serde_json", - "serde_millis", + "rusqlite", + "uuid", +] + +[[package]] +name = "rsqlite-vfs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8a1f2315036ef6b1fbacd1972e8ee7688030b0a2121edfc2a6550febd41574d" +dependencies = [ + "hashbrown 0.16.1", + "thiserror", +] + +[[package]] +name = "rusqlite" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1c93dd1c9683b438c392c492109cb702b8090b2bfc8fed6f6e4eb4523f17af3" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", + "sqlite-wasm-rs", ] [[package]] @@ -1259,7 +1293,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", - "serde_derive", ] [[package]] @@ -1295,15 +1328,6 @@ dependencies = [ "zmij", ] -[[package]] -name = "serde_millis" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e2dc780ca5ee2c369d1d01d100270203c4ff923d2a4264812d723766434d00" -dependencies = [ - "serde", -] - [[package]] name = "shlex" version = "1.3.0" @@ -1331,6 +1355,18 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4206ed3a67690b9c29b77d728f6acc3ce78f16bf846d83c94f76400320181b" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -1394,6 +1430,18 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "uuid" +version = "1.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a68d3c8f01c0cfa54a75291d83601161799e4a89a39e0929f4b0354d88757a37" +dependencies = [ + "getrandom 0.4.2", + "js-sys", + "serde_core", + "wasm-bindgen", +] + [[package]] name = "v_frame" version = "0.3.9" @@ -1405,6 +1453,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "wasip2" version = "1.0.2+wasi-0.2.9" @@ -1425,9 +1479,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" +checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e" dependencies = [ "cfg-if", "once_cell", @@ -1438,9 +1492,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1448,9 +1502,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3" dependencies = [ "bumpalo", "proc-macro2", @@ -1461,9 +1515,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16" dependencies = [ "unicode-ident", ] diff --git a/rklipd/src/database.rs b/rklipd/src/database.rs index 856cb4c..52496dd 100644 --- a/rklipd/src/database.rs +++ b/rklipd/src/database.rs @@ -2,6 +2,7 @@ use crate::models::{ClipboardData, ClipboardEntry, Image}; use rusqlite::Connection; use std::error::Error; use std::fs; +use std::path::Path; use std::time::{Duration, UNIX_EPOCH}; use uuid::Uuid; @@ -12,20 +13,12 @@ pub struct Database { impl Database { pub fn init(dir_path: &str) -> Result> { - if !std::fs::exists(dir_path)? { - fs::create_dir(dir_path)?; - } else { - println!("{:?} dir already exists.", { dir_path }); - } + let base_path = Path::new(dir_path); + let images_path = base_path.join("images"); + std::fs::create_dir_all(&images_path)?; - let image_path = format!("{}/images", dir_path); - if !std::fs::exists(&image_path)? { - fs::create_dir(&image_path)?; - } else { - println!("{:?} dir already exists.", { image_path }); - } + let db_path = base_path.join("clipboard.db"); - let db_path = format!("{}/clipboard.db", dir_path); let conn = Connection::open(&db_path)?; conn.execute( @@ -51,7 +44,9 @@ impl Database { ClipboardData::Text(text) => ("text", text.clone()), ClipboardData::Image(img) => { if let Some(bytes) = &img.bytes { - let img_path = format!("{}/images/{}.png", self.dir_path, img.id); + let img_path = Path::new(&self.dir_path) + .join("images") + .join(format!("{}.png", img.id)); fs::write(&img_path, bytes)?; } ("image", img.id.to_string()) @@ -91,18 +86,6 @@ impl Database { ClipboardData::Image(Image { id, bytes: None }) }; - // let data = if ty == "text" { - // ClipboardData::Text(content) - // } else { - // let id = Uuid::parse_str(&content)?; - // let img_path = format!("{}/images/{}.png", self.dir_path, id); - // let bytes = fs::read(&img_path).unwrap_or_default(); - // ClipboardData::Image(Image { - // bytes: Some(bytes), - // id, - // }) - // }; - entries.push(ClipboardEntry { content: data, timestamp, diff --git a/rklipd/src/models.rs b/rklipd/src/models.rs index 3124587..037094b 100644 --- a/rklipd/src/models.rs +++ b/rklipd/src/models.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::time::SystemTime; use std::{fs, io}; use uuid::Uuid; @@ -21,11 +22,16 @@ pub struct Image { } impl Image { + pub fn file_path(&self, base_dir: &str) -> PathBuf { + std::path::Path::new(base_dir) + .join("images") + .join(format!("{}.png", self.id)) + } + pub fn load_bytes(&self, dir_path: &str) -> io::Result> { if let Some(b) = &self.bytes { return Ok(b.clone()); } - let img_path = format!("{}/images/{}.png", dir_path, self.id); - fs::read(img_path) + fs::read(self.file_path(dir_path)) } } diff --git a/rklipd/src/ws.rs b/rklipd/src/ws.rs index 515d9e2..66d5250 100644 --- a/rklipd/src/ws.rs +++ b/rklipd/src/ws.rs @@ -1,2 +1,4 @@ +#[cfg(feature = "wayland")] pub mod wayland; +#[cfg(feature = "x11")] pub mod x11; diff --git a/rklipd/src/ws/wayland.rs b/rklipd/src/ws/wayland.rs index 9fe77ee..58488d8 100644 --- a/rklipd/src/ws/wayland.rs +++ b/rklipd/src/ws/wayland.rs @@ -1,7 +1,44 @@ -use crate::database::Database; +use crate::{database::Database, models::ClipboardEntry}; use arboard::Clipboard; -use std::error::Error; +use std::time::Duration; +use std::{error::Error, sync::mpsc::channel}; +use wayland_clipboard_listener::{WlClipboardPasteStream, WlListenType}; pub fn start(db: Database, mut clipboard: Clipboard) -> Result<(), Box> { + let (tx, rx) = channel(); + + std::thread::spawn( + move || match WlClipboardPasteStream::init(WlListenType::ListenOnCopy) { + Ok(mut stream) => { + for _ in stream.paste_stream().flatten() { + std::thread::sleep(Duration::new(1, 0)); + if let Err(e) = tx.send(()) { + eprintln!("{}", e); + break; + } + } + } + Err(e) => { + eprintln!("{}", 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!"); + // } + // } + // + match ClipboardEntry::new(&mut clipboard) { + Ok(entry) => db.append(entry)?, + Err(e) => eprintln!("{}", e), + } + } + Ok(()) }