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::{
|
|
|
|
sync,
|
|
|
|
sync::mpsc::{Receiver, Sender},
|
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2021-03-29 15:59:14 +08:00
|
|
|
|
|
|
|
pub fn new() -> GenericService {
|
2021-05-25 12:01:27 +08:00
|
|
|
let sp = GenericService::new(NAME, true);
|
2021-10-25 16:25:23 +08:00
|
|
|
|
|
|
|
thread::spawn(|| {
|
|
|
|
let (tx, rx) = sync::mpsc::channel();
|
|
|
|
unsafe {
|
|
|
|
listen::RECEIVER = Some(rx);
|
|
|
|
}
|
|
|
|
let _ = listen::notify(tx);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2021-10-25 16:25:23 +08:00
|
|
|
pub(super) static mut RECEIVER: Option<Receiver<()>> = None;
|
|
|
|
static mut CTX: Option<ClipboardContext> = None;
|
|
|
|
static WAIT: Duration = Duration::from_millis(1000);
|
|
|
|
|
2021-10-24 23:47:02 +08:00
|
|
|
struct ClipHandle {
|
|
|
|
tx: Sender<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClipboardHandler for ClipHandle {
|
|
|
|
fn on_clipboard_change(&mut self) -> CallbackResult {
|
|
|
|
let _ = self.tx.send(());
|
|
|
|
CallbackResult::Next
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
2021-10-24 23:47:02 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 16:25:23 +08:00
|
|
|
pub fn notify(tx: Sender<()>) -> ResultType<()> {
|
2021-10-24 23:47:02 +08:00
|
|
|
Master::new(ClipHandle { tx }).run()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(sp: GenericService) -> ResultType<()> {
|
2021-10-25 16:25:23 +08:00
|
|
|
if unsafe { CTX.as_ref() }.is_none() {
|
|
|
|
match ClipboardContext::new() {
|
|
|
|
Ok(ctx) => unsafe {
|
|
|
|
CTX = Some(ctx);
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("Failed to start {}: {}", NAME, err);
|
|
|
|
return Err(anyhow::Error::from(err));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-10-24 23:47:02 +08:00
|
|
|
|
|
|
|
while sp.ok() {
|
2021-10-25 16:25:23 +08:00
|
|
|
if let Ok(_) = unsafe { RECEIVER.as_ref() }.unwrap().recv_timeout(WAIT) {
|
|
|
|
if let Some(mut ctx) = unsafe { CTX.as_mut() } {
|
|
|
|
if let Some(msg) = check_clipboard(&mut ctx, None) {
|
|
|
|
sp.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
*CONTENT.lock().unwrap() = Default::default();
|
2021-10-24 23:47:02 +08:00
|
|
|
Ok(())
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
}
|