summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 28434d684fc0e3755eadf26f72258b0245985c4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 tokio::task::LocalSet;

pub fn main() {
    // init logging
    let env = Env::default().filter_or("LAN_MOUSE_LOG_LEVEL", "info");
    env_logger::init_from_env(env);

    if let Err(e) = run() {
        log::error!("{e}");
        process::exit(1);
    }
}

pub fn start_service() -> Result<Child> {
    let child = Command::new(std::env::current_exe()?)
        .args(std::env::args().skip(1))
        .arg("--daemon")
        .spawn()?;
    Ok(child)
}

pub fn run() -> Result<()> {
    // 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()?;
    } else if config.test_emulation {
        emulation_test::run()?;
    } else if config.daemon {
        // if daemon is specified we run the service
        run_service(&config)?;
    } else {
        //  otherwise start the service as a child process and
        //  run a frontend
        let mut service = start_service()?;
        frontend::run_frontend(&config)?;
        #[cfg(unix)]
        {
            // on unix we give the service a chance to terminate gracefully
            let pid = service.id() as libc::pid_t;
            unsafe {
                libc::kill(pid, libc::SIGINT);
            }
            service.wait()?;
        }
        service.kill()?;
    }

    anyhow::Ok(())
}

fn run_service(config: &Config) -> Result<()> {
    // create single threaded tokio runtime
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_io()
        .enable_time()
        .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);

        let server = Server::new(config);
        server
            .run(config.capture_backend, config.emulation_backend)
            .await?;

        log::debug!("service exiting");
        anyhow::Ok(())
    }))?;
    Ok(())
}