Clipboard listener lifecycle follows service
This commit is contained in:
parent
318e3cbd7d
commit
0cf2db9239
@ -6,26 +6,16 @@ pub use crate::common::{
|
|||||||
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
|
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
|
||||||
use hbb_common::{anyhow, ResultType};
|
use hbb_common::{anyhow, ResultType};
|
||||||
use std::{
|
use std::{
|
||||||
sync,
|
io, sync,
|
||||||
sync::mpsc::{Receiver, Sender},
|
sync::{
|
||||||
thread::{self},
|
atomic::{AtomicBool, Ordering},
|
||||||
|
mpsc::SyncSender,
|
||||||
|
},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn new() -> GenericService {
|
pub fn new() -> GenericService {
|
||||||
let sp = GenericService::new(NAME, true);
|
let sp = GenericService::new(NAME, true);
|
||||||
|
|
||||||
// Listening service needs to run for a long time,
|
|
||||||
// otherwise it will cause part of the content to
|
|
||||||
// be missed during the closure of the clipboard
|
|
||||||
// (during the closure, the content copied by the
|
|
||||||
// remote machine will be missed, and the clipboard
|
|
||||||
// will not be synchronized immediately when it is
|
|
||||||
// opened again), and CONTENT will not be updated
|
|
||||||
thread::spawn(|| {
|
|
||||||
let _ = listen::notify();
|
|
||||||
});
|
|
||||||
|
|
||||||
sp.run::<_>(listen::run);
|
sp.run::<_>(listen::run);
|
||||||
sp
|
sp
|
||||||
}
|
}
|
||||||
@ -33,53 +23,56 @@ pub fn new() -> GenericService {
|
|||||||
mod listen {
|
mod listen {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
static mut CHANNEL: Option<(Sender<()>, Receiver<()>)> = None;
|
static RUNNING: AtomicBool = AtomicBool::new(true);
|
||||||
static mut CTX: Option<ClipboardContext> = None;
|
|
||||||
static WAIT: Duration = Duration::from_millis(1500);
|
static WAIT: Duration = Duration::from_millis(1500);
|
||||||
|
|
||||||
struct ClipHandle;
|
struct ClipHandle {
|
||||||
|
tx: SyncSender<()>,
|
||||||
|
}
|
||||||
|
|
||||||
impl ClipboardHandler for ClipHandle {
|
impl ClipboardHandler for ClipHandle {
|
||||||
fn on_clipboard_change(&mut self) -> CallbackResult {
|
fn on_clipboard_change(&mut self) -> CallbackResult {
|
||||||
if let Some((tx, _rx)) = unsafe { CHANNEL.as_ref() } {
|
if !RUNNING.load(Ordering::SeqCst) {
|
||||||
let _ = tx.send(());
|
return CallbackResult::Stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let _ = self.tx.send(());
|
||||||
CallbackResult::Next
|
CallbackResult::Next
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn notify() -> ResultType<()> {
|
fn on_clipboard_error(&mut self, error: io::Error) -> CallbackResult {
|
||||||
Master::new(ClipHandle).run()?;
|
if !RUNNING.load(Ordering::SeqCst) {
|
||||||
Ok(())
|
CallbackResult::Stop
|
||||||
}
|
} else {
|
||||||
|
CallbackResult::StopWithError(error)
|
||||||
pub fn run(sp: GenericService) -> ResultType<()> {
|
|
||||||
unsafe {
|
|
||||||
if CHANNEL.is_none() {
|
|
||||||
CHANNEL = Some(sync::mpsc::channel());
|
|
||||||
}
|
|
||||||
|
|
||||||
if CTX.is_none() {
|
|
||||||
match ClipboardContext::new() {
|
|
||||||
Ok(ctx) => {
|
|
||||||
CTX = Some(ctx);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
log::error!("Failed to start {}: {}", NAME, err);
|
|
||||||
return Err(anyhow::Error::from(err));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
pub async fn run(sp: GenericService) -> ResultType<()> {
|
||||||
|
let mut ctx = match ClipboardContext::new() {
|
||||||
|
Ok(ctx) => ctx,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to start {}: {}", NAME, err);
|
||||||
|
return Err(anyhow::Error::from(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !RUNNING.load(Ordering::SeqCst) {
|
||||||
|
RUNNING.store(true, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (tx, rx) = sync::mpsc::sync_channel(12);
|
||||||
|
let listener = tokio::spawn(async {
|
||||||
|
log::info!("Clipboard listener running!");
|
||||||
|
let _ = Master::new(ClipHandle { tx }).run();
|
||||||
|
});
|
||||||
|
|
||||||
while sp.ok() {
|
while sp.ok() {
|
||||||
if let Some((_tx, rx)) = unsafe { CHANNEL.as_ref() } {
|
if let Ok(_) = rx.recv_timeout(WAIT) {
|
||||||
if let Ok(_) = rx.recv_timeout(WAIT) {
|
if let Some(msg) = check_clipboard(&mut ctx, None) {
|
||||||
if let Some(mut ctx) = unsafe { CTX.as_mut() } {
|
sp.send(msg);
|
||||||
if let Some(msg) = check_clipboard(&mut ctx, None) {
|
|
||||||
sp.send(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +86,21 @@ mod listen {
|
|||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
RUNNING.store(false, Ordering::SeqCst);
|
||||||
|
trigger(&mut ctx);
|
||||||
|
let _ = listener.await;
|
||||||
|
log::info!("Clipboard listener stopped!");
|
||||||
|
}
|
||||||
|
|
||||||
*CONTENT.lock().unwrap() = Default::default();
|
*CONTENT.lock().unwrap() = Default::default();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trigger(ctx: &mut ClipboardContext) {
|
||||||
|
let _ = match ctx.get_text() {
|
||||||
|
Ok(text) => ctx.set_text(text),
|
||||||
|
Err(_) => ctx.set_text(Default::default()),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user