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 the current brightness as a percentage.
29    fn get(&self) -> Result<u32, Error>;
30
31    /// Sets the brightness as a percentage.
32    fn set(&self, percentage: u32) -> Result<(), Error>;
33}
34
35impl Brightness for BrightnessDevice {
36    fn device_name(&self) -> Result<String, Error> {
37        self.0.device_name()
38    }
39
40    fn get(&self) -> Result<u32, Error> {
41        self.0.get()
42    }
43
44    fn set(&self, percentage: u32) -> Result<(), Error> {
45        self.0.set(percentage)
46    }
47}
48
49/// Blocking function that returns all brightness devices on the running system.
50pub fn brightness_devices() -> impl Iterator<Item = Result<BrightnessDevice, Error>> {
51    platform::brightness_devices().map(|r| r.map(BrightnessDevice).map_err(Into::into))
52}