From 43aa62e2129adf16b5b8fa3147511eaaa65bba6a Mon Sep 17 00:00:00 2001 From: ClSlaid Date: Mon, 30 Oct 2023 12:00:44 +0800 Subject: [PATCH] patch: fix active enable of file copy paste Signed-off-by: ClSlaid --- libs/clipboard/src/context_send.rs | 13 ++++++++++ libs/clipboard/src/platform/unix/mod.rs | 3 +-- libs/clipboard/src/platform/unix/x11.rs | 33 ++++--------------------- src/client/io_loop.rs | 6 +++++ 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/libs/clipboard/src/context_send.rs b/libs/clipboard/src/context_send.rs index 64ab85983..f3606509f 100644 --- a/libs/clipboard/src/context_send.rs +++ b/libs/clipboard/src/context_send.rs @@ -47,6 +47,19 @@ impl ContextSend { } } + /// make sure the clipboard context is enabled. + pub fn make_sure_enabled() -> ResultType<()> { + let mut lock = CONTEXT_SEND.addr.lock().unwrap(); + if lock.is_some() { + return Ok(()); + } + + let ctx = crate::create_cliprdr_context(true, false, CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS)?; + *lock = Some(ctx); + log::info!("clipboard context for file transfer recreated."); + Ok(()) + } + pub fn proc) -> ResultType<()>>( f: F, ) -> ResultType<()> { diff --git a/libs/clipboard/src/platform/unix/mod.rs b/libs/clipboard/src/platform/unix/mod.rs index 39b851e10..cd499d0d8 100644 --- a/libs/clipboard/src/platform/unix/mod.rs +++ b/libs/clipboard/src/platform/unix/mod.rs @@ -63,7 +63,6 @@ fn add_remote_format(local_name: &str, remote_id: i32) { trait SysClipboard: Send + Sync { fn start(&self); - fn stop(&self); fn set_file_list(&self, paths: &[PathBuf]) -> Result<(), CliprdrError>; fn get_file_list(&self) -> Vec; @@ -524,7 +523,7 @@ impl CliprdrServiceContext for ClipboardContext { if let Some(fuse_handle) = self.fuse_handle.lock().take() { fuse_handle.join(); } - self.clipboard.stop(); + // we don't stop the clipboard, keep listening in case of restart Ok(()) } diff --git a/libs/clipboard/src/platform/unix/x11.rs b/libs/clipboard/src/platform/unix/x11.rs index 9f4c9e4c6..4ca3a2c0b 100644 --- a/libs/clipboard/src/platform/unix/x11.rs +++ b/libs/clipboard/src/platform/unix/x11.rs @@ -1,8 +1,4 @@ -use std::{ - collections::BTreeSet, - path::PathBuf, - sync::atomic::{AtomicBool, Ordering}, -}; +use std::{collections::BTreeSet, path::PathBuf}; use hbb_common::log; use once_cell::sync::OnceCell; @@ -21,7 +17,6 @@ fn get_clip() -> Result<&'static Clipboard, CliprdrError> { } pub struct X11Clipboard { - stop: AtomicBool, ignore_path: PathBuf, text_uri_list: Atom, gnome_copied_files: Atom, @@ -47,7 +42,6 @@ impl X11Clipboard { .map_err(|_| CliprdrError::CliprdrInit)?; Ok(Self { ignore_path: ignore_path.to_owned(), - stop: AtomicBool::new(false), text_uri_list, gnome_copied_files, nautilus_clipboard, @@ -85,22 +79,12 @@ impl X11Clipboard { } fn wait_file_list(&self) -> Result>, CliprdrError> { - if self.stop.load(Ordering::Relaxed) { - return Ok(None); - } let v = self.load(self.text_uri_list)?; let p = parse_plain_uri_list(v)?; Ok(Some(p)) } } -impl X11Clipboard { - #[inline] - fn is_stopped(&self) -> bool { - self.stop.load(Ordering::Relaxed) - } -} - impl SysClipboard for X11Clipboard { fn set_file_list(&self, paths: &[PathBuf]) -> Result<(), CliprdrError> { *self.former_file_list.lock() = paths.to_vec(); @@ -118,13 +102,11 @@ impl SysClipboard for X11Clipboard { .map_err(|_| CliprdrError::ClipboardInternalError) } - fn stop(&self) { - self.stop.store(true, Ordering::Relaxed); - } - fn start(&self) { - self.stop.store(false, Ordering::Relaxed); - + { + // clear cached file list + *self.former_file_list.lock() = vec![]; + } loop { let sth = match self.wait_file_list() { Ok(sth) => sth, @@ -135,11 +117,6 @@ impl SysClipboard for X11Clipboard { } }; - if self.is_stopped() { - std::thread::sleep(std::time::Duration::from_millis(100)); - continue; - } - let Some(paths) = sth else { // just sleep std::thread::sleep(std::time::Duration::from_millis(100)); diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index 377da85e3..ca5ffee3b 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -317,6 +317,9 @@ impl Remote { if stop { ContextSend::set_is_stopped(); } else { + if let Err(e) = ContextSend::make_sure_enabled() { + log::error!("failed to restart clipboard context: {}", e); + }; log::debug!("Send system clipboard message to remote"); let msg = crate::clipboard_file::clip_2_msg(clip); allow_err!(peer.send(&msg).await); @@ -1742,6 +1745,9 @@ impl Remote { "Process clipboard message from server peer, stop: {}, is_stopping_allowed: {}, file_transfer_enabled: {}", stop, is_stopping_allowed, file_transfer_enabled); if !stop { + if let Err(e) = ContextSend::make_sure_enabled() { + log::error!("failed to restart clipboard context: {}", e); + }; let _ = ContextSend::proc(|context| -> ResultType<()> { context .server_clip_file(self.client_conn_id, clip)