Files
btle-proximity/src/main.rs
T
Bastian Mäuser dcbe307c4a Bump sd-notify to 0.5 and toml to 1.0
sd-notify 0.5 drops the unset_environment bool from notify() (we always
passed false; the unset variant is a separate function we never used).
toml 1.x is API-compatible for our Config deserialization and its MSRV
is exactly 1.85, matching the project floor. zbus stays on 5.x: v6
requires rustc 1.89, past the Ubuntu 24.04 toolchain this repo commits
to. Resolver locked all updated deps at Rust-1.85-compatible versions.
2026-11-12 01:55:53 +01:00

92 lines
2.4 KiB
Rust

mod config;
mod locker;
mod policy;
mod scanner;
mod unlock_notice;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(
name = "btle-proximity",
version,
about = "Locks the desktop session when your phone leaves Bluetooth range"
)]
struct Cli {
/// Path to the TOML config file
#[arg(long, short, value_name = "FILE")]
config: Option<std::path::PathBuf>,
/// Log lock actions instead of executing them
#[arg(long)]
dry_run: bool,
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Subcommand)]
enum Command {
/// Run the daemon (default)
Run,
/// List paired Bluetooth devices and exit
ListPaired,
/// Wait for the session to unlock and show the lock explanation
/// notification (for testing the unlock notice path)
#[command(hide = true)]
TestNotice,
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
match cli.command.as_ref().unwrap_or(&Command::Run) {
Command::ListPaired => {
for dev in scanner::list_paired().await? {
println!(
"{} {:20} {}",
dev.address,
dev.alias,
if dev.connected { "connected" } else { "" }
);
}
Ok(())
}
Command::Run => run(cli).await,
Command::TestNotice => {
unlock_notice::notify_after_unlock(
std::time::Instant::now() - std::time::Duration::from_secs(300),
)
.await;
Ok(())
}
}
}
async fn run(cli: Cli) -> Result<()> {
let path = cli.config.or_else(config::default_path).context(
"no config file found: pass --config or install one at \
$XDG_CONFIG_HOME/btle-proximity/config.toml",
)?;
let mut cfg = config::Config::load(&path)?;
if cli.dry_run {
cfg.dry_run = true;
}
scanner::run(cfg).await
}
pub fn notify_ready() {
let _ = sd_notify::notify(&[sd_notify::NotifyState::Ready]);
}
pub fn notify_stopping() {
let _ = sd_notify::notify(&[sd_notify::NotifyState::Stopping]);
}