From 209ec8e4d595d844e1013ca4f66a2e5e2cd4e9a9 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 20 May 2024 22:46:32 +0500 Subject: [PATCH] gradual move --- src/lib.rs | 1 + src/utils.rs | 40 +++++++++++++++ src/virtual_device.rs | 112 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 src/utils.rs diff --git a/src/lib.rs b/src/lib.rs index 8c1f059..35913ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ macro_rules! uin { pub mod key_codes; mod virtual_device; +mod utils; pub use crate::key_codes::*; pub use virtual_device::*; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..4695ec4 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,40 @@ +use std::cmp::min; +use crate::Coord; + +#[derive(PartialEq, Copy, Clone, Default, Debug)] +pub struct GradualMove { + pub x_direction: Coord, + pub y_direction: Coord, + pub both_move: Coord, + pub move_only_x: Coord, + pub move_only_y: Coord, +} + +impl GradualMove { + pub fn calculate(x: Coord, y: Coord) -> Self { + // println!("Diff X: {}, Diff Y: {}", mouse_diff.x, mouse_diff.y); + + let x_direction = x.signum(); + let y_direction = y.signum(); + + let move_x = x.abs(); + let move_y = y.abs(); + + let both_move = min(move_x, move_y); + + // println!("Dir X: {}, Dir Y: {}, Move both: {}", x_direction, y_direction, both_move); + + let move_only_x = move_x - both_move; + let move_only_y = move_y - both_move; + + // println!("Only X: {}, Only Y: {}\n", move_only_x, move_only_y); + + Self { + x_direction, + y_direction, + both_move, + move_only_x, + move_only_y, + } + } +} \ No newline at end of file diff --git a/src/virtual_device.rs b/src/virtual_device.rs index 2542e52..9c20b03 100644 --- a/src/virtual_device.rs +++ b/src/virtual_device.rs @@ -11,6 +11,7 @@ use crossbeam_channel::{Sender, Receiver, bounded}; use libc::gettimeofday; use crate::*; +use crate::utils::GradualMove; pub type Result = std::result::Result>; pub type EmptyResult = Result<()>; @@ -424,6 +425,42 @@ impl VirtualDevice { ] } + #[inline] + pub fn gradual_move_mouse_raw(&mut self, x: Coord, y: Coord) -> Result<()> { + let gradual_move = GradualMove::calculate(x, y); + + for _ in 0..gradual_move.both_move { + self.move_mouse_raw(gradual_move.x_direction, gradual_move.y_direction)?; + } + for _ in 0..gradual_move.move_only_x { + self.move_mouse_raw_x(gradual_move.x_direction)?; + } + for _ in 0..gradual_move.move_only_y { + self.move_mouse_raw_y(gradual_move.y_direction)?; + } + self.synchronize()?; + + Ok(()) + } + + #[inline] + pub fn buffered_gradual_move_mouse(&mut self, x: Coord, y: Coord) -> Vec { + let mut write_buffer: Vec = vec![]; + let gradual_move = GradualMove::calculate(x, y); + + for _ in 0..gradual_move.both_move { + write_buffer.extend(self.buffered_move_mouse(gradual_move.x_direction, gradual_move.y_direction)); + } + for _ in 0..gradual_move.move_only_x { + write_buffer.extend(self.buffered_move_mouse_x(gradual_move.x_direction)); + } + for _ in 0..gradual_move.move_only_y { + write_buffer.extend(self.buffered_move_mouse_y(gradual_move.y_direction)); + } + + write_buffer + } + #[inline] pub fn move_mouse_x(&mut self, x: Coord) -> EmptyResult { self.write_batch(vec![ @@ -449,6 +486,43 @@ impl VirtualDevice { ]) } + // #[inline] + // pub fn move_mouse_with_options(&mut self, x: Coord, y: Coord, buffered: bool, gradual_move: bool, raw_operations:bool) -> EmptyResult { + // let (mouse_x, mouse_y, mouse) = match buffered { + // true => { + // ( + // Self::buffered_move_mouse_x, + // Self::buffered_move_mouse_y, + // Self::buffered_move_mouse, + // ) + // } + // false => { + // match raw_operations { + // true => { + // ( + // Self::move_mouse_raw_x, + // Self::move_mouse_raw_y, + // Self::move_mouse_raw, + // ) + // } + // false => { + // ( + // Self::move_mouse_x, + // Self::move_mouse_y, + // Self::move_mouse, + // ) + // } + // } + // } + // }; + // match gradual_move { + // true => { + // + // } + // false => {} + // } + // } + #[inline] pub fn scroll_raw_x(&mut self, value: Coord) -> EmptyResult { self.write(EV_REL, REL_HWHEEL, value) @@ -483,6 +557,44 @@ impl VirtualDevice { ]) } + #[inline] + pub fn gradual_scroll_raw(&mut self, x: Coord, y: Coord) -> Result<()> { + let gradual_move = GradualMove::calculate(x, y); + + for _ in 0..gradual_move.both_move { + self.scroll_raw_x(gradual_move.x_direction)?; + self.scroll_raw_y(gradual_move.y_direction)?; + } + for _ in 0..gradual_move.move_only_x { + self.scroll_raw_x(gradual_move.x_direction)?; + } + for _ in 0..gradual_move.move_only_y { + self.scroll_raw_y(gradual_move.y_direction)?; + } + self.synchronize()?; + + Ok(()) + } + + #[inline] + pub fn buffered_gradual_scroll(&mut self, x: Coord, y: Coord) -> Vec { + let mut write_buffer: Vec = vec![]; + let gradual_move = GradualMove::calculate(x, y); + + for _ in 0..gradual_move.both_move { + write_buffer.extend(self.buffered_scroll_x(gradual_move.x_direction)); + write_buffer.extend(self.buffered_scroll_y(gradual_move.y_direction)); + } + for _ in 0..gradual_move.move_only_x { + write_buffer.extend(self.buffered_scroll_x(gradual_move.x_direction)); + } + for _ in 0..gradual_move.move_only_y { + write_buffer.extend(self.buffered_scroll_y(gradual_move.y_direction)); + } + + write_buffer + } + #[inline] pub fn scroll_y(&mut self, value: Coord) -> EmptyResult { self.write_batch(vec![