From 972ec8d79d02ed68938b0a5825ae7f6fccd42bc5 Mon Sep 17 00:00:00 2001 From: cc-morning <90960002+cc-morning@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:47:02 +0800 Subject: [PATCH] Fix missing clipboard content --- src/server/clipboard_service.rs | 82 +++++++++++++++++++-------------- src/server/service.rs | 34 -------------- 2 files changed, 48 insertions(+), 68 deletions(-) diff --git a/src/server/clipboard_service.rs b/src/server/clipboard_service.rs index 3e94d2824..a0711c8d9 100644 --- a/src/server/clipboard_service.rs +++ b/src/server/clipboard_service.rs @@ -1,12 +1,11 @@ -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, CONTENT, }; +use clipboard_master::{CallbackResult, ClipboardHandler, Master}; +use hbb_common::ResultType; +use std::{sync, sync::mpsc::Sender, thread}; struct State { ctx: Option, @@ -31,41 +30,56 @@ impl super::service::Reset for State { } } -struct ClipHandle { - tx: Sender, -} - -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.listen::(notify, run); + sp.run::<_>(listen::run); sp } -fn notify(tx: Sender) -> ResultType<()> { - Master::new(ClipHandle { tx }).run()?; - Ok(()) -} +mod listen { + use super::*; -fn run(sp: GenericService, state: &mut State) -> ResultType<()> { - if let Some(ctx) = state.ctx.as_mut() { - if let Some(msg) = check_clipboard(ctx, None) { - sp.send(msg); - } - 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(()) - })?; + struct ClipHandle { + tx: Sender<()>, + } + + impl ClipboardHandler for ClipHandle { + fn on_clipboard_change(&mut self) -> CallbackResult { + let _ = self.tx.send(()); + CallbackResult::Next + } + } + + fn notify(tx: Sender<()>) -> ResultType<()> { + Master::new(ClipHandle { tx }).run()?; + Ok(()) + } + + pub fn run(sp: GenericService) -> ResultType<()> { + let mut state = State::default(); + let (tx, rx) = sync::mpsc::channel::<()>(); + thread::spawn(|| { + let _ = notify(tx); + }); + + while sp.ok() { + 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(()) + })?; + + if let Ok(()) = rx.recv() { + if let Some(ctx) = state.ctx.as_mut() { + if let Some(msg) = check_clipboard(ctx, None) { + sp.send(msg); + } + } + } + } + Ok(()) } - Ok(()) } diff --git a/src/server/service.rs b/src/server/service.rs index 3e8c840a2..b60f5a8d4 100644 --- a/src/server/service.rs +++ b/src/server/service.rs @@ -1,6 +1,5 @@ use super::*; use std::{ - sync::{self, mpsc::Sender}, thread::{self, JoinHandle}, time, }; @@ -154,39 +153,6 @@ impl> ServiceTmpl { } } - pub fn listen(&self, notify: N, callback: F) - where - N: 'static + FnMut(Sender) -> 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(&self, interval_ms: u64, callback: F) where F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,