patch: add more logs

Signed-off-by: ClSlaid <cailue@bupt.edu.cn>
This commit is contained in:
ClSlaid
2023-10-16 18:42:02 +08:00
parent 9adda25e00
commit 1f91d4fa7b
5 changed files with 59 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
use std::{
collections::BTreeSet,
path::PathBuf,
sync::atomic::{AtomicBool, Ordering},
};
@@ -8,6 +9,7 @@ use hbb_common::{
log,
};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use x11_clipboard::Clipboard;
use x11rb::protocol::xproto::Atom;
@@ -23,6 +25,9 @@ use super::{encode_path_to_uri, parse_plain_uri_list, SysClipboard};
static X11_CLIPBOARD: OnceCell<Clipboard> = OnceCell::new();
// this is tested on an Arch Linux with X11
const X11_CLIPBOARD_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(70);
fn get_clip() -> Result<&'static Clipboard, CliprdrError> {
X11_CLIPBOARD.get_or_try_init(|| Clipboard::new().map_err(|_| CliprdrError::CliprdrInit))
}
@@ -32,6 +37,8 @@ pub struct X11Clipboard {
ignore_path: PathBuf,
text_uri_list: Atom,
gnome_copied_files: Atom,
former_file_list: Mutex<Vec<PathBuf>>,
}
impl X11Clipboard {
@@ -50,15 +57,18 @@ impl X11Clipboard {
stop: AtomicBool::new(false),
text_uri_list,
gnome_copied_files,
former_file_list: Mutex::new(vec![]),
})
}
fn load(&self, target: Atom) -> Result<Vec<u8>, CliprdrError> {
let clip = get_clip()?.setter.atoms.clipboard;
let prop = get_clip()?.setter.atoms.property;
log::debug!("try to load clipboard content");
// NOTE:
// # why not use `load_wait`
// load_wait is likely to wait forever, which is not what we want
get_clip()?
.load_wait(clip, target, prop)
.load(clip, target, prop, X11_CLIPBOARD_TIMEOUT)
.map_err(|_| CliprdrError::ConversionFailure)
}
@@ -90,6 +100,8 @@ impl X11Clipboard {
impl SysClipboard for X11Clipboard {
fn set_file_list(&self, paths: &[PathBuf]) -> Result<(), CliprdrError> {
*self.former_file_list.lock() = paths.to_vec();
let uri_list: Vec<String> = paths.iter().map(|pb| encode_path_to_uri(pb)).collect();
let uri_list = uri_list.join("\n");
let text_uri_list_data = uri_list.as_bytes().to_vec();
@@ -109,12 +121,22 @@ impl SysClipboard for X11Clipboard {
fn start(&self) {
self.stop.store(false, Ordering::Relaxed);
while let Ok(sth) = self.wait_file_list() {
loop {
let sth = match self.wait_file_list() {
Ok(sth) => sth,
Err(e) => {
log::warn!("failed to get file list from clipboard: {}", e);
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
};
if self.is_stopped() {
break;
}
let Some(paths) = sth else {
// just sleep
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
};
@@ -129,8 +151,20 @@ impl SysClipboard for X11Clipboard {
continue;
}
// send update to server
log::debug!("clipboard updated: {:?}", filtered);
{
let mut former = self.former_file_list.lock();
let filtered_st: BTreeSet<_> = filtered.iter().collect();
let former_st = former.iter().collect();
if filtered_st == former_st {
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
// send update to server
log::debug!("clipboard updated: {:?}", filtered);
*former = filtered;
}
if let Err(e) = send_format_list(0) {
log::warn!("failed to send format list: {}", e);
@@ -138,6 +172,7 @@ impl SysClipboard for X11Clipboard {
std::thread::sleep(std::time::Duration::from_millis(100));
}
log::debug!("stop listening file related atoms on clipboard");
}
fn send_format_list(&self, conn_id: i32) -> Result<(), CliprdrError> {