Skip to content

Commit 76deec9

Browse files
committed
cleanup
1 parent 06ca3a1 commit 76deec9

File tree

1 file changed

+58
-63
lines changed

1 file changed

+58
-63
lines changed

src/backend/producer/libei.rs

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{
1717
pin::Pin,
1818
task::{ready, Context, Poll},
1919
};
20-
use tokio::task::JoinHandle;
20+
use tokio::{task::JoinHandle, sync::mpsc::Sender};
2121

2222
use futures_core::Stream;
2323
use once_cell::sync::Lazy;
@@ -64,7 +64,7 @@ fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) {
6464
Position::Left => (x, y, x, y + height - 1), // start pos, end pos, inclusive
6565
Position::Right => (x + width, y, x + width, y + height - 1),
6666
Position::Top => (x, y, x + width - 1, y),
67-
Position::Bottom => (x, y + height - 1, x + width - 1, y + height - 1),
67+
Position::Bottom => (x, y + height, x + width - 1, y + height),
6868
}
6969
}
7070

@@ -184,79 +184,33 @@ impl LibeiProducer {
184184
}
185185
Some(c) => *c,
186186
};
187+
event_tx.send((current_client, Event::Enter())).await?;
188+
tokio::select! {
189+
res = do_capture(&context, &mut event_stream, current_client, event_tx.clone()) => {
190+
res?;
191+
}
192+
producer_event = notify_rx.recv() => {
193+
let producer_event = match producer_event {
194+
Some(e) => e,
195+
None => continue,
196+
};
197+
handle_producer_event(producer_event, &input_capture, &session, &mut next_barrier_id, &mut client_for_barrier_id, &mut active_clients).await?;
198+
}
199+
}
187200
(activated, current_client)
188201
}
189202
producer_event = notify_rx.recv() => {
190203
let producer_event = match producer_event {
191204
Some(e) => e,
192205
None => continue,
193206
};
194-
log::debug!("handling event: {producer_event:?}");
195-
match producer_event {
196-
ProducerEvent::Release => {
197-
continue;
198-
},
199-
ProducerEvent::ClientEvent(c) => match c {
200-
ClientEvent::Create(c, p) => {
201-
active_clients.push((c, p));
202-
update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?;
203-
log::debug!("client for barrier id: {client_for_barrier_id:?}");
204-
}
205-
ClientEvent::Destroy(c) => {
206-
active_clients.retain(|(h, _)| *h != c);
207-
update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?;
208-
log::debug!("client for barrier id: {client_for_barrier_id:?}");
209-
}
210-
},
211-
}
207+
handle_producer_event(producer_event, &input_capture, &session, &mut next_barrier_id, &mut client_for_barrier_id, &mut active_clients).await?;
212208
continue;
213209
},
214210
}
215211
};
216212

217-
let mut entered = false;
218-
loop {
219-
tokio::select! { biased;
220-
ei_event = event_stream.next() => {
221-
let ei_event = match ei_event {
222-
Some(Ok(e)) => e,
223-
_ => return Ok(()),
224-
};
225-
let lan_mouse_event = to_lan_mouse_event(ei_event, &context);
226-
if !entered {
227-
let _ = event_tx.send((current_client, Event::Enter())).await;
228-
entered = true;
229-
}
230-
if let Some(event) = lan_mouse_event {
231-
let _ = event_tx.send((current_client, event)).await;
232-
}
233-
}
234-
producer_event = notify_rx.recv() => {
235-
let producer_event = match producer_event {
236-
Some(e) => e,
237-
None => break,
238-
};
239-
log::debug!("handling event: {producer_event:?}");
240-
match producer_event {
241-
ProducerEvent::Release => {},
242-
ProducerEvent::ClientEvent(c) => match c {
243-
ClientEvent::Create(c, p) => {
244-
active_clients.push((c, p));
245-
update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?;
246-
log::debug!("client for barrier id: {client_for_barrier_id:?}");
247-
}
248-
ClientEvent::Destroy(c) => {
249-
active_clients.retain(|(h, _)| *h != c);
250-
update_barriers(&input_capture, &session, &active_clients, &mut client_for_barrier_id, &mut next_barrier_id).await?;
251-
log::debug!("client for barrier id: {client_for_barrier_id:?}");
252-
}
253-
},
254-
}
255-
break;
256-
},
257-
}
258-
}
259-
log::debug!("releasing input capture");
213+
log::debug!("releasing input capture {}", activated.activation_id());
260214
let (x, y) = activated.cursor_position();
261215
let pos = active_clients
262216
.iter()
@@ -289,6 +243,47 @@ impl LibeiProducer {
289243
}
290244
}
291245

246+
async fn do_capture(context: &ei::Context, event_stream: &mut EiConvertEventStream, current_client: ClientHandle, event_tx: Sender<(ClientHandle, Event)>) -> Result<()> {
247+
loop {
248+
let ei_event = match event_stream.next().await {
249+
Some(Ok(event)) => event,
250+
Some(Err(e)) => return Err(anyhow!("libei connection closed: {e:?}")),
251+
None => return Err(anyhow!("libei connection closed")),
252+
};
253+
let lan_mouse_event = to_lan_mouse_event(ei_event, &context);
254+
if let Some(event) = lan_mouse_event {
255+
let _ = event_tx.send((current_client, event)).await;
256+
}
257+
}
258+
}
259+
260+
async fn handle_producer_event(
261+
producer_event: ProducerEvent,
262+
input_capture: &InputCapture<'_>,
263+
session: &Session<'_>,
264+
next_barrier_id: &mut u32,
265+
client_for_barrier_id: &mut HashMap<BarrierID, ClientHandle>,
266+
active_clients: &mut Vec<(ClientHandle, Position)>,
267+
) -> Result<()> {
268+
log::debug!("handling event: {producer_event:?}");
269+
match producer_event {
270+
ProducerEvent::Release => { },
271+
ProducerEvent::ClientEvent(c) => match c {
272+
ClientEvent::Create(c, p) => {
273+
active_clients.push((c, p));
274+
update_barriers(&input_capture, &session, &active_clients, client_for_barrier_id, next_barrier_id).await?;
275+
log::debug!("client for barrier id: {client_for_barrier_id:?}");
276+
}
277+
ClientEvent::Destroy(c) => {
278+
active_clients.retain(|(h, _)| *h != c);
279+
update_barriers(&input_capture, &session, &active_clients, client_for_barrier_id, next_barrier_id).await?;
280+
log::debug!("client for barrier id: {client_for_barrier_id:?}");
281+
}
282+
},
283+
}
284+
Ok(())
285+
}
286+
292287
fn to_lan_mouse_event(ei_event: EiEvent, context: &ei::Context) -> Option<Event> {
293288
match ei_event {
294289
EiEvent::SeatAdded(seat_event) => {

0 commit comments

Comments
 (0)