Skip to content

Commit

Permalink
gradual move
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveway committed May 20, 2024
1 parent ae4cc39 commit 209ec8e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
40 changes: 40 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
112 changes: 112 additions & 0 deletions src/virtual_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crossbeam_channel::{Sender, Receiver, bounded};
use libc::gettimeofday;

use crate::*;
use crate::utils::GradualMove;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub type EmptyResult = Result<()>;
Expand Down Expand Up @@ -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<EventParams> {
let mut write_buffer: Vec<EventParams> = 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![
Expand All @@ -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)
Expand Down Expand Up @@ -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<EventParams> {
let mut write_buffer: Vec<EventParams> = 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![
Expand Down

0 comments on commit 209ec8e

Please sign in to comment.