Compare commits

..

1 Commits

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

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(())
}