Skip to main content

cadmus_core/frontlight/
standard.rs

1use super::{Frontlight, LightLevel, LightLevels};
2use anyhow::Error;
3use nix::ioctl_write_int_bad;
4use std::fs::File;
5use std::fs::OpenOptions;
6use std::os::unix::io::AsRawFd;
7
8ioctl_write_int_bad!(write_frontlight_intensity, 241);
9const FRONTLIGHT_INTERFACE: &str = "/dev/ntx_io";
10
11pub struct StandardFrontlight {
12    value: LightLevel,
13    interface: File,
14}
15
16impl StandardFrontlight {
17    pub fn new(value: LightLevel) -> Result<StandardFrontlight, Error> {
18        let interface = OpenOptions::new().write(true).open(FRONTLIGHT_INTERFACE)?;
19        Ok(StandardFrontlight { value, interface })
20    }
21}
22
23impl Frontlight for StandardFrontlight {
24    /// # SAFETY
25    /// `self.interface` is an open `/dev/ntx_io` handle owned by this
26    /// `StandardFrontlight`, so `as_raw_fd()` yields a valid descriptor for the
27    /// duration of the call. The ioctl request code and integer payload match the
28    /// kernel frontlight brightness interface expected by `write_frontlight_intensity`.
29    fn set_intensity(&mut self, value: LightLevel) -> Result<(), Error> {
30        unsafe {
31            write_frontlight_intensity(self.interface.as_raw_fd(), libc::c_int::from(value))
32        }?;
33        self.value = value;
34        Ok(())
35    }
36
37    fn set_warmth(&mut self, _value: LightLevel) -> Result<(), Error> {
38        Ok(())
39    }
40
41    fn levels(&self) -> LightLevels {
42        LightLevels {
43            intensity: self.value,
44            warmth: LightLevel::off(),
45        }
46    }
47}