From 0e98a517752ccac7d51c5280f5604ba6cc0fd735 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Mon, 29 Jul 2024 15:40:02 +0800 Subject: [PATCH] fix: clipboard, set formats and enable option (#8873) Signed-off-by: fufesou --- src/client/io_loop.rs | 4 ++-- src/clipboard.rs | 37 +++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index a182a5fe0..ea2afff1d 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -1367,17 +1367,17 @@ impl Remote { // https://github.com/rustdesk/rustdesk/issues/3703#issuecomment-1474734754 match p.permission.enum_value() { Ok(Permission::Keyboard) => { + *self.handler.server_keyboard_enabled.write().unwrap() = p.enabled; #[cfg(feature = "flutter")] #[cfg(not(any(target_os = "android", target_os = "ios")))] crate::flutter::update_text_clipboard_required(); - *self.handler.server_keyboard_enabled.write().unwrap() = p.enabled; self.handler.set_permission("keyboard", p.enabled); } Ok(Permission::Clipboard) => { + *self.handler.server_clipboard_enabled.write().unwrap() = p.enabled; #[cfg(feature = "flutter")] #[cfg(not(any(target_os = "android", target_os = "ios")))] crate::flutter::update_text_clipboard_required(); - *self.handler.server_clipboard_enabled.write().unwrap() = p.enabled; self.handler.set_permission("clipboard", p.enabled); } Ok(Permission::Audio) => { diff --git a/src/clipboard.rs b/src/clipboard.rs index fe30189ca..44a3c09d3 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -3,9 +3,7 @@ use clipboard_master::{ClipboardHandler, Master, Shutdown}; use hbb_common::{log, message_proto::*, ResultType}; use std::{ sync::{mpsc::Sender, Arc, Mutex}, - thread, thread::JoinHandle, - time::Duration, }; pub const CLIPBOARD_NAME: &'static str = "clipboard"; @@ -18,6 +16,10 @@ lazy_static::lazy_static! { static ref ARBOARD_MTX: Arc> = Arc::new(Mutex::new(())); // cache the clipboard msg static ref LAST_MULTI_CLIPBOARDS: Arc> = Arc::new(Mutex::new(MultiClipboards::new())); + // Clipboard on Linux is "server--clients" mode. + // The clipboard content is owned by the server and passed to the clients when requested. + // Plain text is the only exception, it does not require the server to be present. + static ref CLIPBOARD_UPDATE_CTX: Arc>> = Arc::new(Mutex::new(None)); } const SUPPORTED_FORMATS: &[ClipboardFormat] = &[ @@ -162,20 +164,27 @@ fn update_clipboard_(multi_clipboards: Vec, side: ClipboardSide) { if to_update_data.is_empty() { return; } - match ClipboardContext::new() { - Ok(mut ctx) => { - to_update_data.push(ClipboardData::Special(( - RUSTDESK_CLIPBOARD_OWNER_FORMAT.to_owned(), - side.get_owner_data(), - ))); - if let Err(e) = ctx.set(&to_update_data) { - log::debug!("Failed to set clipboard: {}", e); - } else { - log::debug!("{} updated on {}", CLIPBOARD_NAME, side); + let mut ctx = CLIPBOARD_UPDATE_CTX.lock().unwrap(); + if ctx.is_none() { + match ClipboardContext::new() { + Ok(x) => { + *ctx = Some(x); + } + Err(e) => { + log::error!("Failed to create clipboard context: {}", e); + return; } } - Err(err) => { - log::error!("Failed to create clipboard context: {}", err); + } + if let Some(ctx) = ctx.as_mut() { + to_update_data.push(ClipboardData::Special(( + RUSTDESK_CLIPBOARD_OWNER_FORMAT.to_owned(), + side.get_owner_data(), + ))); + if let Err(e) = ctx.set(&to_update_data) { + log::debug!("Failed to set clipboard: {}", e); + } else { + log::debug!("{} updated on {}", CLIPBOARD_NAME, side); } } }