Skip to content

Commit

Permalink
Look in gmux_backlight for display backlight
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpenedblade committed Mar 28, 2024
1 parent e8542b0 commit 3edb3ac
Showing 1 changed file with 52 additions and 38 deletions.
90 changes: 52 additions & 38 deletions src/backlight.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::config::Config;
use crate::TIMEOUT_MS;
use anyhow::{anyhow, Result};
use input::event::{
switch::{Switch, SwitchEvent, SwitchState},
Event,
};
use std::{
fs::{File, OpenOptions, self},
path::{PathBuf, Path},
time::Instant,
io::Write,
cmp::min,
fs::{self, File, OpenOptions},
io::Write,
path::{Path, PathBuf},
time::Instant,
};
use anyhow::{Result, anyhow};
use input::event::{
Event, switch::{Switch, SwitchEvent, SwitchState},
};
use crate::config::Config;
use crate::TIMEOUT_MS;

const MAX_DISPLAY_BRIGHTNESS: u32 = 509;
const MAX_TOUCH_BAR_BRIGHTNESS: u32 = 255;
Expand All @@ -32,7 +33,10 @@ fn find_backlight() -> Result<PathBuf> {
let file_name = entry.file_name();
let name = file_name.to_string_lossy();

if ["display-pipe", "appletb_backlight"].iter().any(|s| name.contains(s)) {
if ["display-pipe", "appletb_backlight"]
.iter()
.any(|s| name.contains(s))
{
return Ok(entry.path());
}
}
Expand All @@ -42,7 +46,10 @@ fn find_backlight() -> Result<PathBuf> {
fn find_display_backlight() -> Result<PathBuf> {
for entry in fs::read_dir("/sys/class/backlight/")? {
let entry = entry?;
if ["apple_panel_bl", "intel_backlight"].iter().any(|s| entry.file_name().to_string_lossy().contains(s)) {
if ["apple_panel_bl", "gmux_backlight", "intel_backlight"]
.iter()
.any(|s| entry.file_name().to_string_lossy().contains(s))
{
return Ok(entry.path());
}
}
Expand All @@ -59,21 +66,24 @@ pub struct BacklightManager {
current_bl: u32,
lid_state: SwitchState,
bl_file: File,
display_bl_path: PathBuf
display_bl_path: PathBuf,
}

impl BacklightManager {
pub fn new() -> BacklightManager {
let bl_path = find_backlight().unwrap();
let display_bl_path = find_display_backlight().unwrap();
let bl_file = OpenOptions::new().write(true).open(bl_path.join("brightness")).unwrap();
let bl_file = OpenOptions::new()
.write(true)
.open(bl_path.join("brightness"))
.unwrap();
BacklightManager {
bl_file,
lid_state: SwitchState::Off,
max_bl: read_attr(&bl_path, "max_brightness"),
current_bl: read_attr(&bl_path, "brightness"),
last_active: Instant::now(),
display_bl_path
display_bl_path,
}
}
fn display_to_touchbar(display: u32, active_brightness: u32) -> u32 {
Expand All @@ -86,37 +96,41 @@ impl BacklightManager {
match event {
Event::Keyboard(_) | Event::Pointer(_) | Event::Gesture(_) | Event::Touch(_) => {
self.last_active = Instant::now();
},
Event::Switch(SwitchEvent::Toggle(toggle)) => {
match toggle.switch() {
Some(Switch::Lid) => {
self.lid_state = toggle.switch_state();
println!("Lid Switch event: {:?}", self.lid_state);
if toggle.switch_state() == SwitchState::Off {
self.last_active = Instant::now();
}
}
Event::Switch(SwitchEvent::Toggle(toggle)) => match toggle.switch() {
Some(Switch::Lid) => {
self.lid_state = toggle.switch_state();
println!("Lid Switch event: {:?}", self.lid_state);
if toggle.switch_state() == SwitchState::Off {
self.last_active = Instant::now();
}
_ => {}
}
}
_ => {}
},
_ => {}
}
}
pub fn update_backlight(&mut self, cfg: &Config) {
let since_last_active = (Instant::now() - self.last_active).as_millis() as u64;
let new_bl = min(self.max_bl, if self.lid_state == SwitchState::On {
0
} else if since_last_active < BRIGHTNESS_DIM_TIMEOUT as u64 {
if cfg.adaptive_brightness {
BacklightManager::display_to_touchbar(read_attr(&self.display_bl_path, "brightness"), cfg.active_brightness)
let new_bl = min(
self.max_bl,
if self.lid_state == SwitchState::On {
0
} else if since_last_active < BRIGHTNESS_DIM_TIMEOUT as u64 {
if cfg.adaptive_brightness {
BacklightManager::display_to_touchbar(
read_attr(&self.display_bl_path, "brightness"),
cfg.active_brightness,
)
} else {
cfg.active_brightness
}
} else if since_last_active < BRIGHTNESS_OFF_TIMEOUT as u64 {
DIMMED_BRIGHTNESS
} else {
cfg.active_brightness
}
} else if since_last_active < BRIGHTNESS_OFF_TIMEOUT as u64 {
DIMMED_BRIGHTNESS
} else {
0
});
0
},
);
if self.current_bl != new_bl {
self.current_bl = new_bl;
set_backlight(&self.bl_file, self.current_bl);
Expand Down

0 comments on commit 3edb3ac

Please sign in to comment.