//! The built-in screen's brightness, 0 to 100, as the `brightness` system //! call reads and sets it: through Intel's graphics (`i915`, the chipset's //! PWM) where it drives the panel, else through ACPI's methods on the //! integrated panel's output device (`_BCL`, `_BCM`, `_BQC`; ACPI 6.5, //! appendix B). None on a machine with neither, QEMU's among them. //! //! ACPI's way is the fallback: on most Intel laptops `_BCM` hands the //! level to the graphics driver through the firmware's OpRegion (ASLE) //! and an interrupt Pinnacle does not answer, so it changes nothing; on //! those whose firmware sets it through the embedded controller, it works. //! //! Never set below [`i915::MIN_LEVEL`]: a black screen is one nobody can //! turn back up. #[cfg(target_arch = "x86_64")] use super::i915::{self, MIN_LEVEL}; use crate::sync::Mutex; #[cfg(target_arch = "x86_64")] use alloc::vec::Vec; #[cfg(target_arch = "x86_64")] use pinnacle_aml::{Path, Value}; enum Control { #[cfg(target_arch = "x86_64")] Native(i915::Backlight), #[cfg(target_arch = "x86_64")] Acpi(Acpi), } /// A Mutex, not a spin lock: ACPI's methods may wait for the embedded /// controller. static CONTROL: Mutex> = Mutex::new(None); /// Finds what sets the brightness, once, after PCI's scan and the /// firmware's AML are loaded (a PC's: an ARM board has neither yet). Says /// on the boot log what it found. #[cfg(target_arch = "x86_64")] pub fn init() { let control = match i915::init() { Some(native) => Some(Control::Native(native)), None => Acpi::find().map(Control::Acpi), }; if let Some(Control::Acpi(acpi)) = &control { crate::println!( "backlight: by ACPI, {} ({} levels up to {})", acpi.device, acpi.levels.len(), acpi.levels.last().copied().unwrap_or(0) ); } if control.is_none() { crate::println!("backlight: none to set"); } *CONTROL.lock() = control; } /// The brightness, 0 to 100; None when the screen's cannot be set. pub fn level() -> Option { match CONTROL.lock().as_mut()? { #[cfg(target_arch = "x86_64")] Control::Native(b) => Some(b.level()), #[cfg(target_arch = "x86_64")] Control::Acpi(a) => Some(a.level()), #[allow(unreachable_patterns)] _ => None, } } /// Sets the brightness to `percent` (at least the least level, at most /// 100) and returns it as it is then; None when it cannot be set. pub fn set(percent: u8) -> Option { let _ = percent; match CONTROL.lock().as_mut()? { #[cfg(target_arch = "x86_64")] Control::Native(b) => Some(b.set(percent)), #[cfg(target_arch = "x86_64")] Control::Acpi(a) => a.set(percent), #[allow(unreachable_patterns)] _ => None, } } /// The integrated panel's output device in ACPI: the levels its `_BCL` /// lists, set by `_BCM`, read by `_BQC` where it has one. #[cfg(target_arch = "x86_64")] struct Acpi { device: Path, /// The levels it takes, ascending, none of them 0. levels: Vec, /// What was last set (a percent), for a device without `_BQC`. last: u8, } #[cfg(target_arch = "x86_64")] impl Acpi { /// The output device with `_BCM` and `_BCL`: the integrated flat /// panel's (its `_ADR`'s display type, bits 8 to 11, 4) if one says so, /// else the first. fn find() -> Option { crate::power::with_namespace(|ns, host| { let has = |ns: &pinnacle_aml::Namespace, d: &Path, m: &[u8; 4]| ns.get(&d.child(*m)).is_some(); let mut found: Vec<(Path, bool)> = Vec::new(); for d in ns.devices() { if !(has(ns, &d, b"_BCM") && has(ns, &d, b"_BCL")) { continue; } let panel = match ns.evaluate_if(&d.child(*b"_ADR"), Vec::new(), host) { Some(Ok(v)) => v.integer().is_some_and(|a| a >> 8 & 0xf == 4), _ => false, }; found.push((d, panel)); } let device = found .iter() .find(|(_, panel)| *panel) .or(found.first()) .map(|(d, _)| d.clone())?; let bcl = ns.evaluate(&device.child(*b"_BCL"), Vec::new(), host).ok()?; let list = bcl.package()?; // The first two are the levels on mains and on battery; the // rest are the levels it takes. let mut levels: Vec = list .iter() .skip(2) .filter_map(Value::integer) .filter(|&l| l > 0) .collect(); levels.sort_unstable(); levels.dedup(); let full = list.first().and_then(Value::integer)?; let max = *levels.last()?; Some(Acpi { device, levels, last: (full.min(max) * 100 / max) as u8, }) })? } fn level(&mut self) -> u8 { let max = self.levels.last().copied().unwrap_or(1).max(1); let path = self.device.child(*b"_BQC"); let now = crate::power::with_namespace(|ns, host| match ns.evaluate_if(&path, Vec::new(), host) { Some(Ok(v)) => v.integer(), _ => None, }) .flatten(); match now { Some(v) => (v.min(max) * 100 / max) as u8, None => self.last, } } /// Sets the level `percent` asks for: the least listed level at or /// above that share of the most, never below [`MIN_LEVEL`]'s. fn set(&mut self, percent: u8) -> Option { let percent = percent.clamp(MIN_LEVEL, 100); let max = *self.levels.last()?; let wanted = (max * u64::from(percent)).div_ceil(100); let level = self.levels.iter().copied().find(|&l| l >= wanted).unwrap_or(max); let path = self.device.child(*b"_BCM"); let done = crate::power::with_namespace(|ns, host| { ns.evaluate(&path, alloc::vec![Value::Integer(level)], host) .is_ok() })?; if !done { return None; } self.last = (level * 100 / max) as u8; Some(self.level()) } }