2020-11-16 04:55:20 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2022-09-03 04:47:44 +00:00
|
|
|
our $maxfile = "/sys/class/leds/tpacpi\:\:kbd_backlight/max_brightness";
|
|
|
|
our $current = "/sys/class/leds/tpacpi\:\:kbd_backlight/brightness";
|
|
|
|
our $sleepfile = "$ENV{HOME}/.spool/kbd_sleep";
|
|
|
|
|
|
|
|
sub readFile
|
|
|
|
{
|
|
|
|
my $file = shift;
|
|
|
|
my $ret;
|
|
|
|
if (open(my $fh, '<', $file)) {
|
|
|
|
$ret = readline($fh);
|
|
|
|
chomp $ret;
|
|
|
|
close($fh);
|
|
|
|
} else {
|
|
|
|
die "Failed to read $file: $?\n";
|
|
|
|
}
|
|
|
|
return $ret;
|
2020-11-16 04:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 04:47:44 +00:00
|
|
|
|
|
|
|
sub writeFile
|
|
|
|
{
|
|
|
|
my ($file, $value) = @_;
|
|
|
|
if (open(my $fh, '>', $file)) {
|
|
|
|
print $fh $value;
|
|
|
|
close($fh);
|
|
|
|
} else {
|
|
|
|
die "Failed to write $current\n";
|
|
|
|
}
|
2020-11-16 04:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 04:47:44 +00:00
|
|
|
sub sleepFile
|
|
|
|
{
|
|
|
|
my $now = readFile($current);
|
|
|
|
writeFile($sleepfile,$now);
|
|
|
|
writeFile($current,0);
|
2020-11-16 04:55:20 +00:00
|
|
|
}
|
2022-09-03 04:47:44 +00:00
|
|
|
|
|
|
|
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();
|