patch: fix arboard pollution

Signed-off-by: ClSlaid <cailue@bupt.edu.cn>
This commit is contained in:
ClSlaid 2023-10-28 09:55:34 +08:00
parent 053723647b
commit ed0ded33b7
No known key found for this signature in database
GPG Key ID: E0A5F564C51C056E
5 changed files with 78 additions and 2 deletions

2
Cargo.lock generated
View File

@ -5294,6 +5294,8 @@ dependencies = [
"winreg 0.11.0", "winreg 0.11.0",
"winres", "winres",
"wol-rs", "wol-rs",
"x11-clipboard",
"x11rb 0.12.0",
"zip", "zip",
] ]

View File

@ -132,6 +132,8 @@ dbus = "0.9"
dbus-crossroads = "0.5" dbus-crossroads = "0.5"
pam = { git="https://github.com/fufesou/pam", optional = true } pam = { git="https://github.com/fufesou/pam", optional = true }
users = { version = "0.11" } users = { version = "0.11" }
x11-clipboard = {git="https://github.com/clslaid/x11-clipboard", branch = "feat/store-batch"}
x11rb = {version = "0.12", features = ["all-extensions"]}
[target.'cfg(target_os = "android")'.dependencies] [target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13" android_logger = "0.13"

View File

@ -122,7 +122,7 @@ impl LocalFile {
let wstr: WString<utf16string::LE> = WString::from(&path); let wstr: WString<utf16string::LE> = WString::from(&path);
let name = wstr.as_bytes(); let name = wstr.as_bytes();
log::debug!( log::trace!(
"put file to list: name_len {}, name {}", "put file to list: name_len {}, name {}",
name.len(), name.len(),
&self.name &self.name

View File

@ -328,6 +328,7 @@ impl ClipboardContext {
} }
pub fn serve(&self, conn_id: i32, msg: ClipboardFile) -> Result<(), CliprdrError> { pub fn serve(&self, conn_id: i32, msg: ClipboardFile) -> Result<(), CliprdrError> {
log::debug!("serve clipboard file from conn: {}", conn_id);
if self.is_stopped() { if self.is_stopped() {
log::debug!("cliprdr stopped, restart it"); log::debug!("cliprdr stopped, restart it");
self.run()?; self.run()?;

View File

@ -11,9 +11,80 @@ pub enum GrabState {
Exit, Exit,
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(
target_os = "android",
target_os = "ios",
all(target_os = "linux", not(feature = "wayland"))
)))]
pub use arboard::Clipboard as ClipboardContext; pub use arboard::Clipboard as ClipboardContext;
#[cfg(all(target_os = "linux", not(feature = "wayland")))]
pub struct ClipboardContext {
string_setter: x11rb::protocol::xproto::Atom,
string_getter: x11rb::protocol::xproto::Atom,
text_uri_list: x11rb::protocol::xproto::Atom,
clip: x11_clipboard::Clipboard,
}
#[cfg(all(target_os = "linux", not(feature = "wayland")))]
impl ClipboardContext {
pub fn new() -> Result<Self, String> {
log::debug!("create clipboard context");
let clip = x11_clipboard::Clipboard::new().map_err(|e| e.to_string())?;
let string_getter = clip
.getter
.get_atom("UTF8_STRING")
.map_err(|e| e.to_string())?;
let string_setter = clip
.setter
.get_atom("UTF8_STRING")
.map_err(|e| e.to_string())?;
let text_uri_list = clip
.getter
.get_atom("text/uri-list")
.map_err(|e| e.to_string())?;
log::debug!("clipboard context created");
Ok(Self {
clip,
text_uri_list,
string_setter,
string_getter,
})
}
pub fn get_text(&mut self) -> Result<String, String> {
let clip = self.clip.getter.atoms.clipboard;
let prop = self.clip.getter.atoms.property;
log::trace!("clipboard get text");
const TIMEOUT: std::time::Duration = std::time::Duration::from_millis(100);
let text_content = self
.clip
.load(clip, self.string_getter, prop, TIMEOUT)
.map_err(|e| e.to_string())?;
let file_urls = self.clip.load(clip, self.text_uri_list, prop, TIMEOUT);
if file_urls.is_err() || file_urls.as_ref().unwrap().is_empty() {
log::trace!("clipboard get text, no file urls");
String::from_utf8(text_content).map_err(|e| e.to_string())
} else {
log::trace!("clipboard get text, file urls found");
Err("clipboard polluted".to_owned())
}
}
pub fn set_text(&mut self, content: String) -> Result<(), String> {
let clip = self.clip.setter.atoms.clipboard;
let value = content.into_bytes();
self.clip
.store(clip, self.string_setter, value)
.map_err(|e| e.to_string())
}
}
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::compress::decompress; use hbb_common::compress::decompress;
use hbb_common::{ use hbb_common::{