brightness/
blocking.rs

1// Copyright (C) 2022 The brightness project authors. Distributed under the 0BSD license.
2
3//! The blocking API.
4
5use crate::Error;
6
7cfg_if::cfg_if! {
8    if #[cfg(target_os = "linux")] {
9        pub(crate) mod linux;
10        use self::linux as platform;
11    } else if #[cfg(windows)] {
12        pub mod windows;
13        use self::windows as platform;
14    } else {
15        compile_error!("unsupported platform");
16    }
17}
18
19/// Blocking brightness device.
20#[derive(Debug)]
21pub struct BrightnessDevice(platform::BlockingDeviceImpl);
22
23/// Blocking interface to get and set brightness.
24pub trait Brightness {
25    /// Returns the device name.
26    fn device_name(&self) -> Result<String, Error>;
27
28    /// Returns a human-readable device name suitable for display to users.
29    /// This may differ from the technical device identifier returned by `device_name()`.
30    fn friendly_device_name(&self) -> Result<String, Error>;
31
32    /// Returns the current brightness as a percentage.
33    fn get(&self) -> Result<u32, Error>;
34
35    /// Sets the brightness as a percentage.
36    fn set(&self, percentage: u32) -> Result<(), Error>;
37}
38
39impl Brightness for BrightnessDevice {
40    fn device_name(&self) -> Result<String, Error> {
41        self.0.device_name()
42    }
43
44    fn friendly_device_name(&self) -> Result<String, Error> {
45        self.0.friendly_device_name()
46    }
47
48    fn get(&self) -> Result<u32, Error> {
49        self.0.get()
50    }
51
52    fn set(&self, percentage: u32) -> Result<(), Error> {
53        self.0.set(percentage)
54    }
55}
56
57/// Blocking function that returns all brightness devices on the running system.
58pub fn brightness_devices() -> impl Iterator<Item = Result<BrightnessDevice, Error>> {
59    platform::brightness_devices().map(|r| r.map(BrightnessDevice).map_err(Into::into))
60}