Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/brightness_backend/brightnessctl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::global_utils::div_round_u32;

use super::{BrightnessBackend, BrightnessBackendConstructor};

const EXPECT_STR: &str = "VirtualDevice didn't test the command during initialization";
Expand Down Expand Up @@ -111,10 +113,16 @@ 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);
self.current = self.max.map(|max| div_round_u32(val * max, 100));
let _: String = self.run(("set", &*format!("{val}%")))?;
Ok(())
}

pub fn get_percent(&mut self) -> u32 {
let curr = self.get_current();
let max = self.get_max();
div_round_u32(curr * 100, max)
}
}

impl BrightnessBackendConstructor for BrightnessCtl {
Expand All @@ -135,20 +143,12 @@ 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 curr = self.device.get_percent();
self.device.set_percent(curr.saturating_sub(by))
}

fn raise(&mut self, by: u32) -> anyhow::Result<()> {
let curr = self.get_current();
let max = self.get_max();

let curr = curr * 100 / max;

let curr = self.device.get_percent();
self.device.set_percent(curr + by)
}

Expand Down
4 changes: 4 additions & 0 deletions src/global_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,7 @@ fn volume_parser(is_sink: bool, value: &str) -> Result<(ArgTypes, Option<String>
}
Ok(v)
}

pub fn div_round_u32(a: u32, b: u32) -> u32 {
(a + b / 2) / b
}
2 changes: 1 addition & 1 deletion src/server/osd_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl SwayosdWindow {
let max = brightness_backend.get_max() as f64;
let progress = self.build_progress_widget(brightness / max);
let label = self.build_text_widget(
Some(&format!("{}%", (brightness / max * 100.) as i32)),
Some(&format!("{}%", (brightness / max * 100.).round() as i32)),
Some(4),
);

Expand Down