fix(macos): forward back/forward mouse buttons in capture and emulation (#392)

* fix(macos): forward back/forward mouse buttons in capture and emulation

OtherMouseDown/Up events on macOS carry a button number field that
distinguishes middle (2), back (3), and forward (4) buttons. The
capture backend was unconditionally mapping all OtherMouse events to
BTN_MIDDLE, silently dropping back/forward. The emulation backend had
no match arms for BTN_BACK/BTN_FORWARD, causing them to be dropped
with a warning.

Fix capture by reading MOUSE_EVENT_BUTTON_NUMBER and mapping 3->BTN_BACK,
4->BTN_FORWARD. Fix emulation by adding match arms for BTN_BACK/BTN_FORWARD
and setting MOUSE_EVENT_BUTTON_NUMBER on the emitted CGEvent so macOS
apps receive the correct button identity.

* fix(macos): track button state and double-clicks by evdev code instead of CGMouseButton

Back, forward, and middle buttons all map to CGMouseButton::Center on
macOS, which caused them to share a single pressed-state boolean and
alias in double-click detection. Replace the ButtonState struct with a
HashSet<u32> keyed by evdev button code so each button is tracked
independently.

---------

Co-authored-by: Ferdinand Schober <ferdinandschober20@gmail.com>
This commit is contained in:
Ty Smith
2026-02-22 08:45:53 -08:00
committed by GitHub
parent bcf9c35301
commit 27225ed564
2 changed files with 73 additions and 69 deletions

View File

@@ -18,7 +18,9 @@ use core_graphics::{
event_source::{CGEventSource, CGEventSourceStateID}, event_source::{CGEventSource, CGEventSourceStateID},
}; };
use futures_core::Stream; use futures_core::Stream;
use input_event::{BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent}; use input_event::{
BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent,
};
use keycode::{KeyMap, KeyMapping}; use keycode::{KeyMap, KeyMapping};
use libc::c_void; use libc::c_void;
use once_cell::unsync::Lazy; use once_cell::unsync::Lazy;
@@ -304,16 +306,28 @@ fn get_events(
}))) })))
} }
CGEventType::OtherMouseDown => { CGEventType::OtherMouseDown => {
let btn_num = ev.get_integer_value_field(EventField::MOUSE_EVENT_BUTTON_NUMBER);
let button = match btn_num {
3 => BTN_BACK,
4 => BTN_FORWARD,
_ => BTN_MIDDLE,
};
result.push(CaptureEvent::Input(Event::Pointer(PointerEvent::Button { result.push(CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
time: 0, time: 0,
button: BTN_MIDDLE, button,
state: 1, state: 1,
}))) })))
} }
CGEventType::OtherMouseUp => { CGEventType::OtherMouseUp => {
let btn_num = ev.get_integer_value_field(EventField::MOUSE_EVENT_BUTTON_NUMBER);
let button = match btn_num {
3 => BTN_BACK,
4 => BTN_FORWARD,
_ => BTN_MIDDLE,
};
result.push(CaptureEvent::Input(Event::Pointer(PointerEvent::Button { result.push(CaptureEvent::Input(Event::Pointer(PointerEvent::Button {
time: 0, time: 0,
button: BTN_MIDDLE, button,
state: 0, state: 0,
}))) })))
} }

View File

