Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add movemouse-smooth-diagonals #568

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ fn parse_defcfg(expr: &[SExpr]) -> Result<HashMap<String, String>> {
"log-layer-changes",
"delegate-to-first-layer",
"linux-continue-if-no-devs-found",
"movemouse-smooth-diagonals",
"movemouse-inherit-accel-state",
];
let mut cfg = HashMap::default();
Expand Down
120 changes: 95 additions & 25 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ pub struct Kanata {
/// If a mousemove action is active and another mousemove action is activated,
/// reuse the acceleration state.
movemouse_inherit_accel_state: bool,
/// Removes jaggedneess of vertical and horizontal mouse movements when used
/// simultaneously at the cost of increased mousemove actions latency.
movemouse_smooth_diagonals: bool,
/// If movemouse_smooth_diagonals is enabled, the previous mouse actions
/// gets stored in this buffer and if the next movemouse action is opposite axis
/// than the one stored in the buffer, both events are outputted at the same time.
movemouse_buffer: Option<(Axis, CalculatedMouseMove)>,
}

#[derive(PartialEq, Clone, Copy)]
pub enum Axis {
Vertical,
Horizontal,
}

impl Into<Axis> for MoveDirection {
fn into(self) -> Axis {
match self {
MoveDirection::Up | MoveDirection::Down => Axis::Vertical,
MoveDirection::Left | MoveDirection::Right => Axis::Horizontal,
}
}
}

#[derive(Clone, Copy)]
pub struct CalculatedMouseMove {
pub direction: MoveDirection,
pub distance: u16,
}

pub struct ScrollState {
Expand Down Expand Up @@ -349,6 +377,11 @@ impl Kanata {
dynamic_macros: Default::default(),
log_layer_changes,
caps_word: None,
movemouse_smooth_diagonals: cfg
.items
.get("movemouse-smooth-diagonals")
.map(|s| TRUE_VALUES.contains(&s.to_lowercase().as_str()))
.unwrap_or_default(),
movemouse_inherit_accel_state: cfg
.items
.get("movemouse-inherit-accel-state")
Expand All @@ -358,6 +391,7 @@ impl Kanata {
defcfg_items: cfg.items,
waiting_for_idle: HashSet::default(),
ticks_since_idle: 0,
movemouse_buffer: None,
})
}

