input service, trivial refactor for cursor

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-11-14 17:54:39 +08:00
parent 3d5be47b1b
commit 47d7e7ad93

View File

@ -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<Mutex<HashMap<u64, Instant>>> = Default::default();
static ref LATEST_INPUT_CURSOR: Arc<Mutex<Input>> = Default::default();
static ref LATEST_CURSOR_POS: Arc<Mutex<(Instant, (i32, i32))>> = Arc::new(Mutex::new((Instant::now().sub(MOUSE_MOVE_PROTECTION_TIMEOUT), (0, 0))));
static ref LATEST_PEER_INPUT_CURSOR: Arc<Mutex<Input>> = Default::default();
static ref LATEST_SYS_CURSOR_POS: Arc<Mutex<(Instant, (i32, i32))>> = 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<ControlKey>], 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 {