Skip to content

Commit e1681a5

Browse files
committed
backlightd will now refresh the list of monitors if setting the brightness for one of them results in a failure
1 parent d461bb1 commit e1681a5

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

backlightd/src/monitors.rs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,58 @@ pub(crate) fn refresh_monitors_list() {
8888
}
8989

9090
pub(crate) fn set_brightness_percent(percent: u8) -> anyhow::Result<()> {
91+
let mut last_error = None;
92+
9193
for monitor in MONITORS.lock().unwrap().iter_mut() {
92-
monitor.set_brightness(percent)?;
94+
let res = monitor.set_brightness(percent);
95+
96+
if let Err(err) = res {
97+
error!("Unable to set brightness of {}", monitor.name());
98+
last_error = Some(err);
99+
}
100+
}
101+
102+
if let Some(err) = last_error {
103+
info!("Trying to refresh monitors list to fix the error...");
104+
refresh_monitors_list();
105+
Err(err)
106+
} else {
107+
info!("Brightness of all monitors has been set to {percent}%");
108+
Ok(())
93109
}
94-
info!("Brightness of all monitors has been set to {percent}%");
95-
Ok(())
96110
}
97111

98112
pub(crate) fn increase_brightness_percent(percent: u8) -> anyhow::Result<()> {
113+
let mut last_error = None;
114+
99115
for monitor in MONITORS.lock().unwrap().iter_mut() {
100116
let mut new_brightness = monitor.get_brightness() + percent;
101117

102118
if new_brightness > 100 {
103119
new_brightness = 100;
104120
}
105121

106-
monitor.set_brightness(new_brightness)?;
122+
let res = monitor.set_brightness(new_brightness);
123+
124+
if let Err(err) = res {
125+
error!("Unable to set brightness of {}", monitor.name());
126+
last_error = Some(err);
127+
}
128+
}
129+
130+
if let Some(err) = last_error {
131+
info!("Trying to refresh monitors list to fix the error...");
132+
refresh_monitors_list();
133+
Err(err)
134+
} else {
135+
info!("Brightness of all monitors has been set to {percent}%");
136+
Ok(())
107137
}
108-
Ok(())
109138
}
110139

111140
pub(crate) fn decrease_brightness_percent(percent: u8) -> anyhow::Result<()> {
141+
let mut last_error = None;
142+
112143
for monitor in MONITORS.lock().unwrap().iter_mut() {
113144
let mut new_brightness = monitor.get_brightness() as i8 - percent as i8;
114145

@@ -117,7 +148,20 @@ pub(crate) fn decrease_brightness_percent(percent: u8) -> anyhow::Result<()> {
117148
new_brightness = 1;
118149
}
119150

120-
monitor.set_brightness(new_brightness as u8)?;
151+
let res = monitor.set_brightness(new_brightness as u8);
152+
153+
if let Err(err) = res {
154+
error!("Unable to set brightness of {}: {err}", monitor.name());
155+
last_error = Some(err);
156+
}
157+
}
158+
159+
if let Some(err) = last_error {
160+
info!("Trying to refresh monitors list to fix the error...");
161+
refresh_monitors_list();
162+
Err(err)
163+
} else {
164+
info!("Brightness of all monitors has been set to {percent}%");
165+
Ok(())
121166
}
122-
Ok(())
123167
}

0 commit comments

Comments
 (0)