From 1658999ba132fdce1d6e72b6ffdf7513b9c33eee Mon Sep 17 00:00:00 2001 From: John Mertz Date: Sat, 17 Sep 2022 15:22:18 -0400 Subject: [PATCH] Track backlight brightness better to allow reverting (by screensaver, etc.) --- thinkpad/blc.pl | 52 +++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/thinkpad/blc.pl b/thinkpad/blc.pl index d9ba621..d0fb906 100755 --- a/thinkpad/blc.pl +++ b/thinkpad/blc.pl @@ -66,13 +66,13 @@ # Files containing current and max brightness values my $cur_file = "/sys/class/backlight/intel_backlight/brightness"; my $max_file = "/sys/class/backlight/intel_backlight/max_brightness"; -my $last_file = "/home/jpm/.config/blc.last"; +my $cur_log = $ENV{HOME}."/.spool/blc.current"; +my $last_log = $ENV{HOME}."/.spool/blc.last"; sub to_percent { my $value = shift; - $value = int($value/get_max()*100); - return $value . '%'; + return int($value/get_max()*100); } sub get_offset @@ -108,7 +108,26 @@ sub writable if (! -w $cur_file) { die "You don't have permission to write $cur_file\n"; } - return 1; + return 1; +} + +sub logger +{ + my ($current, $target) = @_; + open(my $fh,'>',$last_log); + print $fh to_percent($current); + close($fh); + open($fh,'>',$cur_log); + print $fh to_percent($target); + close($fh); +} + +sub writef +{ + my $target = shift; + open(my $fh,'>',$cur_file); + print $fh $target; + close($fh); } sub increment @@ -120,9 +139,6 @@ sub increment if ($target > $max) { $target = $max; } - open(my $c,'>',"$cur_file"); - print $c $target; - close $c; return $target; } } @@ -136,9 +152,7 @@ sub decrement if ($target < $min) { $target = $min; } - open(my $c,'>',"$cur_file"); - print $c $target; - close $c; + set($target); return $target; } } @@ -147,18 +161,15 @@ sub set { my $value = shift; if (writable()) { - $current = get_current(); + my $current = get_current(); + logger($current,$value); if ($value > $current) { for (my $i=$current;$i<=$value;$i++) { - open(my $c,'>',"$cur_file"); - print $c $i; - close $c; + writef($i); } } else { for (my $i=$current;$i>=$value;$i--) { - open(my $c,'>',"$cur_file"); - print $c $i; - close $c; + writef($i); } } return $value; @@ -243,7 +254,7 @@ my $operation_found = 0; if (scalar @ARGV) { while ($arg = shift) { if ($arg eq '%') { - push @output,int(get_current()/get_max()*100); + push @output,to_percent(get_current); } elsif ($arg eq '^') { push @output,get_max(); } elsif ($arg eq '==') { @@ -341,11 +352,6 @@ if (scalar @ARGV) { ); } -open(my $fh,'>',$last_file); -print $fh get_current(); -close($fh); - -print $_."\n" foreach(@output); if ($silent) { exit(); } elsif ($notify) {