mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-07 20:09:59 +03:00
Macos cleanup event thread (#324)
This commit is contained in:
committed by
GitHub
parent
e29eb7134c
commit
9f10ebcbd2
@@ -426,8 +426,8 @@ fn event_tap_thread(
|
|||||||
client_state: Arc<Mutex<InputCaptureState>>,
|
client_state: Arc<Mutex<InputCaptureState>>,
|
||||||
event_tx: Sender<(Position, CaptureEvent)>,
|
event_tx: Sender<(Position, CaptureEvent)>,
|
||||||
notify_tx: Sender<ProducerEvent>,
|
notify_tx: Sender<ProducerEvent>,
|
||||||
ready: std::sync::mpsc::Sender<Result<(), MacosCaptureCreationError>>,
|
ready: std::sync::mpsc::Sender<Result<CFRunLoop, MacosCaptureCreationError>>,
|
||||||
exit: oneshot::Sender<Result<(), &'static str>>,
|
exit: oneshot::Sender<()>,
|
||||||
) {
|
) {
|
||||||
let _tap = match create_event_tap(client_state, notify_tx, event_tx) {
|
let _tap = match create_event_tap(client_state, notify_tx, event_tx) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -435,18 +435,22 @@ fn event_tap_thread(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Ok(tap) => {
|
Ok(tap) => {
|
||||||
ready.send(Ok(())).expect("channel closed");
|
let run_loop = CFRunLoop::get_current();
|
||||||
|
ready.send(Ok(run_loop)).expect("channel closed");
|
||||||
tap
|
tap
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
log::debug!("running CFRunLoop...");
|
||||||
CFRunLoop::run_current();
|
CFRunLoop::run_current();
|
||||||
|
log::debug!("event tap thread exiting!...");
|
||||||
|
|
||||||
let _ = exit.send(Err("tap thread exited"));
|
let _ = exit.send(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MacOSInputCapture {
|
pub struct MacOSInputCapture {
|
||||||
event_rx: Receiver<(Position, CaptureEvent)>,
|
event_rx: Receiver<(Position, CaptureEvent)>,
|
||||||
notify_tx: Sender<ProducerEvent>,
|
notify_tx: Sender<ProducerEvent>,
|
||||||
|
run_loop: CFRunLoop,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacOSInputCapture {
|
impl MacOSInputCapture {
|
||||||
@@ -475,36 +479,44 @@ impl MacOSInputCapture {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// wait for event tap creation result
|
// wait for event tap creation result
|
||||||
ready_rx.recv().expect("channel closed")?;
|
let run_loop = ready_rx.recv().expect("channel closed")?;
|
||||||
|
|
||||||
let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move {
|
let _tap_task: tokio::task::JoinHandle<()> = tokio::task::spawn_local(async move {
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
producer_event = notify_rx.recv() => {
|
producer_event = notify_rx.recv() => {
|
||||||
let producer_event = producer_event.expect("channel closed");
|
let Some(producer_event) = producer_event else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
let mut state = state.lock().await;
|
let mut state = state.lock().await;
|
||||||
state.handle_producer_event(producer_event).await.unwrap_or_else(|e| {
|
state.handle_producer_event(producer_event).await.unwrap_or_else(|e| {
|
||||||
log::error!("Failed to handle producer event: {e}");
|
log::error!("Failed to handle producer event: {e}");
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
res = &mut tap_exit_rx => {
|
_ = &mut tap_exit_rx => {
|
||||||
if let Err(e) = res.expect("channel closed") {
|
|
||||||
log::error!("Tap thread failed: {:?}", e);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// show cursor
|
||||||
|
let _ = CGDisplay::show_cursor(&CGDisplay::main());
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
event_rx,
|
event_rx,
|
||||||
notify_tx,
|
notify_tx,
|
||||||
|
run_loop,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for MacOSInputCapture {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.run_loop.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Capture for MacOSInputCapture {
|
impl Capture for MacOSInputCapture {
|
||||||
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {
|
||||||
|
|||||||
Reference in New Issue
Block a user