1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Copyright 2021 David Karrick

Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
<LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
option. This file may not be copied, modified, or distributed
except according to those terms.
*/

use std::{fmt, fs, path::Path};

#[cfg(not(feature = "dbus"))]
use std::{fs::OpenOptions, io::prelude::*};

use super::BACKLIGHT_DIR;
use crate::{
    error::Error,
    utils::{read_sys_backlight, SysBacklightInterface},
};

#[cfg(feature = "dbus")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "dbus")]
use zbus::Connection;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "dbus", derive(Serialize, Deserialize))]
/// Monitor Device information.
///
/// Devices are extracted from the `/sys/class/backlight/` directory.
pub struct MonitorDevice {
    /// Value taken from `/sys/class/backlight/<backlight>`.
    ///
    /// Name of the backlight device
    pub device: String,
    /// Value taken from
    /// [`/sys/class/backlight/<backlight>/bl_power`](https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-class-backlight-backlight-bl-power).
    ///
    /// Controls the power of `<backlight>`.
    pub bl_power: u32,
    /// Value taken from
    /// [`/sys/class/backlight/<backlight>/brightness`](https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-class-backlight-backlight-brightness).
    ///
    /// Control the brightness for this `<backlight>`. Values
    /// are between 0 and [max_brightness](struct.MonitorDevice.html#structfield.max_brightness).
    /// This file will also show the brightness level stored in the driver, which
    /// may not be the actual brightness (see [actual_brightness](struct.MonitorDevice.html#structfield.actual_brightness)).
    pub brightness: u32,
    /// Value taken from
    /// [`/sys/class/backlight/<backlight>/actual_brightness`](https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-class-backlight-backlight-actual-brightness).
    ///
    /// Show the actual brightness by querying the hardware.
    pub actual_brightness: u32,
    /// Value taken from
    /// [`/sys/class/backlight/<backlight>/max_brightness`](https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-class-backlight-backlight-max-brightness).
    ///
    /// Maximum brightness for `<backlight>`.
    pub max_brightness: u32,
    /// Value taken from
    /// [`/sys/class/backlight/<backlight>/type`](https://www.kernel.org/doc/html/latest/admin-guide/abi-stable.html#abi-sys-class-backlight-backlight-type).
    ///
    /// The type of interface controlled by `<backlight>`.
    pub bl_type: BackLightType,
}

/// The type of interface controlled by [`<backlight>`](struct.MonitorDevice.html).
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "dbus", derive(Serialize, Deserialize))]
pub enum BackLightType {
    /// The driver uses a standard firmware interface
    FirmWare,
    /// The driver uses a platform-specific interface
    PlatForm,
    /// The driver controls hardware registers directly
    Raw,
}

impl From<&BackLightType> for &str {
    fn from(val: &BackLightType) -> &'static str {
        match val {
            BackLightType::FirmWare => "Firmware",
            BackLightType::PlatForm => "Platform",
            BackLightType::Raw => "Raw",
        }
    }
}

impl From<BackLightType> for &str {
    fn from(val: BackLightType) -> &'static str {
        match val {
            BackLightType::FirmWare => "Firmware",
            BackLightType::PlatForm => "Platform",
            BackLightType::Raw => "Raw",
        }
    }
}

impl From<&BackLightType> for String {
    fn from(val: &BackLightType) -> String {
        match val {
            BackLightType::FirmWare => String::from("Firmware"),
            BackLightType::PlatForm => String::from("Platform"),
            BackLightType::Raw => String::from("Raw"),
        }
    }
}

impl From<BackLightType> for String {
    fn from(val: BackLightType) -> String {
        match val {
            BackLightType::FirmWare => String::from("Firmware"),
            BackLightType::PlatForm => String::from("Platform"),
            BackLightType::Raw => String::from("Raw"),
        }
    }
}

