Private
Public Access
1
0
Files
Kordophone/core/kordophoned/src/main.rs

76 lines
2.0 KiB
Rust
Raw Normal View History

#[cfg(target_os = "linux")]
mod dbus;
2025-02-11 23:15:24 -08:00
2025-08-01 12:26:17 -07:00
#[cfg(target_os = "macos")]
mod xpc;
2025-02-11 23:15:24 -08:00
use log::LevelFilter;
use std::future;
2025-02-12 00:32:44 -08:00
2025-08-20 23:12:31 -07:00
use kordophoned::daemon::Daemon;
2025-04-27 22:44:05 -07:00
2025-02-11 23:15:24 -08:00
fn initialize_logging() {
// 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);
env_logger::Builder::from_default_env()
2025-06-12 18:09:58 -07:00
.format_timestamp_millis()
.filter_level(log_level)
2025-02-11 23:15:24 -08:00
.init();
}
#[cfg(target_os = "linux")]
2025-07-31 19:40:03 -07:00
async fn start_ipc_agent(daemon: &mut 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")]
2025-07-31 19:40:03 -07:00
async fn start_ipc_agent(daemon: &mut Daemon) {
2025-08-01 12:26:17 -07:00
// Start the macOS XPC agent (events in, signals out) on a dedicated thread.
2025-08-23 20:02:54 -07:00
let agent =
xpc::agent::XpcAgent::new(daemon.event_sender.clone(), daemon.obtain_signal_receiver());
2025-08-01 12:26:17 -07:00
std::thread::spawn(move || {
// Use a single-threaded Tokio runtime for the XPC agent.
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Unable to create tokio runtime for XPC agent");
rt.block_on(agent.run());
});
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
2025-07-31 19:40:03 -07:00
async fn start_ipc_agent(daemon: &mut Daemon) {
panic!("Unsupported IPC platform");
}
2025-02-11 23:15:24 -08:00
#[tokio::main]
async fn main() {
initialize_logging();
// Create the daemon
let mut daemon = Daemon::new()
.map_err(|e| {
log::error!("Failed to initialize daemon: {}", e);
std::process::exit(1);
})
.unwrap();
// Start the IPC agent.
2025-07-31 19:40:03 -07:00
start_ipc_agent(&mut daemon).await;
2025-04-27 22:44:05 -07:00
// Run the main daemon loop.
daemon.run().await;
// Keep the process alive as long as any background tasks are running.
2025-02-11 23:15:24 -08:00
future::pending::<()>().await;
}