input service, trivial refactor for cursor
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
parent
3d5be47b1b
commit
47d7e7ad93
@ -126,7 +126,7 @@ pub fn new_pos() -> GenericService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update_last_cursor_pos(x: i32, y: i32) {
|
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 {
|
if lock.1 .0 != x || lock.1 .1 != y {
|
||||||
(lock.0, lock.1) = (Instant::now(), (x, 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 exclude = {
|
||||||
let now = get_time();
|
let now = get_time();
|
||||||
let lock = LATEST_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 {
|
||||||
@ -203,8 +203,8 @@ lazy_static::lazy_static! {
|
|||||||
Arc::new(Mutex::new(Enigo::new()))
|
Arc::new(Mutex::new(Enigo::new()))
|
||||||
};
|
};
|
||||||
static ref KEYS_DOWN: Arc<Mutex<HashMap<u64, Instant>>> = Default::default();
|
static ref KEYS_DOWN: Arc<Mutex<HashMap<u64, Instant>>> = Default::default();
|
||||||
static ref LATEST_INPUT_CURSOR: Arc<Mutex<Input>> = Default::default();
|
static ref LATEST_PEER_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_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);
|
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 {
|
fn active_mouse_(conn: i32) -> bool {
|
||||||
// out of time protection
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut last_input = LATEST_INPUT_CURSOR.lock().unwrap();
|
|
||||||
// last conn input may be protected
|
// last conn input may be protected
|
||||||
if last_input.conn != conn {
|
if LATEST_PEER_INPUT_CURSOR.lock().unwrap().conn != conn {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,20 +411,25 @@ fn active_mouse_(conn: i32) -> bool {
|
|||||||
// check if input is in valid range
|
// check if input is in valid range
|
||||||
match crate::get_cursor_pos() {
|
match crate::get_cursor_pos() {
|
||||||
Some((x, y)) => {
|
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 =
|
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 {
|
if !can_active {
|
||||||
// Try agin
|
// Try agin
|
||||||
// No need to care about sleep here. It's not a common case.
|
// No need to care about sleep here. It's not a common case.
|
||||||
std::thread::sleep(std::time::Duration::from_micros(10));
|
std::thread::sleep(std::time::Duration::from_micros(10));
|
||||||
if let Some((x2, y2)) = crate::get_cursor_pos() {
|
if let Some((x2, y2)) = crate::get_cursor_pos() {
|
||||||
can_active =
|
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 {
|
if !can_active {
|
||||||
last_input.x = INVALID_CURSOR_POS / 2;
|
let mut lock = LATEST_PEER_INPUT_CURSOR.lock().unwrap();
|
||||||
last_input.y = INVALID_CURSOR_POS / 2;
|
lock.x = INVALID_CURSOR_POS / 2;
|
||||||
|
lock.y = INVALID_CURSOR_POS / 2;
|
||||||
}
|
}
|
||||||
can_active
|
can_active
|
||||||
}
|
}
|
||||||
@ -446,15 +450,6 @@ fn handle_mouse_(evt: &MouseEvent, conn: i32) {
|
|||||||
crate::platform::windows::try_change_desktop();
|
crate::platform::windows::try_change_desktop();
|
||||||
let buttons = evt.mask >> 3;
|
let buttons = evt.mask >> 3;
|
||||||
let evt_type = evt.mask & 0x7;
|
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();
|
let mut en = ENIGO.lock().unwrap();
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[cfg(not(target_os = "macos"))]
|
||||||
let mut to_release = Vec::new();
|
let mut to_release = Vec::new();
|
||||||
@ -479,6 +474,14 @@ fn handle_mouse_(evt: &MouseEvent, conn: i32) {
|
|||||||
}
|
}
|
||||||
match evt_type {
|
match evt_type {
|
||||||
0 => {
|
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);
|
en.mouse_move_to(evt.x, evt.y);
|
||||||
}
|
}
|
||||||
1 => match buttons {
|
1 => match buttons {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user