Skip to content

Commit

Permalink
feat: add movemouse-inherit-accel-state cfg option
Browse files Browse the repository at this point in the history
  • Loading branch information
rszyma committed Sep 14, 2023
1 parent 4da1456 commit a6853e4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ fn parse_defcfg(expr: &[SExpr]) -> Result<HashMap<String, String>> {
"log-layer-changes",
"delegate-to-first-layer",
"linux-continue-if-no-devs-found",
"movemouse-inherit-accel-state",
];
let mut cfg = HashMap::default();
let mut exprs = check_first_expr(expr.iter(), "defcfg")?;
Expand Down
67 changes: 49 additions & 18 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ pub struct Kanata {
pub waiting_for_idle: HashSet<FakeKeyOnIdle>,
/// Number of ticks since kanata was idle.
pub ticks_since_idle: u16,
/// If a mousemove action is active and another mousemove action is activated,
/// reuse the acceleration state.
movemouse_inherit_accel_state: bool,
}

pub struct ScrollState {
Expand All @@ -161,6 +164,7 @@ pub struct MoveMouseState {
pub move_mouse_accel_state: Option<MoveMouseAccelState>,
}

#[derive(Clone, Copy)]
pub struct MoveMouseAccelState {
pub accel_ticks_from_min: u16,
pub accel_ticks_until_max: u16,
Expand Down Expand Up @@ -345,6 +349,11 @@ impl Kanata {
dynamic_macros: Default::default(),
log_layer_changes,
caps_word: None,
movemouse_inherit_accel_state: cfg
.items
.get("movemouse-inherit-accel-state")
.map(|s| TRUE_VALUES.contains(&s.to_lowercase().as_str()))
.unwrap_or_default(),
#[cfg(target_os = "linux")]
defcfg_items: cfg.items,
waiting_for_idle: HashSet::default(),
Expand Down Expand Up @@ -918,24 +927,52 @@ impl Kanata {
min_distance,
max_distance,
} => {
let f_max_distance: f64 = *max_distance as f64;
let f_min_distance: f64 = *min_distance as f64;
let f_accel_time: f64 = *accel_time as f64;
let increment = (f_max_distance - f_min_distance) / f_accel_time;
let move_mouse_accel_state = match (
self.movemouse_inherit_accel_state,
&self.move_mouse_state_horizontal,
&self.move_mouse_state_vertical,
) {
(
true,
Some(MoveMouseState {
move_mouse_accel_state: Some(s),
..
}),
_,
)
| (
true,
_,
Some(MoveMouseState {
move_mouse_accel_state: Some(s),
..
}),
) => *s,
_ => {
let f_max_distance: f64 = *max_distance as f64;
let f_min_distance: f64 = *min_distance as f64;
let f_accel_time: f64 = *accel_time as f64;
let increment =
(f_max_distance - f_min_distance) / f_accel_time;

MoveMouseAccelState {
accel_ticks_from_min: 0,
accel_ticks_until_max: *accel_time,
accel_increment: increment,
min_distance: *min_distance,
max_distance: *max_distance,
}
}
};

match direction {
MoveDirection::Up | MoveDirection::Down => {
self.move_mouse_state_vertical = Some(MoveMouseState {
direction: *direction,
distance: *min_distance,
ticks_until_move: 0,
interval: *interval,
move_mouse_accel_state: Some(MoveMouseAccelState {
accel_ticks_from_min: 0,
accel_ticks_until_max: *accel_time,
accel_increment: increment,
min_distance: *min_distance,
max_distance: *max_distance,
}),
move_mouse_accel_state: Some(move_mouse_accel_state),
})
}
MoveDirection::Left | MoveDirection::Right => {
Expand All @@ -944,13 +981,7 @@ impl Kanata {
distance: *min_distance,
ticks_until_move: 0,
interval: *interval,
move_mouse_accel_state: Some(MoveMouseAccelState {
accel_ticks_from_min: 0,
accel_ticks_until_max: *accel_time,
accel_increment: increment,
min_distance: *min_distance,
max_distance: *max_distance,
}),
move_mouse_accel_state: Some(move_mouse_accel_state),
})
}
}
Expand Down

0 comments on commit a6853e4

Please sign in to comment.