@@ -10,10 +10,13 @@ use core_graphics::event::{
ScrollEventUnit, ScrollEventUnit,
}; };
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID}; use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use input_event::{BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent, scancode}; use input_event::{
BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, Event, KeyboardEvent, PointerEvent,
scancode,
};
use keycode::{KeyMap, KeyMapping}; use keycode::{KeyMap, KeyMapping};
use std::cell::Cell; use std::cell::Cell;
use std::ops::{Index, IndexMut}; use std::collections::HashSet;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@@ -30,10 +33,10 @@ pub(crate) struct MacOSEmulation {
event_source: CGEventSource, event_source: CGEventSource,
/// task handle for key repeats /// task handle for key repeats
repeat_task: Option<JoinHandle<()>>, repeat_task: Option<JoinHandle<()>>,
/// current state of the mouse buttons /// current state of the mouse buttons (tracked by evdev button code)
button_state: ButtonState, pressed_buttons: HashSet<u32>,
/// button previously pressed /// button previously pressed (evdev button code)
previous_button: Option<CGMouseButton>, previous_button: Option<u32>,
/// timestamp of previous click (button down) /// timestamp of previous click (button down)
previous_button_click: Option<Instant>, previous_button_click: Option<Instant>,
/// click state, i.e. number of clicks in quick succession /// click state, i.e. number of clicks in quick succession
@@ -44,31 +47,13 @@ pub(crate) struct MacOSEmulation {
notify_repeat_task: Arc<Notify>, notify_repeat_task: Arc<Notify>,
} }
struct ButtonState { /// Maps an evdev button code to the CGEventType used for drag events.
left: bool, fn drag_event_type(button: u32) -> CGEventType {
right: bool, match button {
center: bool, BTN_LEFT => CGEventType::LeftMouseDragged,
} BTN_RIGHT => CGEventType::RightMouseDragged,
// middle, back, forward, and any other button all use OtherMouseDragged
impl Index<CGMouseButton> for ButtonState { _ => CGEventType::OtherMouseDragged,
type Output = bool;
fn index(&self, index: CGMouseButton) -> &Self::Output {
match index {
CGMouseButton::Left => &self.left,
CGMouseButton::Right => &self.right,
CGMouseButton::Center => &self.center,
}
}
}
impl IndexMut<CGMouseButton> for ButtonState {
fn index_mut(&mut self, index: CGMouseButton) -> &mut Self::Output {
match index {
CGMouseButton::Left => &mut self.left,
CGMouseButton::Right => &mut self.right,
CGMouseButton::Center => &mut self.center,
}
} }
} }
@@ -78,14 +63,9 @@ impl MacOSEmulation {
pub(crate) fn new() -> Result<Self, MacOSEmulationCreationError> { pub(crate) fn new() -> Result<Self, MacOSEmulationCreationError> {
let event_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState) let event_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
.map_err(|_| MacOSEmulationCreationError::EventSourceCreation)?; .map_err(|_| MacOSEmulationCreationError::EventSourceCreation)?;
let button_state = ButtonState {
left: false,
right: false,
center: false,
};
Ok(Self { Ok(Self {
event_source, event_source,
button_state, pressed_buttons: HashSet::new(),
previous_button: None, previous_button: None,
previous_button_click: None, previous_button_click: None,
button_click_state: 0, button_click_state: 0,
@@ -261,14 +241,14 @@ impl Emulation for MacOSEmulation {
mouse_location.x = new_mouse_x; mouse_location.x = new_mouse_x;
mouse_location.y = new_mouse_y; mouse_location.y = new_mouse_y;
let mut event_type = CGEventType::MouseMoved; // If any button is held, emit a drag event for it;
if self.button_state.left { // otherwise emit a normal mouse-moved event.
event_type = CGEventType::LeftMouseDragged let event_type = self
} else if self.button_state.right { .pressed_buttons
event_type = CGEventType::RightMouseDragged .iter()
} else if self.button_state.center { .next()
event_type = CGEventType::OtherMouseDragged .map(|&btn| drag_event_type(btn))
}; .unwrap_or(CGEventType::MouseMoved);
let event = match CGEvent::new_mouse_event( let event = match CGEvent::new_mouse_event(
self.event_source.clone(), self.event_source.clone(),
event_type, event_type,
@@ -290,6 +270,12 @@ impl Emulation for MacOSEmulation {
button, button,
state, state,
} => { } => {
// button number for OtherMouse events (3 = back, 4 = forward, etc.)
let cg_button_number: Option<i64> = match button {
BTN_BACK => Some(3),
BTN_FORWARD => Some(4),
_ => None,
};
let (event_type, mouse_button) = match (button, state) { let (event_type, mouse_button) = match (button, state) {
(BTN_LEFT, 1) => (CGEventType::LeftMouseDown, CGMouseButton::Left), (BTN_LEFT, 1) => (CGEventType::LeftMouseDown, CGMouseButton::Left),
(BTN_LEFT, 0) => (CGEventType::LeftMouseUp, CGMouseButton::Left), (BTN_LEFT, 0) => (CGEventType::LeftMouseUp, CGMouseButton::Left),
@@ -297,17 +283,29 @@ impl Emulation for MacOSEmulation {
(BTN_RIGHT, 0) => (CGEventType::RightMouseUp, CGMouseButton::Right), (BTN_RIGHT, 0) => (CGEventType::RightMouseUp, CGMouseButton::Right),
(BTN_MIDDLE, 1) => (CGEventType::OtherMouseDown, CGMouseButton::Center), (BTN_MIDDLE, 1) => (CGEventType::OtherMouseDown, CGMouseButton::Center),
(BTN_MIDDLE, 0) => (CGEventType::OtherMouseUp, CGMouseButton::Center), (BTN_MIDDLE, 0) => (CGEventType::OtherMouseUp, CGMouseButton::Center),
(BTN_BACK, 1) | (BTN_FORWARD, 1) => {
(CGEventType::OtherMouseDown, CGMouseButton::Center)
}
(BTN_BACK, 0) | (BTN_FORWARD, 0) => {
(CGEventType::OtherMouseUp, CGMouseButton::Center)
}
_ => { _ => {
log::warn!("invalid button event: {button},{state}"); log::warn!("invalid button event: {button},{state}");
return Ok(()); return Ok(());
} }
}; };
// store button state // store button state using the evdev button code so
self.button_state[mouse_button] = state == 1; // back, forward, and middle are tracked independently
// update previous button state
if state == 1 { if state == 1 {
if self.previous_button.is_some_and(|b| b.eq(&mouse_button)) self.pressed_buttons.insert(button);
} else {
self.pressed_buttons.remove(&button);
}
// update double-click tracking using the evdev button
// code so that back/forward don't alias with middle
if state == 1 {
if self.previous_button == Some(button)
&& self && self
.previous_button_click .previous_button_click
.is_some_and(|i| i.elapsed() < DOUBLE_CLICK_INTERVAL) .is_some_and(|i| i.elapsed() < DOUBLE_CLICK_INTERVAL)
@@ -316,7 +314,7 @@ impl Emulation for MacOSEmulation {
} else { } else {
self.button_click_state = 1; self.button_click_state = 1;
} }
self.previous_button = Some(mouse_button); self.previous_button = Some(button);
self.previous_button_click = Some(Instant::now()); self.previous_button_click = Some(Instant::now());
} }
@@ -338,6 +336,13 @@ impl Emulation for MacOSEmulation {
EventField::MOUSE_EVENT_CLICK_STATE, EventField::MOUSE_EVENT_CLICK_STATE,
self.button_click_state, self.button_click_state,
); );
// Set the button number for extra buttons (back=3, forward=4)
if let Some(btn_num) = cg_button_number {
event.set_integer_value_field(
EventField::MOUSE_EVENT_BUTTON_NUMBER,
btn_num,
);
}
event.post(CGEventTapLocation::HID); event.post(CGEventTapLocation::HID);
} }
PointerEvent::Axis { PointerEvent::Axis {
@@ -448,21 +453,6 @@ impl Emulation for MacOSEmulation {
async fn terminate(&mut self) {} async fn terminate(&mut self) {}
} }
trait ButtonEq {
fn eq(&self, other: &Self) -> bool;
}
impl ButtonEq for CGMouseButton {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(CGMouseButton::Left, CGMouseButton::Left)
| (CGMouseButton::Right, CGMouseButton::Right)
| (CGMouseButton::Center, CGMouseButton::Center)
)
}
}
fn update_modifiers(modifiers: &Cell<XMods>, key: u32, state: u8) -> bool { fn update_modifiers(modifiers: &Cell<XMods>, key: u32, state: u8) -> bool {
if let Ok(key) = scancode::Linux::try_from(key) { if let Ok(key) = scancode::Linux::try_from(key) {
let mask = match key { let mask = match key {