From f97621e987ba7b9cbaa7679361b7d9dce5e59832 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Thu, 4 Jul 2024 23:31:34 +0200 Subject: [PATCH] adjust error handling --- input-emulation/src/dummy.rs | 9 ++++- input-emulation/src/error.rs | 21 +++++++++- input-emulation/src/lib.rs | 7 +++- input-emulation/src/libei.rs | 18 +++++---- input-emulation/src/macos.rs | 28 ++++++++----- input-emulation/src/windows.rs | 6 ++- input-emulation/src/wlroots.rs | 34 +++++++++------- input-emulation/src/x11.rs | 6 ++- input-emulation/src/xdg_desktop_portal.rs | 49 +++++++++-------------- src/emulation_test.rs | 16 +++----- src/server/emulation_task.rs | 38 ++++++++++-------- 11 files changed, 135 insertions(+), 97 deletions(-) diff --git a/input-emulation/src/dummy.rs b/input-emulation/src/dummy.rs index f077639..596b71d 100644 --- a/input-emulation/src/dummy.rs +++ b/input-emulation/src/dummy.rs @@ -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) {} diff --git a/input-emulation/src/error.rs b/input-emulation/src/error.rs index 267df43..0d5b025 100644 --- a/input-emulation/src/error.rs +++ b/input-emulation/src/error.rs @@ -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")))] diff --git a/input-emulation/src/lib.rs b/input-emulation/src/lib.rs index 9bfb7b0..17a8c7d 100644 --- a/input-emulation/src/lib.rs +++ b/input-emulation/src/lib.rs @@ -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); } diff --git a/input-emulation/src/libei.rs b/input-emulation/src/libei.rs index 8f2cdf6..d417431 100644 --- a/input-emulation/src/libei.rs +++ b/input-emulation/src/libei.rs @@ -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> = 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::() { - 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::() { - 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::() { - 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::