diff --git a/libs/clipboard/src/context_send.rs b/libs/clipboard/src/context_send.rs index a195850f2..16363250e 100644 --- a/libs/clipboard/src/context_send.rs +++ b/libs/clipboard/src/context_send.rs @@ -1,27 +1,19 @@ use crate::cliprdr::*; use hbb_common::log; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; lazy_static::lazy_static! { - static ref CONTEXT_SEND: Arc> = Arc::new(Mutex::new(ContextSend::new())); + static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(0)}; } pub struct ContextSend { - cm_enabled: bool, - addr: u64, + addr: Mutex, } impl ContextSend { - fn new() -> Self { - Self { - cm_enabled: false, - addr: 0, - } - } - #[inline] - pub fn is_cm_enabled() -> bool { - CONTEXT_SEND.lock().unwrap().cm_enabled + pub fn is_enabled() -> bool { + *CONTEXT_SEND.addr.lock().unwrap() != 0 } pub fn set_is_stopped() { @@ -31,14 +23,14 @@ impl ContextSend { }); } - pub fn enable(enabled: bool, is_cm_side: bool, is_server_process: bool) { - let mut lock = CONTEXT_SEND.lock().unwrap(); + pub fn enable(enabled: bool) { + let mut lock = CONTEXT_SEND.addr.lock().unwrap(); if enabled { - if lock.addr == 0 { + if *lock == 0 { match crate::create_cliprdr_context(true, false) { Ok(context) => { log::info!("clipboard context for file transfer created."); - lock.addr = Box::into_raw(context) as _; + *lock = Box::into_raw(context) as _; } Err(err) => { log::error!( @@ -48,28 +40,22 @@ impl ContextSend { } } } - if is_cm_side { - lock.cm_enabled = true; - } } else { - if lock.addr != 0 { - if is_server_process { - unsafe { - let _ = Box::from_raw(lock.addr as *mut CliprdrClientContext); - } - log::info!("clipboard context for file transfer destroyed."); - lock.addr = 0; + if *lock != 0 { + unsafe { + let _ = Box::from_raw(*lock as *mut CliprdrClientContext); } - lock.cm_enabled = false; + log::info!("clipboard context for file transfer destroyed."); + *lock = 0; } } } pub fn proc) -> u32>(f: F) -> u32 { - let lock = CONTEXT_SEND.lock().unwrap(); - if lock.addr != 0 { + let lock = CONTEXT_SEND.addr.lock().unwrap(); + if *lock != 0 { unsafe { - let mut context = Box::from_raw(lock.addr as *mut CliprdrClientContext); + let mut context = Box::from_raw(*lock as *mut CliprdrClientContext); let code = f(&mut context); std::mem::forget(context); code diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index e830309d0..d75b53888 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -1550,7 +1550,7 @@ impl Remote { { let enabled = *self.handler.server_file_transfer_enabled.read().unwrap() && self.handler.lc.read().unwrap().enable_file_transfer.v; - ContextSend::enable(enabled, false, false); + ContextSend::enable(enabled); } } diff --git a/src/core_main.rs b/src/core_main.rs index 59c72fa66..f8741a72f 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -123,7 +123,7 @@ pub fn core_main() -> Option> { init_plugins(&args); if args.is_empty() { #[cfg(windows)] - clipboard::ContextSend::enable(true, false, false); + clipboard::ContextSend::enable(true); std::thread::spawn(move || crate::start_server(false)); } else { #[cfg(windows)] diff --git a/src/ui_cm_interface.rs b/src/ui_cm_interface.rs index af3bf2b3a..7e46fd314 100644 --- a/src/ui_cm_interface.rs +++ b/src/ui_cm_interface.rs @@ -338,7 +338,7 @@ impl IpcTaskRunner { #[cfg(windows)] { - if ContextSend::is_cm_enabled() { + if ContextSend::is_enabled() { allow_err!( self.stream .send(&Data::ClipboardFile(clipboard::ClipboardFile::MonitorReady)) @@ -404,7 +404,7 @@ impl IpcTaskRunner { #[cfg(windows)] { let is_stopping_allowed = _clip.is_stopping_allowed_from_peer(); - let is_clipboard_enabled = ContextSend::is_cm_enabled(); + let is_clipboard_enabled = ContextSend::is_enabled(); let file_transfer_enabled = self.file_transfer_enabled; let stop = !is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled); log::debug!( @@ -469,7 +469,7 @@ impl IpcTaskRunner { #[cfg(windows)] { let is_stopping_allowed = _clip.is_stopping_allowed(); - let is_clipboard_enabled = ContextSend::is_cm_enabled(); + let is_clipboard_enabled = ContextSend::is_enabled(); let file_transfer_enabled = self.file_transfer_enabled; let file_transfer_enabled_peer = self.file_transfer_enabled_peer; let stop = is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled && file_transfer_enabled_peer); @@ -537,11 +537,7 @@ pub async fn start_ipc(cm: ConnectionManager) { }); #[cfg(target_os = "windows")] - ContextSend::enable( - Config::get_option("enable-file-transfer").is_empty(), - true, - crate::is_server(), - ); + ContextSend::enable(Config::get_option("enable-file-transfer").is_empty()); match ipc::new_listener("_cm").await { Ok(mut incoming) => { diff --git a/src/ui_interface.rs b/src/ui_interface.rs index 0945e59af..a901fd4e6 100644 --- a/src/ui_interface.rs +++ b/src/ui_interface.rs @@ -882,11 +882,7 @@ async fn check_connect_status_(reconnect: bool, rx: mpsc::UnboundedReceiver