Track backlight brightness better to allow reverting (by screensaver, etc.)

This commit is contained in:
John Mertz 2022-09-17 15:22:18 -04:00
parent b403d8987c
commit 1658999ba1
1 changed files with 29 additions and 23 deletions

View File

@ -66,13 +66,13 @@
# Files containing current and max brightness values # Files containing current and max brightness values
my $cur_file = "/sys/class/backlight/intel_backlight/brightness"; my $cur_file = "/sys/class/backlight/intel_backlight/brightness";
my $max_file = "/sys/class/backlight/intel_backlight/max_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 sub to_percent
{ {
my $value = shift; my $value = shift;
$value = int($value/get_max()*100); return int($value/get_max()*100);
return $value . '%';
} }
sub get_offset sub get_offset
@ -111,6 +111,25 @@ sub writable
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 sub increment
{ {
if (writable()) { if (writable()) {
@ -120,9 +139,6 @@ sub increment
if ($target > $max) { if ($target > $max) {
$target = $max; $target = $max;
} }
open(my $c,'>',"$cur_file");
print $c $target;
close $c;
return $target; return $target;
} }
} }
@ -136,9 +152,7 @@ sub decrement
if ($target < $min) { if ($target < $min) {
$target = $min; $target = $min;
} }
open(my $c,'>',"$cur_file"); set($target);
print $c $target;
close $c;
return $target; return $target;
} }
} }
@ -147,18 +161,15 @@ sub set
{ {
my $value = shift; my $value = shift;
if (writable()) { if (writable()) {
$current = get_current(); my $current = get_current();
logger($current,$value);
if ($value > $current) { if ($value > $current) {
for (my $i=$current;$i<=$value;$i++) { for (my $i=$current;$i<=$value;$i++) {
open(my $c,'>',"$cur_file"); writef($i);
print $c $i;
close $c;
} }
} else { } else {
for (my $i=$current;$i>=$value;$i--) { for (my $i=$current;$i>=$value;$i--) {
open(my $c,'>',"$cur_file"); writef($i);
print $c $i;
close $c;
} }
} }
return $value; return $value;
@ -243,7 +254,7 @@ my $operation_found = 0;
if (scalar @ARGV) { if (scalar @ARGV) {
while ($arg = shift) { while ($arg = shift) {
if ($arg eq '%') { if ($arg eq '%') {
push @output,int(get_current()/get_max()*100); push @output,to_percent(get_current);
} elsif ($arg eq '^') { } elsif ($arg eq '^') {
push @output,get_max(); push @output,get_max();
} elsif ($arg eq '==') { } 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) { if ($silent) {
exit(); exit();
} elsif ($notify) { } elsif ($notify) {