rustdesk/src/server/clipboard_service.rs

100 lines
2.9 KiB
Rust
Raw Normal View History

2021-03-29 15:59:14 +08:00
use super::*;
pub use crate::common::{
check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME,
CONTENT,
};
2021-10-24 23:47:02 +08:00
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
2021-10-25 16:25:23 +08:00
use hbb_common::{anyhow, ResultType};
use std::{
sync,
sync::mpsc::{Receiver, Sender},
2021-10-26 16:59:31 +08:00
thread::{self},
2021-10-25 16:25:23 +08:00
time::Duration,
};
2021-03-29 15:59:14 +08:00
pub fn new() -> GenericService {
let sp = GenericService::new(NAME, true);
2021-10-25 16:25:23 +08:00
2021-10-26 16:59:31 +08:00
// Listening service needs to run for a long time,
// otherwise it will cause part of the content to
// be missed during the closure of the clipboard
// (during the closure, the content copied by the
// remote machine will be missed, and the clipboard
// will not be synchronized immediately when it is
// opened again), and CONTENT will not be updated
2021-10-25 16:25:23 +08:00
thread::spawn(|| {
2021-10-26 16:59:31 +08:00
let _ = listen::notify();
2021-10-25 16:25:23 +08:00
});
2021-10-24 23:47:02 +08:00
sp.run::<_>(listen::run);
2021-03-29 15:59:14 +08:00
sp
}
2021-10-24 23:47:02 +08:00
mod listen {
use super::*;
2021-10-22 16:02:01 +08:00
2021-10-26 16:59:31 +08:00
static mut CHANNEL: Option<(Sender<()>, Receiver<()>)> = None;
2021-10-25 16:25:23 +08:00
static mut CTX: Option<ClipboardContext> = None;
2021-10-26 16:59:31 +08:00
static WAIT: Duration = Duration::from_millis(1500);
2021-10-25 16:25:23 +08:00
2021-10-26 16:59:31 +08:00
struct ClipHandle;
2021-10-24 23:47:02 +08:00
impl ClipboardHandler for ClipHandle {
fn on_clipboard_change(&mut self) -> CallbackResult {
2021-10-26 16:59:31 +08:00
if let Some((tx, _rx)) = unsafe { CHANNEL.as_ref() } {
let _ = tx.send(());
}
2021-10-24 23:47:02 +08:00
CallbackResult::Next
2021-03-29 15:59:14 +08:00
}
2021-10-24 23:47:02 +08:00
}
2021-10-26 16:59:31 +08:00
pub fn notify() -> ResultType<()> {
Master::new(ClipHandle).run()?;
2021-10-24 23:47:02 +08:00
Ok(())
}
pub fn run(sp: GenericService) -> ResultType<()> {
2021-10-26 16:59:31 +08:00
unsafe {
if CHANNEL.is_none() {
CHANNEL = Some(sync::mpsc::channel());
}
if CTX.is_none() {
match ClipboardContext::new() {
Ok(ctx) => {
CTX = Some(ctx);
}
Err(err) => {
log::error!("Failed to start {}: {}", NAME, err);
return Err(anyhow::Error::from(err));
}
};
}
2021-10-25 16:25:23 +08:00
}
2021-10-24 23:47:02 +08:00
while sp.ok() {
2021-10-26 16:59:31 +08:00
if let Some((_tx, rx)) = unsafe { CHANNEL.as_ref() } {
if let Ok(_) = rx.recv_timeout(WAIT) {
if let Some(mut ctx) = unsafe { CTX.as_mut() } {
if let Some(msg) = check_clipboard(&mut ctx, None) {
sp.send(msg);
}
2021-10-25 16:25:23 +08:00
}
}
}
2021-10-24 23:47:02 +08:00
sp.snapshot(|sps| {
let txt = crate::CONTENT.lock().unwrap().clone();
if !txt.is_empty() {
let msg_out = crate::create_clipboard_msg(txt);
sps.send_shared(Arc::new(msg_out));
}
Ok(())
})?;
}
2021-10-25 16:25:23 +08:00
*CONTENT.lock().unwrap() = Default::default();
2021-10-24 23:47:02 +08:00
Ok(())
2021-03-29 15:59:14 +08:00
}
}