Fix/arboard clipboard context timeout (#7217)

* Fix. Set custom timeout for arboard clipboard

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* fix build

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* Do not block tokio runtime

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2024-02-21 22:05:27 +08:00
committed by GitHub
parent c690d5e940
commit 25afdda2b2
6 changed files with 237 additions and 104 deletions

View File

@@ -26,6 +26,8 @@ use hbb_common::tokio;
#[cfg(not(feature = "flutter"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::tokio::sync::mpsc::UnboundedSender;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
use hbb_common::{
allow_err,
anyhow::{anyhow, Context},
@@ -65,7 +67,7 @@ use crate::{
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::ui_session_interface::SessionPermissionConfig;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::{check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL};
use crate::{check_clipboard, CLIPBOARD_INTERVAL};
pub use super::lang::*;
@@ -717,47 +719,48 @@ impl Client {
//
// If clipboard update is detected, the text will be sent to all sessions by `send_text_clipboard_msg`.
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn try_start_clipboard(_ctx: Option<ClientClipboardContext>) {
fn try_start_clipboard(_ctx: Option<ClientClipboardContext>) -> Option<UnboundedReceiver<()>> {
let mut clipboard_lock = TEXT_CLIPBOARD_STATE.lock().unwrap();
if clipboard_lock.running {
return;
return None;
}
clipboard_lock.running = true;
let (tx, rx) = unbounded_channel();
match ClipboardContext::new() {
Ok(mut ctx) => {
clipboard_lock.running = true;
// ignore clipboard update before service start
check_clipboard(&mut ctx, Some(&OLD_CLIPBOARD_TEXT));
std::thread::spawn(move || {
log::info!("Start text clipboard loop");
loop {
std::thread::sleep(Duration::from_millis(CLIPBOARD_INTERVAL));
if !TEXT_CLIPBOARD_STATE.lock().unwrap().running {
break;
}
log::info!("Start text clipboard loop");
std::thread::spawn(move || {
let mut is_sent = false;
let mut ctx = None;
loop {
if !TEXT_CLIPBOARD_STATE.lock().unwrap().running {
break;
}
if !TEXT_CLIPBOARD_STATE.lock().unwrap().is_required {
continue;
}
if !TEXT_CLIPBOARD_STATE.lock().unwrap().is_required {
continue;
}
if let Some(msg) = check_clipboard(&mut ctx, Some(&OLD_CLIPBOARD_TEXT)) {
#[cfg(feature = "flutter")]
crate::flutter::send_text_clipboard_msg(msg);
#[cfg(not(feature = "flutter"))]
if let Some(ctx) = &_ctx {
if ctx.cfg.is_text_clipboard_required() {
let _ = ctx.tx.send(Data::Message(msg));
}
}
if let Some(msg) = check_clipboard(&mut ctx, Some(&OLD_CLIPBOARD_TEXT)) {
#[cfg(feature = "flutter")]
crate::flutter::send_text_clipboard_msg(msg);
#[cfg(not(feature = "flutter"))]
if let Some(ctx) = &_ctx {
if ctx.cfg.is_text_clipboard_required() {
let _ = ctx.tx.send(Data::Message(msg));
}
}
log::info!("Stop text clipboard loop");
});
}
if !is_sent {
is_sent = true;
tx.send(()).ok();
}
std::thread::sleep(Duration::from_millis(CLIPBOARD_INTERVAL));
}
Err(err) => {
log::error!("Failed to start clipboard service of client: {}", err);
}
}
log::info!("Stop text clipboard loop");
});
Some(rx)
}
#[inline]
@@ -1532,8 +1535,7 @@ impl LoginConfigHandler {
n += 1;
} else if q == "custom" {
let config = self.load_config();
let allow_more =
!crate::using_public_server() || self.direct == Some(true);
let allow_more = !crate::using_public_server() || self.direct == Some(true);
let quality = if config.custom_image_quality.is_empty() {
50
} else {