Enhance kdb_backlight with sleep/restore function

This commit is contained in:
John Mertz 2022-09-02 22:47:44 -06:00
parent 156ff9221d
commit 3d6195bbcb
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
1 changed files with 62 additions and 26 deletions

View File

@ -3,35 +3,71 @@
use strict; use strict;
use warnings; use warnings;
my $maxfile = "/sys/class/leds/tpacpi\:\:kbd_backlight/max_brightness"; our $maxfile = "/sys/class/leds/tpacpi\:\:kbd_backlight/max_brightness";
my $current = "/sys/class/leds/tpacpi\:\:kbd_backlight/brightness"; our $current = "/sys/class/leds/tpacpi\:\:kbd_backlight/brightness";
our $sleepfile = "$ENV{HOME}/.spool/kbd_sleep";
my ($max, $now, $new); sub readFile
if (open(my $m, '<', $maxfile)) { {
$max = readline($m); my $file = shift;
chomp $max; my $ret;
close($m); if (open(my $fh, '<', $file)) {
} else { $ret = readline($fh);
print "Failed to read $maxfile\n"; chomp $ret;
exit; close($fh);
} else {
die "Failed to read $file: $?\n";
}
return $ret;
} }
if (open(my $c, '<', $current)) {
$now = readline($c); sub writeFile
chomp $now; {
close($c); my ($file, $value) = @_;
} else { if (open(my $fh, '>', $file)) {
print "Failed to read $current\n"; print $fh $value;
exit; close($fh);
} else {
die "Failed to write $current\n";
}
} }
if (open(my $fh, '>', $current)) { sub sleepFile
$new = (($now+1) % ($max+1)); {
chomp $new; my $now = readFile($current);
print $fh $new; writeFile($sleepfile,$now);
close($fh); writeFile($current,0);
} else {
print "Failed to write $current\n";
exit;
} }
#print STDERR "$new\n";
sub restoreFile
{
unless (-e $sleepfile) {
die "Missing '$sleepfile'. Must not have slept prior to restore.\n"
}
my $value = readFile($sleepfile);
writeFile($current,$value);
unlink($sleepfile);
}
sub rotateFile
{
my $max = readFile($maxfile);
my $now = readFile($current);
my $new = (($now+1) % ($max+1));
writeFile($current,$new);
}
if (defined($ARGV[0])) {
if ($ARGV[0] eq 'sleep') {
sleepFile();
exit(0);
} elsif ($ARGV[0] eq 'restore') {
restoreFile();
exit(0);
} elsif ($ARGV[0] ne 'rotate') {
die "Invalid mode '".$ARGV[0]."'. 'rotate', 'sleep', or 'restore'\n";
}
}
rotateFile();