scripts/thinkpad/kbd_backlight.pl

74 lines
1.4 KiB
Perl
Raw Normal View History

#!/usr/bin/perl
use strict;
use warnings;
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
{
2023-01-06 19:41:31 +00:00
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;
}
sub writeFile
{
2023-01-06 19:41:31 +00:00
my ($file, $value) = @_;
if (open(my $fh, '>', $file)) {
print $fh $value;
close($fh);
} else {
die "Failed to write $current\n";
}
}
sub sleepFile
{
2023-01-06 19:41:31 +00:00
my $now = readFile($current);
writeFile($sleepfile,$now);
writeFile($current,0);
}
sub restoreFile
{
2023-01-06 19:41:31 +00:00
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
{
2023-01-06 19:41:31 +00:00
my $max = readFile($maxfile);
my $now = readFile($current);
my $new = (($now+1) % ($max+1));
writeFile($current,$new);
}
if (defined($ARGV[0])) {
2023-01-06 19:41:31 +00:00
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();