Files
os/crates/sf-launcher/src/status.rs
2026-10-25 14:11:49 +01:00

71 lines
2.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Live TV state for the home screen: network, the Siri Remote's battery and
//! the display mode. The probes shell out (ip, busctl, bluetoothctl,
//! wlr-randr), so they run on a background thread.
use std::time::Duration;
use crate::system;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Status {
/// "wired" | "wifi" | "none" (icon names in ui/icons.slint).
pub net: &'static str,
/// "Vezetékes hálózat", the Wi-Fi name, or "Nincs hálózat".
pub net_label: String,
pub remote_battery: Option<u8>,
pub remote_connected: bool,
/// "4K UHD · 60 Hz".
pub display: Option<String>,
}
pub fn probe() -> Status {
let info = system::network();
let (net, net_label) = match (&info.iface, info.wireless) {
(None, _) => ("none", "Nincs hálózat".to_string()),
(Some(_), false) => ("wired", "Vezetékes hálózat".to_string()),
(Some(_), true) => {
let name = system::wifi()
.ok()
.flatten()
.and_then(|w| w.networks.into_iter().find(|n| n.connected).map(|n| n.name));
("wifi", name.unwrap_or_else(|| "Wi-Fi".into()))
}
};
let remote = system::bt_devices().ok().and_then(|d| d.into_iter().find(|d| d.remote));
let display = system::display().ok().and_then(|d| {
d.current_mode().map(|m| {
let hz = m.hz.round() as u32;
match m.name() {
"" => format!("{} × {} · {hz} Hz", m.w, m.h),
name => format!("{name} · {hz} Hz"),
}
})
});
Status {
net,
net_label,
remote_battery: remote.as_ref().and_then(|r| r.battery),
remote_connected: remote.as_ref().is_some_and(|r| r.connected),
display,
}
}
/// Probes now and then every 20 s; `on_change` runs (on the probe thread)
/// whenever something changed.
pub fn spawn(on_change: impl Fn(Status) + Send + 'static) {
let spawned = std::thread::Builder::new().name("status".into()).spawn(move || {
let mut last: Option<Status> = None;
loop {
let now = probe();
if last.as_ref() != Some(&now) {
on_change(now.clone());
last = Some(now);
}
std::thread::sleep(Duration::from_secs(20));
}
});
if let Err(e) = spawned {
log::warn!("status thread: {e}");
}
}