Skip to content

Commit 59ccc41

Browse files
isoamyl50WhatAmISupposedToPutHere
authored andcommitted
Added adaptive brightness functionality
Un-hard-code the lookup table & minor fixes Minor fix: clamp the brightness value down so that it does not exceed max
1 parent c2b3b23 commit 59ccc41

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/backlight.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ use input::event::{
1010
};
1111
use crate::TIMEOUT_MS;
1212

13+
const MAX_DISPLAY_BRIGHTNESS: u32 = 509;
14+
const MAX_TOUCH_BAR_BRIGHTNESS: u32 = 255;
15+
const LOOKUP_TABLE_SIZE: usize = MAX_DISPLAY_BRIGHTNESS as usize + 1;
1316
const BRIGHTNESS_DIM_TIMEOUT: i32 = TIMEOUT_MS * 3; // should be a multiple of TIMEOUT_MS
1417
const BRIGHTNESS_OFF_TIMEOUT: i32 = TIMEOUT_MS * 6; // should be a multiple of TIMEOUT_MS
15-
const DEFAULT_BRIGHTNESS: u32 = 128;
1618
const DIMMED_BRIGHTNESS: u32 = 1;
1719

1820
fn read_attr(path: &Path, attr: &str) -> u32 {
@@ -30,7 +32,17 @@ fn find_backlight() -> Result<PathBuf> {
3032
return Ok(entry.path());
3133
}
3234
}
33-
Err(anyhow!("No backlight device found"))
35+
Err(anyhow!("No Touch Bar backlight device found"))
36+
}
37+
38+
fn find_display_backlight() -> Result<PathBuf> {
39+
for entry in fs::read_dir("/sys/class/backlight/")? {
40+
let entry = entry?;
41+
if entry.file_name().to_string_lossy().eq("apple-panel-bl") {
42+
return Ok(entry.path());
43+
}
44+
}
45+
Err(anyhow!("No Built-in Retina Display backlight device found"))
3446
}
3547

3648
fn set_backlight(mut file: &File, value: u32) {
@@ -41,20 +53,29 @@ pub struct BacklightManager {
4153
last_active: Instant,
4254
current_bl: u32,
4355
lid_state: SwitchState,
44-
bl_file: File
56+
bl_file: File,
57+
display_bl_path: PathBuf
4558
}
4659

4760
impl BacklightManager {
4861
pub fn new() -> BacklightManager {
4962
let bl_path = find_backlight().unwrap();
63+
let display_bl_path = find_display_backlight().unwrap();
5064
let bl_file = OpenOptions::new().write(true).open(bl_path.join("brightness")).unwrap();
5165
BacklightManager {
5266
bl_file,
5367
lid_state: SwitchState::Off,
5468
current_bl: read_attr(&bl_path, "brightness"),
55-
last_active: Instant::now()
69+
last_active: Instant::now(),
70+
display_bl_path
5671
}
5772
}
73+
fn display_to_touchbar(display: u32) -> u32 {
74+
let normalized = display as f64 / MAX_DISPLAY_BRIGHTNESS as f64;
75+
// Add one so that the touch bar does not turn off
76+
let adjusted = (normalized.powf(0.5) * MAX_TOUCH_BAR_BRIGHTNESS as f64) as u32 + 1;
77+
adjusted.min(MAX_TOUCH_BAR_BRIGHTNESS) // Clamp the value to the maximum allowed brightness
78+
}
5879
pub fn process_event(&mut self, event: &Event) {
5980
match event {
6081
Event::Keyboard(_) | Event::Pointer(_) | Event::Gesture(_) | Event::Touch(_) => {
@@ -80,7 +101,7 @@ impl BacklightManager {
80101
let new_bl = if self.lid_state == SwitchState::On {
81102
0
82103
} else if since_last_active < BRIGHTNESS_DIM_TIMEOUT as u64 {
83-
DEFAULT_BRIGHTNESS
104+
BacklightManager::display_to_touchbar(read_attr(&self.display_bl_path, "brightness"))
84105
} else if since_last_active < BRIGHTNESS_OFF_TIMEOUT as u64 {
85106
DIMMED_BRIGHTNESS
86107
} else {

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,4 @@ impl ConfigManager {
124124
pub fn pollfd(&self) -> PollFd {
125125
PollFd::new(&self.inotify_fd, PollFlags::POLLIN)
126126
}
127-
}
127+
}

0 commit comments

Comments
 (0)