mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-29 08:00:55 +03:00
fix emulation notify
This commit is contained in:
@@ -95,7 +95,7 @@ async fn capture_task(
|
||||
)
|
||||
.await
|
||||
{
|
||||
log::warn!("input emulation exited: {e}");
|
||||
log::warn!("input capture exited: {e}");
|
||||
}
|
||||
let _ = frontend_tx
|
||||
.send(FrontendEvent::CaptureStatus(Status::Disabled))
|
||||
@@ -249,7 +249,7 @@ async fn handle_capture_event(
|
||||
};
|
||||
|
||||
if start_timer {
|
||||
timer_notify.notify_waiters();
|
||||
timer_notify.notify_one();
|
||||
}
|
||||
if enter {
|
||||
spawn_hook_command(server, handle);
|
||||
|
||||
@@ -108,7 +108,9 @@ async fn emulation_task(
|
||||
if cancellation_token.is_cancelled() {
|
||||
break;
|
||||
}
|
||||
log::info!("waiting for user to request input emulation ...");
|
||||
notify_emulation.notified().await;
|
||||
log::info!("... done");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,11 +126,26 @@ async fn do_emulation(
|
||||
cancellation_token: &CancellationToken,
|
||||
) -> Result<(), LanMouseEmulationError> {
|
||||
let backend = backend.map(|b| b.into());
|
||||
log::info!("creating input emulation...");
|
||||
let mut emulation = input_emulation::create(backend).await?;
|
||||
let _ = frontend_tx
|
||||
.send(FrontendEvent::EmulationStatus(Status::Enabled))
|
||||
.await;
|
||||
|
||||
let res = do_emulation_session(
|
||||
&mut emulation,
|
||||
rx,
|
||||
server,
|
||||
udp_rx,
|
||||
sender_tx,
|
||||
capture_tx,
|
||||
timer_notify,
|
||||
cancellation_token,
|
||||
)
|
||||
.await;
|
||||
emulation.terminate().await;
|
||||
res?;
|
||||
|
||||
// FIXME DUPLICATES
|
||||
// add clients
|
||||
// let clients = server
|
||||
@@ -141,40 +158,48 @@ async fn do_emulation(
|
||||
// emulation.create(handle).await;
|
||||
// }
|
||||
|
||||
let mut last_ignored = None;
|
||||
loop {
|
||||
tokio::select! {
|
||||
udp_event = udp_rx.recv() => {
|
||||
let udp_event = match udp_event {
|
||||
Some(Ok(e)) => e,
|
||||
Some(Err(e)) => {
|
||||
log::warn!("network error: {e}");
|
||||
continue;
|
||||
}
|
||||
None => break,
|
||||
};
|
||||
handle_udp_rx(&server, &capture_tx, &mut emulation, &sender_tx, &mut last_ignored, udp_event, &timer_notify).await?;
|
||||
}
|
||||
emulate_event = rx.recv() => {
|
||||
match emulate_event {
|
||||
Some(e) => match e {
|
||||
EmulationEvent::Create(h) => emulation.create(h).await,
|
||||
EmulationEvent::Destroy(h) => emulation.destroy(h).await,
|
||||
EmulationEvent::ReleaseKeys(c) => release_keys(&server, &mut emulation, c).await?,
|
||||
},
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
_ = cancellation_token.cancelled() => break,
|
||||
}
|
||||
}
|
||||
|
||||
// release potentially still pressed keys
|
||||
release_all_keys(server, &mut emulation).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn do_emulation_session(
|
||||
emulation: &mut Box<dyn InputEmulation>,
|
||||
rx: &mut Receiver<EmulationEvent>,
|
||||
server: &Server,
|
||||
udp_rx: &mut Receiver<Result<(Event, SocketAddr), NetworkError>>,
|
||||
sender_tx: &Sender<(Event, SocketAddr)>,
|
||||
capture_tx: &Sender<CaptureEvent>,
|
||||
timer_notify: &Notify,
|
||||
cancellation_token: &CancellationToken,
|
||||
) -> Result<(), LanMouseEmulationError> {
|
||||
let mut last_ignored = None;
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
udp_event = udp_rx.recv() => {
|
||||
let udp_event = match udp_event.expect("channel closed") {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
log::warn!("network error: {e}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
handle_udp_rx(&server, &capture_tx, emulation, &sender_tx, &mut last_ignored, udp_event, &timer_notify).await?;
|
||||
}
|
||||
emulate_event = rx.recv() => {
|
||||
match emulate_event.expect("channel closed") {
|
||||
EmulationEvent::Create(h) => emulation.create(h).await,
|
||||
EmulationEvent::Destroy(h) => emulation.destroy(h).await,
|
||||
EmulationEvent::ReleaseKeys(c) => release_keys(&server, emulation, c).await?,
|
||||
}
|
||||
}
|
||||
_ = cancellation_token.cancelled() => break Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_udp_rx(
|
||||
server: &Server,
|
||||
capture_tx: &Sender<CaptureEvent>,
|
||||
@@ -232,7 +257,7 @@ async fn handle_udp_rx(
|
||||
);
|
||||
// restart timer if necessary
|
||||
if restart_timer {
|
||||
timer_notify.notify_waiters();
|
||||
timer_notify.notify_one();
|
||||
}
|
||||
ignore_event
|
||||
} else {
|
||||
|
||||
@@ -33,8 +33,8 @@ pub(crate) fn new(
|
||||
mut frontend: FrontendListener,
|
||||
mut event: Receiver<FrontendEvent>,
|
||||
server: Server,
|
||||
notify_capture: Arc<Notify>,
|
||||
notify_emulation: Arc<Notify>,
|
||||
notify_capture: Arc<Notify>,
|
||||
capture: Sender<CaptureEvent>,
|
||||
emulate: Sender<EmulationEvent>,
|
||||
resolve_ch: Sender<DnsRequest>,
|
||||
@@ -127,10 +127,11 @@ async fn handle_frontend_event(
|
||||
log::debug!("frontend: {event:?}");
|
||||
match event {
|
||||
FrontendRequest::EnableCapture => {
|
||||
notify_capture.notify_waiters();
|
||||
notify_capture.notify_one();
|
||||
}
|
||||
FrontendRequest::EnableEmulation => {
|
||||
notify_emulation.notify_waiters();
|
||||
log::info!("received emulation enable request");
|
||||
notify_emulation.notify_one();
|
||||
}
|
||||
FrontendRequest::Create => {
|
||||
let handle = add_client(server, frontend).await;
|
||||
|
||||
Reference in New Issue
Block a user