impl fmt::Display for BackLightType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            BackLightType::FirmWare => write!(f, "Firmware"),
            BackLightType::PlatForm => write!(f, "Platform"),
            BackLightType::Raw => write!(f, "Raw"),
        }
    }
}

impl MonitorDevice {
    /// Get monitor by device name.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let device_name = format!("amdgpu_bl0");
    /// let monitor = MonitorDevice::get_monitor_device(device_name.clone()).unwrap();
    /// assert_eq!(monitor.get_device_name(), device_name);
    /// ```
    pub fn get_monitor_device(device: String) -> Result<MonitorDevice, Error> {
        if Path::new(format!("{}/{}", BACKLIGHT_DIR, &device).as_str()).is_dir() {
            let bl_power =
                read_sys_backlight(&device, SysBacklightInterface::Power)?.parse::<u32>()?;
            let brightness =
                read_sys_backlight(&device, SysBacklightInterface::Brightness)?.parse::<u32>()?;
            let actual_brightness =
                read_sys_backlight(&device, SysBacklightInterface::ActualBrightness)?
                    .parse::<u32>()?;
            let max_brightness = read_sys_backlight(&device, SysBacklightInterface::MaxBrightness)?
                .parse::<u32>()?;
            let bl_type = match read_sys_backlight(&device, SysBacklightInterface::Type)?.as_str() {
                "firmware" => BackLightType::FirmWare,
                "platform" => BackLightType::PlatForm,
                "raw" => BackLightType::Raw,
                _ => unreachable!(),
            };

            Ok(MonitorDevice {
                device,
                bl_power,
                brightness,
                actual_brightness,
                max_brightness,
                bl_type,
            })
        } else {
            Err(Error::InvalidDeviceName { device })
        }
    }

    /// Get all monitor devices.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     println!("Monitor: {:?}", monitor);
    /// }
    /// ```
    pub fn get_all_monitor_devices() -> Result<Vec<MonitorDevice>, Error> {
        let mut monitors = Vec::with_capacity(1);

        if Path::new(BACKLIGHT_DIR).is_dir() {
            for device in fs::read_dir(BACKLIGHT_DIR)? {
                let device = device?;
                let device_name = device.file_name().into_string().unwrap();

                match MonitorDevice::get_monitor_device(device_name) {
                    Ok(dev) => monitors.push(dev),
                    Err(e) => return Err(e),
                }
            }
        }

        Ok(monitors)
    }

    /// Get device name of monitor.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     let max_brightness = monitor.get_max_brightness();
    ///     let device = monitor.get_device_name();
    ///     println!("Device: {}", device);
    ///     assert!(!device.is_empty())
    /// }
    /// ```
    pub fn get_device_name(&self) -> &str {
        &self.device
    }

    /// Get power of monitor.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     let power = monitor.get_power();
    ///     println!("Power: {}", power);
    /// }
    /// ```
    pub fn get_power(&self) -> u32 {
        self.bl_power
    }

    /// Get brightness of monitor.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     let max_brightness = monitor.get_max_brightness();
    ///     let brightness = monitor.get_brightness();
    ///     println!("Brightness: {}", brightness);
    ///     assert!(brightness >= 0 && brightness <= max_brightness)
    /// }
    /// ```
    pub fn get_brightness(&self) -> u32 {
        self.brightness
    }

    /// Get actual brightness of monitor
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     let max_brightness = monitor.get_max_brightness();
    ///     let brightness = monitor.get_actual_brightness();
    ///     println!("Brightness: {}", brightness);
    ///     assert!(brightness >= 0 && brightness <= max_brightness)
    /// }
    /// ```
    pub fn get_actual_brightness(&self) -> u32 {
        self.actual_brightness
    }

    /// Get the maximum brightness value of monitor.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     let max_brightness = monitor.get_max_brightness();
    ///     println!("Max Brightness: {}", max_brightness);
    ///     assert!(max_brightness > 0)
    /// }
    /// ```
    pub fn get_max_brightness(&self) -> u32 {
        self.max_brightness
    }

