-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Brightness control #4
Conversation
Sorry for the delay. I'll look at this this afternoon. If I forget, ping me! :) |
Doesn't seem to work on my system. It failed to write to the brightness file. In the blight README, it states that the user needs to be in the video group and some udev rules may be needed. https://github.com/VoltaireNoir/blight/blob/main/src/utils/setup.rs |
src/application.rs
Outdated
(OsdTypes::BrightnessRaise, _) => { | ||
change_brightness(BrightnessChangeType::Raise); | ||
for window in self.windows.borrow().to_owned() { | ||
window.changed_brightness(); | ||
} | ||
} | ||
(OsdTypes::BrightnessLower, _) => { | ||
change_brightness(BrightnessChangeType::Lower); | ||
for window in self.windows.borrow().to_owned() { | ||
window.changed_brightness(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format with rustfmt
src/osd_window.rs
Outdated
pub fn changed_brightness(&self) { | ||
self.clear_osd(); | ||
|
||
let bl = Device::new(None).unwrap(); | ||
let brightness = bl.current() as f64; | ||
|
||
// Using the icon from Adwaita for now? | ||
let icon_name = "display-brightness-symbolic"; | ||
|
||
let icon = self.build_icon_widget(&icon_name); | ||
let progress = self.build_progress_widget(brightness / 255.0); | ||
|
||
self.container.add(&icon); | ||
self.container.add(&progress.bar); | ||
|
||
self.run_timeout(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format with rustfmt
src/utils.rs
Outdated
pub fn change_brightness(change_type: BrightnessChangeType) { | ||
|
||
const BRIGHTNESS_CHANGE_DELTA: u16 = 5; | ||
match change_type { | ||
BrightnessChangeType::Raise => { | ||
match change_bl(BRIGHTNESS_CHANGE_DELTA, Change::Regular, Direction::Inc, None) { | ||
Err(e) => eprintln!("Brightness Error: {}", e), | ||
_ => () | ||
} | ||
} | ||
BrightnessChangeType::Lower => { | ||
match change_bl(BRIGHTNESS_CHANGE_DELTA, Change::Regular, Direction::Dec, None) { | ||
Err(e) => eprintln!("Brightness Error: {}", e), | ||
_ => () | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format with rustfmt
Here's a patch with a few fixes: Adds lower brightness limit and fixes progress bar not moving for some devices :) diff --git a/src/application.rs b/src/application.rs
index 3f91cf8..9f301cd 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -122,7 +122,7 @@ impl SwayOSDApplication {
"Shows brightness osd and raises or loweres all available sources of brightness device",
Some("raise|lower"),
);
-
+
// Parse args
app.connect_handle_local_options(|app, args| -> i32 {
let variant = args.to_variant();
@@ -303,16 +303,18 @@ impl SwayOSDApplication {
}
}
(OsdTypes::BrightnessRaise, _) => {
- change_brightness(BrightnessChangeType::Raise);
- for window in self.windows.borrow().to_owned() {
- window.changed_brightness();
- }
+ if let Ok(Some(device)) = change_brightness(BrightnessChangeType::Raise) {
+ for window in self.windows.borrow().to_owned() {
+ window.changed_brightness(&device);
+ }
+ }
}
(OsdTypes::BrightnessLower, _) => {
- change_brightness(BrightnessChangeType::Lower);
- for window in self.windows.borrow().to_owned() {
- window.changed_brightness();
- }
+ if let Ok(Some(device)) = change_brightness(BrightnessChangeType::Lower) {
+ for window in self.windows.borrow().to_owned() {
+ window.changed_brightness(&device);
+ }
+ }
}
(OsdTypes::CapsLock, led) => {
let state = get_caps_lock_state(led);
diff --git a/src/osd_window.rs b/src/osd_window.rs
index 6e5aa0e..11e613a 100644
--- a/src/osd_window.rs
+++ b/src/osd_window.rs
@@ -156,17 +156,16 @@ impl SwayosdWindow {
self.run_timeout();
}
- pub fn changed_brightness(&self) {
+ pub fn changed_brightness(&self, device: &Device) {
self.clear_osd();
- let bl = Device::new(None).unwrap();
- let brightness = bl.current() as f64;
-
- // Using the icon from Adwaita for now?
- let icon_name = "display-brightness-symbolic";
-
+ // Using the icon from Adwaita for now?
+ let icon_name = "display-brightness-symbolic";
let icon = self.build_icon_widget(&icon_name);
- let progress = self.build_progress_widget(brightness / 255.0);
+
+ let brightness = device.current() as f64;
+ let max = device.max() as f64;
+ let progress = self.build_progress_widget(brightness / max);
self.container.add(&icon);
self.container.add(&progress.bar);
diff --git a/src/utils.rs b/src/utils.rs
index 7184082..7f6bd1b 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -5,9 +5,9 @@ use std::{
io::{prelude::*, BufReader},
};
+use blight::{change_bl, err::BlibError, Change, Device, Direction};
use pulse::volume::Volume;
use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController};
-use blight:: { change_bl, Change, Direction };
pub fn get_caps_lock_state(led: Option<String>) -> bool {
const BASE_PATH: &str = "/sys/class/leds";
@@ -163,22 +163,27 @@ pub fn change_source_volume(change_type: VolumeChangeType) -> Option<DeviceInfo>
}
}
-pub fn change_brightness(change_type: BrightnessChangeType) {
-
+pub fn change_brightness(change_type: BrightnessChangeType) -> Result<Option<Device>, BlibError> {
const BRIGHTNESS_CHANGE_DELTA: u16 = 5;
- match change_type {
- BrightnessChangeType::Raise => {
- match change_bl(BRIGHTNESS_CHANGE_DELTA, Change::Regular, Direction::Inc, None) {
- Err(e) => eprintln!("Brightness Error: {}", e),
- _ => ()
- }
- }
- BrightnessChangeType::Lower => {
- match change_bl(BRIGHTNESS_CHANGE_DELTA, Change::Regular, Direction::Dec, None) {
- Err(e) => eprintln!("Brightness Error: {}", e),
- _ => ()
+ let direction = match change_type {
+ BrightnessChangeType::Raise => Direction::Inc,
+ BrightnessChangeType::Lower => {
+ let device = Device::new(None)?;
+ let change = device.calculate_change(BRIGHTNESS_CHANGE_DELTA, Direction::Dec) as f64;
+ let max = device.max() as f64;
+ // Limits the lowest brightness to 5%
+ if change / max < (BRIGHTNESS_CHANGE_DELTA as f64) * 0.01 {
+ return Ok(Some(device));
}
+ Direction::Dec
+ }
+ };
+ match change_bl(BRIGHTNESS_CHANGE_DELTA, Change::Regular, direction, None) {
+ Err(e) => {
+ eprintln!("Brightness Error: {}", e);
+ Err(e)
}
+ _ => Ok(Some(Device::new(None)?)),
}
} |
Then you'll also need to update the README with additional instructions for setting up the permissions. I just needed to add my user to the
|
I've modified the files and the methods. |
README.md
Outdated
Some devices may not have permission to write `/sys/class/backlight/*/brightness`. | ||
Workaround will be adding a rule inside `udev`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also mention the command to add the user to video group :)
GTK requires the adwaita icons to be installed so that icon should work :) Too bad that they don't provide more brightness icons... |
Sorry, forgot to commit the change. Some of the themes have multiple icons available, probably could replace it with Font Emoji or something else? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Appreciate the help! :)
I'd prefer using icons. The one used right now works good enough :) |
This is my first time working with rust, so if anything can be improved, please tell me.
I add a blight package to let users control the backlight.
It's tested on my laptop with multiple external screens running and worked flawlessly.
Sadly, I don't have an external monitor that can be adjusted the backlight from the drivers.
The brightness icon heavily depends on the theme files. For now, I'm using Adwaita's
display-brightness-symbolic
icon.if desire to have different icon between different level of the brightness,
perhaps have to do something like this
What changed
General
blight
package.Package
blight = "0.4.1"
Issue related
#1