periodicly get cursor pos on conn

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-03-11 17:18:13 +08:00
parent 2e579d6e31
commit e77fc25836
2 changed files with 72 additions and 33 deletions

View File

@ -541,7 +541,9 @@ impl Connection {
conn.reset_resolution(); conn.reset_resolution();
ALIVE_CONNS.lock().unwrap().retain(|&c| c != id); ALIVE_CONNS.lock().unwrap().retain(|&c| c != id);
if let Some(s) = conn.server.upgrade() { if let Some(s) = conn.server.upgrade() {
s.write().unwrap().remove_connection(&conn.inner); let mut s = s.write().unwrap();
s.remove_connection(&conn.inner);
try_stop_record_cursor_pos();
} }
log::info!("#{} connection loop exited", id); log::info!("#{} connection loop exited", id);
} }
@ -948,9 +950,9 @@ impl Connection {
if !self.audio_enabled() { if !self.audio_enabled() {
noperms.push(super::audio_service::NAME); noperms.push(super::audio_service::NAME);
} }
s.write() let mut s = s.write().unwrap();
.unwrap() s.add_connection(self.inner.clone(), &noperms);
.add_connection(self.inner.clone(), &noperms); try_start_record_cursor_pos();
} }
} }
} }

View File

@ -13,7 +13,8 @@ use std::{
convert::TryFrom, convert::TryFrom,
ops::Sub, ops::Sub,
sync::atomic::{AtomicBool, Ordering}, sync::atomic::{AtomicBool, Ordering},
time::Instant, thread,
time::{self, Instant},
}; };
const INVALID_CURSOR_POS: i32 = i32::MIN; const INVALID_CURSOR_POS: i32 = i32::MIN;
@ -128,6 +129,7 @@ pub fn new_pos() -> GenericService {
sp sp
} }
#[inline]
fn update_last_cursor_pos(x: i32, y: i32) { fn update_last_cursor_pos(x: i32, y: i32) {
let mut lock = LATEST_SYS_CURSOR_POS.lock().unwrap(); let mut lock = LATEST_SYS_CURSOR_POS.lock().unwrap();
if lock.1 .0 != x || lock.1 .1 != y { if lock.1 .0 != x || lock.1 .1 != y {
@ -136,28 +138,29 @@ fn update_last_cursor_pos(x: i32, y: i32) {
} }
fn run_pos(sp: GenericService, state: &mut StatePos) -> ResultType<()> { fn run_pos(sp: GenericService, state: &mut StatePos) -> ResultType<()> {
if let Some((x, y)) = crate::get_cursor_pos() { let (_, (x, y)) = *LATEST_SYS_CURSOR_POS.lock().unwrap();
update_last_cursor_pos(x, y); if state.is_moved(x, y) {
if state.is_moved(x, y) { let mut msg_out = Message::new();
let mut msg_out = Message::new(); msg_out.set_cursor_position(CursorPosition {
msg_out.set_cursor_position(CursorPosition { x,
x, y,
y, ..Default::default()
..Default::default() });
}); let exclude = {
let exclude = { let now = get_time();
let now = get_time(); let lock = LATEST_PEER_INPUT_CURSOR.lock().unwrap();
let lock = LATEST_PEER_INPUT_CURSOR.lock().unwrap(); if now - lock.time < 300 {
if now - lock.time < 300 { lock.conn
lock.conn } else {
} else { 0
0 }
} };
}; sp.send_without(msg_out, exclude);
sp.send_without(msg_out, exclude); } else if x == 0 && y == 0 {
} // Early return if cursor retains origin.
state.cursor_pos = (x, y); return Ok(());
} }
state.cursor_pos = (x, y);
sp.snapshot(|sps| { sp.snapshot(|sps| {
let mut msg_out = Message::new(); let mut msg_out = Message::new();
@ -221,6 +224,44 @@ const MOUSE_MOVE_PROTECTION_TIMEOUT: Duration = Duration::from_millis(1_000);
// Actual diff of (x,y) is (1,1) here. But 5 may be tolerant. // Actual diff of (x,y) is (1,1) here. But 5 may be tolerant.
const MOUSE_ACTIVE_DISTANCE: i32 = 5; const MOUSE_ACTIVE_DISTANCE: i32 = 5;
static RECORD_CURSOR_POS_RUNNING: AtomicBool = AtomicBool::new(false);
pub fn try_start_record_cursor_pos() {
if RECORD_CURSOR_POS_RUNNING.load(Ordering::SeqCst) {
return;
}
if *CONN_COUNT.lock().unwrap() == 0 {
return;
}
RECORD_CURSOR_POS_RUNNING.store(true, Ordering::SeqCst);
thread::spawn(|| {
let interval = time::Duration::from_millis(33);
loop {
if !RECORD_CURSOR_POS_RUNNING.load(Ordering::SeqCst) {
break;
}
let now = time::Instant::now();
if let Some((x, y)) = crate::get_cursor_pos() {
update_last_cursor_pos(x, y);
}
let elapsed = now.elapsed();
if elapsed < interval {
thread::sleep(interval - elapsed);
}
}
});
}
pub fn try_stop_record_cursor_pos() {
let count_lock = CONN_COUNT.lock().unwrap();
if *count_lock > 0 {
return;
}
RECORD_CURSOR_POS_RUNNING.store(false, Ordering::SeqCst);
}
// mac key input must be run in main thread, otherwise crash on >= osx 10.15 // mac key input must be run in main thread, otherwise crash on >= osx 10.15
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
lazy_static::lazy_static! { lazy_static::lazy_static! {
@ -717,11 +758,8 @@ pub fn handle_key(evt: &KeyEvent) {
fn reset_input() { fn reset_input() {
unsafe { unsafe {
let _lock = VIRTUAL_INPUT_MTX.lock(); let _lock = VIRTUAL_INPUT_MTX.lock();
VIRTUAL_INPUT = VirtualInput::new( VIRTUAL_INPUT =
CGEventSourceStateID::Private, VirtualInput::new(CGEventSourceStateID::Private, CGEventTapLocation::Session).ok();
CGEventTapLocation::Session,
)
.ok();
} }
} }
@ -1095,8 +1133,7 @@ fn translate_keyboard_mode(evt: &KeyEvent) {
Some(key_event::Union::Seq(seq)) => { Some(key_event::Union::Seq(seq)) => {
ENIGO.lock().unwrap().key_sequence(seq); ENIGO.lock().unwrap().key_sequence(seq);
} }
Some(key_event::Union::Chr(..)) => Some(key_event::Union::Chr(..)) => {
{
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
translate_process_code(evt.chr(), evt.down); translate_process_code(evt.chr(), evt.down);
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]