#[cfg(test)]
mod tests;
use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, HashSet};
use std::io::Read;
use std::path::Path;
use std::rc::{Rc, Weak};
use std::sync::Once;
use crabshell_reactive::{ReadSignal, Signal, untrack};
use crabshell_ui::Cx;
use inotify::WatchMask;
use crate::Shared;
use crate::dbus::set_changed;
use crate::logind;
use crate::watch::{self, Watch};
const MAX_DEVICES: usize = 32;
const MAX_READ: u64 = 64;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Backlight {
pub name: String,
pub brightness: u32,
pub max_brightness: u32,
}
#[derive(Clone)]
pub struct Brightness {
service: Option<Rc<Service>>,
devices: ReadSignal<Vec<Backlight>>,
}
impl Brightness {
pub fn devices(&self) -> ReadSignal<Vec<Backlight>> {
self.devices
}
pub fn set(&self, name: &str, value: u32) {
let Some(service) = &self.service else {
return;
};
let max = untrack(|| {
self.devices.try_with(|devices| {
let device = devices.iter().find(|device| device.name == name)?;
Some(device.max_brightness)
})
});
match max.flatten() {
Some(max) => service.set(name, value.min(max)),
None => tracing::warn!(name, "no such backlight, so its brightness wasn't set"),
}
}
}
pub(crate) fn brightness(cx: &Cx) -> Brightness {
let Some(service) = cx.service::<Service>() else {
static LOGGED: Once = Once::new();
LOGGED.call_once(|| {
tracing::warn!("cx.brightness() needs the services the shell provides; it stays empty");
});
return Brightness {
service: None,
devices: cx.signal(Vec::new()).read_only(),
};
};
Brightness {
devices: service.start(),
service: Some(service),
}
}
pub(crate) struct Service {
shared: Rc<Shared>,
logind: Rc<logind::Service>,
state: RefCell<Option<State>>,
pending: RefCell<BTreeMap<String, u32>>,
logged: Cell<bool>,
}
struct State {
devices: Signal<Vec<Backlight>>,
names: Vec<String>,
logged: HashSet<String>,
_watches: Vec<Watch>,
}
impl Service {
pub(crate) fn new(shared: Rc<Shared>, logind: Rc<logind::Service>) -> Service {
Service {
shared,
logind,
state: RefCell::new(None),
pending: RefCell::default(),
logged: Cell::new(false),
}
}
fn set(&self, name: &str, value: u32) {
if self.logind.connection().is_some() {
return self.logind.set_brightness(name, value);
}
if self.shared.config.system_bus.is_none() {
if !self.logged.replace(true) {
tracing::info!("no system bus, so logind can't set a brightness");
}
return;
}
self.pending.borrow_mut().insert(name.to_owned(), value);
if !self.logged.replace(true) {
tracing::info!("logind isn't reached yet, so the brightness is set once it is");
}
}
fn start(self: &Rc<Self>) -> ReadSignal<Vec<Backlight>> {
if let Some(state) = &*self.state.borrow() {
return state.devices.read_only();
}
self.logind.start();
if let Some(connected) = self.logind.connected() {
let service = Rc::downgrade(self);
self.shared.root.effect(move || {
if connected.get()
&& let Some(service) = Weak::upgrade(&service)
{
for (name, value) in service.pending.take() {
service.logind.set_brightness(&name, value);
}
}
});
}
let root = &self.shared.config.backlight;
let names = names(root);
let watches = names
.iter()
.filter_map(|name| {
let path = root.join(name).join("actual_brightness");
let service = Rc::downgrade(self);
let on_change = move |_: &watch::Change| {
if let Some(service) = Weak::upgrade(&service) {
service.read();
}
};
watch::watch(&self.shared, &path, WatchMask::MODIFY, on_change)
.inspect_err(|error| {
tracing::warn!(%error, path = %path.display(), "can't follow this backlight");
})
.ok()
})
.collect();
let devices = self.shared.root.signal(Vec::new());
*self.state.borrow_mut() = Some(State {
devices,
names,
logged: HashSet::new(),
_watches: watches,
});
self.read();
devices.read_only()
}
fn read(&self) {
let root = &self.shared.config.backlight;
let read = {
let mut state = self.state.borrow_mut();
let Some(state) = state.as_mut() else {
return;
};
let mut devices = Vec::new();
for name in &state.names {
let dir = root.join(name);
let read = read_value(&dir.join("brightness"))
.and_then(|brightness| Ok((brightness, read_value(&dir.join("max_brightness"))?)));
match read {
Ok((brightness, max_brightness)) => devices.push(Backlight {
name: name.clone(),
brightness: brightness.min(max_brightness),
max_brightness,
}),
Err(error) => {
if state.logged.insert(name.clone()) {
tracing::warn!(name, %error, "can't read this backlight, so it isn't listed");
}
}
}
}
(state.devices, devices)
};
set_changed(read.0, read.1);
}
}
fn names(root: &Path) -> Vec<String> {
let entries = match std::fs::read_dir(root) {
Ok(entries) => entries,
Err(error) => {
tracing::info!(%error, root = %root.display(), "no backlights");
return Vec::new();
}
};
let mut names: Vec<String> = entries
.filter_map(|entry| entry.ok()?.file_name().into_string().ok())
.collect();
names.sort();
if names.len() > MAX_DEVICES {
tracing::warn!(
count = names.len(),
"more backlights than {MAX_DEVICES}; the rest aren't listed"
);
names.truncate(MAX_DEVICES);
}
names
}
fn read_value(path: &Path) -> std::io::Result<u32> {
let mut text = String::new();
std::fs::File::open(path)?
.take(MAX_READ + 1)
.read_to_string(&mut text)?;
if text.len() as u64 > MAX_READ {
return Err(std::io::Error::other(format!(
"{} is over {MAX_READ} bytes",
path.display()
)));
}
text
.trim()
.parse()
.map_err(|error| std::io::Error::other(format!("{} holds {text:?}: {error}", path.display())))
}