    /// Get the backlight type of monitor.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// for monitor in monitors {
    ///     let mon_type = monitor.get_type();
    ///     println!("Type: {}", mon_type);
    /// }
    /// ```
    pub fn get_type(&self) -> BackLightType {
        self.bl_type
    }

    /// Set brightness of monitor.
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// monitors[0].set_brightness(20);
    /// ```
    #[cfg(feature = "dbus")]
    pub fn set_brightness(&self, level: u32) -> Result<(), Error> {
        if level <= self.max_brightness {
            let sd_bus = Connection::new_system().unwrap();
            match sd_bus.call_method(
                Some("org.freedesktop.login1"),
                "/org/freedesktop/login1/session/auto",
                Some("org.freedesktop.login1.Session"),
                "SetBrightness",
                &("backlight", &self.device, level),
            ) {
                Ok(_) => Ok(()),
                Err(e) => Err(Error::SetBrightnessDBusError(e)),
            }
        } else {
            Err(Error::InvalidBrightnessLevel {
                given: level,
                max: self.max_brightness,
            })
        }
    }

    /// Set brightness of monitor.
    ///
    /// ### NOTE
    ///
    /// This method writes to `/sys/class/backlight/<backlight>/brightness`
    /// and will fail if user is not root (even when executed with sudo).
    /// It is recommended to create a udev rule to allow user of a certain
    /// group to write to the file. The example below will allow all users in
    /// the `video` group to change the brightness of all devices in `/sys/class/backlight/`.
    ///
    /// ```ignore
    /// ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness"
    /// ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness"
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use bulbb::monitor::MonitorDevice;
    ///
    /// let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
    /// monitors[0].set_brightness(20);
    /// ```
    #[cfg(not(feature = "dbus"))]
    pub fn set_brightness(&self, level: u32) -> Result<(), Error> {
        if level <= self.max_brightness {
            // write to /sys/class/backlight/<backlight>/brightness
            let mut brightness = OpenOptions::new()
                .write(true)
                .open(format!("{}/{}/brightness", BACKLIGHT_DIR, &self.device))?;
            match brightness.write_all(level.to_string().as_bytes()) {
                Ok(_) => Ok(()),
                Err(e) => Err(Error::Io(e)),
            }
        } else {
            Err(Error::InvalidBrightnessLevel {
                given: level,
                max: self.max_brightness,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::utils::format_monitor_device;

    #[test]
    fn get_all_monitor_devices() {
        let monitors = MonitorDevice::get_all_monitor_devices().unwrap();
        for monitor in monitors {
            format_monitor_device(monitor)
        }
    }

    #[test]
    fn set_monitor() {
        let mut monitors = MonitorDevice::get_all_monitor_devices().unwrap();
        if !monitors.is_empty() {
            let first_device = monitors.remove(0);
            let device_name = first_device.get_device_name();
            let starting_brightness = first_device.get_actual_brightness();
            let max_brightness = first_device.get_max_brightness();
            let new_brightness: u32 = if starting_brightness > (max_brightness / 2) {
                0
            } else {
                max_brightness
            };

            let _set_brightness_1 =
                MonitorDevice::set_brightness(&first_device.clone(), new_brightness);

            let udpated_device =
                MonitorDevice::get_monitor_device(String::from(device_name)).unwrap();
            let updated_brightness = udpated_device.get_actual_brightness();

            assert_ne!(starting_brightness, updated_brightness);
            assert_eq!(updated_brightness, new_brightness);

            let _set_brightness_2 =
                MonitorDevice::set_brightness(&udpated_device, starting_brightness);

            let final_device =
                MonitorDevice::get_monitor_device(String::from(device_name)).unwrap();
            let final_brightness = final_device.get_actual_brightness();

            assert_ne!(final_brightness, updated_brightness);
            assert_eq!(final_brightness, starting_brightness);
        }
    }
}