rustdesk/src/server/clipboard_service.rs

50 lines
1.2 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,
};
2022-05-12 17:35:25 +08:00
struct State {
ctx: Option<ClipboardContext>,
2021-03-29 15:59:14 +08:00
}
2022-05-12 17:35:25 +08:00
impl Default for State {
fn default() -> Self {
let ctx = match ClipboardContext::new(true) {
2022-05-12 17:35:25 +08:00
Ok(ctx) => Some(ctx),
Err(err) => {
log::error!("Failed to start {}: {}", NAME, err);
2022-05-12 17:35:25 +08:00
None
2021-10-26 16:59:31 +08:00
}
};
2022-05-12 17:35:25 +08:00
Self { ctx }
}
}
2021-10-26 16:59:31 +08:00
2022-05-12 17:35:25 +08:00
impl super::service::Reset for State {
fn reset(&mut self) {
2021-10-25 16:25:23 +08:00
*CONTENT.lock().unwrap() = Default::default();
2021-03-29 15:59:14 +08:00
}
2022-05-12 17:35:25 +08:00
}
pub fn new() -> GenericService {
let svc = EmptyExtraFieldService::new(NAME.to_owned(), true);
GenericService::repeat::<State, _, _>(&svc.clone(), INTERVAL, run);
svc.sp
2022-05-12 17:35:25 +08:00
}
fn run(sp: EmptyExtraFieldService, state: &mut State) -> ResultType<()> {
if let Some(msg) = check_clipboard(&mut state.ctx, None) {
sp.send(msg);
}
sp.snapshot(|sps| {
let data = crate::CONTENT.lock().unwrap().clone();
if !data.is_empty() {
let msg_out = data.create_msg();
sps.send_shared(Arc::new(msg_out));
}
Ok(())
})?;
2022-05-12 17:35:25 +08:00
Ok(())
2021-03-29 15:59:14 +08:00
}