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
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) {