Expand Down Expand Up @@ -392,6 +426,11 @@ impl Kanata {
self.sequences = cfg.sequences;
self.overrides = cfg.overrides;
self.log_layer_changes = log_layer_changes;
self.movemouse_smooth_diagonals = cfg
.items
.get("movemouse-smooth-diagonals")
.map(|s| TRUE_VALUES.contains(&s.to_lowercase().as_str()))
.unwrap_or_default();
self.movemouse_inherit_accel_state = cfg
.items
.get("movemouse-inherit-accel-state")
Expand Down Expand Up @@ -539,7 +578,32 @@ impl Kanata {
let scaled_distance =
apply_mouse_distance_modifiers(mmsv.distance, &self.move_mouse_speed_modifiers);
log::debug!("handle_move_mouse: scaled vdistance: {}", scaled_distance);
self.kbd_out.move_mouse(mmsv.direction, scaled_distance)?;

let current_move = CalculatedMouseMove {
direction: mmsv.direction,
distance: scaled_distance,
};

if self.movemouse_smooth_diagonals {
let axis: Axis = current_move.direction.into();
match &self.movemouse_buffer {
Some((previous_axis, previous_move)) => {
if axis == *previous_axis {
self.kbd_out.move_mouse(*previous_move)?;
self.movemouse_buffer = Some((axis, current_move));
} else {
self.kbd_out
.move_mouse_many(&[*previous_move, current_move])?;
self.movemouse_buffer = None;
}
}
None => {
self.movemouse_buffer = Some((axis, current_move));
}
}
} else {
self.kbd_out.move_mouse(current_move)?;
}
} else {
mmsv.ticks_until_move -= 1;
}
Expand All @@ -561,7 +625,32 @@ impl Kanata {
let scaled_distance =
apply_mouse_distance_modifiers(mmsh.distance, &self.move_mouse_speed_modifiers);
log::debug!("handle_move_mouse: scaled hdistance: {}", scaled_distance);
self.kbd_out.move_mouse(mmsh.direction, scaled_distance)?;

let current_move = CalculatedMouseMove {
direction: mmsh.direction,
distance: scaled_distance,
};

if self.movemouse_smooth_diagonals {
let axis: Axis = current_move.direction.into();
match &self.movemouse_buffer {
Some((previous_axis, previous_move)) => {
if axis == *previous_axis {
self.kbd_out.move_mouse(*previous_move)?;
self.movemouse_buffer = Some((axis, current_move));
} else {
self.kbd_out
.move_mouse_many(&[*previous_move, current_move])?;
self.movemouse_buffer = None;
}
}
None => {
self.movemouse_buffer = Some((axis, current_move));
}
}
} else {
self.kbd_out.move_mouse(current_move)?;
}
} else {
mmsh.ticks_until_move -= 1;
}
Expand Down Expand Up @@ -1217,7 +1306,8 @@ impl Kanata {
}
pbtn
}
CustomAction::MoveMouse { direction, .. } => {
CustomAction::MoveMouse { direction, .. }
| CustomAction::MoveMouseAccel { direction, .. } => {
match direction {
MoveDirection::Up | MoveDirection::Down => {
if let Some(move_mouse_state_vertical) =
Expand All @@ -1238,28 +1328,8 @@ impl Kanata {
}
}
}
pbtn
}
CustomAction::MoveMouseAccel { direction, .. } => {
match direction {
MoveDirection::Up | MoveDirection::Down => {
if let Some(move_mouse_state_vertical) =
&self.move_mouse_state_vertical
{
if move_mouse_state_vertical.direction == *direction {
self.move_mouse_state_vertical = None;
}
}
}
MoveDirection::Left | MoveDirection::Right => {
if let Some(move_mouse_state_horizontal) =
&self.move_mouse_state_horizontal
{
if move_mouse_state_horizontal.direction == *direction {
self.move_mouse_state_horizontal = None;
}
}
}
if self.movemouse_smooth_diagonals {
self.movemouse_buffer = None
}
pbtn
}
Expand Down
28 changes: 21 additions & 7 deletions src/oskbd/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::PathBuf;
use std::thread;

use super::*;
use crate::oskbd::KeyEvent;
use crate::{kanata::CalculatedMouseMove, oskbd::KeyEvent};
use kanata_parser::custom_action::*;
use kanata_parser::keys::*;

Expand Down Expand Up @@ -540,16 +540,30 @@ impl KbdOut {
}
}

pub fn move_mouse(&mut self, direction: MoveDirection, distance: u16) -> Result<(), io::Error> {
let (axis, distance) = match direction {
MoveDirection::Up => (RelativeAxisType::REL_Y, -i32::from(distance)),
MoveDirection::Down => (RelativeAxisType::REL_Y, i32::from(distance)),
MoveDirection::Left => (RelativeAxisType::REL_X, -i32::from(distance)),
MoveDirection::Right => (RelativeAxisType::REL_X, i32::from(distance)),
pub fn move_mouse(&mut self, mv: CalculatedMouseMove) -> Result<(), io::Error> {
let (axis, distance) = match mv.direction {
MoveDirection::Up => (RelativeAxisType::REL_Y, -i32::from(mv.distance)),
MoveDirection::Down => (RelativeAxisType::REL_Y, i32::from(mv.distance)),
MoveDirection::Left => (RelativeAxisType::REL_X, -i32::from(mv.distance)),
MoveDirection::Right => (RelativeAxisType::REL_X, i32::from(mv.distance)),
};
self.write(InputEvent::new(EventType::RELATIVE, axis.0, distance))
}

pub fn move_mouse_many(&mut self, moves: &[CalculatedMouseMove]) -> Result<(), io::Error> {
let mut events = vec![];
for mv in moves {
let (axis, distance) = match mv.direction {
MoveDirection::Up => (RelativeAxisType::REL_Y, -i32::from(mv.distance)),
MoveDirection::Down => (RelativeAxisType::REL_Y, i32::from(mv.distance)),
MoveDirection::Left => (RelativeAxisType::REL_X, -i32::from(mv.distance)),
MoveDirection::Right => (RelativeAxisType::REL_X, i32::from(mv.distance)),
};
events.push(InputEvent::new(EventType::RELATIVE, axis.0, distance));
}
self.write_many(&events)
}

pub fn set_mouse(&mut self, _x: u16, _y: u16) -> Result<(), io::Error> {
log::warn!("setmouse does not work in Linux yet. Maybe try out warpd:\n\thttps://github.com/rvaiya/warpd");
Ok(())
Expand Down