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 async_trait::async_trait;
use input_event::Event; use input_event::Event;
use crate::error::EmulationError;
use super::{EmulationHandle, InputEmulation}; use super::{EmulationHandle, InputEmulation};
#[derive(Default)] #[derive(Default)]
@@ -14,8 +16,13 @@ impl DummyEmulation {
#[async_trait] #[async_trait]
impl InputEmulation for DummyEmulation { 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}"); log::info!("received event: ({client_handle}) {event}");
Ok(())
} }
async fn create(&mut self, _: EmulationHandle) {} async fn create(&mut self, _: EmulationHandle) {}
async fn destroy(&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; use thiserror::Error;
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))] #[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")))] #[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
use reis::tokio::HandshakeError; 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)] #[derive(Debug, Error)]
pub enum EmulationCreationError { pub enum EmulationCreationError {
#[cfg(all(unix, feature = "wayland", not(target_os = "macos")))] #[cfg(all(unix, feature = "wayland", not(target_os = "macos")))]

View File

@@ -1,4 +1,5 @@
use async_trait::async_trait; use async_trait::async_trait;
use error::EmulationError;
use std::fmt::Display; use std::fmt::Display;
use input_event::Event; use input_event::Event;
@@ -70,7 +71,11 @@ impl Display for Backend {
#[async_trait] #[async_trait]
pub trait InputEmulation: Send { 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 create(&mut self, handle: EmulationHandle);
async fn destroy(&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 input_event::{Event, KeyboardEvent, PointerEvent};
use crate::error::EmulationError;
use super::{error::LibeiEmulationCreationError, EmulationHandle, InputEmulation}; use super::{error::LibeiEmulationCreationError, EmulationHandle, InputEmulation};
static INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| { static INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| {
@@ -136,7 +138,11 @@ impl Drop for LibeiEmulation {
#[async_trait] #[async_trait]
impl InputEmulation for LibeiEmulation { 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() let now = SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.unwrap() .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) {} async fn create(&mut self, _: EmulationHandle) {}
@@ -262,9 +271,7 @@ async fn ei_event_handler(
log::debug!("device added: {device_type:?}"); log::debug!("device added: {device_type:?}");
e.device().device(); e.device().device();
let device = e.device(); let device = e.device();
log::info!("GOT A DEVICE: {device:?}");
if let Some(pointer) = e.device().interface::<Pointer>() { if let Some(pointer) = e.device().interface::<Pointer>() {
log::info!("GOT POINTER");
devices devices
.pointer .pointer
.write() .write()
@@ -272,7 +279,6 @@ async fn ei_event_handler(
.replace((device.device().clone(), pointer)); .replace((device.device().clone(), pointer));
} }
if let Some(keyboard) = e.device().interface::<Keyboard>() { if let Some(keyboard) = e.device().interface::<Keyboard>() {
log::info!("GOT KEYBOARD");
devices devices
.keyboard .keyboard
.write() .write()
@@ -280,7 +286,6 @@ async fn ei_event_handler(
.replace((device.device().clone(), keyboard)); .replace((device.device().clone(), keyboard));
} }
if let Some(scroll) = e.device().interface::<Scroll>() { if let Some(scroll) = e.device().interface::<Scroll>() {
log::info!("GOT SCROLL");
devices devices
.scroll .scroll
.write() .write()
@@ -288,7 +293,6 @@ async fn ei_event_handler(
.replace((device.device().clone(), scroll)); .replace((device.device().clone(), scroll));
} }
if let Some(button) = e.device().interface::<Button>() { if let Some(button) = e.device().interface::<Button>() {
log::info!("GOT BUTTON");
devices devices
.button .button
.write() .write()

View File

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

View File

@@ -1,3 +1,5 @@
use crate::error::EmulationError;
use super::{error::WlrootsEmulationCreationError, InputEmulation}; use super::{error::WlrootsEmulationCreationError, InputEmulation};
use async_trait::async_trait; use async_trait::async_trait;
use std::collections::HashMap; use std::collections::HashMap;
@@ -115,38 +117,40 @@ impl State {
#[async_trait] #[async_trait]
impl InputEmulation for WlrootsEmulation { 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 let Some(virtual_input) = self.state.input_for_client.get(&handle) {
if self.last_flush_failed { if self.last_flush_failed {
if let Err(WaylandError::Io(e)) = self.queue.flush() { match self.queue.flush() {
if e.kind() == io::ErrorKind::WouldBlock { Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => {
/* /*
* outgoing buffer is full - sending more events * outgoing buffer is full - sending more events
* will overwhelm the output buffer and leave the * will overwhelm the output buffer and leave the
* wayland connection in a broken state * wayland connection in a broken state
*/ */
log::warn!("can't keep up, discarding event: ({handle}) - {event:?}"); 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() { match self.queue.flush() {
Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => { Err(WaylandError::Io(e)) if e.kind() == io::ErrorKind::WouldBlock => {
self.last_flush_failed = true; self.last_flush_failed = true;
log::warn!("can't keep up, retrying ..."); log::warn!("can't keep up, discarding event: ({handle}) - {event:?}");
}
Err(WaylandError::Io(e)) => {
log::error!("{e}")
}
Err(WaylandError::Protocol(e)) => {
panic!("wayland protocol violation: {e}")
}
Ok(()) => {
self.last_flush_failed = false;
} }
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) { 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, Event, KeyboardEvent, PointerEvent, BTN_BACK, BTN_FORWARD, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
}; };
use crate::error::EmulationError;
use super::{error::X11EmulationCreationError, EmulationHandle, InputEmulation}; use super::{error::X11EmulationCreationError, EmulationHandle, InputEmulation};
pub struct X11Emulation { pub struct X11Emulation {
@@ -98,7 +100,7 @@ impl Drop for X11Emulation {
#[async_trait] #[async_trait]
impl InputEmulation for X11Emulation { impl InputEmulation for X11Emulation {
async fn consume(&mut self, event: Event, _: EmulationHandle) { async fn consume(&mut self, event: Event, _: EmulationHandle) -> Result<(), EmulationError> {
match event { match event {
Event::Pointer(pointer_event) => match pointer_event { Event::Pointer(pointer_event) => match pointer_event {
PointerEvent::Motion { PointerEvent::Motion {
@@ -139,6 +141,8 @@ impl InputEmulation for X11Emulation {
unsafe { unsafe {
xlib::XFlush(self.display); xlib::XFlush(self.display);
} }
// FIXME
Ok(())
} }
async fn create(&mut self, _: EmulationHandle) { async fn create(&mut self, _: EmulationHandle) {

View File

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

View File

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