2022-10-26 22:37:45 +08:00
|
|
|
use crate::cliprdr::*;
|
|
|
|
use hbb_common::log;
|
2023-06-18 20:23:54 +08:00
|
|
|
use std::sync::{Arc, Mutex};
|
2022-10-26 22:37:45 +08:00
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
2023-06-18 20:23:54 +08:00
|
|
|
static ref CONTEXT_SEND: Arc<Mutex<ContextSend>> = Arc::new(Mutex::new(ContextSend::new()));
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ContextSend {
|
2023-06-19 22:06:08 +08:00
|
|
|
cm_enabled: bool,
|
2023-06-18 20:23:54 +08:00
|
|
|
addr: u64,
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContextSend {
|
2023-06-18 20:23:54 +08:00
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
2023-06-19 22:06:08 +08:00
|
|
|
cm_enabled: false,
|
2023-06-18 20:23:54 +08:00
|
|
|
addr: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 21:44:32 +08:00
|
|
|
#[inline]
|
2023-06-19 22:06:08 +08:00
|
|
|
pub fn is_cm_enabled() -> bool {
|
|
|
|
CONTEXT_SEND.lock().unwrap().cm_enabled
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
2023-06-19 22:06:08 +08:00
|
|
|
pub fn enable(enabled: bool, is_cm_side: bool, is_server_process: bool) {
|
2023-06-18 20:23:54 +08:00
|
|
|
let mut lock = CONTEXT_SEND.lock().unwrap();
|
2022-10-26 22:37:45 +08:00
|
|
|
if enabled {
|
2023-06-18 20:23:54 +08:00
|
|
|
if lock.addr == 0 {
|
|
|
|
match crate::create_cliprdr_context(true, false) {
|
2022-10-26 22:37:45 +08:00
|
|
|
Ok(context) => {
|
|
|
|
log::info!("clipboard context for file transfer created.");
|
2023-06-18 20:23:54 +08:00
|
|
|
lock.addr = 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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 22:06:08 +08:00
|
|
|
if is_cm_side {
|
|
|
|
lock.cm_enabled = true;
|
2023-06-18 20:23:54 +08:00
|
|
|
}
|
2022-10-26 22:37:45 +08:00
|
|
|
} else {
|
2023-06-18 20:23:54 +08:00
|
|
|
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;
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
2023-06-19 22:06:08 +08:00
|
|
|
lock.cm_enabled = false;
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn proc<F: FnOnce(&mut Box<CliprdrClientContext>) -> u32>(f: F) -> u32 {
|
2023-06-18 20:23:54 +08:00
|
|
|
let lock = CONTEXT_SEND.lock().unwrap();
|
|
|
|
if lock.addr != 0 {
|
2022-10-26 22:37:45 +08:00
|
|
|
unsafe {
|
2023-06-18 20:23:54 +08:00
|
|
|
let mut context = Box::from_raw(lock.addr 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|