From 76deec9c110b20a68c8cd7c3ff8b201edd3f39f8 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Sun, 17 Mar 2024 12:06:54 +0100 Subject: [PATCH] cleanup --- src/backend/producer/libei.rs | 121 ++++++++++++++++------------------ 1 file changed, 58 insertions(+), 63 deletions(-) diff --git a/src/backend/producer/libei.rs b/src/backend/producer/libei.rs index c68edcec..c58ae597 100644 --- a/src/backend/producer/libei.rs +++ b/src/backend/producer/libei.rs @@ -17,7 +17,7 @@ use std::{ pin::Pin, task::{ready, Context, Poll}, }; -use tokio::task::JoinHandle; +use tokio::{task::JoinHandle, sync::mpsc::Sender}; use futures_core::Stream; use once_cell::sync::Lazy; @@ -64,7 +64,7 @@ fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) { Position::Left => (x, y, x, y + height - 1), // start pos, end pos, inclusive Position::Right => (x + width, y, x + width, y + height - 1), Position::Top => (x, y, x + width - 1, y), - Position::Bottom => (x, y + height - 1, x + width - 1, y + height - 1), + Position::Bottom => (x, y + height, x + width - 1, y + height), } } @@ -184,6 +184,19 @@ impl LibeiProducer { } Some(c) => *c, }; + event_tx.send((current_client, Event::Enter())).await?; + tokio::select! { + res = do_capture(&context, &mut event_stream, current_client, event_tx.clone()) => { + res?; + } + producer_event = notify_rx.recv() => { + let producer_event = match producer_event { + Some(e) => e, + None => continue, + }; + handle_producer_event(producer_event, &input_capture, &session, &mut next_barrier_id, &mut client_for_barrier_id, &mut active_clients).await?; + } + } (activated, current_client) } producer_event = notify_rx.recv() => { @@ -191,72 +204,13 @@ impl LibeiProducer { Some(e) => e, None => continue, }; - log::debug!("handling event: {producer_event:?}"); - match producer_event { - ProducerEvent::Release => { - continue; - }, - ProducerEvent::ClientEvent(c) => match c { - ClientEvent::Create(c, p) => { - active_clients.push((c, p)); - update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?; - log::debug!("client for barrier id: {client_for_barrier_id:?}"); - } - ClientEvent::Destroy(c) => { - active_clients.retain(|(h, _)| *h != c); - update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?; - log::debug!("client for barrier id: {client_for_barrier_id:?}"); - } - }, - } + handle_producer_event(producer_event, &input_capture, &session, &mut next_barrier_id, &mut client_for_barrier_id, &mut active_clients).await?; continue; }, } }; - let mut entered = false; - loop { - tokio::select! { biased; - ei_event = event_stream.next() => { - let ei_event = match ei_event { - Some(Ok(e)) => e, - _ => return Ok(()), - }; - let lan_mouse_event = to_lan_mouse_event(ei_event, &context); - if !entered { - let _ = event_tx.send((current_client, Event::Enter())).await; - entered = true; - } - if let Some(event) = lan_mouse_event { - let _ = event_tx.send((current_client, event)).await; - } - } - producer_event = notify_rx.recv() => { - let producer_event = match producer_event { - Some(e) => e, - None => break, - }; - log::debug!("handling event: {producer_event:?}"); - match producer_event { - ProducerEvent::Release => {}, - ProducerEvent::ClientEvent(c) => match c { - ClientEvent::Create(c, p) => { - active_clients.push((c, p)); - update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?; - log::debug!("client for barrier id: {client_for_barrier_id:?}"); - } - ClientEvent::Destroy(c) => { - active_clients.retain(|(h, _)| *h != c); - update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?; - log::debug!("client for barrier id: {client_for_barrier_id:?}"); - } - }, - } - break; - }, - } - } - log::debug!("releasing input capture"); + log::debug!("releasing input capture {}", activated.activation_id()); let (x, y) = activated.cursor_position(); let pos = active_clients .iter() @@ -289,6 +243,47 @@ impl LibeiProducer { } } +async fn do_capture(context: &ei::Context, event_stream: &mut EiConvertEventStream, current_client: ClientHandle, event_tx: Sender<(ClientHandle, Event)>) -> Result<()> { + loop { + let ei_event = match event_stream.next().await { + Some(Ok(event)) => event, + Some(Err(e)) => return Err(anyhow!("libei connection closed: {e:?}")), + None => return Err(anyhow!("libei connection closed")), + }; + let lan_mouse_event = to_lan_mouse_event(ei_event, &context); + if let Some(event) = lan_mouse_event { + let _ = event_tx.send((current_client, event)).await; + } + } +} + +async fn handle_producer_event( + producer_event: ProducerEvent, + input_capture: &InputCapture<'_>, + session: &Session<'_>, + next_barrier_id: &mut u32, + client_for_barrier_id: &mut HashMap, + active_clients: &mut Vec<(ClientHandle, Position)>, +) -> Result<()> { + log::debug!("handling event: {producer_event:?}"); + match producer_event { + ProducerEvent::Release => { }, + ProducerEvent::ClientEvent(c) => match c { + ClientEvent::Create(c, p) => { + active_clients.push((c, p)); + update_barriers(&input_capture, &session, &active_clients, client_for_barrier_id, next_barrier_id).await?; + log::debug!("client for barrier id: {client_for_barrier_id:?}"); + } + ClientEvent::Destroy(c) => { + active_clients.retain(|(h, _)| *h != c); + update_barriers(&input_capture, &session, &active_clients, client_for_barrier_id, next_barrier_id).await?; + log::debug!("client for barrier id: {client_for_barrier_id:?}"); + } + }, + } + Ok(()) +} + fn to_lan_mouse_event(ei_event: EiEvent, context: &ei::Context) -> Option { match ei_event { EiEvent::SeatAdded(seat_event) => {