extract frontend crate (#186)

This commit is contained in:
Ferdinand Schober
2024-09-04 17:29:29 +02:00
committed by GitHub
parent 12bc0d86ca
commit be677d4c81
37 changed files with 959 additions and 753 deletions

View File

@@ -1,9 +1,16 @@
use anyhow::Result;
use std::process::{self, Child, Command};
use env_logger::Env;
use lan_mouse::{capture_test, config::Config, emulation_test, frontend, server::Server};
use lan_mouse::{
capture_test,
config::{Config, ConfigError, Frontend},
emulation_test,
server::{Server, ServiceError},
};
use lan_mouse_ipc::IpcError;
use std::{
io,
process::{self, Child, Command},
};
use thiserror::Error;
use tokio::task::LocalSet;
pub fn main() {
@@ -17,7 +24,7 @@ pub fn main() {
}
}
pub fn start_service() -> Result<Child> {
fn start_service() -> Result<Child, io::Error> {
let child = Command::new(std::env::current_exe()?)
.args(std::env::args().skip(1))
.arg("--daemon")
@@ -25,16 +32,28 @@ pub fn start_service() -> Result<Child> {
Ok(child)
}
pub fn run() -> Result<()> {
#[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()?;
log::debug!("{config:?}");
log::info!("release bind: {:?}", config.release_bind);
if config.test_capture {
capture_test::run()?;
capture_test::run().unwrap();
} else if config.test_emulation {
emulation_test::run()?;
emulation_test::run().unwrap();
} else if config.daemon {
// if daemon is specified we run the service
run_service(config)?;
@@ -42,7 +61,7 @@ pub fn run() -> Result<()> {
// otherwise start the service as a child process and
// run a frontend
let mut service = start_service()?;
frontend::run_frontend(&config)?;
run_frontend(&config)?;
#[cfg(unix)]
{
// on unix we give the service a chance to terminate gracefully
@@ -55,10 +74,10 @@ pub fn run() -> Result<()> {
service.kill()?;
}
anyhow::Ok(())
Ok(())
}
fn run_service(config: Config) -> Result<()> {
fn run_service(config: Config) -> Result<(), ServiceError> {
// create single threaded tokio runtime
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
@@ -74,7 +93,22 @@ fn run_service(config: Config) -> Result<()> {
server.run().await?;
log::debug!("service exiting");
anyhow::Ok(())
Result::<(), ServiceError>::Ok(())
}))?;
Ok(())
}
fn run_frontend(config: &Config) -> Result<(), IpcError> {
match config.frontend {
#[cfg(feature = "gtk")]
Frontend::Gtk => {
lan_mouse_gtk::run();
}
#[cfg(not(feature = "gtk"))]
Frontend::Gtk => panic!("gtk frontend requested but feature not enabled!"),
Frontend::Cli => {
lan_mouse_cli::run()?;
}
};
Ok(())
}