Skip to content

Commit

Permalink
Merge pull request rustdesk#268 from cc-morning/master
Browse files Browse the repository at this point in the history
Optimize clipboard change listener
  • Loading branch information
rustdesk authored Oct 23, 2021
2 parents 8f22273 + 1bf80e3 commit 5169045
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mac_address = "1.1"
sciter-rs = { git = "https://github.com/open-trade/rust-sciter", branch = "dyn" }
ctrlc = "3.2"
arboard = "2.0"
clipboard-master = "3"

[target.'cfg(target_os = "windows")'.dependencies]
#systray = { git = "https://github.com/open-trade/systray-rs" }
Expand Down
22 changes: 21 additions & 1 deletion src/server/clipboard_service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::sync::mpsc::Sender;

use clipboard_master::{CallbackResult, ClipboardHandler, Master};

use super::*;
pub use crate::common::{
check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME,
Expand Down Expand Up @@ -27,12 +31,28 @@ impl super::service::Reset for State {
}
}

struct ClipHandle {
tx: Sender<bool>,
}

impl ClipboardHandler for ClipHandle {
fn on_clipboard_change(&mut self) -> CallbackResult {
let _ = self.tx.send(true);
CallbackResult::Next
}
}

pub fn new() -> GenericService {
let sp = GenericService::new(NAME, true);
sp.repeat::<State, _>(INTERVAL, run);
sp.listen::<State, _, _>(notify, run);
sp
}

fn notify(tx: Sender<bool>) -> ResultType<()> {
Master::new(ClipHandle { tx }).run()?;
Ok(())
}

fn run(sp: GenericService, state: &mut State) -> ResultType<()> {
if let Some(ctx) = state.ctx.as_mut() {
if let Some(msg) = check_clipboard(ctx, None) {
Expand Down
34 changes: 34 additions & 0 deletions src/server/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use std::{
sync::{self, mpsc::Sender},
thread::{self, JoinHandle},
time,
};
Expand Down Expand Up @@ -153,6 +154,39 @@ impl<T: Subscriber + From<ConnInner>> ServiceTmpl<T> {
}
}

pub fn listen<S, N, F>(&self, notify: N, callback: F)
where
N: 'static + FnMut(Sender<bool>) -> ResultType<()> + Send,
F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,
S: 'static + Default + Reset,
{
let mut notify = notify;
let mut callback = callback;
let sp = self.clone();
let (tx, rx) = sync::mpsc::channel();
thread::spawn(move || {
let _ = notify(tx);
});

let thread = thread::spawn(move || {
let mut state = S::default();
while let Ok(changed) = rx.recv() {
if changed && sp.active() {
if sp.has_subscribes() {
if let Err(err) = callback(sp.clone(), &mut state) {
log::error!("Error of {} service: {}", sp.name(), err);
#[cfg(windows)]
crate::platform::windows::try_change_desktop();
}
} else {
state.reset();
}
}
}
});
self.0.write().unwrap().handle = Some(thread);
}

pub fn repeat<S, F>(&self, interval_ms: u64, callback: F)
where
F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,
Expand Down

0 comments on commit 5169045

Please sign in to comment.