Skip to content

Commit ffd340e

Browse files
authored
Added min_brightness config option (#183)
* Added min_brightness config option * Fixed the min brightness and max volume not being set to new default on start
1 parent f56791c commit ffd340e

File tree

10 files changed

+103
-20
lines changed

10 files changed

+103
-20
lines changed

data/config/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
## The maximum volume that can be reached in %
99
# max_volume = 150
1010

11+
## The minimum brightness that can be reached in %
12+
min_brightness = 5
13+
1114
## show percentage on the right of the OSD
1215
# show_percentage = true
1316

src/argtypes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum ArgTypes {
1111
Player = (i32::MIN + 4) as isize,
1212
MonitorName = (i32::MIN + 5) as isize,
1313
CustomProgressText = (i32::MIN + 6) as isize,
14+
MinBrightness = (i32::MIN + 7) as isize,
1415
// Other
1516
None = 0,
1617
CapsLock = 1,
@@ -56,6 +57,7 @@ impl fmt::Display for ArgTypes {
5657
ArgTypes::MonitorName => "MONITOR-NAME",
5758
ArgTypes::CustomProgress => "CUSTOM-PROGRESS",
5859
ArgTypes::CustomProgressText => "CUSTOM-PROGRESS-TEXT",
60+
ArgTypes::MinBrightness => "MIN-BRIGHTNESS",
5961
};
6062
write!(f, "{}", string)
6163
}
@@ -88,6 +90,7 @@ impl str::FromStr for ArgTypes {
8890
"MONITOR-NAME" => ArgTypes::MonitorName,
8991
"CUSTOM-PROGRESS" => ArgTypes::CustomProgress,
9092
"CUSTOM-PROGRESS-TEXT" => ArgTypes::CustomProgressText,
93+
"MIN-BRIGHTNESS" => ArgTypes::MinBrightness,
9194
other_type => return Err(other_type.to_owned()),
9295
};
9396
Ok(result)

src/brightness_backend/blight.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ impl BrightnessBackend for Blight {
2525
self.device.max()
2626
}
2727

28-
fn lower(&mut self, by: u32) -> anyhow::Result<()> {
29-
let val = self.device.calculate_change(by, Direction::Dec);
28+
fn lower(&mut self, by: u32, min: u32) -> anyhow::Result<()> {
29+
let val = self.device.calculate_change(by, Direction::Dec).max(min);
3030
Ok(self.device.write_value(val)?)
3131
}
3232

33-
fn raise(&mut self, by: u32) -> anyhow::Result<()> {
34-
let val = self.device.calculate_change(by, Direction::Inc);
33+
fn raise(&mut self, by: u32, min: u32) -> anyhow::Result<()> {
34+
let val = self.device.calculate_change(by, Direction::Inc).max(min);
3535
Ok(self.device.write_value(val)?)
3636
}
3737

38-
fn set(&mut self, val: u32) -> anyhow::Result<()> {
38+
fn set(&mut self, val: u32, min: u32) -> anyhow::Result<()> {
39+
let val = val.max(min);
3940
Ok(self.device.write_value(val)?)
4041
}
4142
}

src/brightness_backend/brightnessctl.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,20 @@ impl BrightnessBackend for BrightnessCtl {
142142
self.device.get_max()
143143
}
144144

145-
fn lower(&mut self, by: u32) -> anyhow::Result<()> {
145+
fn lower(&mut self, by: u32, min: u32) -> anyhow::Result<()> {
146146
let curr = self.device.get_percent();
147-
self.device.set_percent(curr.saturating_sub(by))
147+
let val = curr.saturating_sub(by).max(min);
148+
self.device.set_percent(val)
148149
}
149150

150-
fn raise(&mut self, by: u32) -> anyhow::Result<()> {
151+
fn raise(&mut self, by: u32, min: u32) -> anyhow::Result<()> {
151152
let curr = self.device.get_percent();
152-
self.device.set_percent(curr + by)
153+
let val = (curr + by).max(min);
154+
self.device.set_percent(val)
153155
}
154156

155-
fn set(&mut self, val: u32) -> anyhow::Result<()> {
157+
fn set(&mut self, val: u32, min: u32) -> anyhow::Result<()> {
158+
let val = val.max(min);
156159
self.device.set_percent(val)
157160
}
158161
}

src/brightness_backend/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ pub trait BrightnessBackend {
2323
fn get_current(&mut self) -> u32;
2424
fn get_max(&mut self) -> u32;
2525

26-
fn lower(&mut self, by: u32) -> anyhow::Result<()>;
27-
fn raise(&mut self, by: u32) -> anyhow::Result<()>;
28-
fn set(&mut self, val: u32) -> anyhow::Result<()>;
26+
fn lower(&mut self, by: u32, min: u32) -> anyhow::Result<()>;
27+
fn raise(&mut self, by: u32, min: u32) -> anyhow::Result<()>;
28+
fn set(&mut self, val: u32, min: u32) -> anyhow::Result<()>;
2929
}
3030

3131
#[allow(dead_code)]

src/client/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ fn main() -> Result<(), glib::Error> {
171171
"Sets the maximum Volume",
172172
Some("(+)number"),
173173
);
174+
app.add_main_option(
175+
"min-brightness",
176+
glib::Char::from(0),
177+
OptionFlags::NONE,
178+
OptionArg::String,
179+
"Sets the minimum Brightness",
180+
Some("(+)number"),
181+
);
174182
app.add_main_option(
175183
"device",
176184
glib::Char::from(0),

src/config/user.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct ServerConfig {
1717
pub max_volume: Option<u8>,
1818
pub show_percentage: Option<bool>,
1919
pub playerctl_format: Option<String>,
20+
pub min_brightness: Option<u32>,
2021
}
2122

2223
#[derive(Deserialize, Default, Debug)]

src/global_utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ pub(crate) fn handle_application_args(
107107
}
108108
}
109109
}
110+
"min-brightness" => {
111+
let value = child.value().str().unwrap_or("").trim();
112+
match value.parse::<u8>() {
113+
Ok(_) => (ArgTypes::MinBrightness, Some(value.to_string())),
114+
Err(_) => {
115+
eprintln!("{} is not a number between 0 and {}!", value, 100);
116+
return (HandleLocalStatus::FAILURE, actions);
117+
}
118+
}
119+
}
110120
"playerctl" => {
111121
let value = child.value().str().unwrap_or("");
112122
match value {

src/server/application.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ impl SwayOSDApplication {
8484
}
8585
if let Some(max_volume) = server_config.max_volume {
8686
set_default_max_volume(max_volume);
87+
reset_max_volume();
88+
}
89+
if let Some(min_brightness) = server_config.min_brightness {
90+
set_default_min_brightness(min_brightness);
91+
reset_min_brightness();
8792
}
8893
if let Some(show) = server_config.show_percentage {
8994
set_show_percentage(show);
@@ -120,6 +125,13 @@ impl SwayOSDApplication {
120125
set_default_max_volume(max);
121126
}
122127
}
128+
(ArgTypes::MinBrightness, min) => {
129+
let min: Option<u32> = min.and_then(|min| min.parse().ok());
130+
131+
if let Some(min) = min {
132+
set_default_min_brightness(min);
133+
}
134+
}
123135
(arg_type, data) => Self::action_activated(
124136
&osd_app,
125137
server_config_shared.clone(),
@@ -429,6 +441,7 @@ impl SwayOSDApplication {
429441
window.changed_brightness(brightness_backend.as_mut());
430442
}
431443
}
444+
reset_min_brightness();
432445
reset_monitor_name();
433446
}
434447
(ArgTypes::BrightnessLower, step) => {
@@ -439,6 +452,7 @@ impl SwayOSDApplication {
439452
window.changed_brightness(brightness_backend.as_mut());
440453
}
441454
}
455+
reset_min_brightness();
442456
reset_monitor_name();
443457
}
444458
(ArgTypes::BrightnessSet, value) => {
@@ -449,6 +463,7 @@ impl SwayOSDApplication {
449463
window.changed_brightness(brightness_backend.as_mut());
450464
}
451465
}
466+
reset_min_brightness();
452467
reset_monitor_name();
453468
}
454469
(ArgTypes::CapsLock, value) => {
@@ -494,6 +509,16 @@ impl SwayOSDApplication {
494509
};
495510
set_max_volume(volume)
496511
}
512+
(ArgTypes::MinBrightness, min) => {
513+
let brightness: u32 = match min {
514+
Some(min) => match min.parse() {
515+
Ok(min) => min,
516+
_ => get_default_min_brightness(),
517+
},
518+
_ => get_default_min_brightness(),
519+
};
520+
set_min_brightness(brightness)
521+
}
497522
(ArgTypes::Player, name) => set_player(name.unwrap_or("".to_string())),
498523
(ArgTypes::Playerctl, value) => {
499524
let value = &value.unwrap_or("".to_string());

src/server/utils.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ use crate::brightness_backend;
1616
use crate::playerctl::PlayerctlDeviceRaw;
1717

1818
static PRIV_MAX_VOLUME_DEFAULT: u8 = 100_u8;
19+
static PRIV_MIN_BRIGHTNESS_DEFAULT: u32 = 5_u32;
1920

2021
lazy_static! {
2122
static ref MAX_VOLUME_DEFAULT: Mutex<u8> = Mutex::new(PRIV_MAX_VOLUME_DEFAULT);
2223
static ref MAX_VOLUME: Mutex<u8> = Mutex::new(PRIV_MAX_VOLUME_DEFAULT);
24+
static ref MIN_BRIGHTNESS_DEFAULT: Mutex<u32> = Mutex::new(PRIV_MIN_BRIGHTNESS_DEFAULT);
25+
static ref MIN_BRIGHTNESS: Mutex<u32> = Mutex::new(PRIV_MIN_BRIGHTNESS_DEFAULT);
2326
pub static ref DEVICE_NAME_DEFAULT: &'static str = "default";
2427
static ref DEVICE_NAME: Mutex<Option<String>> = Mutex::new(None);
2528
static ref MONITOR_NAME: Mutex<Option<String>> = Mutex::new(None);
@@ -62,6 +65,29 @@ pub fn reset_max_volume() {
6265
*vol = *MAX_VOLUME_DEFAULT.lock().unwrap();
6366
}
6467

68+
pub fn get_default_min_brightness() -> u32 {
69+
*MIN_BRIGHTNESS_DEFAULT.lock().unwrap()
70+
}
71+
72+
pub fn set_default_min_brightness(brightness: u32) {
73+
let mut min = MIN_BRIGHTNESS_DEFAULT.lock().unwrap();
74+
*min = brightness;
75+
}
76+
77+
pub fn get_min_brightness() -> u32 {
78+
*MIN_BRIGHTNESS.lock().unwrap()
79+
}
80+
81+
pub fn set_min_brightness(brightness: u32) {
82+
let mut min = MIN_BRIGHTNESS.lock().unwrap();
83+
*min = brightness;
84+
}
85+
86+
pub fn reset_min_brightness() {
87+
let mut min = MIN_BRIGHTNESS.lock().unwrap();
88+
*min = *MIN_BRIGHTNESS_DEFAULT.lock().unwrap();
89+
}
90+
6591
pub fn get_top_margin() -> f32 {
6692
*TOP_MARGIN.lock().unwrap()
6793
}
@@ -401,19 +427,22 @@ pub fn change_brightness(
401427
change_type: BrightnessChangeType,
402428
step: Option<String>,
403429
) -> brightness_backend::BrightnessBackendResult {
430+
let min_brightness = get_min_brightness();
404431
const BRIGHTNESS_CHANGE_DELTA: u8 = 5;
405432
let value = step.unwrap_or_default().parse::<u8>();
406433

407434
let mut backend = brightness_backend::get_preferred_backend(get_device_name())?;
408435

409436
match change_type {
410-
BrightnessChangeType::Raise => {
411-
backend.raise(value.unwrap_or(BRIGHTNESS_CHANGE_DELTA) as u32)?
412-
}
413-
BrightnessChangeType::Lower => {
414-
backend.lower(value.unwrap_or(BRIGHTNESS_CHANGE_DELTA) as u32)?
415-
}
416-
BrightnessChangeType::Set => backend.set(value.unwrap() as u32)?,
437+
BrightnessChangeType::Raise => backend.raise(
438+
value.unwrap_or(BRIGHTNESS_CHANGE_DELTA) as u32,
439+
min_brightness,
440+
)?,
441+
BrightnessChangeType::Lower => backend.lower(
442+
value.unwrap_or(BRIGHTNESS_CHANGE_DELTA) as u32,
443+
min_brightness,
444+
)?,
445+
BrightnessChangeType::Set => backend.set(value.unwrap() as u32, min_brightness)?,
417446
};
418447

419448
Ok(backend)

0 commit comments

Comments
 (0)