diff --git a/src/client.rs b/src/client.rs index 44d3e0201..f39110a78 100644 --- a/src/client.rs +++ b/src/client.rs @@ -95,6 +95,12 @@ lazy_static::lazy_static! { static ref TEXT_CLIPBOARD_STATE: Arc> = Arc::new(Mutex::new(TextClipboardState::new())); } +#[inline] +#[cfg(not(any(target_os = "android", target_os = "ios")))] +pub fn get_old_clipboard_text() -> &'static Arc> { + &OLD_CLIPBOARD_TEXT +} + #[cfg(not(any(target_os = "android", target_os = "ios")))] pub fn get_key_state(key: enigo::Key) -> bool { use enigo::KeyboardControllable; @@ -635,8 +641,13 @@ impl Client { TEXT_CLIPBOARD_STATE.lock().unwrap().running = false; } + // `try_start_clipboard` is called by all session when connection is established. (When handling peer info). + // This function only create one thread with a loop, the loop is shared by all sessions. + // After all sessions are end, the loop exists. + // + // If clipboard update is detected, the text will be sent to all sessions by `send_text_clipboard_msg`. #[cfg(not(any(target_os = "android", target_os = "ios")))] - fn try_start_clipboard(_conf_tx: Option) { + fn try_start_clipboard(_ctx: Option) { let mut clipboard_lock = TEXT_CLIPBOARD_STATE.lock().unwrap(); if clipboard_lock.running { return; @@ -661,16 +672,10 @@ impl Client { if let Some(msg) = check_clipboard(&mut ctx, Some(&OLD_CLIPBOARD_TEXT)) { #[cfg(feature = "flutter")] - crate::flutter::send_text_clipboard_msg( - &*OLD_CLIPBOARD_TEXT.lock().unwrap(), - msg, - ); + crate::flutter::send_text_clipboard_msg(msg); #[cfg(not(feature = "flutter"))] if let Some(ctx) = &_ctx { - if ctx.cfg.is_text_clipboard_required() - && *OLD_CLIPBOARD_TEXT.lock().unwrap() - != *ctx.old.lock().unwrap() - { + if ctx.cfg.is_text_clipboard_required() { let _ = ctx.tx.send(Data::Message(msg)); } } @@ -2439,6 +2444,5 @@ pub(crate) struct ClientClipboardContext; #[cfg(not(any(target_os = "android", target_os = "ios")))] pub(crate) struct ClientClipboardContext { pub cfg: SessionPermissionConfig, - pub old: Arc>, pub tx: UnboundedSender, } diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index d5143c0a8..1c009d84e 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -935,11 +935,12 @@ impl Remote { Client::try_start_clipboard(None); #[cfg(not(feature = "flutter"))] #[cfg(not(any(target_os = "android", target_os = "ios")))] - Client::try_start_clipboard(Some(ClientClipboardContext { - cfg: permission_config.clone(), - old: self.handler.old_clipboard.clone(), - tx: sender.clone(), - })); + Client::try_start_clipboard(Some( + crate::client::ClientClipboardContext { + cfg: permission_config.clone(), + tx: sender.clone(), + }, + )); #[cfg(not(any(target_os = "android", target_os = "ios")))] tokio::spawn(async move { @@ -972,7 +973,7 @@ impl Remote { Some(message::Union::Clipboard(cb)) => { if !self.handler.lc.read().unwrap().disable_clipboard.v { #[cfg(not(any(target_os = "android", target_os = "ios")))] - update_clipboard(cb, Some(&self.handler.old_clipboard)); + update_clipboard(cb, Some(&crate::client::get_old_clipboard_text())); #[cfg(any(target_os = "android", target_os = "ios"))] { let content = if cb.compress { diff --git a/src/flutter.rs b/src/flutter.rs index e49f04043..6c9ff7f37 100644 --- a/src/flutter.rs +++ b/src/flutter.rs @@ -725,9 +725,9 @@ pub fn other_sessions_running(id: &str) -> bool { } #[cfg(not(any(target_os = "android", target_os = "ios")))] -pub fn send_text_clipboard_msg(text: &str, msg: Message) { +pub fn send_text_clipboard_msg(msg: Message) { for (_id, session) in SESSIONS.read().unwrap().iter() { - if session.is_text_clipboard_required() && text != *session.old_clipboard.lock().unwrap() { + if session.is_text_clipboard_required() { session.send(Data::Message(msg.clone())); } } diff --git a/src/ui_session_interface.rs b/src/ui_session_interface.rs index 7ae1d5b24..504982f50 100644 --- a/src/ui_session_interface.rs +++ b/src/ui_session_interface.rs @@ -43,8 +43,6 @@ pub struct Session { pub server_keyboard_enabled: Arc>, pub server_file_transfer_enabled: Arc>, pub server_clipboard_enabled: Arc>, - #[cfg(not(any(target_os = "android", target_os = "ios")))] - pub old_clipboard: Arc>, } #[derive(Clone)]