2022-10-26 22:37:45 +08:00
|
|
|
use crate::cliprdr::*;
|
|
|
|
use hbb_common::log;
|
2023-06-20 00:50:01 +08:00
|
|
|
use std::sync::Mutex;
|
2022-10-26 22:37:45 +08:00
|
|
|
|
2023-06-29 18:12:34 +08:00
|
|
|
const CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS: u32 = 0;
|
2023-06-29 11:28:55 +08:00
|
|
|
|
2022-10-26 22:37:45 +08:00
|
|
|
lazy_static::lazy_static! {
|
2023-06-20 00:50:01 +08:00
|
|
|
static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(0)};
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ContextSend {
|
2023-06-20 00:50:01 +08:00
|
|
|
addr: Mutex<u64>,
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContextSend {
|
2023-06-19 21:44:32 +08:00
|
|
|
#[inline]
|
2023-06-20 00:50:01 +08:00
|
|
|
pub fn is_enabled() -> bool {
|
|
|
|
*CONTEXT_SEND.addr.lock().unwrap() != 0
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
2023-06-19 22:52:17 +08:00
|
|
|
pub fn set_is_stopped() {
|
|
|
|
let _res = Self::proc(|c| {
|
|
|
|
c.IsStopped = TRUE;
|
|
|
|
0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-20 00:50:01 +08:00
|
|
|
pub fn enable(enabled: bool) {
|
|
|
|
let mut lock = CONTEXT_SEND.addr.lock().unwrap();
|
2022-10-26 22:37:45 +08:00
|
|
|
if enabled {
|
2023-06-20 00:50:01 +08:00
|
|
|
if *lock == 0 {
|
2023-06-29 11:28:55 +08:00
|
|
|
match crate::create_cliprdr_context(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS,
|
|
|
|
) {
|
2022-10-26 22:37:45 +08:00
|
|
|
Ok(context) => {
|
|
|
|
log::info!("clipboard context for file transfer created.");
|
2023-06-20 00:50:01 +08:00
|
|
|
*lock = Box::into_raw(context) as _;
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::error!(
|
|
|
|
"Create clipboard context for file transfer: {}",
|
|
|
|
err.to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-20 00:50:01 +08:00
|
|
|
if *lock != 0 {
|
|
|
|
unsafe {
|
|
|
|
let _ = Box::from_raw(*lock as *mut CliprdrClientContext);
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
2023-06-20 00:50:01 +08:00
|
|
|
log::info!("clipboard context for file transfer destroyed.");
|
|
|
|
*lock = 0;
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn proc<F: FnOnce(&mut Box<CliprdrClientContext>) -> u32>(f: F) -> u32 {
|
2023-06-20 00:50:01 +08:00
|
|
|
let lock = CONTEXT_SEND.addr.lock().unwrap();
|
|
|
|
if *lock != 0 {
|
2022-10-26 22:37:45 +08:00
|
|
|
unsafe {
|
2023-06-20 00:50:01 +08:00
|
|
|
let mut context = Box::from_raw(*lock as *mut CliprdrClientContext);
|
2023-06-18 22:55:38 +08:00
|
|
|
let code = f(&mut context);
|
|
|
|
std::mem::forget(context);
|
|
|
|
code
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|