2025-02-11 23:15:24 -08:00
|
|
|
mod daemon;
|
2025-07-31 19:16:44 -07:00
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2025-05-25 18:52:18 -07:00
|
|
|
mod dbus;
|
2025-02-11 23:15:24 -08:00
|
|
|
|
|
|
|
|
use log::LevelFilter;
|
2025-05-25 18:52:18 -07:00
|
|
|
use std::future;
|
2025-02-12 00:32:44 -08:00
|
|
|
|
2025-05-26 15:49:29 -07:00
|
|
|
use daemon::Daemon;
|
2025-04-27 22:44:05 -07:00
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
fn initialize_logging() {
|
2025-04-28 16:00:04 -07:00
|
|
|
// Weird: is this the best way to do this?
|
|
|
|
|
let log_level = std::env::var("RUST_LOG")
|
|
|
|
|
.map(|s| s.parse::<LevelFilter>().unwrap_or(LevelFilter::Info))
|
|
|
|
|
.unwrap_or(LevelFilter::Info);
|
|
|
|
|
|
2025-05-25 18:52:18 -07:00
|
|
|
env_logger::Builder::from_default_env()
|
2025-06-12 18:09:58 -07:00
|
|
|
.format_timestamp_millis()
|
2025-04-28 16:00:04 -07:00
|
|
|
.filter_level(log_level)
|
2025-02-11 23:15:24 -08:00
|
|
|
.init();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 19:16:44 -07:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
async fn start_ipc_agent(daemon: &Daemon) {
|
|
|
|
|
use dbus::agent::DBusAgent;
|
|
|
|
|
|
|
|
|
|
// Start the D-Bus agent (events in, signals out).
|
|
|
|
|
let agent = DBusAgent::new(daemon.event_sender.clone(), daemon.obtain_signal_receiver());
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
agent.run().await;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
async fn start_ipc_agent(daemon: &Daemon) {
|
|
|
|
|
// TODO: Implement macOS IPC agent.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
|
|
|
|
async fn start_ipc_agent(daemon: &Daemon) {
|
|
|
|
|
panic!("Unsupported IPC platform");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
|
|
|
|
initialize_logging();
|
|
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
// Create the daemon
|
2025-04-27 12:53:45 -07:00
|
|
|
let mut daemon = Daemon::new()
|
|
|
|
|
.map_err(|e| {
|
2025-06-18 01:03:14 -07:00
|
|
|
log::error!("Failed to initialize daemon: {}", e);
|
2025-04-27 12:53:45 -07:00
|
|
|
std::process::exit(1);
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-07-31 19:16:44 -07:00
|
|
|
// Start the IPC agent.
|
|
|
|
|
start_ipc_agent(&daemon).await;
|
2025-04-27 22:44:05 -07:00
|
|
|
|
2025-06-18 01:03:14 -07:00
|
|
|
// Run the main daemon loop.
|
2025-04-27 12:53:45 -07:00
|
|
|
daemon.run().await;
|
2025-04-25 21:42:29 -07:00
|
|
|
|
2025-06-18 01:03:14 -07:00
|
|
|
// Keep the process alive as long as any background tasks are running.
|
2025-02-11 23:15:24 -08:00
|
|
|
future::pending::<()>().await;
|
|
|
|
|
}
|