Enhance kdb_backlight with sleep/restore function
This commit is contained in:
parent
540dc56478
commit
3a3bfcd6a6
|
@ -3,35 +3,71 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $maxfile = "/sys/class/leds/tpacpi\:\:kbd_backlight/max_brightness";
|
||||
my $current = "/sys/class/leds/tpacpi\:\:kbd_backlight/brightness";
|
||||
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";
|
||||
|
||||
my ($max, $now, $new);
|
||||
if (open(my $m, '<', $maxfile)) {
|
||||
$max = readline($m);
|
||||
chomp $max;
|
||||
close($m);
|
||||
} else {
|
||||
print "Failed to read $maxfile\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
if (open(my $c, '<', $current)) {
|
||||
$now = readline($c);
|
||||
chomp $now;
|
||||
close($c);
|
||||
} else {
|
||||
print "Failed to read $current\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
if (open(my $fh, '>', $current)) {
|
||||
$new = (($now+1) % ($max+1));
|
||||
chomp $new;
|
||||
print $fh $new;
|
||||
sub readFile
|
||||
{
|
||||
my $file = shift;
|
||||
my $ret;
|
||||
if (open(my $fh, '<', $file)) {
|
||||
$ret = readline($fh);
|
||||
chomp $ret;
|
||||
close($fh);
|
||||
} else {
|
||||
print "Failed to write $current\n";
|
||||
exit;
|
||||
} else {
|
||||
die "Failed to read $file: $?\n";
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
#print STDERR "$new\n";
|
||||
|
||||
|
||||
sub writeFile
|
||||
{
|
||||
my ($file, $value) = @_;
|
||||
if (open(my $fh, '>', $file)) {
|
||||
print $fh $value;
|
||||
close($fh);
|
||||
} else {
|
||||
die "Failed to write $current\n";
|
||||
}
|
||||
}
|
||||
|
||||
sub sleepFile
|
||||
{
|
||||
my $now = readFile($current);
|
||||
writeFile($sleepfile,$now);
|
||||
writeFile($current,0);
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue