rustdesk/src/server/clipboard_service.rs

105 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::{
io, sync,
sync::{
atomic::{AtomicBool, Ordering},
mpsc::SyncSender,
},
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-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
static RUNNING: AtomicBool = AtomicBool::new(true);
2021-10-26 16:59:31 +08:00
static WAIT: Duration = Duration::from_millis(1500);
2021-10-25 16:25:23 +08:00
struct ClipHandle {
tx: SyncSender<()>,
}
2021-10-24 23:47:02 +08:00
impl ClipboardHandler for ClipHandle {
fn on_clipboard_change(&mut self) -> CallbackResult {
if !RUNNING.load(Ordering::SeqCst) {
return CallbackResult::Stop;
2021-10-26 16:59:31 +08:00
}
let _ = self.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
fn on_clipboard_error(&mut self, error: io::Error) -> CallbackResult {
if !RUNNING.load(Ordering::SeqCst) {
CallbackResult::Stop
} else {
CallbackResult::StopWithError(error)
}
}
2021-10-24 23:47:02 +08:00
}
#[tokio::main]
pub async fn run(sp: GenericService) -> ResultType<()> {
let mut ctx = match ClipboardContext::new() {
Ok(ctx) => ctx,
Err(err) => {
log::error!("Failed to start {}: {}", NAME, err);
return Err(anyhow::Error::from(err));
2021-10-26 16:59:31 +08:00
}
};
2021-10-26 16:59:31 +08:00
if !RUNNING.load(Ordering::SeqCst) {
RUNNING.store(true, Ordering::SeqCst);
2021-10-25 16:25:23 +08:00
}
2021-10-24 23:47:02 +08:00
let (tx, rx) = sync::mpsc::sync_channel(12);
let listener = tokio::spawn(async {
log::info!("Clipboard listener running!");
let _ = Master::new(ClipHandle { tx }).run();
});
2021-10-24 23:47:02 +08:00
while sp.ok() {
if let Ok(_) = rx.recv_timeout(WAIT) {
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
2021-10-30 14:03:33 +08:00
RUNNING.store(false, Ordering::SeqCst);
trigger(&mut ctx);
let _ = listener.await;
log::info!("Clipboard listener stopped!");
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
}
fn trigger(ctx: &mut ClipboardContext) {
let _ = match ctx.get_text() {
Ok(text) => ctx.set_text(text),
Err(_) => ctx.set_text(Default::default()),
};
}
2021-03-29 15:59:14 +08:00
}