Background service (#43)

better handling of background-service: lan-mouse can now be run without a gui by specifying --daemon as an argument.
Otherwise the servic will be run as a child process and correctly terminate when the window is closed / frontend exits.

Closes #38
This commit is contained in:
Ferdinand Schober
2023-12-09 00:36:01 +01:00
committed by GitHub
parent 9b242f6138
commit 56e5f7a30d
13 changed files with 488 additions and 170 deletions

View File

@@ -2,7 +2,7 @@ mod window;
mod client_object;
mod client_row;
use std::{io::{Result, Read, ErrorKind}, thread::{self, JoinHandle}, env, process, path::Path, os::unix::net::UnixStream, str};
use std::{io::{Read, ErrorKind}, env, process, str};
use crate::{frontend::gtk::window::Window, config::DEFAULT_PORT};
@@ -14,11 +14,11 @@ use self::client_object::ClientObject;
use super::FrontendNotify;
pub fn start() -> Result<JoinHandle<glib::ExitCode>> {
log::debug!("starting gtk frontend");
thread::Builder::new()
.name("gtk-thread".into())
.spawn(gtk_main)
pub fn run() -> glib::ExitCode {
log::debug!("running gtk frontend");
let ret = gtk_main();
log::debug!("frontend exited");
ret
}
fn gtk_main() -> glib::ExitCode {
@@ -33,7 +33,8 @@ fn gtk_main() -> glib::ExitCode {
app.connect_startup(|_| load_css());
app.connect_activate(build_ui);
app.run()
let args: Vec<&'static str> = vec![];
app.run_with_args(&args)
}
fn load_css() {
@@ -52,20 +53,14 @@ fn load_icons() {
}
fn build_ui(app: &Application) {
let xdg_runtime_dir = match env::var("XDG_RUNTIME_DIR") {
Ok(v) => v,
log::debug!("connecting to lan-mouse-socket");
let mut rx = match super::wait_for_service() {
Ok(stream) => stream,
Err(e) => {
log::error!("{e}");
log::error!("could not connect to lan-mouse-socket: {e}");
process::exit(1);
}
};
log::debug!("connecting to lan-mouse-socket ... ");
let socket_path = Path::new(xdg_runtime_dir.as_str())
.join("lan-mouse-socket.sock");
let Ok(mut rx) = UnixStream::connect(&socket_path) else {
log::error!("Could not connect to lan-mouse-socket @ {socket_path:?}");
process::exit(1);
};
let tx = match rx.try_clone() {
Ok(sock) => sock,
Err(e) => {