patch: fix active enable of file copy paste

Signed-off-by: ClSlaid <cailue@bupt.edu.cn>
This commit is contained in:
ClSlaid 2023-10-30 12:00:44 +08:00
parent 803509d952
commit 43aa62e212
No known key found for this signature in database
GPG Key ID: E0A5F564C51C056E
4 changed files with 25 additions and 30 deletions

View File

@ -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<F: FnOnce(&mut Box<dyn CliprdrServiceContext>) -> ResultType<()>>(
f: F,
) -> ResultType<()> {

View File

@ -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<PathBuf>;
@ -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(())
}

View File

@ -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<Option<Vec<PathBuf>>, 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));

View File

@ -317,6 +317,9 @@ impl<T: InvokeUiSession> Remote<T> {
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<T: InvokeUiSession> Remote<T> {
"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)