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 mouse wheel events to defsrc #592

Merged
merged 16 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions parser/src/custom_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ impl TryFrom<OsCode> for MWheelDirection {
fn try_from(value: OsCode) -> Result<Self, Self::Error> {
use OsCode::*;
Ok(match value {
MouseWheelUp | MouseWheelUpHiRes => MWheelDirection::Up,
MouseWheelDown | MouseWheelDownHiRes => MWheelDirection::Down,
MouseWheelLeft | MouseWheelLeftHiRes => MWheelDirection::Left,
MouseWheelRight | MouseWheelRightHiRes => MWheelDirection::Right,
MouseWheelUp => MWheelDirection::Up,
MouseWheelDown => MWheelDirection::Down,
MouseWheelLeft => MWheelDirection::Left,
MouseWheelRight => MWheelDirection::Right,
_ => return Err(()),
})
}
Expand Down
5 changes: 0 additions & 5 deletions parser/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,11 +1038,6 @@ pub enum OsCode {
MouseWheelLeft = 747,
MouseWheelRight = 748,

MouseWheelUpHiRes = 749,
MouseWheelDownHiRes = 750,
MouseWheelLeftHiRes = 751,
MouseWheelRightHiRes = 752,

KEY_MAX = 767,
}

Expand Down
133 changes: 69 additions & 64 deletions src/kanata/linux.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, bail, Result};
use evdev::{InputEvent, InputEventKind, RelativeAxisType};
use log::info;
use parking_lot::Mutex;
use std::convert::TryFrom;
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Kanata {
let events = kbd_in.read().map_err(|e| anyhow!("failed read: {}", e))?;
log::trace!("{events:?}");

for in_event in events.into_iter() {
for in_event in events.iter().copied() {
let key_event = match KeyEvent::try_from(in_event) {
Ok(ev) => ev,
_ => {
Expand All @@ -51,75 +52,16 @@ impl Kanata {

check_for_exit(&key_event);

let KeyEvent { code, value } = key_event;

if value == KeyValue::Tap {
if key_event.value == KeyValue::Tap {
// Scroll event for sure. Only scroll events produce Tap.

let direction: MWheelDirection = code.try_into().unwrap();

match code
.try_into()
.expect("scroll event OsCode should not fail this")
{
ScrollEventKind::Standard => {
if kanata.lock().scroll_wheel_mapped {
if MAPPED_KEYS.lock().contains(&code) {
// Send this event to processing loop.
} else {
// We can't simply passthough with `write_raw`, because hi-res events will not
// be passed-through when scroll_wheel_mapped==true. If we just used
// `write_raw` here, some of the scrolls issued by kanata would be
// REL_WHEEL_HI_RES + REL_HWHEEL and some just REL_HWHEEL and an issue
// like this one would happen: https://github.com/jtroo/kanata/issues/395
//
// So to fix this case, we need to use `scroll`
// which will also send hi-res scrolls along normal scrolls.

let scroll_distance = in_event.value().unsigned_abs() as u16;

let mut kanata = kanata.lock();
kanata
.kbd_out
.scroll(
direction,
scroll_distance * HI_RES_SCROLL_UNITS_IN_LO_RES,
)
.map_err(|e| anyhow!("failed write: {}", e))?;
continue;
}
} else {
// Passthrough if none of the scroll wheel events are mapped
// in the configuration.
let mut kanata = kanata.lock();
kanata
.kbd_out
.write_raw(in_event)
.map_err(|e| anyhow!("failed write: {}", e))?;
continue;
}
}
ScrollEventKind::HiRes => {
// Don't passthrough hi-res mouse wheel events when scroll wheel is remapped,
if kanata.lock().scroll_wheel_mapped {
continue;
}
// Passthrough if none of the scroll wheel events are mapped
// in the configuration.
let mut kanata = kanata.lock();
kanata
.kbd_out
.write_raw(in_event)
.map_err(|e| anyhow!("failed write: {}", e))?;
continue;
}
if !handle_scroll(&kanata, in_event, key_event.code, &events)? {
continue;
}
} else {
// Handle normal keypresses.

// Check if this keycode is mapped in the configuration.
// If it hasn't been mapped, send it immediately.
if !MAPPED_KEYS.lock().contains(&code) {
if !MAPPED_KEYS.lock().contains(&key_event.code) {
let mut kanata = kanata.lock();
kanata
.kbd_out
Expand Down Expand Up @@ -180,3 +122,66 @@ impl Kanata {
Ok(())
}
}

/// Returns true if the scroll event should be sent to the processing loop, otherwise returns
/// false.
fn handle_scroll(
kanata: &Mutex<Kanata>,
in_event: InputEvent,
code: OsCode,
all_events: &[InputEvent],
) -> Result<bool> {
let direction: MWheelDirection = code.try_into().unwrap();
let scroll_distance = in_event.value().unsigned_abs() as u16;
match in_event.kind() {
InputEventKind::RelAxis(axis_type) => {
match axis_type {
RelativeAxisType::REL_WHEEL | RelativeAxisType::REL_HWHEEL => {
if MAPPED_KEYS.lock().contains(&code) {
return Ok(true);
}
// If we just used `write_raw` here, some of the scrolls issued by kanata would be
// REL_WHEEL_HI_RES + REL_WHEEL and some just REL_WHEEL and an issue like this one
// would happen: https://github.com/jtroo/kanata/issues/395
//
// So to fix this case, we need to use `scroll` which will also send hi-res scrolls
// along normal scrolls.
//
// However, if this is a normal scroll event, it may be sent alongside a
jtroo marked this conversation as resolved.
Show resolved Hide resolved
let mut kanata = kanata.lock();
if !all_events.iter().any(|ev| {
matches!(
ev.kind(),
InputEventKind::RelAxis(
RelativeAxisType::REL_WHEEL_HI_RES
| RelativeAxisType::REL_HWHEEL_HI_RES
)
)
}) {
kanata
.kbd_out
.scroll(direction, scroll_distance * HI_RES_SCROLL_UNITS_IN_LO_RES)
.map_err(|e| anyhow!("failed write: {}", e))?;
}
Ok(false)
}
RelativeAxisType::REL_WHEEL_HI_RES | RelativeAxisType::REL_HWHEEL_HI_RES => {
if !MAPPED_KEYS.lock().contains(&code) {
// Passthrough if the scroll wheel event is not mapped
// in the configuration.
let mut kanata = kanata.lock();
kanata
.kbd_out
.scroll(direction, scroll_distance)
.map_err(|e| anyhow!("failed write: {}", e))?;
}
// Kanata will not handle high resolution scroll events for now.
// Full notch scrolling only.
Ok(false)
}
_ => unreachable!("expect to be handling a wheel event"),
}
}
_ => unreachable!("expect to be handling a wheel event"),
}
}
11 changes: 0 additions & 11 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ pub struct Kanata {
/// 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)>,
scroll_wheel_mapped: bool,
}

#[derive(PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -334,11 +333,6 @@ impl Kanata {
.map(|s| !FALSE_VALUES.contains(&s.to_lowercase().as_str()))
.unwrap_or(true);

let scroll_wheel_mapped = cfg.mapped_keys.contains(&OsCode::MouseWheelUp)
|| cfg.mapped_keys.contains(&OsCode::MouseWheelDown)
|| cfg.mapped_keys.contains(&OsCode::MouseWheelLeft)
|| cfg.mapped_keys.contains(&OsCode::MouseWheelRight);

*MAPPED_KEYS.lock() = cfg.mapped_keys;

Ok(Self {
Expand Down Expand Up @@ -398,7 +392,6 @@ impl Kanata {
waiting_for_idle: HashSet::default(),
ticks_since_idle: 0,
movemouse_buffer: None,
scroll_wheel_mapped,
})
}

Expand Down Expand Up @@ -443,10 +436,6 @@ impl Kanata {
.get("movemouse-inherit-accel-state")
.map(|s| TRUE_VALUES.contains(&s.to_lowercase().as_str()))
.unwrap_or_default();
self.scroll_wheel_mapped = cfg.mapped_keys.contains(&OsCode::MouseWheelUp)
|| cfg.mapped_keys.contains(&OsCode::MouseWheelDown)
|| cfg.mapped_keys.contains(&OsCode::MouseWheelLeft)
|| cfg.mapped_keys.contains(&OsCode::MouseWheelRight);
*MAPPED_KEYS.lock() = cfg.mapped_keys;
Kanata::set_repeat_rate(&cfg.items)?;
log::info!("Live reload successful");
Expand Down
18 changes: 2 additions & 16 deletions src/oskbd/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,34 +271,20 @@ impl TryFrom<InputEvent> for KeyEvent {
evdev::InputEventKind::RelAxis(axis_type) => {
let dist = item.value();
let code: OsCode = match axis_type {
RelativeAxisType::REL_WHEEL => {
RelativeAxisType::REL_WHEEL | RelativeAxisType::REL_WHEEL_HI_RES => {
if dist > 0 {
MouseWheelUp
} else {
MouseWheelDown
}
}
RelativeAxisType::REL_HWHEEL => {
RelativeAxisType::REL_HWHEEL | RelativeAxisType::REL_HWHEEL_HI_RES => {
if dist > 0 {
MouseWheelRight
} else {
MouseWheelLeft
}
}
RelativeAxisType::REL_WHEEL_HI_RES => {
if dist > 0 {
MouseWheelUpHiRes
} else {
MouseWheelDownHiRes
}
}
RelativeAxisType::REL_HWHEEL_HI_RES => {
if dist > 0 {
MouseWheelRightHiRes
} else {
MouseWheelLeftHiRes
}
}
_ => return Err(()),
};
Ok(KeyEvent {
Expand Down
23 changes: 1 addition & 22 deletions src/oskbd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl From<KeyValue> for bool {
}
}

use kanata_parser::{custom_action::MWheelDirection, keys::OsCode};
use kanata_parser::keys::OsCode;

#[derive(Debug, Clone, Copy)]
pub struct KeyEvent {
Expand All @@ -60,24 +60,3 @@ impl KeyEvent {
Self { code, value }
}
}

#[derive(Debug, Clone, Copy)]
pub enum ScrollEventKind {
Standard,
HiRes,
}

impl TryFrom<OsCode> for ScrollEventKind {
type Error = ();
fn try_from(value: OsCode) -> Result<Self, Self::Error> {
use OsCode::*;
Ok(match value {
MouseWheelUp | MouseWheelDown | MouseWheelLeft | MouseWheelRight => {
ScrollEventKind::Standard
}
MouseWheelUpHiRes | MouseWheelDownHiRes | MouseWheelLeftHiRes
| MouseWheelRightHiRes => ScrollEventKind::HiRes,
_ => return Err(()),
})
}
}