From 6e61cfb381bc7e2d32cfef9748174effbd6b54ff Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 10 Apr 2023 16:02:21 +0800 Subject: [PATCH 1/4] fix chain update Signed-off-by: fufesou --- src/client.rs | 5 +++++ src/common.rs | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 44d3e0201..f080eeacb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -95,6 +95,11 @@ lazy_static::lazy_static! { static ref TEXT_CLIPBOARD_STATE: Arc> = Arc::new(Mutex::new(TextClipboardState::new())); } +#[cfg(not(any(target_os = "android", target_os = "ios")))] +pub fn update_clipboard_text(text: String) { + *OLD_CLIPBOARD_TEXT.lock().unwrap() = text; +} + #[cfg(not(any(target_os = "android", target_os = "ios")))] pub fn get_key_state(key: enigo::Key) -> bool { use enigo::KeyboardControllable; diff --git a/src/common.rs b/src/common.rs index 3cf10eaba..43776820e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -187,9 +187,14 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc>>) } match ClipboardContext::new() { Ok(mut ctx) => { - let side = if old.is_none() { "host" } else { "client" }; + let host_side = "host"; + let client_side = "client"; + let side = if old.is_none() { host_side } else { client_side }; let old = if let Some(old) = old { old } else { &CONTENT }; *old.lock().unwrap() = content.clone(); + if side == client_side { + crate::client::update_clipboard_text(content.clone()); + } let _lock = ARBOARD_MTX.lock().unwrap(); allow_err!(ctx.set_text(content)); log::debug!("{} updated on {}", CLIPBOARD_NAME, side); From 9ebfe7f9ce75ea9a4e54deb31cec7e397a6b30a4 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 10 Apr 2023 16:16:09 +0800 Subject: [PATCH 2/4] fix build Signed-off-by: fufesou --- src/client.rs | 2 +- src/client/io_loop.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index f080eeacb..843befd4e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -641,7 +641,7 @@ impl Client { } #[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; diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index d5143c0a8..eb2c96b06 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -935,7 +935,7 @@ 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 { + Client::try_start_clipboard(Some(crate::client::ClientClipboardContext { cfg: permission_config.clone(), old: self.handler.old_clipboard.clone(), tx: sender.clone(), From e0f4cdd18cad8f7f816ec53a152d944826ad7cef Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 10 Apr 2023 16:58:58 +0800 Subject: [PATCH 3/4] remove session's old_clipboard Signed-off-by: fufesou --- src/client.rs | 16 +++++----------- src/client/io_loop.rs | 13 +++++++------ src/common.rs | 7 +------ src/flutter.rs | 4 ++-- src/ui_session_interface.rs | 2 -- 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/client.rs b/src/client.rs index 843befd4e..4b06074eb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -95,9 +95,10 @@ 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 update_clipboard_text(text: String) { - *OLD_CLIPBOARD_TEXT.lock().unwrap() = text; +pub fn get_old_clipboard_text() -> &'static Arc> { + &OLD_CLIPBOARD_TEXT } #[cfg(not(any(target_os = "android", target_os = "ios")))] @@ -666,16 +667,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)); } } @@ -2444,6 +2439,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 eb2c96b06..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(crate::client::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/common.rs b/src/common.rs index 43776820e..3cf10eaba 100644 --- a/src/common.rs +++ b/src/common.rs @@ -187,14 +187,9 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc>>) } match ClipboardContext::new() { Ok(mut ctx) => { - let host_side = "host"; - let client_side = "client"; - let side = if old.is_none() { host_side } else { client_side }; + let side = if old.is_none() { "host" } else { "client" }; let old = if let Some(old) = old { old } else { &CONTENT }; *old.lock().unwrap() = content.clone(); - if side == client_side { - crate::client::update_clipboard_text(content.clone()); - } let _lock = ARBOARD_MTX.lock().unwrap(); allow_err!(ctx.set_text(content)); log::debug!("{} updated on {}", CLIPBOARD_NAME, side); 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)] From 51432df0055359fec4e165f30c3e3022b26c0b1d Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 10 Apr 2023 17:11:17 +0800 Subject: [PATCH 4/4] add some comments Signed-off-by: fufesou --- src/client.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client.rs b/src/client.rs index 4b06074eb..f39110a78 100644 --- a/src/client.rs +++ b/src/client.rs @@ -641,6 +641,11 @@ 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(_ctx: Option) { let mut clipboard_lock = TEXT_CLIPBOARD_STATE.lock().unwrap();