Fix lost clipboard content again

This commit is contained in:
cc-morning 2021-10-25 16:25:23 +08:00
parent 972ec8d79d
commit 5164f76919

View File

@ -4,34 +4,25 @@ pub use crate::common::{
CONTENT, CONTENT,
}; };
use clipboard_master::{CallbackResult, ClipboardHandler, Master}; use clipboard_master::{CallbackResult, ClipboardHandler, Master};
use hbb_common::ResultType; use hbb_common::{anyhow, ResultType};
use std::{sync, sync::mpsc::Sender, thread}; use std::{
sync,
struct State { sync::mpsc::{Receiver, Sender},
ctx: Option<ClipboardContext>, thread,
} time::Duration,
};
impl Default for State {
fn default() -> Self {
let ctx = match ClipboardContext::new() {
Ok(ctx) => Some(ctx),
Err(err) => {
log::error!("Failed to start {}: {}", NAME, err);
None
}
};
Self { ctx }
}
}
impl super::service::Reset for State {
fn reset(&mut self) {
*CONTENT.lock().unwrap() = Default::default();
}
}
pub fn new() -> GenericService { pub fn new() -> GenericService {
let sp = GenericService::new(NAME, true); let sp = GenericService::new(NAME, true);
thread::spawn(|| {
let (tx, rx) = sync::mpsc::channel();
unsafe {
listen::RECEIVER = Some(rx);
}
let _ = listen::notify(tx);
});
sp.run::<_>(listen::run); sp.run::<_>(listen::run);
sp sp
} }
@ -39,6 +30,10 @@ pub fn new() -> GenericService {
mod listen { mod listen {
use super::*; use super::*;
pub(super) static mut RECEIVER: Option<Receiver<()>> = None;
static mut CTX: Option<ClipboardContext> = None;
static WAIT: Duration = Duration::from_millis(1000);
struct ClipHandle { struct ClipHandle {
tx: Sender<()>, tx: Sender<()>,
} }
@ -50,19 +45,33 @@ mod listen {
} }
} }
fn notify(tx: Sender<()>) -> ResultType<()> { pub fn notify(tx: Sender<()>) -> ResultType<()> {
Master::new(ClipHandle { tx }).run()?; Master::new(ClipHandle { tx }).run()?;
Ok(()) Ok(())
} }
pub fn run(sp: GenericService) -> ResultType<()> { pub fn run(sp: GenericService) -> ResultType<()> {
let mut state = State::default(); if unsafe { CTX.as_ref() }.is_none() {
let (tx, rx) = sync::mpsc::channel::<()>(); match ClipboardContext::new() {
thread::spawn(|| { Ok(ctx) => unsafe {
let _ = notify(tx); CTX = Some(ctx);
}); },
Err(err) => {
log::error!("Failed to start {}: {}", NAME, err);
return Err(anyhow::Error::from(err));
}
};
}
while sp.ok() { while sp.ok() {
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);
}
}
}
sp.snapshot(|sps| { sp.snapshot(|sps| {
let txt = crate::CONTENT.lock().unwrap().clone(); let txt = crate::CONTENT.lock().unwrap().clone();
if !txt.is_empty() { if !txt.is_empty() {
@ -71,15 +80,9 @@ mod listen {
} }
Ok(()) 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);
}
}
}
} }
*CONTENT.lock().unwrap() = Default::default();
Ok(()) Ok(())
} }
} }