Skip to content

Commit

Permalink
added parameter for checking hw brighness state
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Mar 7, 2020
1 parent 4cb0931 commit 398a003
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tp-auto-kbbl"
version = "0.1.2"
version = "0.1.3"
authors = ["Tobias Dühr <[email protected]>"]
edition = "2018"

Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Options:
-h, --help prints this help message
-v, --version prints the version
-n, --no-dim don't dim before bg turns off
-l, --lazy don't check actual hw brightness state
-d, --device specify the device file
-b, --brightness target keyboard brightness (1-2)
-t, --timeout time before the bg light turns off
Expand Down
60 changes: 52 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ fn main() {
time::Duration::from_millis(5000),
);

// Was there an key event?
let mut key_event = false;
// Desired brightness
let mut brightness = 0;
// Current brightness (internal state)
let mut current_brightness = -1;
// timestamp of the last keyboard event
let mut last_event_ts = time::SystemTime::now();
let timeout: u64 = config.timeout as u64;
// tick counter for non-lazy hw state reading
let mut ticks = 0;

loop {
// Wait 100ms in each loop to limit CPU usage
Expand All @@ -81,19 +86,50 @@ fn main() {
for msg in rx.try_iter() {
key_event = msg;
}
debug!("e: {:?}, b: {:?}, t: {:?}", key_event, brightness, timeout);
debug!(
"e: {:?}, b: {:?}, ts: {:?}",
key_event, brightness, last_event_ts
);
if key_event {
brightness = config.brightness;
last_event_ts = time::SystemTime::now();
key_event = false;
} else {
// Elapsed seconds since the last keyboard event
let es = last_event_ts.elapsed().unwrap().as_secs();
if es >= timeout {
if es >= config.timeout {
// Larger than timeout: Lights off
brightness = 0
} else if config.dim && config.brightness > 1 && es >= timeout / 2 {
} else if config.dim
&& config.brightness > 1
&& current_brightness > 1
&& es >= config.timeout / 2
{
// Larger than half of timeout: Dim lights
brightness = 1
}
}
// Check the actual hardware state, if not lazy
if !config.lazy {
// Do this only every second
if ticks >= 10 {
// The actual brightness might differ, e.g. after standby
let actual_brightness = proxy.get_brightness().unwrap();
if actual_brightness != current_brightness {
println!(
"Actual brightness differs: {} != {}",
actual_brightness, current_brightness
);
current_brightness = actual_brightness;
}
// Reset ticks
ticks = 0;
} else {
// Increase ticks
ticks += 1;
}
}
// Set backlight brightness
if brightness != current_brightness {
println!("Setting brightness to {}", brightness);
proxy.set_brightness(brightness).unwrap();
Expand All @@ -106,17 +142,19 @@ fn main() {
struct Config {
device_file: String,
brightness: i32,
timeout: i32,
timeout: u64,
dim: bool,
lazy: bool,
}

impl Config {
fn new(device_file: String, brightness: i32, timeout: i32, dim: bool) -> Self {
fn new(device_file: String, brightness: i32, timeout: u64, dim: bool, lazy: bool) -> Self {
Config {
device_file: device_file,
brightness: brightness,
timeout: timeout,
dim: dim,
lazy: lazy,
}
}
}
Expand All @@ -134,6 +172,7 @@ fn parse_args() -> Config {
opts.optflag("h", "help", "prints this help message");
opts.optflag("v", "version", "prints the version");
opts.optflag("n", "no-dim", "don't dim before bg turns off");
opts.optflag("l", "lazy", "don't check actual hw brightness state");
opts.optopt("d", "device", "specify the device file", "DEVICE");
opts.optopt(
"b",
Expand Down Expand Up @@ -164,6 +203,11 @@ fn parse_args() -> Config {
dim = false;
}

let mut lazy = true;
if matches.opt_present("l") {
lazy = false;
}

let device_file = matches
.opt_str("d")
.unwrap_or("/dev/input/event3".to_string());
Expand All @@ -174,11 +218,11 @@ fn parse_args() -> Config {
.parse()
.unwrap();

let timeout: i32 = matches
let timeout: u64 = matches
.opt_str("t")
.unwrap_or("15".to_string())
.parse()
.unwrap();

Config::new(device_file, brightness, timeout, dim)
Config::new(device_file, brightness, timeout, dim, lazy)
}

0 comments on commit 398a003

Please sign in to comment.