Skip to content

Commit

Permalink
Fix bogus brightness changes when using dbus mode
Browse files Browse the repository at this point in the history
Changing brightness value via DBUS is async and does not instantly result in actually changing the value in the underlying file.
This can result in changing the value via DBUS, and then still reading the unchanged value from the file, and thinking that it's a new user value.
As such, return cached "desired" value until the file was actually written after changing it via DBUS.
  • Loading branch information
maximbaz committed Oct 26, 2024
1 parent 4111eb8 commit 36db642
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/brightness/backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Backlight {
current: Option<u64>,
dbus: Option<Dbus>,
has_write_permission: bool,
pending_dbus_write: bool,
}

impl Backlight {
Expand Down Expand Up @@ -89,6 +90,7 @@ impl Backlight {
current: None,
dbus,
has_write_permission,
pending_dbus_write: false,
})
}
}
Expand All @@ -105,10 +107,11 @@ impl super::Brightness for Backlight {
match (self.inotify.read_events(&mut buffer), self.current) {
(_, None) => update(self),
(Ok(mut events), Some(cached)) => {
if events.next().is_some() {
update(self)
} else {
if self.pending_dbus_write || events.next().is_none() {
self.pending_dbus_write = false;
Ok(cached)
} else {
update(self)
}
}
(Err(err), Some(cached)) if err.kind() == ErrorKind::WouldBlock => Ok(cached),
Expand All @@ -125,6 +128,7 @@ impl super::Brightness for Backlight {
dbus.connection
.send(dbus.message.duplicate()?.append1(value as u32))
.map_err(|_| "Unable to send brightness change message via dbus")?;
self.pending_dbus_write = true;
} else {
Err(std::io::Error::from(ErrorKind::PermissionDenied))?
}
Expand Down

0 comments on commit 36db642

Please sign in to comment.