Permalink
Browse files

large refactor, added --list, added --get, formatted leds, dont requi…

…re root for anything but writing
  • Loading branch information...
teleshoes committed Jan 17, 2012
1 parent 15278b0 commit a4b1a3d757bb2b75a69261c78e05900d306890a3
Showing with 93 additions and 46 deletions.
  1. +93 −46 led-controls/led
View
@@ -4,17 +4,30 @@ use warnings;
my $LED_DIR = "/sys/devices/platform/thinkpad_acpi/leds";
-if(`whoami` ne "root\n"){
- my @cmd = ('sudo', $0, @ARGV);
- exec @cmd;
+sub sudoRexec(){
+ if(`whoami` ne "root\n"){
+ my @cmd = ('sudo', $0, @ARGV);
+ exec @cmd;
+ }
}
-my @validLeds = `ls $LED_DIR`;
-s/^tpacpi(::|:)//g foreach @validLeds;
-chomp foreach @validLeds;
-@validLeds = sort @validLeds;
-my $usage = ''
- . "Usage: $0 <led-name> [<on|off|toggle> | brightness-integer]\n"
+sub getValidLeds();
+sub readDev($);
+sub writeDev($$);
+
+my $usage = "Usage:\n"
+ . " $0 <led-name> --get\n"
+ . " print current value of LED/brightness\n"
+ . "\n"
+ . " $0 <led-name>\n"
+ . " toggle LED/brightness\n"
+ . "\n"
+ . " $0 <led-name> [<on|off|toggle> | brightness-integer]\n"
+ . " set LED/brightness to 1, 0, or a specified integer\n"
+ . "\n"
+ . " $0 --list\n"
+ . " print available LED names:\n" . getValidLeds()
+ . "\n"
. " led-name must be [a-zA-Z0-9:_-]+\n"
. " {only letters, numbers, colons, underscores or hyphens}\n"
. " LED devices are in $LED_DIR/<led-name>/brightness\n"
@@ -27,56 +40,90 @@ my $usage = ''
. " 'mute_mic' => 'tpacpi::mute_mic'\n"
. " 'tpacpi:mute_mic' => 'tpacpi::mute_mic'\n"
. " 'tpacpi::mute_mic' => 'tpacpi::mute_mic'\n"
- . " available LEDs: @validLeds\n"
;
-my $name = shift;
-die $usage if not defined $name;
-
-$name = '' if not defined $name;
-$name =~ s/^tpacpi://;
-$name = ":$name" if $name !~ /:/;
-$name = "tpacpi:$name";
-
-if($name !~ /^([a-zA-Z0-9:_-]+)$/){
- die $usage;
+sub main(@){
+ if(@_ == 1 and $_[0] eq '--list'){
+ print getValidLeds();
+ exit 0;
+ }
+
+ my $name = $_[0];
+ die $usage if not defined $name;
+
+ $name =~ s/^tpacpi://;
+ $name = ":$name" if $name !~ /:/;
+ $name = "tpacpi:$name";
+
+ die $usage if $name !~ /^([a-zA-Z0-9:_-]+)$/;
+
+ my $arg = lc $_[1] if @_ >= 2;
+ $arg = 'toggle' if not defined $arg;
+ if(@_ > 2 or $arg !~ /^(--get|on|off|toggle|\d+)$/i){
+ die $usage;
+ }
+
+ my $dev = "$LED_DIR/$name/brightness";
+
+ if(not -e $dev){
+ die "No led device found at $dev\n";
+ }
+
+ if($arg eq '--get'){
+ my $cur = readDev $dev;
+ chomp $cur;
+ print "LED $name: $cur\n";
+ }else{
+ sudoRexec();
+ my $cur = readDev $dev;
+ chomp $cur;
+ print "LED $name: $cur\n";
+ my $brightness;
+ $brightness = 1 if $arg eq 'on';
+ $brightness = 0 if $arg eq 'off';
+ $brightness = $cur == 0 ? 1 : 0 if lc $arg eq 'toggle';
+ $brightness = $arg if $arg =~ /^\d+$/;
+
+ writeDev $dev, $brightness;
+ print "LED $name: $cur => " . readDev($dev);
+ }
}
-$name = $1; #untaint
-
-my $brightness = shift;
-$brightness = 'toggle' if not defined $brightness;
-if(@ARGV > 0 or $brightness !~ /^(on|off|toggle|\d+)$/i){
- die $usage;
-}
-
-my $dev = "$LED_DIR/$name/brightness";
-if(not -e $dev){
- die "No led device found at $dev\n";
+sub getValidLeds(){
+ my @validLeds = `ls $LED_DIR`;
+ s/^tpacpi(::|:)//g foreach @validLeds;
+ chomp foreach @validLeds;
+ @validLeds = sort @validLeds;
+
+ my $linePrefix = " ";
+ my $devSep = " ";
+
+ my $valid = '';
+ my @acc;
+ for(my $i=0; $i<@validLeds; $i++){
+ if($i == $#validLeds or @acc == 5){
+ $valid .= $linePrefix . (join $devSep, @acc) . "\n";
+ @acc = ();
+ }
+ push @acc, $validLeds[$i];
+ }
+ return $valid;
}
-sub readDev(){
- open FH, "< $dev";
+sub readDev($){
+ my $dev = shift;
+ open FH, "< $dev" or die "Could not read $dev: $!\n";
my @out = <FH>;
close FH;
return join '', @out;
}
-sub writeDev($){
+sub writeDev($$){
+ my $dev = shift;
my $val = shift;
- open FH, "> $dev";
+ open FH, "> $dev" or die "Could not write $dev: $!\n";
print FH "$val\n";
close FH;
}
-my $cur = readDev();
-chomp $cur;
-
-
-$brightness = 1 if lc $brightness eq 'on';
-$brightness = 0 if lc $brightness eq 'off';
-$brightness = $cur == 0 ? 1 : 0 if lc $brightness eq 'toggle';
-
-
-writeDev $brightness;
-print "LED $name $cur => " . readDev();
+&main(@ARGV);

0 comments on commit a4b1a3d

Please sign in to comment.