initialize change_count to 1 so that first data always got

This commit is contained in:
rustdesk 2024-06-30 23:27:33 +08:00
parent 01672bc697
commit 84b5cd70ed

View File

@ -1,5 +1,4 @@
use std::{ use std::{
borrow::Cow,
collections::HashMap, collections::HashMap,
future::Future, future::Future,
sync::{ sync::{
@ -10,7 +9,6 @@ use std::{
}; };
use clipboard_master::{CallbackResult, ClipboardHandler, Master, Shutdown}; use clipboard_master::{CallbackResult, ClipboardHandler, Master, Shutdown};
use scrap::libc::RUSAGE_SELF;
use serde_json::Value; use serde_json::Value;
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
@ -293,10 +291,7 @@ pub fn check_clipboard(
} else { } else {
CONTENT.clone() CONTENT.clone()
}; };
let content = { let content = ctx2.get();
let _lock = ARBOARD_MTX.lock().unwrap();
ctx2.get()
};
if let Ok(content) = content { if let Ok(content) = content {
if !content.is_empty() { if !content.is_empty() {
let changed = content != *old.lock().unwrap(); let changed = content != *old.lock().unwrap();
@ -373,9 +368,8 @@ fn update_clipboard_(clipboard: Clipboard, old: Option<Arc<Mutex<ClipboardData>>
} else { } else {
CONTENT.clone() CONTENT.clone()
}; };
*old.lock().unwrap() = content.clone(); allow_err!(ctx.set(&content));
let _lock = ARBOARD_MTX.lock().unwrap(); *old.lock().unwrap() = content;
allow_err!(ctx.set(content));
log::debug!("{} updated on {}", CLIPBOARD_NAME, side); log::debug!("{} updated on {}", CLIPBOARD_NAME, side);
} }
Err(err) => { Err(err) => {
@ -1658,7 +1652,8 @@ impl ClipboardContext {
} }
} }
let change_count: Arc<AtomicU64> = Default::default(); // starting from 1 so that we can always get initial clipboard data no matter if change
let change_count: Arc<AtomicU64> = Arc::new(AtomicU64::new(1));
let mut shutdown = None; let mut shutdown = None;
if listen { if listen {
struct Handler(Arc<AtomicU64>); struct Handler(Arc<AtomicU64>);
@ -1702,6 +1697,7 @@ impl ClipboardContext {
pub fn get(&mut self) -> ResultType<ClipboardData> { pub fn get(&mut self) -> ResultType<ClipboardData> {
let cn = self.change_count(); let cn = self.change_count();
let _lock = ARBOARD_MTX.lock().unwrap();
// only for image for the time being, // only for image for the time being,
// because I do not want to change behavior of text clipboard for the time being // because I do not want to change behavior of text clipboard for the time being
if cn != self.1 .1 { if cn != self.1 .1 {
@ -1715,10 +1711,11 @@ impl ClipboardContext {
Ok(ClipboardData::Text(self.0.get_text()?)) Ok(ClipboardData::Text(self.0.get_text()?))
} }
fn set(&mut self, data: ClipboardData) -> ResultType<()> { fn set(&mut self, data: &ClipboardData) -> ResultType<()> {
let _lock = ARBOARD_MTX.lock().unwrap();
match data { match data {
ClipboardData::Text(s) => self.0.set_text(s)?, ClipboardData::Text(s) => self.0.set_text(s)?,
ClipboardData::Image(a, _) => self.0.set_image(a)?, ClipboardData::Image(a, _) => self.0.set_image(a.clone())?,
_ => {} _ => {}
} }
Ok(()) Ok(())