cleanup main (#189)

This commit is contained in:
Ferdinand Schober
2024-09-05 01:06:55 +02:00
committed by GitHub
parent be677d4c81
commit 6cd190191e
3 changed files with 45 additions and 61 deletions

View File

@@ -2,22 +2,9 @@ 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 fn run() -> Result<(), InputCaptureError> {
pub async fn run(config: Config) -> 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,28 +3,17 @@ 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;
async fn input_emulation_test(config: Config) -> Result<(), InputEmulationError> {
pub async fn run(config: Config) -> Result<(), InputEmulationError> {
log::info!("running input emulation test");
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,4 +1,6 @@
use env_logger::Env;
use input_capture::InputCaptureError;
use input_emulation::InputEmulationError;
use lan_mouse::{
capture_test,
config::{Config, ConfigError, Frontend},
@@ -7,12 +9,29 @@ 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");
@@ -24,26 +43,6 @@ 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()?;
@@ -51,12 +50,12 @@ fn run() -> Result<(), LanMouseError> {
log::info!("release bind: {:?}", config.release_bind);
if config.test_capture {
capture_test::run().unwrap();
run_async(capture_test::run(config))?;
} else if config.test_emulation {
emulation_test::run().unwrap();
run_async(emulation_test::run(config))?;
} else if config.daemon {
// if daemon is specified we run the service
run_service(config)?;
run_async(run_service(config))?;
} else {
// otherwise start the service as a child process and
// run a frontend
@@ -77,7 +76,11 @@ fn run() -> Result<(), LanMouseError> {
Ok(())
}
fn run_service(config: Config) -> Result<(), ServiceError> {
fn run_async<F, E>(f: F) -> Result<(), LanMouseError>
where
F: Future<Output = Result<(), E>>,
LanMouseError: From<E>,
{
// create single threaded tokio runtime
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
@@ -85,16 +88,21 @@ fn run_service(config: Config) -> Result<(), ServiceError> {
.build()?;
// run async event loop
runtime.block_on(LocalSet::new().run_until(async {
// run main loop
log::info!("Press {:?} to release the mouse", config.release_bind);
Ok(runtime.block_on(LocalSet::new().run_until(f))?)
}
let mut server = Server::new(config);
server.run().await?;
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)
}
log::debug!("service exiting");
Result::<(), ServiceError>::Ok(())
}))?;
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!");
Ok(())
}