Compare commits

..

1 Commits

Author SHA1 Message Date
Ferdinand Schober
377997c6d2 extract frontend crate 2024-09-04 16:59:23 +02:00
8 changed files with 141 additions and 120 deletions

View File

@@ -30,7 +30,6 @@ pub fn run() -> Result<(), IpcError> {
struct Cli {
clients: Vec<(ClientHandle, ClientConfig, ClientState)>,
changed: Option<ClientHandle>,
rx: AsyncFrontendEventReader,
tx: AsyncFrontendRequestWriter,
}
@@ -39,7 +38,6 @@ impl Cli {
fn new(rx: AsyncFrontendEventReader, tx: AsyncFrontendRequestWriter) -> Cli {
Self {
clients: vec![],
changed: None,
rx,
tx,
}
@@ -82,14 +80,9 @@ impl Cli {
event = self.rx.next() => {
if let Some(event) = event {
self.handle_event(event?);
} else {
break Ok(());
}
}
}
if let Some(handle) = self.changed.take() {
self.update_client(handle).await?;
}
}
}
@@ -209,7 +202,6 @@ impl Cli {
fn handle_event(&mut self, event: FrontendEvent) {
match event {
FrontendEvent::Changed(h) => self.changed = Some(h),
FrontendEvent::Created(h, c, s) => {
eprint!("client added ({h}): ");
print_config(&c);

View File

@@ -6,7 +6,7 @@ use std::{env, process, str};
use window::Window;
use lan_mouse_ipc::{FrontendEvent, FrontendRequest};
use lan_mouse_ipc::FrontendEvent;
use adw::Application;
use gtk::{
@@ -92,9 +92,6 @@ fn build_ui(app: &Application) {
loop {
let notify = receiver.recv().await.unwrap_or_else(|_| process::exit(1));
match notify {
FrontendEvent::Changed(handle) => {
window.request(FrontendRequest::GetState(handle));
}
FrontendEvent::Created(handle, client, state) => {
window.new_client(handle, client, state);
}

View File

@@ -247,11 +247,7 @@ impl Window {
}
pub fn request_client_state(&self, client: &ClientObject) {
self.request_client_state_for(client.handle());
}
pub fn request_client_state_for(&self, handle: ClientHandle) {
self.request(FrontendRequest::GetState(handle));
self.request(FrontendRequest::GetState(client.handle()));
}
pub fn request_client_create(&self) {

View File

@@ -166,8 +166,6 @@ pub struct ClientState {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FrontendEvent {
/// client state has changed, new state must be requested via [`FrontendRequest::GetState`]
Changed(ClientHandle),
/// a client was created
Created(ClientHandle, ClientConfig, ClientState),
/// no such client

View File

@@ -2,9 +2,22 @@ use crate::config::Config;
use futures::StreamExt;
use input_capture::{self, CaptureError, CaptureEvent, InputCapture, InputCaptureError, Position};
use input_event::{Event, KeyboardEvent};
use tokio::task::LocalSet;
pub async fn run(config: Config) -> Result<(), InputCaptureError> {
pub fn run() -> Result<(), InputCaptureError> {
log::info!("running input capture test");
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.unwrap();
let config = Config::new().unwrap();
runtime.block_on(LocalSet::new().run_until(input_capture_test(config)))
}
async fn input_capture_test(config: Config) -> Result<(), InputCaptureError> {
log::info!("creating input capture");
let backend = config.capture_backend.map(|b| b.into());
loop {

View File

@@ -3,17 +3,28 @@ use input_emulation::{InputEmulation, InputEmulationError};
use input_event::{Event, PointerEvent};
use std::f64::consts::PI;
use std::time::{Duration, Instant};
use tokio::task::LocalSet;
pub fn run() -> Result<(), InputEmulationError> {
log::info!("running input emulation test");
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.unwrap();
let config = Config::new().unwrap();
runtime.block_on(LocalSet::new().run_until(input_emulation_test(config)))
}
const FREQUENCY_HZ: f64 = 1.0;
const RADIUS: f64 = 100.0;
pub async fn run(config: Config) -> Result<(), InputEmulationError> {
log::info!("running input emulation test");
async fn input_emulation_test(config: Config) -> Result<(), InputEmulationError> {
let backend = config.emulation_backend.map(|b| b.into());
let mut emulation = InputEmulation::new(backend).await?;
emulation.create(0).await;
let start = Instant::now();
let mut offset = (0, 0);
loop {

View File

@@ -1,6 +1,4 @@
use env_logger::Env;
use input_capture::InputCaptureError;
use input_emulation::InputEmulationError;
use lan_mouse::{
capture_test,
config::{Config, ConfigError, Frontend},
@@ -9,29 +7,12 @@ use lan_mouse::{
};
use lan_mouse_ipc::IpcError;
use std::{
future::Future,
io,
process::{self, Child, Command},
};
use thiserror::Error;
use tokio::task::LocalSet;
#[derive(Debug, Error)]
enum LanMouseError {
#[error(transparent)]
Service(#[from] ServiceError),
#[error(transparent)]
IpcError(#[from] IpcError),
#[error(transparent)]
Config(#[from] ConfigError),
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Capture(#[from] InputCaptureError),
#[error(transparent)]
Emulation(#[from] InputEmulationError),
}
pub fn main() {
// init logging
let env = Env::default().filter_or("LAN_MOUSE_LOG_LEVEL", "info");
@@ -43,6 +24,26 @@ pub fn main() {
}
}
fn start_service() -> Result<Child, io::Error> {
let child = Command::new(std::env::current_exe()?)
.args(std::env::args().skip(1))
.arg("--daemon")
.spawn()?;
Ok(child)
}
#[derive(Debug, Error)]
enum LanMouseError {
#[error(transparent)]
Service(#[from] ServiceError),
#[error(transparent)]
IpcError(#[from] IpcError),
#[error(transparent)]
Config(#[from] ConfigError),
#[error(transparent)]
Io(#[from] io::Error),
}
fn run() -> Result<(), LanMouseError> {
// parse config file + cli args
let config = Config::new()?;
@@ -50,12 +51,12 @@ fn run() -> Result<(), LanMouseError> {
log::info!("release bind: {:?}", config.release_bind);
if config.test_capture {
run_async(capture_test::run(config))?;
capture_test::run().unwrap();
} else if config.test_emulation {
run_async(emulation_test::run(config))?;
emulation_test::run().unwrap();
} else if config.daemon {
// if daemon is specified we run the service
run_async(run_service(config))?;
run_service(config)?;
} else {
// otherwise start the service as a child process and
// run a frontend
@@ -76,11 +77,7 @@ fn run() -> Result<(), LanMouseError> {
Ok(())
}
fn run_async<F, E>(f: F) -> Result<(), LanMouseError>
where
F: Future<Output = Result<(), E>>,
LanMouseError: From<E>,
{
fn run_service(config: Config) -> Result<(), ServiceError> {
// create single threaded tokio runtime
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
@@ -88,21 +85,16 @@ where
.build()?;
// run async event loop
Ok(runtime.block_on(LocalSet::new().run_until(f))?)
}
runtime.block_on(LocalSet::new().run_until(async {
// run main loop
log::info!("Press {:?} to release the mouse", config.release_bind);
fn start_service() -> Result<Child, io::Error> {
let child = Command::new(std::env::current_exe()?)
.args(std::env::args().skip(1))
.arg("--daemon")
.spawn()?;
Ok(child)
}
let mut server = Server::new(config);
server.run().await?;
async fn run_service(config: Config) -> Result<(), ServiceError> {
log::info!("Press {:?} to release the mouse", config.release_bind);
Server::new(config).run().await?;
log::info!("service exited!");
log::debug!("service exiting");
Result::<(), ServiceError>::Ok(())
}))?;
Ok(())
}

View File

@@ -58,6 +58,7 @@ pub struct Server {
notifies: Rc<Notifies>,
config: Rc<Config>,
pending_frontend_events: Rc<RefCell<VecDeque<FrontendEvent>>>,
pending_dns_requests: Rc<RefCell<VecDeque<ClientHandle>>>,
capture_status: Rc<Cell<Status>>,
emulation_status: Rc<Cell<Status>>,
}
@@ -69,6 +70,7 @@ struct Notifies {
ping: Notify,
port_changed: Notify,
frontend_event_pending: Notify,
dns_request_pending: Notify,
cancel: CancellationToken,
}
@@ -112,6 +114,7 @@ impl Server {
release_bind,
notifies,
pending_frontend_events: Rc::new(RefCell::new(VecDeque::new())),
pending_dns_requests: Rc::new(RefCell::new(VecDeque::new())),
capture_status: Default::default(),
emulation_status: Default::default(),
}
@@ -134,10 +137,17 @@ impl Server {
let (udp_send_tx, udp_send_rx) = channel(); /* udp sender */
let (dns_tx, dns_rx) = channel(); /* dns requests */
// udp task
let network = network_task::new(self.clone(), udp_recv_tx.clone(), udp_send_rx).await?;
// input capture
let capture = capture_task::new(self.clone(), capture_rx, udp_send_tx.clone());
// input emulation
let emulation =
emulation_task::new(self.clone(), emulation_rx, udp_recv_rx, udp_send_tx.clone());
// create dns resolver
let resolver = DnsResolver::new(dns_rx)?;
let dns_task = tokio::task::spawn_local(resolver.run(self.clone()));
@@ -150,9 +160,11 @@ impl Server {
);
for handle in self.active_clients() {
dns_tx.send(handle).expect("channel closed");
self.request_dns(handle);
}
log::info!("running service");
loop {
tokio::select! {
request = frontend.next() => {
@@ -164,8 +176,9 @@ impl Server {
}
None => break,
};
log::debug!("handle frontend request: {request:?}");
self.handle_request(&capture_tx.clone(), &emulation_tx.clone(), request, &dns_tx);
log::debug!("received frontend request: {request:?}");
self.handle_request(&capture_tx.clone(), &emulation_tx.clone(), request).await;
log::debug!("handled frontend request");
}
_ = self.notifies.frontend_event_pending.notified() => {
while let Some(event) = {
@@ -176,6 +189,15 @@ impl Server {
frontend.broadcast(event).await;
}
},
_ = self.notifies.dns_request_pending.notified() => {
while let Some(request) = {
/* need to drop borrow before next iteration! */
let request = self.pending_dns_requests.borrow_mut().pop_front();
request
} {
dns_tx.send(request).expect("channel closed");
}
}
_ = self.cancelled() => break,
r = signal::ctrl_c() => {
r.expect("failed to wait for CTRL+C");
@@ -210,7 +232,6 @@ impl Server {
}
fn notify_capture(&self) {
log::info!("received capture enable request");
self.notifies.capture.notify_waiters()
}
@@ -219,7 +240,6 @@ impl Server {
}
fn notify_emulation(&self) {
log::info!("received emulation enable request");
self.notifies.emulation.notify_waiters()
}
@@ -246,7 +266,10 @@ impl Server {
}
pub(crate) fn client_updated(&self, handle: ClientHandle) {
self.notify_frontend(FrontendEvent::Changed(handle));
let state = self.client_manager.borrow().get(handle).cloned();
if let Some((config, state)) = state {
self.notify_frontend(FrontendEvent::State(handle, config, state));
}
}
fn active_clients(&self) -> Vec<ClientHandle> {
@@ -258,43 +281,55 @@ impl Server {
.collect()
}
fn handle_request(
fn request_dns(&self, handle: ClientHandle) {
self.pending_dns_requests.borrow_mut().push_back(handle);
self.notifies.dns_request_pending.notify_one();
}
async fn handle_request(
&self,
capture: &Sender<CaptureRequest>,
emulate: &Sender<EmulationRequest>,
event: FrontendRequest,
dns: &Sender<ClientHandle>,
) -> bool {
log::debug!("frontend: {event:?}");
match event {
FrontendRequest::EnableCapture => self.notify_capture(),
FrontendRequest::EnableEmulation => self.notify_emulation(),
FrontendRequest::EnableCapture => {
log::info!("received capture enable request");
self.notify_capture();
}
FrontendRequest::EnableEmulation => {
log::info!("received emulation enable request");
self.notify_emulation();
}
FrontendRequest::Create => {
self.add_client();
let handle = self.add_client().await;
self.request_dns(handle);
}
FrontendRequest::Activate(handle, active) => {
if active {
self.activate_client(capture, emulate, handle);
self.activate_client(capture, emulate, handle).await;
} else {
self.deactivate_client(capture, emulate, handle);
self.deactivate_client(capture, emulate, handle).await;
}
}
FrontendRequest::ChangePort(port) => self.request_port_change(port),
FrontendRequest::Delete(handle) => {
self.remove_client(capture, emulate, handle);
self.remove_client(capture, emulate, handle).await;
self.notify_frontend(FrontendEvent::Deleted(handle));
}
FrontendRequest::Enumerate() => self.enumerate(),
FrontendRequest::GetState(handle) => self.broadcast_client(handle),
FrontendRequest::UpdateFixIps(handle, fix_ips) => self.update_fix_ips(handle, fix_ips),
FrontendRequest::UpdateHostname(handle, host) => {
self.update_hostname(handle, host, dns)
FrontendRequest::UpdateFixIps(handle, fix_ips) => {
self.update_fix_ips(handle, fix_ips);
self.request_dns(handle);
}
FrontendRequest::UpdateHostname(handle, host) => self.update_hostname(handle, host),
FrontendRequest::UpdatePort(handle, port) => self.update_port(handle, port),
FrontendRequest::UpdatePosition(handle, pos) => {
self.update_pos(handle, capture, emulate, pos)
self.update_pos(handle, capture, emulate, pos).await;
}
FrontendRequest::ResolveDns(handle) => dns.send(handle).expect("channel closed"),
FrontendRequest::ResolveDns(handle) => self.request_dns(handle),
FrontendRequest::Sync => {
self.enumerate();
self.notify_frontend(FrontendEvent::EmulationStatus(self.emulation_status.get()));
@@ -315,7 +350,7 @@ impl Server {
self.notify_frontend(FrontendEvent::Enumerate(clients));
}
fn add_client(&self) -> ClientHandle {
async fn add_client(&self) -> ClientHandle {
let handle = self.client_manager.borrow_mut().add_client();
log::info!("added client {handle}");
let (c, s) = self.client_manager.borrow().get(handle).unwrap().clone();
@@ -323,40 +358,41 @@ impl Server {
handle
}
fn deactivate_client(
async fn deactivate_client(
&self,
capture: &Sender<CaptureRequest>,
emulate: &Sender<EmulationRequest>,
handle: ClientHandle,
) {
log::debug!("deactivating client {handle}");
match self.client_manager.borrow_mut().get_mut(handle) {
None => return,
Some((_, s)) if !s.active => return,
Some((_, s)) => s.active = false,
None => return,
};
let _ = capture.send(CaptureRequest::Destroy(handle));
let _ = emulate.send(EmulationRequest::Destroy(handle));
self.client_updated(handle);
log::info!("deactivated client {handle}");
log::debug!("deactivating client {handle} done");
}
fn activate_client(
async fn activate_client(
&self,
capture: &Sender<CaptureRequest>,
emulate: &Sender<EmulationRequest>,
handle: ClientHandle,
) {
log::debug!("activating client");
/* deactivate potential other client at this position */
let pos = match self.client_manager.borrow().get(handle) {
None => return,
Some((_, s)) if s.active => return,
Some((client, _)) => client.pos,
None => return,
};
let other = self.client_manager.borrow_mut().find_client(pos);
if let Some(other) = other {
self.deactivate_client(capture, emulate, other);
if other != handle {
self.deactivate_client(capture, emulate, other).await;
}
}
/* activate the client */
@@ -369,13 +405,10 @@ impl Server {
/* notify emulation, capture and frontends */
let _ = capture.send(CaptureRequest::Create(handle, to_capture_pos(pos)));
let _ = emulate.send(EmulationRequest::Create(handle));
self.client_updated(handle);
log::info!("activated client {handle} ({pos})");
log::debug!("activating client {handle} done");
}
fn remove_client(
async fn remove_client(
&self,
capture: &Sender<CaptureRequest>,
emulate: &Sender<EmulationRequest>,
@@ -407,7 +440,6 @@ impl Server {
c.fix_ips = fix_ips;
};
self.update_ips(handle);
self.client_updated(handle);
}
pub(crate) fn update_dns_ips(&self, handle: ClientHandle, dns_ips: Vec<IpAddr>) {
@@ -415,7 +447,6 @@ impl Server {
s.dns_ips = dns_ips;
};
self.update_ips(handle);
self.client_updated(handle);
}
fn update_ips(&self, handle: ClientHandle) {
@@ -429,12 +460,7 @@ impl Server {
}
}
fn update_hostname(
&self,
handle: ClientHandle,
hostname: Option<String>,
dns: &Sender<ClientHandle>,
) {
fn update_hostname(&self, handle: ClientHandle, hostname: Option<String>) {
let mut client_manager = self.client_manager.borrow_mut();
let Some((c, s)) = client_manager.get_mut(handle) else {
return;
@@ -443,13 +469,10 @@ impl Server {
// hostname changed
if c.hostname != hostname {
c.hostname = hostname;
s.ips = HashSet::from_iter(c.fix_ips.iter().cloned());
s.active_addr = None;
s.dns_ips.clear();
drop(client_manager);
self.update_ips(handle);
dns.send(handle).expect("channel closed");
self.request_dns(handle);
}
self.client_updated(handle);
}
fn update_port(&self, handle: ClientHandle, port: u16) {
@@ -464,7 +487,7 @@ impl Server {
}
}
fn update_pos(
async fn update_pos(
&self,
handle: ClientHandle,
capture: &Sender<CaptureRequest>,
@@ -478,19 +501,18 @@ impl Server {
};
let changed = c.pos != pos;
if changed {
log::info!("update pos {handle} {} -> {}", c.pos, pos);
}
c.pos = pos;
(changed, s.active)
};
// update state in event input emulator & input capture
if changed {
self.deactivate_client(capture, emulate, handle);
if active {
self.activate_client(capture, emulate, handle);
let _ = capture.send(CaptureRequest::Destroy(handle));
let _ = emulate.send(EmulationRequest::Destroy(handle));
}
let _ = capture.send(CaptureRequest::Create(handle, to_capture_pos(pos)));
let _ = emulate.send(EmulationRequest::Create(handle));
}
}
@@ -523,7 +545,7 @@ impl Server {
self.client_updated(handle);
}
pub(crate) fn get_hostname(&self, handle: ClientHandle) -> Option<String> {
pub(crate) fn get_hostname(&self, handle: u64) -> Option<String> {
self.client_manager
.borrow_mut()
.get_mut(handle)
@@ -539,12 +561,12 @@ impl Server {
self.state.replace(state);
}
fn set_active(&self, handle: Option<ClientHandle>) {
fn set_active(&self, handle: Option<u64>) {
log::debug!("active client => {handle:?}");
self.active_client.replace(handle);
}
fn active_addr(&self, handle: ClientHandle) -> Option<SocketAddr> {
fn active_addr(&self, handle: u64) -> Option<SocketAddr> {
self.client_manager
.borrow()
.get(handle)