Skip to content

Commit 750b2f0

Browse files
Configurable adaptive brightness
1 parent 59ccc41 commit 750b2f0

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

share/tiny-dfr/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ EnablePixelShift = false
2424
# https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
2525
FontTemplate = ":bold"
2626

27+
# Set this to false if you want the brightness of the touchbar
28+
# to be set to a static value instead of following the primary
29+
# screen's brightness
30+
AdaptiveBrightness = true
31+
2732
# This key defines the contents of the primary layer
2833
# (the one with F{number} keys)
2934
# You can change the individual buttons, add, or remove them

src/backlight.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ use anyhow::{Result, anyhow};
88
use input::event::{
99
Event, switch::{Switch, SwitchEvent, SwitchState},
1010
};
11+
use crate::config::Config;
1112
use crate::TIMEOUT_MS;
1213

1314
const MAX_DISPLAY_BRIGHTNESS: u32 = 509;
1415
const MAX_TOUCH_BAR_BRIGHTNESS: u32 = 255;
15-
const LOOKUP_TABLE_SIZE: usize = MAX_DISPLAY_BRIGHTNESS as usize + 1;
1616
const BRIGHTNESS_DIM_TIMEOUT: i32 = TIMEOUT_MS * 3; // should be a multiple of TIMEOUT_MS
1717
const BRIGHTNESS_OFF_TIMEOUT: i32 = TIMEOUT_MS * 6; // should be a multiple of TIMEOUT_MS
18+
const DEFAULT_BRIGHTNESS: u32 = 128;
1819
const DIMMED_BRIGHTNESS: u32 = 1;
1920

2021
fn read_attr(path: &Path, attr: &str) -> u32 {
@@ -96,12 +97,16 @@ impl BacklightManager {
9697
_ => {}
9798
}
9899
}
99-
pub fn update_backlight(&mut self) {
100+
pub fn update_backlight(&mut self, cfg: &Config) {
100101
let since_last_active = (Instant::now() - self.last_active).as_millis() as u64;
101102
let new_bl = if self.lid_state == SwitchState::On {
102103
0
103104
} else if since_last_active < BRIGHTNESS_DIM_TIMEOUT as u64 {
104-
BacklightManager::display_to_touchbar(read_attr(&self.display_bl_path, "brightness"))
105+
if cfg.adaptive_brightness {
106+
BacklightManager::display_to_touchbar(read_attr(&self.display_bl_path, "brightness"))
107+
} else {
108+
DEFAULT_BRIGHTNESS
109+
}
105110
} else if since_last_active < BRIGHTNESS_OFF_TIMEOUT as u64 {
106111
DIMMED_BRIGHTNESS
107112
} else {

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct Config {
1616
pub show_button_outlines: bool,
1717
pub enable_pixel_shift: bool,
1818
pub font_face: FontFace,
19+
pub adaptive_brightness: bool,
1920
}
2021

2122
#[derive(Deserialize)]
@@ -25,6 +26,7 @@ struct ConfigProxy {
2526
show_button_outlines: Option<bool>,
2627
enable_pixel_shift: Option<bool>,
2728
font_template: Option<String>,
29+
adaptive_brightness: Option<bool>,
2830
primary_layer_keys: Option<Vec<ButtonConfig>>,
2931
media_layer_keys: Option<Vec<ButtonConfig>>
3032
}
@@ -59,6 +61,7 @@ fn load_config() -> (Config, [FunctionLayer; 2]) {
5961
base.show_button_outlines = user.show_button_outlines.or(base.show_button_outlines);
6062
base.enable_pixel_shift = user.enable_pixel_shift.or(base.enable_pixel_shift);
6163
base.font_template = user.font_template.or(base.font_template);
64+
base.adaptive_brightness = user.adaptive_brightness.or(base.adaptive_brightness);
6265
base.media_layer_keys = user.media_layer_keys.or(base.media_layer_keys);
6366
base.primary_layer_keys = user.primary_layer_keys.or(base.primary_layer_keys);
6467
};
@@ -68,6 +71,7 @@ fn load_config() -> (Config, [FunctionLayer; 2]) {
6871
let cfg = Config {
6972
show_button_outlines: base.show_button_outlines.unwrap(),
7073
enable_pixel_shift: base.enable_pixel_shift.unwrap(),
74+
adaptive_brightness: base.adaptive_brightness.unwrap(),
7175
font_face: load_font(&base.font_template.unwrap()),
7276
};
7377
(cfg, layers)

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,6 @@ fn real_main(drm: &mut DrmBackend) {
477477
_ => {}
478478
}
479479
}
480-
backlight.update_backlight();
480+
backlight.update_backlight(&cfg);
481481
}
482482
}

0 commit comments

Comments
 (0)