Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber committed Jun 30, 2024
1 parent 2ac193a commit e2162b9
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 14 deletions.
21 changes: 11 additions & 10 deletions src/emulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use async_trait::async_trait;
use std::future;

use crate::{
client::{ClientEvent, ClientHandle},
event::Event,
client::{ClientEvent, ClientHandle}, config::EmulationBackend, event::Event
};
use anyhow::Result;

use self::error::EmulationCreationError;

#[cfg(windows)]
pub mod windows;

Expand Down Expand Up @@ -42,18 +43,18 @@ pub trait InputEmulation: Send {
async fn destroy(&mut self);
}

pub async fn create(backend: Option<EmulationBackend>) -> Box<dyn InputEmulation> {
pub async fn create(backend: Option<EmulationBackend>) -> Result<Box<dyn InputEmulation>, EmulationCreationError> {
#[cfg(windows)]
match windows::WindowsEmulation::new() {
Ok(c) => return Box::new(c),
Ok(c) => return Ok(Box::new(c)),
Err(e) => log::warn!("windows input emulation unavailable: {e}"),
}

#[cfg(target_os = "macos")]
match macos::MacOSEmulation::new() {
Ok(c) => {
log::info!("using macos input emulation");
return Box::new(c);
return Ok(Box::new(c));
}
Err(e) => log::error!("macos input emulatino not available: {e}"),
}
Expand All @@ -62,7 +63,7 @@ pub async fn create(backend: Option<EmulationBackend>) -> Box<dyn InputEmulation
match wlroots::WlrootsEmulation::new() {
Ok(c) => {
log::info!("using wlroots input emulation");
return Box::new(c);
return Ok(Box::new(c));
}
Err(e) => log::info!("wayland backend not available: {e}"),
}
Expand All @@ -71,7 +72,7 @@ pub async fn create(backend: Option<EmulationBackend>) -> Box<dyn InputEmulation
match libei::LibeiEmulation::new().await {
Ok(c) => {
log::info!("using libei input emulation");
return Box::new(c);
return Ok(Box::new(c));
}
Err(e) => log::info!("libei not available: {e}"),
}
Expand All @@ -80,7 +81,7 @@ pub async fn create(backend: Option<EmulationBackend>) -> Box<dyn InputEmulation
match xdg_desktop_portal::DesktopPortalEmulation::new().await {
Ok(c) => {
log::info!("using xdg-remote-desktop-portal input emulation");
return Box::new(c);
return Ok(Box::new(c));
}
Err(e) => log::info!("remote desktop portal not available: {e}"),
}
Expand All @@ -89,11 +90,11 @@ pub async fn create(backend: Option<EmulationBackend>) -> Box<dyn InputEmulation
match x11::X11Emulation::new() {
Ok(c) => {
log::info!("using x11 input emulation");
return Box::new(c);
return Ok(Box::new(c));
}
Err(e) => log::info!("x11 input emulation not available: {e}"),
}

log::error!("falling back to dummy input emulation");
Box::new(dummy::DummyEmulation::new())
Ok(Box::new(dummy::DummyEmulation::new()))
}
98 changes: 98 additions & 0 deletions src/emulate/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::{fmt::Display, io};

use thiserror::Error;
use wayland_client::{backend::WaylandError, globals::{BindError, GlobalError}, ConnectError, DispatchError};


#[derive(Debug, Error)]
pub enum EmulationCreationError {
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
Wlroots(#[from] WlrootsEmulationCreationError),
#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
Libei(#[from] LibeiEmulationCreationError),
#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
Xdp(#[from] XdpEmulationCreationError),
#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
X11(#[from] X11EmulationCreationError),
}

impl Display for EmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let reason = match self {
EmulationCreationError::Wlroots(e) => format!("wlroots backend: {e}"),
EmulationCreationError::Libei(e) => todo!("libei backend: {e}"),
EmulationCreationError::Xdp(e) => todo!("desktop portal backend: {e}"),
EmulationCreationError::X11(e) => todo!("x11 backend: {e}"),
};
write!(f, "could not create input emulation backend: {reason}")
}
}

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum WlrootsEmulationCreationError {
Connect(#[from] ConnectError),
Global(#[from] GlobalError),
Wayland(#[from] WaylandError),
Bind(#[from] WaylandBindError),
Dispatch(#[from] DispatchError),
Io(#[from] io::Error),
}

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub struct WaylandBindError {
inner: BindError,
protocol: &'static str,
}
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl WaylandBindError {
pub(crate) fn new(inner: BindError, protocol: &'static str) -> Self {
Self { inner, protocol }
}
}


#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl Display for WaylandBindError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} protocol not supported: {}",
self.protocol, self.inner
)
}
}

#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]
impl Display for WlrootsEmulationCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WlrootsEmulationCreationError::Bind(e) => write!(f, "{e}"),
WlrootsEmulationCreationError::Connect(e) => {
write!(f, "could not connect to wayland compositor: {e}")
}
WlrootsEmulationCreationError::Global(e) => write!(f, "wayland error: {e}"),
WlrootsEmulationCreationError::Wayland(e) => write!(f, "wayland error: {e}"),
WlrootsEmulationCreationError::Dispatch(e) => {
write!(f, "error dispatching wayland events: {e}")
}
WlrootsEmulationCreationError::Io(e) => write!(f, "io error: {e}"),
}
}
}

#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum LibeiEmulationCreationError {}


#[cfg(all(unix, feature = "xdg_desktop_portal", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum XdpEmulationCreationError {}



#[cfg(all(unix, feature = "x11", not(target_os = "macos")))]
#[derive(Debug, Error)]
pub enum X11EmulationCreationError {}
1 change: 1 addition & 0 deletions src/emulate/libei.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{anyhow, Result};
use std::{
collections::HashMap,
os::{fd::OwnedFd, unix::net::UnixStream},
Expand Down
9 changes: 6 additions & 3 deletions src/emulation_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::client::{ClientEvent, Position};
use crate::config::Config;
use crate::emulate;
use crate::event::{Event, PointerEvent};
use anyhow::Result;
Expand All @@ -13,14 +14,16 @@ pub fn run() -> Result<()> {
.enable_time()
.build()?;

runtime.block_on(LocalSet::new().run_until(input_emulation_test()))
let config = Config::new()?;

runtime.block_on(LocalSet::new().run_until(input_emulation_test(config)))
}

const FREQUENCY_HZ: f64 = 1.0;
const RADIUS: f64 = 100.0;

async fn input_emulation_test() -> Result<()> {
let mut emulation = emulate::create().await;
async fn input_emulation_test(config: Config) -> Result<()> {
let mut emulation = emulate::create(config.emulation_backend).await?;
emulation
.notify(ClientEvent::Create(0, Position::Left))
.await;
Expand Down
2 changes: 1 addition & 1 deletion src/server/emulation_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn new(
emulate.destroy().await;
anyhow::Ok(())
});
(emulate_task, tx)
Ok((emulate_task, tx))
}

async fn handle_udp_rx(
Expand Down

0 comments on commit e2162b9

Please sign in to comment.