diff --git a/src/brightness_backend/brightnessctl.rs b/src/brightness_backend/brightnessctl.rs index ccb0e11..740fdb6 100644 --- a/src/brightness_backend/brightnessctl.rs +++ b/src/brightness_backend/brightnessctl.rs @@ -107,10 +107,21 @@ impl VirtualDevice { } } - fn set_percent(&mut self, mut val: u32) -> anyhow::Result<()> { - val = val.clamp(0, 100); - self.current = self.max.map(|max| val * max / 100); - let _: String = self.run(("set", &*format!("{val}%")))?; + fn val_to_percent(&mut self, val: u32) -> u32 { + return ((val as f64 / self.get_max() as f64).powf(0.25) * 100_f64).round() as u32; + } + fn percent_to_val(&mut self, perc: u32) -> u32 { + return ((perc as f64 / 100_f64).powf(4_f64) * self.get_max() as f64).round() as u32; + } + + fn set_percent(&mut self, val: u32) -> anyhow::Result<()> { + let new = self.percent_to_val(val); + self.set_val(new) + } + fn set_val(&mut self, val: u32) -> anyhow::Result<()> { + let curr = val.clamp(0, self.get_max()); + self.current = Some(curr); + let _: String = self.run(("set", &*format!("{curr}")))?; Ok(()) } } @@ -134,20 +145,18 @@ impl BrightnessBackend for BrightnessCtl { fn lower(&mut self, by: u32) -> anyhow::Result<()> { let curr = self.get_current(); - let max = self.get_max(); - - let curr = curr * 100 / max; + let mut new = self.device.val_to_percent(curr).saturating_sub(by); + new = self.device.percent_to_val(new).min(curr.saturating_sub(1)); - self.device.set_percent(curr.saturating_sub(by)) + self.device.set_val(new) } fn raise(&mut self, by: u32) -> anyhow::Result<()> { let curr = self.get_current(); - let max = self.get_max(); - - let curr = curr * 100 / max; + let mut new = self.device.val_to_percent(curr) + by; + new = self.device.percent_to_val(new).max(curr + 1); - self.device.set_percent(curr + by) + self.device.set_val(new) } fn set(&mut self, val: u32) -> anyhow::Result<()> {