mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-19 19:20:58 +03:00
Compare commits
1 Commits
macos-capt
...
fix-duplic
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
295be87a29 |
@@ -40,7 +40,6 @@ struct InputCaptureState {
|
|||||||
active_clients: Lazy<HashSet<Position>>,
|
active_clients: Lazy<HashSet<Position>>,
|
||||||
current_pos: Option<Position>,
|
current_pos: Option<Position>,
|
||||||
bounds: Bounds,
|
bounds: Bounds,
|
||||||
modifier_state: XMods,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -58,7 +57,6 @@ impl InputCaptureState {
|
|||||||
active_clients: Lazy::new(HashSet::new),
|
active_clients: Lazy::new(HashSet::new),
|
||||||
current_pos: None,
|
current_pos: None,
|
||||||
bounds: Bounds::default(),
|
bounds: Bounds::default(),
|
||||||
modifier_state: Default::default(),
|
|
||||||
};
|
};
|
||||||
res.update_bounds()?;
|
res.update_bounds()?;
|
||||||
Ok(res)
|
Ok(res)
|
||||||
@@ -182,7 +180,6 @@ fn get_events(
|
|||||||
ev_type: &CGEventType,
|
ev_type: &CGEventType,
|
||||||
ev: &CGEvent,
|
ev: &CGEvent,
|
||||||
result: &mut Vec<CaptureEvent>,
|
result: &mut Vec<CaptureEvent>,
|
||||||
modifier_state: &mut XMods,
|
|
||||||
) -> Result<(), CaptureError> {
|
) -> Result<(), CaptureError> {
|
||||||
fn map_pointer_event(ev: &CGEvent) -> PointerEvent {
|
fn map_pointer_event(ev: &CGEvent) -> PointerEvent {
|
||||||
PointerEvent::Motion {
|
PointerEvent::Motion {
|
||||||
@@ -218,42 +215,29 @@ fn get_events(
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
CGEventType::FlagsChanged => {
|
CGEventType::FlagsChanged => {
|
||||||
let mut depressed = XMods::empty();
|
let mut mods = XMods::empty();
|
||||||
let mut mods_locked = XMods::empty();
|
let mut mods_locked = XMods::empty();
|
||||||
let cg_flags = ev.get_flags();
|
let cg_flags = ev.get_flags();
|
||||||
|
|
||||||
if cg_flags.contains(CGEventFlags::CGEventFlagShift) {
|
if cg_flags.contains(CGEventFlags::CGEventFlagShift) {
|
||||||
depressed |= XMods::ShiftMask;
|
mods |= XMods::ShiftMask;
|
||||||
}
|
}
|
||||||
if cg_flags.contains(CGEventFlags::CGEventFlagControl) {
|
if cg_flags.contains(CGEventFlags::CGEventFlagControl) {
|
||||||
depressed |= XMods::ControlMask;
|
mods |= XMods::ControlMask;
|
||||||
}
|
}
|
||||||
if cg_flags.contains(CGEventFlags::CGEventFlagAlternate) {
|
if cg_flags.contains(CGEventFlags::CGEventFlagAlternate) {
|
||||||
depressed |= XMods::Mod1Mask;
|
mods |= XMods::Mod1Mask;
|
||||||
}
|
}
|
||||||
if cg_flags.contains(CGEventFlags::CGEventFlagCommand) {
|
if cg_flags.contains(CGEventFlags::CGEventFlagCommand) {
|
||||||
depressed |= XMods::Mod4Mask;
|
mods |= XMods::Mod4Mask;
|
||||||
}
|
}
|
||||||
if cg_flags.contains(CGEventFlags::CGEventFlagAlphaShift) {
|
if cg_flags.contains(CGEventFlags::CGEventFlagAlphaShift) {
|
||||||
depressed |= XMods::LockMask;
|
mods |= XMods::LockMask;
|
||||||
mods_locked |= XMods::LockMask;
|
mods_locked |= XMods::LockMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if pressed or released
|
|
||||||
let state = if depressed > *modifier_state { 1 } else { 0 };
|
|
||||||
*modifier_state = depressed;
|
|
||||||
|
|
||||||
if let Ok(key) = map_key(ev) {
|
|
||||||
let key_event = CaptureEvent::Input(Event::Keyboard(KeyboardEvent::Key {
|
|
||||||
time: 0,
|
|
||||||
key,
|
|
||||||
state,
|
|
||||||
}));
|
|
||||||
result.push(key_event);
|
|
||||||
}
|
|
||||||
|
|
||||||
let modifier_event = KeyboardEvent::Modifiers {
|
let modifier_event = KeyboardEvent::Modifiers {
|
||||||
depressed: depressed.bits(),
|
depressed: mods.bits(),
|
||||||
latched: 0,
|
latched: 0,
|
||||||
locked: mods_locked.bits(),
|
locked: mods_locked.bits(),
|
||||||
group: 0,
|
group: 0,
|
||||||
@@ -382,13 +366,7 @@ fn create_event_tap<'a>(
|
|||||||
// Are we in a client?
|
// Are we in a client?
|
||||||
if let Some(current_pos) = state.current_pos {
|
if let Some(current_pos) = state.current_pos {
|
||||||
pos = Some(current_pos);
|
pos = Some(current_pos);
|
||||||
get_events(
|
get_events(&event_type, cg_ev, &mut res_events).unwrap_or_else(|e| {
|
||||||
&event_type,
|
|
||||||
cg_ev,
|
|
||||||
&mut res_events,
|
|
||||||
&mut state.modifier_state,
|
|
||||||
)
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
log::error!("Failed to get events: {e}");
|
log::error!("Failed to get events: {e}");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ impl MacOSEmulation {
|
|||||||
button_state,
|
button_state,
|
||||||
previous_button: None,
|
previous_button: None,
|
||||||
previous_button_click: None,
|
previous_button_click: None,
|
||||||
button_click_state: 0,
|
button_click_state: 1,
|
||||||
repeat_task: None,
|
repeat_task: None,
|
||||||
notify_repeat_task: Arc::new(Notify::new()),
|
notify_repeat_task: Arc::new(Notify::new()),
|
||||||
modifier_state: Rc::new(Cell::new(XMods::empty())),
|
modifier_state: Rc::new(Cell::new(XMods::empty())),
|
||||||
@@ -244,165 +244,158 @@ impl Emulation for MacOSEmulation {
|
|||||||
) -> Result<(), EmulationError> {
|
) -> Result<(), EmulationError> {
|
||||||
log::trace!("{event:?}");
|
log::trace!("{event:?}");
|
||||||
match event {
|
match event {
|
||||||
Event::Pointer(pointer_event) => {
|
Event::Pointer(pointer_event) => match pointer_event {
|
||||||
match pointer_event {
|
PointerEvent::Motion { time: _, dx, dy } => {
|
||||||
PointerEvent::Motion { time: _, dx, dy } => {
|
let mut mouse_location = match self.get_mouse_location() {
|
||||||
let mut mouse_location = match self.get_mouse_location() {
|
Some(l) => l,
|
||||||
Some(l) => l,
|
None => {
|
||||||
None => {
|
log::warn!("could not get mouse location!");
|
||||||
log::warn!("could not get mouse location!");
|
return Ok(());
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let (new_mouse_x, new_mouse_y) =
|
|
||||||
clamp_to_screen_space(mouse_location.x, mouse_location.y, dx, dy);
|
|
||||||
|
|
||||||
mouse_location.x = new_mouse_x;
|
|
||||||
mouse_location.y = new_mouse_y;
|
|
||||||
|
|
||||||
let mut event_type = CGEventType::MouseMoved;
|
|
||||||
if self.button_state.left {
|
|
||||||
event_type = CGEventType::LeftMouseDragged
|
|
||||||
} else if self.button_state.right {
|
|
||||||
event_type = CGEventType::RightMouseDragged
|
|
||||||
} else if self.button_state.center {
|
|
||||||
event_type = CGEventType::OtherMouseDragged
|
|
||||||
};
|
|
||||||
let event = match CGEvent::new_mouse_event(
|
|
||||||
self.event_source.clone(),
|
|
||||||
event_type,
|
|
||||||
mouse_location,
|
|
||||||
CGMouseButton::Left,
|
|
||||||
) {
|
|
||||||
Ok(e) => e,
|
|
||||||
Err(_) => {
|
|
||||||
log::warn!("mouse event creation failed!");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
event.set_integer_value_field(EventField::MOUSE_EVENT_DELTA_X, dx as i64);
|
|
||||||
event.set_integer_value_field(EventField::MOUSE_EVENT_DELTA_Y, dy as i64);
|
|
||||||
event.post(CGEventTapLocation::HID);
|
|
||||||
}
|
|
||||||
PointerEvent::Button {
|
|
||||||
time: _,
|
|
||||||
button,
|
|
||||||
state,
|
|
||||||
} => {
|
|
||||||
let (event_type, mouse_button) = match (button, state) {
|
|
||||||
(BTN_LEFT, 1) => (CGEventType::LeftMouseDown, CGMouseButton::Left),
|
|
||||||
(BTN_LEFT, 0) => (CGEventType::LeftMouseUp, CGMouseButton::Left),
|
|
||||||
(BTN_RIGHT, 1) => (CGEventType::RightMouseDown, CGMouseButton::Right),
|
|
||||||
(BTN_RIGHT, 0) => (CGEventType::RightMouseUp, CGMouseButton::Right),
|
|
||||||
(BTN_MIDDLE, 1) => (CGEventType::OtherMouseDown, CGMouseButton::Center),
|
|
||||||
(BTN_MIDDLE, 0) => (CGEventType::OtherMouseUp, CGMouseButton::Center),
|
|
||||||
_ => {
|
|
||||||
log::warn!("invalid button event: {button},{state}");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// store button state
|
|
||||||
self.button_state[mouse_button] = state == 1;
|
|
||||||
|
|
||||||
// update previous button state
|
|
||||||
if state == 1 {
|
|
||||||
if self.previous_button.is_some_and(|b| b.eq(&mouse_button))
|
|
||||||
&& self
|
|
||||||
.previous_button_click
|
|
||||||
.is_some_and(|i| i.elapsed() < DOUBLE_CLICK_INTERVAL)
|
|
||||||
{
|
|
||||||
self.button_click_state += 1;
|
|
||||||
} else {
|
|
||||||
self.button_click_state = 1;
|
|
||||||
}
|
|
||||||
self.previous_button = Some(mouse_button);
|
|
||||||
self.previous_button_click = Some(Instant::now());
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
log::debug!("click_state: {}", self.button_click_state);
|
let (new_mouse_x, new_mouse_y) =
|
||||||
let location = self.get_mouse_location().unwrap();
|
clamp_to_screen_space(mouse_location.x, mouse_location.y, dx, dy);
|
||||||
let event = match CGEvent::new_mouse_event(
|
|
||||||
self.event_source.clone(),
|
|
||||||
event_type,
|
|
||||||
location,
|
|
||||||
mouse_button,
|
|
||||||
) {
|
|
||||||
Ok(e) => e,
|
|
||||||
Err(()) => {
|
|
||||||
log::warn!("mouse event creation failed!");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
event.set_integer_value_field(
|
|
||||||
EventField::MOUSE_EVENT_CLICK_STATE,
|
|
||||||
self.button_click_state,
|
|
||||||
);
|
|
||||||
event.post(CGEventTapLocation::HID);
|
|
||||||
}
|
|
||||||
PointerEvent::Axis {
|
|
||||||
time: _,
|
|
||||||
axis,
|
|
||||||
value,
|
|
||||||
} => {
|
|
||||||
let value = value as i32;
|
|
||||||
let (count, wheel1, wheel2, wheel3) = match axis {
|
|
||||||
0 => (1, value, 0, 0), // 0 = vertical => 1 scroll wheel device (y axis)
|
|
||||||
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
|
|
||||||
_ => {
|
|
||||||
log::warn!("invalid scroll event: {axis}, {value}");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let event = match CGEvent::new_scroll_event(
|
|
||||||
self.event_source.clone(),
|
|
||||||
ScrollEventUnit::PIXEL,
|
|
||||||
count,
|
|
||||||
wheel1,
|
|
||||||
wheel2,
|
|
||||||
wheel3,
|
|
||||||
) {
|
|
||||||
Ok(e) => e,
|
|
||||||
Err(()) => {
|
|
||||||
log::warn!("scroll event creation failed!");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
event.post(CGEventTapLocation::HID);
|
|
||||||
}
|
|
||||||
PointerEvent::AxisDiscrete120 { axis, value } => {
|
|
||||||
const LINES_PER_STEP: i32 = 3;
|
|
||||||
let (count, wheel1, wheel2, wheel3) = match axis {
|
|
||||||
0 => (1, value / (120 / LINES_PER_STEP), 0, 0), // 0 = vertical => 1 scroll wheel device (y axis)
|
|
||||||
1 => (2, 0, value / (120 / LINES_PER_STEP), 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
|
|
||||||
_ => {
|
|
||||||
log::warn!("invalid scroll event: {axis}, {value}");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let event = match CGEvent::new_scroll_event(
|
|
||||||
self.event_source.clone(),
|
|
||||||
ScrollEventUnit::LINE,
|
|
||||||
count,
|
|
||||||
wheel1,
|
|
||||||
wheel2,
|
|
||||||
wheel3,
|
|
||||||
) {
|
|
||||||
Ok(e) => e,
|
|
||||||
Err(()) => {
|
|
||||||
log::warn!("scroll event creation failed!");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
event.post(CGEventTapLocation::HID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset button click state in case it's not a button event
|
mouse_location.x = new_mouse_x;
|
||||||
if !matches!(pointer_event, PointerEvent::Button { .. }) {
|
mouse_location.y = new_mouse_y;
|
||||||
self.button_click_state = 0;
|
|
||||||
|
let mut event_type = CGEventType::MouseMoved;
|
||||||
|
if self.button_state.left {
|
||||||
|
event_type = CGEventType::LeftMouseDragged
|
||||||
|
} else if self.button_state.right {
|
||||||
|
event_type = CGEventType::RightMouseDragged
|
||||||
|
} else if self.button_state.center {
|
||||||
|
event_type = CGEventType::OtherMouseDragged
|
||||||
|
};
|
||||||
|
let event = match CGEvent::new_mouse_event(
|
||||||
|
self.event_source.clone(),
|
||||||
|
event_type,
|
||||||
|
mouse_location,
|
||||||
|
CGMouseButton::Left,
|
||||||
|
) {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(_) => {
|
||||||
|
log::warn!("mouse event creation failed!");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
event.set_integer_value_field(EventField::MOUSE_EVENT_DELTA_X, dx as i64);
|
||||||
|
event.set_integer_value_field(EventField::MOUSE_EVENT_DELTA_Y, dy as i64);
|
||||||
|
event.post(CGEventTapLocation::HID);
|
||||||
}
|
}
|
||||||
}
|
PointerEvent::Button {
|
||||||
|
time: _,
|
||||||
|
button,
|
||||||
|
state,
|
||||||
|
} => {
|
||||||
|
let (event_type, mouse_button) = match (button, state) {
|
||||||
|
(BTN_LEFT, 1) => (CGEventType::LeftMouseDown, CGMouseButton::Left),
|
||||||
|
(BTN_LEFT, 0) => (CGEventType::LeftMouseUp, CGMouseButton::Left),
|
||||||
|
(BTN_RIGHT, 1) => (CGEventType::RightMouseDown, CGMouseButton::Right),
|
||||||
|
(BTN_RIGHT, 0) => (CGEventType::RightMouseUp, CGMouseButton::Right),
|
||||||
|
(BTN_MIDDLE, 1) => (CGEventType::OtherMouseDown, CGMouseButton::Center),
|
||||||
|
(BTN_MIDDLE, 0) => (CGEventType::OtherMouseUp, CGMouseButton::Center),
|
||||||
|
_ => {
|
||||||
|
log::warn!("invalid button event: {button},{state}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// store button state
|
||||||
|
self.button_state[mouse_button] = state == 1;
|
||||||
|
|
||||||
|
// update previous button state
|
||||||
|
if state == 1 {
|
||||||
|
if self.previous_button.is_some_and(|b| b.eq(&mouse_button))
|
||||||
|
&& self
|
||||||
|
.previous_button_click
|
||||||
|
.is_some_and(|i| i.elapsed() < DOUBLE_CLICK_INTERVAL)
|
||||||
|
{
|
||||||
|
self.button_click_state += 1;
|
||||||
|
} else {
|
||||||
|
self.button_click_state = 1;
|
||||||
|
}
|
||||||
|
self.previous_button = Some(mouse_button);
|
||||||
|
self.previous_button_click = Some(Instant::now());
|
||||||
|
}
|
||||||
|
|
||||||
|
log::debug!("click_state: {}", self.button_click_state);
|
||||||
|
let location = self.get_mouse_location().unwrap();
|
||||||
|
let event = match CGEvent::new_mouse_event(
|
||||||
|
self.event_source.clone(),
|
||||||
|
event_type,
|
||||||
|
location,
|
||||||
|
mouse_button,
|
||||||
|
) {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(()) => {
|
||||||
|
log::warn!("mouse event creation failed!");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
event.set_integer_value_field(
|
||||||
|
EventField::MOUSE_EVENT_CLICK_STATE,
|
||||||
|
self.button_click_state,
|
||||||
|
);
|
||||||
|
event.post(CGEventTapLocation::HID);
|
||||||
|
}
|
||||||
|
PointerEvent::Axis {
|
||||||
|
time: _,
|
||||||
|
axis,
|
||||||
|
value,
|
||||||
|
} => {
|
||||||
|
let value = value as i32;
|
||||||
|
let (count, wheel1, wheel2, wheel3) = match axis {
|
||||||
|
0 => (1, value, 0, 0), // 0 = vertical => 1 scroll wheel device (y axis)
|
||||||
|
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
|
||||||
|
_ => {
|
||||||
|
log::warn!("invalid scroll event: {axis}, {value}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let event = match CGEvent::new_scroll_event(
|
||||||
|
self.event_source.clone(),
|
||||||
|
ScrollEventUnit::PIXEL,
|
||||||
|
count,
|
||||||
|
wheel1,
|
||||||
|
wheel2,
|
||||||
|
wheel3,
|
||||||
|
) {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(()) => {
|
||||||
|
log::warn!("scroll event creation failed!");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
event.post(CGEventTapLocation::HID);
|
||||||
|
}
|
||||||
|
PointerEvent::AxisDiscrete120 { axis, value } => {
|
||||||
|
const LINES_PER_STEP: i32 = 3;
|
||||||
|
let (count, wheel1, wheel2, wheel3) = match axis {
|
||||||
|
0 => (1, value / (120 / LINES_PER_STEP), 0, 0), // 0 = vertical => 1 scroll wheel device (y axis)
|
||||||
|
1 => (2, 0, value / (120 / LINES_PER_STEP), 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
|
||||||
|
_ => {
|
||||||
|
log::warn!("invalid scroll event: {axis}, {value}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let event = match CGEvent::new_scroll_event(
|
||||||
|
self.event_source.clone(),
|
||||||
|
ScrollEventUnit::LINE,
|
||||||
|
count,
|
||||||
|
wheel1,
|
||||||
|
wheel2,
|
||||||
|
wheel3,
|
||||||
|
) {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(()) => {
|
||||||
|
log::warn!("scroll event creation failed!");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
event.post(CGEventTapLocation::HID);
|
||||||
|
}
|
||||||
|
},
|
||||||
Event::Keyboard(keyboard_event) => match keyboard_event {
|
Event::Keyboard(keyboard_event) => match keyboard_event {
|
||||||
KeyboardEvent::Key {
|
KeyboardEvent::Key {
|
||||||
time: _,
|
time: _,
|
||||||
|
|||||||
Reference in New Issue
Block a user