Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber committed Mar 17, 2024
1 parent 06ca3a1 commit 76deec9
Showing 1 changed file with 58 additions and 63 deletions.
121 changes: 58 additions & 63 deletions src/backend/producer/libei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -184,79 +184,33 @@ 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() => {
let producer_event = match producer_event {
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()
Expand Down Expand Up @@ -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<BarrierID, ClientHandle>,
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<Event> {
match ei_event {
EiEvent::SeatAdded(seat_event) => {
Expand Down

0 comments on commit 76deec9

Please sign in to comment.