From 161be031a5c0695c823c9f324b74a34e3ef8e30a Mon Sep 17 00:00:00 2001 From: Daniel Hodges Date: Tue, 11 Jul 2023 19:43:29 -0400 Subject: [PATCH] Use sweep_write when changing brightness This change uses https://docs.rs/blight/latest/blight/struct.Device.html#method.sweep_write for adjusting brightness as well as reusing the `Device`. Signed-off-by: Daniel Hodges --- src/utils.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index c8e2198..7dbd7c0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,7 +8,7 @@ use std::{ sync::Mutex, }; -use blight::{change_bl, err::BlibError, Change, Device, Direction}; +use blight::{err::BlibError, Delay, Device, Direction}; use pulse::volume::Volume; use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController}; @@ -265,25 +265,20 @@ pub fn change_brightness( .unwrap_or(String::new()) .parse::() .unwrap_or(BRIGHTNESS_CHANGE_DELTA) as u16; - let direction = match change_type { - BrightnessChangeType::Raise => Direction::Inc, - BrightnessChangeType::Lower => { - let device = Device::new(None)?; - let change = device.calculate_change(brightness_delta, Direction::Dec) as f64; - let max = device.max() as f64; - // Limits the lowest brightness to 5% - if change / max < (brightness_delta as f64) * 0.01 { - return Ok(Some(device)); - } - Direction::Dec - } + let mut device = Device::new(None)?; + let change = match change_type { + BrightnessChangeType::Raise => device.calculate_change(brightness_delta, Direction::Inc), + BrightnessChangeType::Lower => device.calculate_change(brightness_delta, Direction::Dec), }; - match change_bl(brightness_delta, Change::Regular, direction, None) { + match device.sweep_write(change, Delay::default()) { Err(e) => { eprintln!("Brightness Error: {}", e); Err(e) } - _ => Ok(Some(Device::new(None)?)), + _ => { + device.reload(); + Ok(Some(device)) + } } }