Skip to content

Commit 9f10ebc

Browse files
authored
Macos cleanup event thread (#324)
1 parent e29eb71 commit 9f10ebc

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

input-capture/src/macos.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,27 +426,31 @@ fn event_tap_thread(
426426
client_state: Arc<Mutex<InputCaptureState>>,
427427
event_tx: Sender<(Position, CaptureEvent)>,
428428
notify_tx: Sender<ProducerEvent>,
429-
ready: std::sync::mpsc::Sender<Result<(), MacosCaptureCreationError>>,
430-
exit: oneshot::Sender<Result<(), &'static str>>,
429+
ready: std::sync::mpsc::Sender<Result<CFRunLoop, MacosCaptureCreationError>>,
430+
exit: oneshot::Sender<()>,
431431
) {
432432
let _tap = match create_event_tap(client_state, notify_tx, event_tx) {
433433
Err(e) => {
434434
ready.send(Err(e)).expect("channel closed");
435435
return;
436436
}
437437
Ok(tap) => {
438-
ready.send(Ok(())).expect("channel closed");
438+
let run_loop = CFRunLoop::get_current();
439+
ready.send(Ok(run_loop)).expect("channel closed");
439440
tap
440441
}
441442
};
443+
log::debug!("running CFRunLoop...");
442444
CFRunLoop::run_current();
445+
log::debug!("event tap thread exiting!...");
443446

444-
let _ = exit.send(Err("tap thread exited"));
447+
let _ = exit.send(());
445448
}
446449

447450
pub struct MacOSInputCapture {
448451
event_rx: Receiver<(Position, CaptureEvent)>,
449452
notify_tx: Sender<ProducerEvent>,
453+
run_loop: CFRunLoop,
450454
}
451455

452456
impl MacOSInputCapture {
@@ -475,36 +479,44 @@ impl MacOSInputCapture {
475479
});
476480

477481
// wait for event tap creation result
478-
ready_rx.recv().expect("channel closed")?;
482+
let run_loop = ready_rx.recv().expect("channel closed")?;
479483

480484
let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move {
481485
loop {
482486
tokio::select! {
483487
producer_event = notify_rx.recv() => {
484-
let producer_event = producer_event.expect("channel closed");
488+
let Some(producer_event) = producer_event else {
489+
break;
490+
};
485491
let mut state = state.lock().await;
486492
state.handle_producer_event(producer_event).await.unwrap_or_else(|e| {
487493
log::error!("Failed to handle producer event: {e}");
488494
})
489495
}
490496

491-
res = &mut tap_exit_rx => {
492-
if let Err(e) = res.expect("channel closed") {
493-
log::error!("Tap thread failed: {:?}", e);
494-
break;
495-
}
497+
_ = &mut tap_exit_rx => {
498+
break;
496499
}
497500
}
498501
}
502+
// show cursor
503+
let _ = CGDisplay::show_cursor(&CGDisplay::main());
499504
});
500505

501506
Ok(Self {
502507
event_rx,
503508
notify_tx,
509+
run_loop,
504510
})
505511
}
506512
}
507513

514+
impl Drop for MacOSInputCapture {
515+
fn drop(&mut self) {
516+
self.run_loop.stop();
517+
}
518+
}
519+
508520
#[async_trait]
509521
impl Capture for MacOSInputCapture {
510522
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {

0 commit comments

Comments
 (0)