adjust error handling

This commit is contained in:
Ferdinand Schober
2024-07-04 23:31:34 +02:00
committed by Ferdinand Schober
parent 37a8f729ea
commit ef3ebc59bd
11 changed files with 135 additions and 97 deletions

View File

@@ -1,6 +1,8 @@
use async_trait::async_trait;
use input_event::Event;
use crate::error::EmulationError;
use super::{EmulationHandle, InputEmulation};
#[derive(Default)]
@@ -14,8 +16,13 @@ impl DummyEmulation {
#[async_trait]
impl InputEmulation for DummyEmulation {
async fn consume(&mut self, event: Event, client_handle: EmulationHandle) {
async fn consume(
&mut self,
event: Event,
client_handle: EmulationHandle,
) -> Result<(), EmulationError> {
log::info!("received event: ({client_handle}) {event}");
Ok(())
}
async fn create(&mut self, _: EmulationHandle) {}
async fn destroy(&mut self, _: EmulationHandle) {}

View File

@@ -1,4 +1,4 @@
use std::fmt::Display;
use std::{fmt::Display, io};
use thiserror::Error;
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
@@ -11,6 +11,25 @@ use wayland_client::{
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use reis::tokio::HandshakeError;
#[derive(Debug, Error)]
pub enum EmulationError {
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[error("libei error flushing events: `{0}`")]
Libei(#[from] reis::event::Error),
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[error("wayland error: `{0}`")]
Wayland(#[from] wayland_client::backend::WaylandError),
#[cfg(all(
unix,
any(feature = "xdg_desktop_portal", feature = "libei"),
not(target_os = "macos")
))]
#[error("xdg-desktop-portal: `{0}`")]
Ashpd(#[from] ashpd::Error),
#[error("io error: `{0}`")]
Io(#[from] io::Error),
}
#[derive(Debug, Error)]
pub enum EmulationCreationError {
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]

View File

@@ -1,4 +1,5 @@
use async_trait::async_trait;
use error::EmulationError;
use std::fmt::Display;
use input_event::Event;
@@ -70,7 +71,11 @@ impl Display for Backend {
#[async_trait]
pub trait InputEmulation: Send {
async fn consume(&mut self, event: Event, handle: EmulationHandle);
async fn consume(
&mut self,
event: Event,
handle: EmulationHandle,
) -> Result<(), EmulationError>;
async fn create(&mut self, handle: EmulationHandle);
async fn destroy(&mut self, handle: EmulationHandle);
}

View File

@@ -33,6 +33,8 @@ use reis::{
use input_event::{Event, KeyboardEvent, PointerEvent};
use crate::error::EmulationError;
use super::{error::LibeiEmulationCreationError, EmulationHandle, InputEmulation};
static INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| {
@@ -136,7 +138,11 @@ impl Drop for LibeiEmulation {
#[async_trait]
impl InputEmulation for LibeiEmulation {
async fn consume(&mut self, event: Event, _handle: EmulationHandle) {
async fn consume(
&mut self,
event: Event,
_handle: EmulationHandle,
) -> Result<(), EmulationError> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
@@ -219,7 +225,10 @@ impl InputEmulation for LibeiEmulation {
},
_ => {}
}
self.context.flush().unwrap();
self.context
.flush()
.map_err(|e| io::Error::new(e.kind(), e))?;
Ok(())
}
async fn create(&mut self, _: EmulationHandle) {}
@@ -262,9 +271,7 @@ async fn ei_event_handler(
log::debug!("device added: {device_type:?}");
e.device().device();
let device = e.device();
log::info!("GOT A DEVICE: {device:?}");
if let Some(pointer) = e.device().interface::<Pointer>() {
log::info!("GOT POINTER");
devices
.pointer
.write()
@@ -272,7 +279,6 @@ async fn ei_event_handler(
.replace((device.device().clone(), pointer));
}
if let Some(keyboard) = e.device().interface::<Keyboard>() {
log::info!("GOT KEYBOARD");
devices
.keyboard
.write()
@@ -280,7 +286,6 @@ async fn ei_event_handler(
.replace((device.device().clone(), keyboard));
}
if let Some(scroll) = e.device().interface::<Scroll>() {
log::info!("GOT SCROLL");
devices
.scroll
.write()
@@ -288,7 +293,6 @@ async fn ei_event_handler(
.replace((device.device().clone(), scroll));
}
if let Some(button) = e.device().interface::<Button>() {
log::info!("GOT BUTTON");
devices
.button
.write()

View File

@@ -1,4 +1,4 @@
use super::{EmulationHandle, InputEmulation};
use super::{error::EmulationError, EmulationHandle, InputEmulation};
use async_trait::async_trait;
use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint};
use core_graphics::event::{
@@ -107,7 +107,11 @@ fn key_event(event_source: CGEventSource, key: u16, state: u8) {
#[async_trait]
impl InputEmulation for MacOSEmulation {
async fn consume(&mut self, event: Event, _handle: EmulationHandle) {
async fn consume(
&mut self,
event: Event,
_handle: EmulationHandle,
) -> Result<(), EmulationError> {
match event {
Event::Pointer(pointer_event) => match pointer_event {
PointerEvent::Motion {
@@ -129,7 +133,7 @@ impl InputEmulation for MacOSEmulation {
Some(l) => l,
None => {
log::warn!("could not get mouse location!");
return;
return Ok(());
}
};
@@ -153,7 +157,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e,
Err(_) => {
log::warn!("mouse event creation failed!");
return;
return Ok(());
}
};
event.set_integer_value_field(
@@ -192,7 +196,7 @@ impl InputEmulation for MacOSEmulation {
}
_ => {
log::warn!("invalid button event: {button},{state}");
return;
return Ok(());
}
};
// store button state
@@ -208,7 +212,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e,
Err(()) => {
log::warn!("mouse event creation failed!");
return;
return Ok(());
}
};
event.post(CGEventTapLocation::HID);
@@ -224,7 +228,7 @@ impl InputEmulation for MacOSEmulation {
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
_ => {
log::warn!("invalid scroll event: {axis}, {value}");
return;
return Ok(());
}
};
let event = match CGEvent::new_scroll_event(
@@ -238,7 +242,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e,
Err(()) => {
log::warn!("scroll event creation failed!");
return;
return Ok(());
}
};
event.post(CGEventTapLocation::HID);
@@ -249,7 +253,7 @@ impl InputEmulation for MacOSEmulation {
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
_ => {
log::warn!("invalid scroll event: {axis}, {value}");
return;
return Ok(());
}
};
let event = match CGEvent::new_scroll_event(
@@ -263,7 +267,7 @@ impl InputEmulation for MacOSEmulation {
Ok(e) => e,
Err(()) => {
log::warn!("scroll event creation failed!");
return;
return Ok(());
}
};
event.post(CGEventTapLocation::HID);
@@ -280,7 +284,7 @@ impl InputEmulation for MacOSEmulation {
Ok(k) => k.mac as CGKeyCode,
Err(_) => {
log::warn!("unable to map key event");
return;
return Ok(());
}
};
match state {
@@ -294,6 +298,8 @@ impl InputEmulation for MacOSEmulation {
},
_ => (),
}
// FIXME
Ok(())
}
async fn create(&mut self, _handle: EmulationHandle) {}

View File

@@ -1,4 +1,4 @@
use super::error::WindowsEmulationCreationError;
use super::error::{EmulationError, WindowsEmulationCreationError};
use input_event::{
scancode, Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE,
BTN_RIGHT,
@@ -36,7 +36,7 @@ impl WindowsEmulation {
#[async_trait]
impl InputEmulation for WindowsEmulation {
async fn consume(&mut self, event: Event, _: EmulationHandle) {
async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
match event {
Event::Pointer(pointer_event) => match pointer_event {
PointerEvent::Motion {
@@ -77,6 +77,8 @@ impl InputEmulation for WindowsEmulation {
},
_ => {}
}
// FIXME
Ok(())
}
async fn create(&mut self, _handle: EmulationHandle) {}

View File

@@ -1,3 +1,5 @@
use crate::error::EmulationError;
use super::{error::WlrootsEmulationCreationError, InputEmulation};
use async_trait::async_trait;
use std::collections::HashMap;
@@ -115,38 +117,40 @@ impl State {
#[async_trait]
impl InputEmulation for WlrootsEmulation {
async fn consume(&mut self, event: Event, handle: EmulationHandle) {
async fn consume(
&mut self,
event: Event,
handle: EmulationHandle,
) -> Result<(), EmulationError> {
if let Some(virtual_input) = self.state.input_for_client.get(&handle) {
if self.last_flush_failed {
if let Err(WaylandError::Io(e)) = self.queue.flush() {
if e.kind() == io::ErrorKind::WouldBlock {
match self.queue.flush() {
Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => {
/*
* outgoing buffer is full - sending more events
* will overwhelm the output buffer and leave the
* wayland connection in a broken state
*/
log::warn!("can't keep up, discarding event: ({handle}) - {event:?}");
return;
return Ok(());
}
_ => {}
}
}
virtual_input.consume_event(event).unwrap();
virtual_input
.consume_event(event)
.unwrap_or_else(|_| panic!("failed to convert event: {event:?}"));
match self.queue.flush() {
Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => {
self.last_flush_failed = true;
log::warn!("can't keep up, retrying ...");
}
Err(WaylandError::Io(e)) => {
log::error!("{e}")
}
Err(WaylandError::Protocol(e)) => {
panic!("wayland protocol violation: {e}")
}
Ok(()) => {
self.last_flush_failed = false;
log::warn!("can't keep up, discarding event: ({handle}) - {event:?}");
}
Err(WaylandError::Protocol(e)) => panic!("wayland protocol violation: {e}"),
Ok(()) => self.last_flush_failed = false,
Err(e) => Err(e)?,
}
}
Ok(())
}
async fn create(&mut self, handle: EmulationHandle) {

View File

@@ -9,6 +9,8 @@ use input_event::{
Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
};
use crate::error::EmulationError;
use super::{error::X11EmulationCreationError, EmulationHandle, InputEmulation};
pub struct X11Emulation {
@@ -98,7 +100,7 @@ impl Drop for X11Emulation {
#[async_trait]
impl InputEmulation for X11Emulation {
async fn consume(&mut self, event: Event, _: EmulationHandle) {
async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
match event {
Event::Pointer(pointer_event) => match pointer_event {
PointerEvent::Motion {
@@ -139,6 +141,8 @@ impl InputEmulation for X11Emulation {
unsafe {
xlib::XFlush(self.display);
}
// FIXME
Ok(())
}
async fn create(&mut self, _: EmulationHandle) {

View File

@@ -13,6 +13,8 @@ use input_event::{
KeyboardEvent, PointerEvent,
};
use crate::error::EmulationError;
use super::{error::XdpEmulationCreationError, EmulationHandle, InputEmulation};
pub struct DesktopPortalEmulation<'a> {
@@ -59,7 +61,11 @@ impl<'a> DesktopPortalEmulation<'a> {
#[async_trait]
impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
async fn consume(&mut self, event: input_event::Event, _client: EmulationHandle) {
async fn consume(
&mut self,
event: input_event::Event,
_client: EmulationHandle,
) -> Result<(), EmulationError> {
match event {
Pointer(p) => match p {
PointerEvent::Motion {
@@ -67,17 +73,13 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
relative_x,
relative_y,
} => {
if let Err(e) = self
.proxy
self.proxy
.notify_pointer_motion(
self.session.as_ref().expect("no session"),
relative_x,
relative_y,
)
.await
{
log::warn!("{e}");
}
.await?;
}
PointerEvent::Button {
time: _,
@@ -88,34 +90,26 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
0 => KeyState::Released,
_ => KeyState::Pressed,
};
if let Err(e) = self
.proxy
self.proxy
.notify_pointer_button(
self.session.as_ref().expect("no session"),
button as i32,
state,
)
.await
{
log::warn!("{e}");
}
.await?;
}
PointerEvent::AxisDiscrete120 { axis, value } => {
let axis = match axis {
0 => Axis::Vertical,
_ => Axis::Horizontal,
};
if let Err(e) = self
.proxy
self.proxy
.notify_pointer_axis_discrete(
self.session.as_ref().expect("no session"),
axis,
value,
)
.await
{
log::warn!("{e}");
}
.await?;
}
PointerEvent::Axis {
time: _,
@@ -130,18 +124,14 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
Axis::Vertical => (0., value),
Axis::Horizontal => (value, 0.),
};
if let Err(e) = self
.proxy
self.proxy
.notify_pointer_axis(
self.session.as_ref().expect("no session"),
dx,
dy,
true,
)
.await
{
log::warn!("{e}");
}
.await?;
}
PointerEvent::Frame {} => {}
},
@@ -156,17 +146,13 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
0 => KeyState::Released,
_ => KeyState::Pressed,
};
if let Err(e) = self
.proxy
self.proxy
.notify_keyboard_keycode(
self.session.as_ref().expect("no session"),
key as i32,
state,
)
.await
{
log::warn!("{e}");
}
.await?;
}
KeyboardEvent::Modifiers { .. } => {
// ignore
@@ -175,6 +161,7 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
}
_ => {}
}
Ok(())
}
async fn create(&mut self, _client: EmulationHandle) {}

View File

@@ -38,16 +38,12 @@ async fn input_emulation_test(config: Config) -> Result<()> {
let relative_motion = (new_offset.0 - offset.0, new_offset.1 - offset.1);
offset = new_offset;
let (relative_x, relative_y) = (relative_motion.0 as f64, relative_motion.1 as f64);
emulation
.consume(
Event::Pointer(PointerEvent::Motion {
time: 0,
relative_x,
relative_y,
}),
0,
)
.await;
let event = Event::Pointer(PointerEvent::Motion {
time: 0,
relative_x,
relative_y,
});
emulation.consume(event, 0).await?;
}
}
}

View File

@@ -7,7 +7,11 @@ use tokio::{
};
use crate::{client::ClientHandle, config::EmulationBackend, server::State};
use input_emulation::{self, error::EmulationCreationError, EmulationHandle, InputEmulation};
use input_emulation::{
self,
error::{EmulationCreationError, EmulationError},
EmulationHandle, InputEmulation,
};
use input_event::{Event, KeyboardEvent};
use super::{CaptureEvent, Server};
@@ -42,14 +46,14 @@ pub fn new(
tokio::select! {
udp_event = udp_rx.recv() => {
let udp_event = udp_event.ok_or(anyhow!("receiver closed"))??;
handle_udp_rx(&server, &capture_tx, &mut emulate, &sender_tx, &mut last_ignored, udp_event, &timer_tx).await;
handle_udp_rx(&server, &capture_tx, &mut emulate, &sender_tx, &mut last_ignored, udp_event, &timer_tx).await?;
}
emulate_event = rx.recv() => {
match emulate_event {
Some(e) => match e {
EmulationEvent::Create(h) => emulate.create(h).await,
EmulationEvent::Destroy(h) => emulate.destroy(h).await,
EmulationEvent::ReleaseKeys(c) => release_keys(&server, &mut emulate, c).await,
EmulationEvent::ReleaseKeys(c) => release_keys(&server, &mut emulate, c).await?,
EmulationEvent::Terminate => break,
},
None => break,
@@ -66,7 +70,7 @@ pub fn new(
.map(|(h, _)| h)
.collect::<Vec<_>>();
for client in clients {
release_keys(&server, &mut emulate, client).await;
release_keys(&server, &mut emulate, client).await?;
}
anyhow::Ok(())
@@ -82,7 +86,7 @@ async fn handle_udp_rx(
last_ignored: &mut Option<SocketAddr>,
event: (Event, SocketAddr),
timer_tx: &Sender<()>,
) {
) -> Result<(), EmulationError> {
let (event, addr) = event;
// get handle for addr
@@ -93,7 +97,7 @@ async fn handle_udp_rx(
log::warn!("ignoring events from client {addr}");
last_ignored.replace(addr);
}
return;
return Ok(());
}
};
@@ -107,7 +111,7 @@ async fn handle_udp_rx(
Some((_, s)) => s,
None => {
log::error!("unknown handle");
return;
return Ok(());
}
};
@@ -123,7 +127,7 @@ async fn handle_udp_rx(
let _ = sender_tx.send((Event::Pong(), addr)).await;
}
(Event::Disconnect(), _) => {
release_keys(server, emulate, handle).await;
release_keys(server, emulate, handle).await?;
}
(event, addr) => {
// tell clients that we are ready to receive events
@@ -156,7 +160,7 @@ async fn handle_udp_rx(
s
} else {
log::error!("unknown handle");
return;
return Ok(());
};
if state == 0 {
// ignore release event if key not pressed
@@ -171,7 +175,7 @@ async fn handle_udp_rx(
// workaround buggy rdp backend.
if !ignore_event {
// consume event
emulate.consume(event, handle).await;
emulate.consume(event, handle).await?;
log::trace!("{event} => emulate");
}
}
@@ -196,13 +200,14 @@ async fn handle_udp_rx(
}
}
}
Ok(())
}
async fn release_keys(
server: &Server,
emulate: &mut Box<dyn InputEmulation>,
client: ClientHandle,
) {
) -> Result<(), EmulationError> {
let keys = server
.client_manager
.borrow_mut()
@@ -217,19 +222,18 @@ async fn release_keys(
key,
state: 0,
});
emulate.consume(event, client).await;
emulate.consume(event, client).await?;
if let Ok(key) = input_event::scancode::Linux::try_from(key) {
log::warn!("releasing stuck key: {key:?}");
}
}
let modifiers_event = KeyboardEvent::Modifiers {
let event = Event::Keyboard(KeyboardEvent::Modifiers {
mods_depressed: 0,
mods_latched: 0,
mods_locked: 0,
group: 0,
};
emulate
.consume(Event::Keyboard(modifiers_event), client)
.await;
});
emulate.consume(event, client).await?;
Ok(())
}