rustdesk/src/server/clipboard_service.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

2021-03-29 15:59:14 +08:00
use super::*;
pub use crate::clipboard::{
check_clipboard, get_cache_msg, ClipboardContext, ClipboardSide,
CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME,
2021-03-29 15:59:14 +08:00
};
2024-07-01 00:24:23 +08:00
#[derive(Default)]
2022-05-12 17:35:25 +08:00
struct State {
ctx: Option<ClipboardContext>,
2021-03-29 15:59:14 +08:00
}
2024-07-01 00:24:23 +08:00
impl super::service::Reset for State {
fn reset(&mut self) {
crate::clipboard::reset_cache();
2024-07-01 00:24:23 +08:00
self.ctx = None;
}
fn init(&mut 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
}
};
2024-07-01 00:24:23 +08:00
self.ctx = ctx;
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, ClipboardSide::Host) {
sp.send(msg);
}
sp.snapshot(|sps| {
// Just create a message with multi clipboards here
// The actual peer version and peer platform will be checked again before sending.
if let Some(msg) = get_cache_msg("1.2.7", "Windows") {
sps.send_shared(Arc::new(msg));
}
Ok(())
})?;
2022-05-12 17:35:25 +08:00
Ok(())
2021-03-29 15:59:14 +08:00
}