@@ -17,7 +17,7 @@ use std::{
17
17
pin:: Pin ,
18
18
task:: { ready, Context , Poll } ,
19
19
} ;
20
- use tokio:: task:: JoinHandle ;
20
+ use tokio:: { task:: JoinHandle , sync :: mpsc :: Sender } ;
21
21
22
22
use futures_core:: Stream ;
23
23
use once_cell:: sync:: Lazy ;
@@ -64,7 +64,7 @@ fn pos_to_barrier(r: &Region, pos: Position) -> (i32, i32, i32, i32) {
64
64
Position :: Left => ( x, y, x, y + height - 1 ) , // start pos, end pos, inclusive
65
65
Position :: Right => ( x + width, y, x + width, y + height - 1 ) ,
66
66
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) ,
68
68
}
69
69
}
70
70
@@ -184,79 +184,33 @@ impl LibeiProducer {
184
184
}
185
185
Some ( c) => * c,
186
186
} ;
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
+ }
187
200
( activated, current_client)
188
201
}
189
202
producer_event = notify_rx. recv( ) => {
190
203
let producer_event = match producer_event {
191
204
Some ( e) => e,
192
205
None => continue ,
193
206
} ;
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 ?;
212
208
continue ;
213
209
} ,
214
210
}
215
211
} ;
216
212
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( ) ) ;
260
214
let ( x, y) = activated. cursor_position ( ) ;
261
215
let pos = active_clients
262
216
. iter ( )
@@ -289,6 +243,47 @@ impl LibeiProducer {
289
243
}
290
244
}
291
245
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
+
292
287
fn to_lan_mouse_event ( ei_event : EiEvent , context : & ei:: Context ) -> Option < Event > {
293
288
match ei_event {
294
289
EiEvent :: SeatAdded ( seat_event) => {
0 commit comments