From 47d7e7ad938103193e898cee4a2e730264545cb7 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 14 Nov 2022 17:54:39 +0800 Subject: [PATCH] input service, trivial refactor for cursor Signed-off-by: fufesou --- src/server/input_service.rs | 43 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 653187bc3..4650865bd 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -126,7 +126,7 @@ pub fn new_pos() -> GenericService { } fn update_last_cursor_pos(x: i32, y: i32) { - let mut lock = LATEST_CURSOR_POS.lock().unwrap(); + let mut lock = LATEST_SYS_CURSOR_POS.lock().unwrap(); if lock.1 .0 != x || lock.1 .1 != y { (lock.0, lock.1) = (Instant::now(), (x, y)) } @@ -144,7 +144,7 @@ fn run_pos(sp: GenericService, state: &mut StatePos) -> ResultType<()> { }); let exclude = { let now = get_time(); - let lock = LATEST_INPUT_CURSOR.lock().unwrap(); + let lock = LATEST_PEER_INPUT_CURSOR.lock().unwrap(); if now - lock.time < 300 { lock.conn } else { @@ -203,8 +203,8 @@ lazy_static::lazy_static! { Arc::new(Mutex::new(Enigo::new())) }; static ref KEYS_DOWN: Arc>> = Default::default(); - static ref LATEST_INPUT_CURSOR: Arc> = Default::default(); - static ref LATEST_CURSOR_POS: Arc> = Arc::new(Mutex::new((Instant::now().sub(MOUSE_MOVE_PROTECTION_TIMEOUT), (0, 0)))); + static ref LATEST_PEER_INPUT_CURSOR: Arc> = Default::default(); + static ref LATEST_SYS_CURSOR_POS: Arc> = Arc::new(Mutex::new((Instant::now().sub(MOUSE_MOVE_PROTECTION_TIMEOUT), (0, 0)))); } static EXITING: AtomicBool = AtomicBool::new(false); @@ -397,13 +397,12 @@ fn fix_modifiers(modifiers: &[EnumOrUnknown], en: &mut Enigo, ck: i3 fn active_mouse_(conn: i32) -> bool { // out of time protection - if LATEST_CURSOR_POS.lock().unwrap().0.elapsed() > MOUSE_MOVE_PROTECTION_TIMEOUT { + if LATEST_SYS_CURSOR_POS.lock().unwrap().0.elapsed() > MOUSE_MOVE_PROTECTION_TIMEOUT { return true; } - let mut last_input = LATEST_INPUT_CURSOR.lock().unwrap(); // last conn input may be protected - if last_input.conn != conn { + if LATEST_PEER_INPUT_CURSOR.lock().unwrap().conn != conn { return false; } @@ -412,20 +411,25 @@ fn active_mouse_(conn: i32) -> bool { // check if input is in valid range match crate::get_cursor_pos() { Some((x, y)) => { + let (last_in_x, last_in_y) = { + let lock = LATEST_PEER_INPUT_CURSOR.lock().unwrap(); + (lock.x, lock.y) + }; let mut can_active = - in_actived_dist(last_input.x, x) && in_actived_dist(last_input.y, y); + in_actived_dist(last_in_x, x) && in_actived_dist(last_in_y, y); if !can_active { // Try agin // No need to care about sleep here. It's not a common case. std::thread::sleep(std::time::Duration::from_micros(10)); if let Some((x2, y2)) = crate::get_cursor_pos() { can_active = - in_actived_dist(last_input.x, x2) && in_actived_dist(last_input.y, y2); + in_actived_dist(last_in_x, x2) && in_actived_dist(last_in_y, y2); } } if !can_active { - last_input.x = INVALID_CURSOR_POS / 2; - last_input.y = INVALID_CURSOR_POS / 2; + let mut lock = LATEST_PEER_INPUT_CURSOR.lock().unwrap(); + lock.x = INVALID_CURSOR_POS / 2; + lock.y = INVALID_CURSOR_POS / 2; } can_active } @@ -446,15 +450,6 @@ fn handle_mouse_(evt: &MouseEvent, conn: i32) { crate::platform::windows::try_change_desktop(); let buttons = evt.mask >> 3; let evt_type = evt.mask & 0x7; - if evt_type == 0 { - let time = get_time(); - *LATEST_INPUT_CURSOR.lock().unwrap() = Input { - time, - conn, - x: evt.x, - y: evt.y, - }; - } let mut en = ENIGO.lock().unwrap(); #[cfg(not(target_os = "macos"))] let mut to_release = Vec::new(); @@ -479,6 +474,14 @@ fn handle_mouse_(evt: &MouseEvent, conn: i32) { } match evt_type { 0 => { + let time = get_time(); + *LATEST_PEER_INPUT_CURSOR.lock().unwrap() = Input { + time, + conn, + x: evt.x, + y: evt.y, + }; + en.mouse_move_to(evt.x, evt.y); } 1 => match buttons {