diff --git a/src/emulate/error.rs b/src/emulate/error.rs index 2b141c2..2692ba7 100644 --- a/src/emulate/error.rs +++ b/src/emulate/error.rs @@ -18,6 +18,8 @@ pub enum EmulationCreationError { Xdp(#[from] XdpEmulationCreationError), #[cfg(all(unix, feature = "x11", not(target_os = "macos")))] X11(#[from] X11EmulationCreationError), + #[cfg(target_os = "macos")] + MacOs(#[from] MacOSEmulationCreationError), NoAvailableBackend, } @@ -32,6 +34,8 @@ impl Display for EmulationCreationError { EmulationCreationError::Xdp(e) => format!("desktop portal backend: {e}"), #[cfg(all(unix, feature = "x11", not(target_os = "macos")))] EmulationCreationError::X11(e) => format!("x11 backend: {e}"), + #[cfg(target_os = "macos")] + EmulationCreationError::MacOs(e) => format!("macos backend: {e}"), EmulationCreationError::NoAvailableBackend => format!("no backend available"), }; write!(f, "could not create input emulation backend: {reason}") @@ -137,3 +141,18 @@ impl Display for X11EmulationCreationError { } } } + +#[cfg(target_os = "macos")] +#[derive(Debug, Error)] +pub enum MacOSEmulationCreationError { + EventSourceCreation, +} + +#[cfg(target_os = "macos")] +impl Display for MacOSEmulationCreationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + MacOSEmulationCreationError::EventSourceCreation => write!(f, "could not create event source"), + } + } +} diff --git a/src/emulate/macos.rs b/src/emulate/macos.rs index f69b4fc..16481ed 100644 --- a/src/emulate/macos.rs +++ b/src/emulate/macos.rs @@ -1,7 +1,6 @@ use crate::client::{ClientEvent, ClientHandle}; use crate::emulate::InputEmulation; use crate::event::{Event, KeyboardEvent, PointerEvent}; -use anyhow::{anyhow, Result}; use async_trait::async_trait; use core_graphics::display::{CGDisplayBounds, CGMainDisplayID, CGPoint}; use core_graphics::event::{ @@ -13,6 +12,8 @@ use std::ops::{Index, IndexMut}; use std::time::Duration; use tokio::task::AbortHandle; +use super::error::MacOSEmulationCreationError; + const DEFAULT_REPEAT_DELAY: Duration = Duration::from_millis(500); const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32); @@ -53,11 +54,9 @@ impl IndexMut for ButtonState { unsafe impl Send for MacOSEmulation {} impl MacOSEmulation { - pub fn new() -> Result { - let event_source = match CGEventSource::new(CGEventSourceStateID::CombinedSessionState) { - Ok(e) => e, - Err(_) => return Err(anyhow!("event source creation failed!")), - }; + pub fn new() -> Result { + let event_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState) + .map_err(|_| MacOSEmulationCreationError::EventSourceCreation)?; let button_state = ButtonState { left: false, right: false,