mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-09 15:11:30 +03:00
move libei event conversion to event crate
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1246,6 +1246,7 @@ dependencies = [
|
|||||||
"futures-core",
|
"futures-core",
|
||||||
"log",
|
"log",
|
||||||
"num_enum",
|
"num_enum",
|
||||||
|
"reis",
|
||||||
"serde",
|
"serde",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -61,5 +61,5 @@ default = ["wayland", "x11", "xdg_desktop_portal", "libei", "gtk"]
|
|||||||
wayland = ["input-capture/wayland", "input-emulation/wayland"]
|
wayland = ["input-capture/wayland", "input-emulation/wayland"]
|
||||||
x11 = ["input-capture/x11", "input-emulation/x11"]
|
x11 = ["input-capture/x11", "input-emulation/x11"]
|
||||||
xdg_desktop_portal = ["input-emulation/xdg_desktop_portal"]
|
xdg_desktop_portal = ["input-emulation/xdg_desktop_portal"]
|
||||||
libei = ["input-capture/libei", "input-emulation/libei"]
|
libei = ["input-event/libei", "input-capture/libei", "input-emulation/libei"]
|
||||||
gtk = ["dep:gtk", "dep:adw", "dep:async-channel", "dep:glib-build-tools"]
|
gtk = ["dep:gtk", "dep:adw", "dep:async-channel", "dep:glib-build-tools"]
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ use ashpd::{
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures::{FutureExt, StreamExt};
|
use futures::{FutureExt, StreamExt};
|
||||||
use reis::{
|
use reis::{
|
||||||
ei::{self, keyboard::KeyState},
|
ei,
|
||||||
eis::button::ButtonState,
|
|
||||||
event::{DeviceCapability, EiEvent},
|
event::{DeviceCapability, EiEvent},
|
||||||
tokio::{EiConvertEventStream, EiEventStream},
|
tokio::{EiConvertEventStream, EiEventStream},
|
||||||
};
|
};
|
||||||
@@ -35,7 +34,7 @@ use tokio_util::sync::CancellationToken;
|
|||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use input_event::{Event, KeyboardEvent, PointerEvent};
|
use input_event::Event;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
error::{CaptureError, LibeiCaptureCreationError, ReisConvertEventStreamError},
|
error::{CaptureError, LibeiCaptureCreationError, ReisConvertEventStreamError},
|
||||||
@@ -521,7 +520,7 @@ async fn handle_ei_event(
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(handle) = current_client {
|
if let Some(handle) = current_client {
|
||||||
for event in to_input_events(ei_event).into_iter() {
|
for event in Event::from_ei_event(ei_event) {
|
||||||
event_tx.send((handle, event)).await.expect("no channel");
|
event_tx.send((handle, event)).await.expect("no channel");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -530,141 +529,6 @@ async fn handle_ei_event(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/* not pretty but saves a heap allocation */
|
|
||||||
enum Events {
|
|
||||||
None,
|
|
||||||
One(Event),
|
|
||||||
Two(Event, Event),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Events {
|
|
||||||
fn into_iter(self) -> impl Iterator<Item = Event> {
|
|
||||||
EventIterator::new(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct EventIterator {
|
|
||||||
events: [Option<Event>; 2],
|
|
||||||
pos: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EventIterator {
|
|
||||||
fn new(events: Events) -> Self {
|
|
||||||
let events = match events {
|
|
||||||
Events::None => [None, None],
|
|
||||||
Events::One(e) => [Some(e), None],
|
|
||||||
Events::Two(e, f) => [Some(e), Some(f)],
|
|
||||||
};
|
|
||||||
Self { events, pos: 0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for EventIterator {
|
|
||||||
type Item = Event;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
let res = if self.pos >= self.events.len() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
self.events[self.pos]
|
|
||||||
};
|
|
||||||
self.pos += 1;
|
|
||||||
res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_input_events(ei_event: EiEvent) -> Events {
|
|
||||||
match ei_event {
|
|
||||||
EiEvent::KeyboardModifiers(mods) => {
|
|
||||||
let modifier_event = KeyboardEvent::Modifiers {
|
|
||||||
mods_depressed: mods.depressed,
|
|
||||||
mods_latched: mods.latched,
|
|
||||||
mods_locked: mods.locked,
|
|
||||||
group: mods.group,
|
|
||||||
};
|
|
||||||
Events::One(Event::Keyboard(modifier_event))
|
|
||||||
}
|
|
||||||
EiEvent::Frame(_) => Events::None, /* FIXME */
|
|
||||||
EiEvent::PointerMotion(motion) => {
|
|
||||||
let motion_event = PointerEvent::Motion {
|
|
||||||
time: motion.time as u32,
|
|
||||||
dx: motion.dx as f64,
|
|
||||||
dy: motion.dy as f64,
|
|
||||||
};
|
|
||||||
Events::One(Event::Pointer(motion_event))
|
|
||||||
}
|
|
||||||
EiEvent::PointerMotionAbsolute(_) => Events::None,
|
|
||||||
EiEvent::Button(button) => {
|
|
||||||
let button_event = PointerEvent::Button {
|
|
||||||
time: button.time as u32,
|
|
||||||
button: button.button,
|
|
||||||
state: match button.state {
|
|
||||||
ButtonState::Released => 0,
|
|
||||||
ButtonState::Press => 1,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
Events::One(Event::Pointer(button_event))
|
|
||||||
}
|
|
||||||
EiEvent::ScrollDelta(delta) => {
|
|
||||||
let dy = Event::Pointer(PointerEvent::Axis {
|
|
||||||
time: 0,
|
|
||||||
axis: 0,
|
|
||||||
value: delta.dy as f64,
|
|
||||||
});
|
|
||||||
let dx = Event::Pointer(PointerEvent::Axis {
|
|
||||||
time: 0,
|
|
||||||
axis: 1,
|
|
||||||
value: delta.dx as f64,
|
|
||||||
});
|
|
||||||
if delta.dy != 0. && delta.dx != 0. {
|
|
||||||
Events::Two(dy, dx)
|
|
||||||
} else if delta.dy != 0. {
|
|
||||||
Events::One(dy)
|
|
||||||
} else if delta.dx != 0. {
|
|
||||||
Events::One(dx)
|
|
||||||
} else {
|
|
||||||
Events::None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EiEvent::ScrollStop(_) => Events::None, /* TODO */
|
|
||||||
EiEvent::ScrollCancel(_) => Events::None, /* TODO */
|
|
||||||
EiEvent::ScrollDiscrete(scroll) => {
|
|
||||||
let dy = Event::Pointer(PointerEvent::AxisDiscrete120 {
|
|
||||||
axis: 0,
|
|
||||||
value: scroll.discrete_dy,
|
|
||||||
});
|
|
||||||
let dx = Event::Pointer(PointerEvent::AxisDiscrete120 {
|
|
||||||
axis: 1,
|
|
||||||
value: scroll.discrete_dx,
|
|
||||||
});
|
|
||||||
if scroll.discrete_dy != 0 && scroll.discrete_dx != 0 {
|
|
||||||
Events::Two(dy, dx)
|
|
||||||
} else if scroll.discrete_dy != 0 {
|
|
||||||
Events::One(dy)
|
|
||||||
} else if scroll.discrete_dx != 0 {
|
|
||||||
Events::One(dx)
|
|
||||||
} else {
|
|
||||||
Events::None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EiEvent::KeyboardKey(key) => {
|
|
||||||
let key_event = KeyboardEvent::Key {
|
|
||||||
key: key.key,
|
|
||||||
state: match key.state {
|
|
||||||
KeyState::Press => 1,
|
|
||||||
KeyState::Released => 0,
|
|
||||||
},
|
|
||||||
time: key.time as u32,
|
|
||||||
};
|
|
||||||
Events::One(Event::Keyboard(key_event))
|
|
||||||
}
|
|
||||||
EiEvent::TouchDown(_) => Events::None, /* TODO */
|
|
||||||
EiEvent::TouchUp(_) => Events::None, /* TODO */
|
|
||||||
EiEvent::TouchMotion(_) => Events::None, /* TODO */
|
|
||||||
_ => Events::None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
|
impl<'a> LanMouseInputCapture for LibeiInputCapture<'a> {
|
||||||
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, handle: CaptureHandle, pos: Position) -> Result<(), CaptureError> {
|
||||||
|
|||||||
@@ -12,3 +12,8 @@ log = "0.4.22"
|
|||||||
num_enum = "0.7.2"
|
num_enum = "0.7.2"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
thiserror = "1.0.61"
|
thiserror = "1.0.61"
|
||||||
|
reis = { version = "0.2.0", optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["libei"]
|
||||||
|
libei = ["dep:reis"]
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ pub mod error;
|
|||||||
pub mod proto;
|
pub mod proto;
|
||||||
pub mod scancode;
|
pub mod scancode;
|
||||||
|
|
||||||
|
#[cfg(feature = "libei")]
|
||||||
|
mod libei;
|
||||||
|
|
||||||
// FIXME
|
// FIXME
|
||||||
pub const BTN_LEFT: u32 = 0x110;
|
pub const BTN_LEFT: u32 = 0x110;
|
||||||
pub const BTN_RIGHT: u32 = 0x111;
|
pub const BTN_RIGHT: u32 = 0x111;
|
||||||
|
|||||||
146
input-event/src/libei.rs
Normal file
146
input-event/src/libei.rs
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
use reis::{
|
||||||
|
ei::{button::ButtonState, keyboard::KeyState},
|
||||||
|
event::EiEvent,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{Event, KeyboardEvent, PointerEvent};
|
||||||
|
|
||||||
|
impl Event {
|
||||||
|
pub fn from_ei_event(ei_event: EiEvent) -> impl Iterator<Item = Self> {
|
||||||
|
to_input_events(ei_event).into_iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Events {
|
||||||
|
None,
|
||||||
|
One(Event),
|
||||||
|
Two(Event, Event),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Events {
|
||||||
|
fn into_iter(self) -> impl Iterator<Item = Event> {
|
||||||
|
EventIterator::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EventIterator {
|
||||||
|
events: [Option<Event>; 2],
|
||||||
|
pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventIterator {
|
||||||
|
fn new(events: Events) -> Self {
|
||||||
|
let events = match events {
|
||||||
|
Events::None => [None, None],
|
||||||
|
Events::One(e) => [Some(e), None],
|
||||||
|
Events::Two(e, f) => [Some(e), Some(f)],
|
||||||
|
};
|
||||||
|
Self { events, pos: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for EventIterator {
|
||||||
|
type Item = Event;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let res = if self.pos >= self.events.len() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
self.events[self.pos]
|
||||||
|
};
|
||||||
|
self.pos += 1;
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_input_events(ei_event: EiEvent) -> Events {
|
||||||
|
match ei_event {
|
||||||
|
EiEvent::KeyboardModifiers(mods) => {
|
||||||
|
let modifier_event = KeyboardEvent::Modifiers {
|
||||||
|
mods_depressed: mods.depressed,
|
||||||
|
mods_latched: mods.latched,
|
||||||
|
mods_locked: mods.locked,
|
||||||
|
group: mods.group,
|
||||||
|
};
|
||||||
|
Events::One(Event::Keyboard(modifier_event))
|
||||||
|
}
|
||||||
|
EiEvent::Frame(_) => Events::None, /* FIXME */
|
||||||
|
EiEvent::PointerMotion(motion) => {
|
||||||
|
let motion_event = PointerEvent::Motion {
|
||||||
|
time: motion.time as u32,
|
||||||
|
dx: motion.dx as f64,
|
||||||
|
dy: motion.dy as f64,
|
||||||
|
};
|
||||||
|
Events::One(Event::Pointer(motion_event))
|
||||||
|
}
|
||||||
|
EiEvent::PointerMotionAbsolute(_) => Events::None,
|
||||||
|
EiEvent::Button(button) => {
|
||||||
|
let button_event = PointerEvent::Button {
|
||||||
|
time: button.time as u32,
|
||||||
|
button: button.button,
|
||||||
|
state: match button.state {
|
||||||
|
ButtonState::Released => 0,
|
||||||
|
ButtonState::Press => 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Events::One(Event::Pointer(button_event))
|
||||||
|
}
|
||||||
|
EiEvent::ScrollDelta(delta) => {
|
||||||
|
let dy = Event::Pointer(PointerEvent::Axis {
|
||||||
|
time: 0,
|
||||||
|
axis: 0,
|
||||||
|
value: delta.dy as f64,
|
||||||
|
});
|
||||||
|
let dx = Event::Pointer(PointerEvent::Axis {
|
||||||
|
time: 0,
|
||||||
|
axis: 1,
|
||||||
|
value: delta.dx as f64,
|
||||||
|
});
|
||||||
|
if delta.dy != 0. && delta.dx != 0. {
|
||||||
|
Events::Two(dy, dx)
|
||||||
|
} else if delta.dy != 0. {
|
||||||
|
Events::One(dy)
|
||||||
|
} else if delta.dx != 0. {
|
||||||
|
Events::One(dx)
|
||||||
|
} else {
|
||||||
|
Events::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EiEvent::ScrollStop(_) => Events::None, /* TODO */
|
||||||
|
EiEvent::ScrollCancel(_) => Events::None, /* TODO */
|
||||||
|
EiEvent::ScrollDiscrete(scroll) => {
|
||||||
|
let dy = Event::Pointer(PointerEvent::AxisDiscrete120 {
|
||||||
|
axis: 0,
|
||||||
|
value: scroll.discrete_dy,
|
||||||
|
});
|
||||||
|
let dx = Event::Pointer(PointerEvent::AxisDiscrete120 {
|
||||||
|
axis: 1,
|
||||||
|
value: scroll.discrete_dx,
|
||||||
|
});
|
||||||
|
if scroll.discrete_dy != 0 && scroll.discrete_dx != 0 {
|
||||||
|
Events::Two(dy, dx)
|
||||||
|
} else if scroll.discrete_dy != 0 {
|
||||||
|
Events::One(dy)
|
||||||
|
} else if scroll.discrete_dx != 0 {
|
||||||
|
Events::One(dx)
|
||||||
|
} else {
|
||||||
|
Events::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EiEvent::KeyboardKey(key) => {
|
||||||
|
let key_event = KeyboardEvent::Key {
|
||||||
|
key: key.key,
|
||||||
|
state: match key.state {
|
||||||
|
KeyState::Press => 1,
|
||||||
|
KeyState::Released => 0,
|
||||||
|
},
|
||||||
|
time: key.time as u32,
|
||||||
|
};
|
||||||
|
Events::One(Event::Keyboard(key_event))
|
||||||
|
}
|
||||||
|
EiEvent::TouchDown(_) => Events::None, /* TODO */
|
||||||
|
EiEvent::TouchUp(_) => Events::None, /* TODO */
|
||||||
|
EiEvent::TouchMotion(_) => Events::None, /* TODO */
|
||||||
|
_ => Events::None,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user