Skip to content

An open-source remote desktop application designed for self-hosting, as an alternative to TeamViewer.

Merge branch 'feat/x11/clipboard-file/init' into feat/osx/clipboard-file

ClSlaid62563ad8a1a2
diff --cc libs/clipboard/Cargo.toml
index a05eab3fc,182e73002..36f783fc1
--- a/libs/clipboard/Cargo.toml
+++ b/libs/clipboard/Cargo.toml
@@@ -9,6 -9,20 +9,21 @@@ build = "build.rs
[build-dependencies]
cc = "1.0"
+ [features]
+ default = []
+ unix-file-copy-paste = [
+ "dep:x11rb",
+ "dep:x11-clipboard",
+ "dep:rand",
+ "dep:fuser",
+ "dep:libc",
+ "dep:dashmap",
+ "dep:percent-encoding",
+ "dep:utf16string",
-"dep:once_cell"
++"dep:once_cell",
++"dep:cacao"
+ ]
+
[dependencies]
thiserror = "1.0"
lazy_static = "1.4"
@@@ -18,17 -32,12 +33,18 @@@ hbb_common = { path = "../hbb_common"
parking_lot = {version = "0.12"}
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
- rand = {version = "0.8"}
- fuser = {version = "0.13"}
- libc = {version = "0.2"}
- dashmap = "5.5"
- percent-encoding = "2.3"
- utf16string = "0.2"
- once_cell = "1.18"
-once_cell = {version = "1.18", optional = true}
+ x11rb = {version = "0.12", features = ["all-extensions"], optional = true}
+ rand = {version = "0.8", optional = true}
+ fuser = {version = "0.13", optional = true}
+ libc = {version = "0.2", optional = true}
+ dashmap = {version ="5.5", optional = true}
-percent-encoding = {version ="2.3", optional = true}
+ utf16string = {version = "0.2", optional = true}
++once_cell = {version = "1.18", optional = true}
+
+[target.'cfg(target_os = "linux")'.dependencies]
- x11-clipboard = {git="https://github.com/clslaid/x11-clipboard", branch = "feat/store-batch"}
- x11rb = {version = "0.12", features = ["all-extensions"]}
++percent-encoding = {version ="2.3", optional = true}
+ x11-clipboard = {git="https://github.com/clslaid/x11-clipboard", branch = "feat/store-batch", optional = true}
++x11rb = {version = "0.12", features = ["all-extensions"], optional = true}
+
+[target.'cfg(target_os = "macos")'.dependencies]
- cacao = {git="https://github.com/clslaid/cacao", branch = "feat/set-file-urls"}
++cacao = {git="https://github.com/clslaid/cacao", branch = "feat/set-file-urls", optional = true}
diff --cc libs/clipboard/src/platform/mod.rs
index afc8724c3,9f76fa8bb..0a2988d2c
--- a/libs/clipboard/src/platform/mod.rs
+++ b/libs/clipboard/src/platform/mod.rs
@@@ -21,38 -23,43 +23,44 @@@ pub mod fuse
pub mod unix;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn create_cliprdr_context(
- enable_files: bool,
+ _enable_files: bool,
_enable_others: bool,
- response_wait_timeout_secs: u32,
+ _response_wait_timeout_secs: u32,
) -> crate::ResultType<Box<dyn crate::CliprdrServiceContext>> {
- use std::{fs::Permissions, os::unix::prelude::PermissionsExt};
+ #[cfg(feature = "unix-file-copy-paste")]
+ {
+ use std::{fs::Permissions, os::unix::prelude::PermissionsExt};
- use hbb_common::{config::APP_NAME, log};
+ use hbb_common::{config::APP_NAME, log};
- if !enable_files {
- return Ok(Box::new(DummyCliprdrContext {}) as Box<_>);
- }
+ if !_enable_files {
+ return Ok(Box::new(DummyCliprdrContext {}) as Box<_>);
+ }
- let timeout = std::time::Duration::from_secs(response_wait_timeout_secs as u64);
+ let timeout = std::time::Duration::from_secs(_response_wait_timeout_secs as u64);
- let app_name = APP_NAME.read().unwrap().clone();
+ let app_name = APP_NAME.read().unwrap().clone();
- let mnt_path = format!("/tmp/{}/{}", app_name, "cliprdr");
+ let mnt_path = format!("/tmp/{}/{}", app_name, "cliprdr");
- // this function must be called after the main IPC is up
- std::fs::create_dir(&mnt_path).ok();
- std::fs::set_permissions(&mnt_path, Permissions::from_mode(0o777)).ok();
+ // this function must be called after the main IPC is up
+ std::fs::create_dir(&mnt_path).ok();
+ std::fs::set_permissions(&mnt_path, Permissions::from_mode(0o777)).ok();
- log::info!("clear previously mounted cliprdr FUSE");
- if let Err(e) = std::process::Command::new("umount").arg(&mnt_path).status() {
- log::warn!("umount {:?} may fail: {:?}", mnt_path, e);
- }
+ log::info!("clear previously mounted cliprdr FUSE");
+ if let Err(e) = std::process::Command::new("umount").arg(&mnt_path).status() {
+ log::warn!("umount {:?} may fail: {:?}", mnt_path, e);
+ }
- let unix_ctx = unix::ClipboardContext::new(timeout, mnt_path.parse().unwrap())?;
- log::debug!("start cliprdr FUSE");
- unix_ctx.run().expect("failed to start cliprdr FUSE");
+ let unix_ctx = unix::ClipboardContext::new(timeout, mnt_path.parse().unwrap())?;
+ log::debug!("start cliprdr FUSE");
+ unix_ctx.run().expect("failed to start cliprdr FUSE");
- Ok(Box::new(unix_ctx) as Box<_>)
+ Ok(Box::new(unix_ctx) as Box<_>)
+ }
++
+ #[cfg(not(feature = "unix-file-copy-paste"))]
+ return Ok(Box::new(DummyCliprdrContext {}) as Box<_>);
}
struct DummyCliprdrContext {}
@@@ -73,7 -80,7 +81,8 @@@ impl CliprdrServiceContext for DummyCli
}
}
+ #[cfg(feature = "unix-file-copy-paste")]
++#[cfg(any(target_os = "linux", target_os = "macos"))]
// begin of epoch used by microsoft
// 1601-01-01 00:00:00 + LDAP_EPOCH_DELTA*(100 ns) = 1970-01-01 00:00:00
- #[cfg(any(target_os = "linux", target_os = "macos"))]
const LDAP_EPOCH_DELTA: u64 = 116444772610000000;
diff --cc libs/clipboard/src/platform/unix/mod.rs
index e8665eb91,cd499d0d8..a58361f82
--- a/libs/clipboard/src/platform/unix/mod.rs
+++ b/libs/clipboard/src/platform/unix/mod.rs
@@@ -24,16 -24,11 +24,16 @@@ use self::url::{encode_path_to_uri, par
use super::fuse::FuseServer;
- #[cfg(not(feature = "wayland"))]
#[cfg(target_os = "linux")]
+/// clipboard implementation of x11
pub mod x11;
+#[cfg(target_os = "macos")]
+/// clipboard implementation of macos
+pub mod ns_clipboard;
+
pub mod local_file;
+
#[cfg(target_os = "linux")]
pub mod url;
diff --cc libs/clipboard/src/platform/unix/ns_clipboard.rs
index 19dc3641b,000000000..32c60a464
mode 100644,000000..100644
--- a/libs/clipboard/src/platform/unix/ns_clipboard.rs
+++ b/libs/clipboard/src/platform/unix/ns_clipboard.rs
@@@ -1,114 -1,0 +1,97 @@@
- use std::{
- collections::BTreeSet,
- path::PathBuf,
- sync::atomic::{AtomicBool, Ordering},
- };
++use std::{collections::BTreeSet, path::PathBuf};
+
+use cacao::pasteboard::{Pasteboard, PasteboardName};
+use hbb_common::log;
+use parking_lot::Mutex;
+
+use crate::{platform::unix::send_format_list, CliprdrError};
+
+use super::SysClipboard;
+
+#[inline]
+fn wait_file_list() -> Option<Vec<PathBuf>> {
+ let pb = Pasteboard::named(PasteboardName::General);
+ pb.get_file_urls()
+ .ok()
+ .map(|v| v.into_iter().map(|nsurl| nsurl.pathbuf()).collect())
+}
+
+#[inline]
+fn set_file_list(file_list: &[PathBuf]) -> Result<(), CliprdrError> {
+ let pb = Pasteboard::named(PasteboardName::General);
+ pb.set_files(file_list.to_vec())
+ .map_err(|_| CliprdrError::ClipboardInternalError)
+}
+
+pub struct NsPasteboard {
- stopped: AtomicBool,
+ ignore_path: PathBuf,
+
+ former_file_list: Mutex<Vec<PathBuf>>,
+}
+
+impl NsPasteboard {
+ pub fn new(ignore_path: &PathBuf) -> Result<Self, CliprdrError> {
+ Ok(Self {
- stopped: AtomicBool::new(false),
+ ignore_path: ignore_path.to_owned(),
+ former_file_list: Mutex::new(vec![]),
+ })
+ }
-
- #[inline]
- fn is_stopped(&self) -> bool {
- self.stopped.load(Ordering::Relaxed)
- }
+}
+
+impl SysClipboard for NsPasteboard {
+ fn set_file_list(&self, paths: &[PathBuf]) -> Result<(), CliprdrError> {
+ *self.former_file_list.lock() = paths.to_vec();
+ set_file_list(paths)
+ }
+
+ fn start(&self) {
- self.stopped.store(false, Ordering::Relaxed);
++ {
++ *self.former_file_list.lock() = vec![];
++ }
+
+ loop {
- if self.is_stopped() {
- std::thread::sleep(std::time::Duration::from_millis(100));
- continue;
- }
+ let file_list = match wait_file_list() {
+ Some(v) => v,
+ None => {
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ continue;
+ }
+ };
+
+ let filtered = file_list
+ .into_iter()
+ .filter(|pb| !pb.starts_with(&self.ignore_path))
+ .collect::<Vec<_>>();
+
+ if filtered.is_empty() {
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ continue;
+ }
+
+ {
+ let mut former = self.former_file_list.lock();
+
+ let filtered_st: BTreeSet<_> = filtered.iter().collect();
+ let former_st = former.iter().collect::<BTreeSet<_>>();
+ if filtered_st == former_st {
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ continue;
+ }
+
+ *former = filtered;
+ }
+
+ if let Err(e) = send_format_list(0) {
+ log::warn!("failed to send format list: {}", e);
+ break;
+ }
+
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ }
+ log::debug!("stop listening file related atoms on clipboard");
+ }
+
- fn stop(&self) {
- self.stopped.store(true, Ordering::Relaxed);
- }
-
+ fn get_file_list(&self) -> Vec<PathBuf> {
+ self.former_file_list.lock().clone()
+ }
+}