diff --git a/thinkpad/blc.pl b/thinkpad/blc.pl index 1183039..07f912b 100755 --- a/thinkpad/blc.pl +++ b/thinkpad/blc.pl @@ -1,5 +1,68 @@ #!/usr/bin/perl -w +# TODO: I reworked the argument parsing and broke the formatted output +# It was terrible in the first place, so rework that too. +# +# Work on a `date`-like format like: +# "+%c/%m = %p\%" +# %c - Current absolute +# %m - Maximum absolute value +# %p - Current percent +# +# to output like: +# 1000/10000 = 10% +# +# Create a new array of hashes to classify the ARGV elements, eg: +# $0 + "+%c" -= 10 +=4 =1 =100 "+%p" +# [ +# { +# "action" => "increment", +# "value" => 1 +# },{ +# "action" => "print", +# "value" => "+%c" +# },{ +# "action" => "decrement", +# "value" => 10 +# },{ +# "action" => "increment", +# "value" => 4 +# },{ +# "action" => "set", +# "value" => 1 +# },{ +# "action" => "set", +# "value" => 100 +# },{ +# "action" => "print", +# "value" => "+%p" +# } +# ] +# +# Process all of these in order. Add an option to show/hide action output +# so you can either get (shown): +# 100% 100000 90% 94% 1% 100% 100% +# or (hidden): +# 100000 100% +# +# All of the events would still occur between the output values. +# +# Add a delay option between increments and a delay between actions (does not +# apply to 'print'). This could be used to make a gradual sweep with: +# +# --delay-increment=100 --delay-action=0 +# +# or to blink with: +# +# --delay-increment=0 --delay-action=1000 +# +# With the latter, you could even have the backlight do morse code: +# clear | S ( . . . ) |pause| O ( _ _ _ ) |pause| +# =0 =100 =0 =100 =0 =100 =0 =0 =100 =100 =0 =100 =100 =0 =100 =100 =0 =0 ... +# +# Perhaps add an argument to repeat indefinitely. This is all getting a bit +# silly, but kinda fun to think about. + # Files containing current and max brightness values my $cur_file = "/sys/class/backlight/intel_backlight/brightness"; my $max_file = "/sys/class/backlight/intel_backlight/max_brightness"; @@ -8,12 +71,8 @@ my $last_file = "/home/jpm/.config/blc.last"; sub to_percent { my $value = shift; - if ($value eq "Permission Denied") { - return $value; - } else { - $value = int($value/get_max()*100); - return $value . '%'; - } + $value = int($value/get_max()*100); + return $value . '%'; } sub get_offset @@ -47,10 +106,9 @@ sub get_min sub writable { if (! -w $cur_file) { - return 0; - } else { - return 1; + die "You don't have permission to write $cur_file\n"; } + return 1; } sub increment @@ -66,8 +124,6 @@ sub increment print $c $target; close $c; return $target; - } else { - return "Permission Denied"; } } @@ -84,8 +140,6 @@ sub decrement print $c $target; close $c; return $target; - } else { - return "Permission Denied"; } } @@ -108,8 +162,6 @@ sub set } } return $value; - } else { - return "Permission Denied"; } } @@ -135,9 +187,9 @@ Actions: = VALUE Set backlight to specific value. VALUE greater than 100 will be treated as absolute value. VALUE eqaul to or less than 100 will be treated as a percentage -++ Increment by 1% ++ Increment by 1% += VALUE Increment by VALUE percent --- Decrement by 1% +- Decrement by 1% -= VALUE Decrement by VALUE percent Actions corrected to 1% or 100% if over or under. All actions @@ -187,110 +239,94 @@ Only one \'\\' is removed per block. eg. my $current = get_current(); my (@output, $target, $silent, $notify); +my $operation_found = 0; if (scalar @ARGV) { - for (my $i=0;$i get_max()) { - $target = set(get_max()); - } elsif ($target > 100) { - $target = set($target); - } else { - $target = set( - int((get_max()*$target/100)+1) - ); - } - if ($target eq "Permission Denied") { - @output = $target; - } else { - @output = to_percent($target); - } - last; - } else { - @output = ("No value after $ARGV[$i]"); - last; - } - } elsif ($ARGV[$i] eq '%') { + while ($arg = shift) { + if ($arg eq '%') { push @output,int(get_current()/get_max()*100); - } elsif ($ARGV[$i] eq '^') { + } elsif ($arg eq '^') { push @output,get_max(); - } elsif ($ARGV[$i] eq '==') { + } elsif ($arg eq '==') { push @output,get_current(); - } elsif ($ARGV[$i] eq '--help') { + } elsif ($arg eq '--help') { help(); print " (see --HELP).\n\n"; exit(); - } elsif ($ARGV[$i] eq '--HELP') { + } elsif ($arg eq '--HELP') { help(); advanced(); exit(); - } elsif ($ARGV[$i] eq '--silent') { - $silent = 'TRUE'; - } elsif ($ARGV[$i] =~ /^--notify/) { - $notify = 'TRUE'; - if ($ARGV[$i] =~ /=[0-9]+$/) { - $duration = $ARGV[$i]; + } elsif ($arg eq '--silent') { + $silent = 1; + } elsif ($arg =~ /^--notify/) { + $notify = 1; + if ($arg =~ /=[0-9]+$/) { + $duration = $arg; $duration =~ s/.*=([0-9]+)/$1/; } else { - $duration = 200; + $duration = 1000; } + } elsif ($arg eq '+') { + unshift(@ARGV,1); + unshift(@ARGV,'+='); + } elsif ($arg =~ /^\+=/) { + my $offset; + if ($arg =~ m/^\+=(.+)$/) { + $offset = $1; + } else { + $offset = shift || die "+= without accompanying offset value\n"; + } + unless ($offset =~ /^\d+$/) { + die "+= is not a number ($offset)\n"; + } + for (my $j=0;$j<$offset;$j++) { + $target = increment(); + @output = to_percent($target); + } + $operation_found = 1; + } elsif ($arg eq '-') { + unshift(@ARGV,1); + unshift(@ARGV,'-='); + } elsif ($arg =~ /^-=/) { + my $offset; + if ($arg =~ m/^-=(.+)$/) { + $offset = $1; + } else { + $offset = shift || die "-= without accompanying offset value\n"; + } + unless ($offset =~ /^\d+$/) { + die "-= is not a number ($offset)\n"; + } + for (my $j=0;$j<$offset;$j++) { + $target = decrement(); + @output = to_percent($target); + } + $operation_found = 1; + } elsif ($arg =~ m/^=/) { + my $value; + if ($arg =~ m/^=(.+)$/) { + $target = $1; + } else { + $target = shift || die "= without accompanying absolute value\n"; + } + unless ($target =~ /^\d+$/) { + die "= is not a number ($target)\n"; + } + if ($target < 1) { + $target = set(get_min()); + } elsif ($target > get_max()) { + $target = set(get_max()); + } elsif ($target > 100) { + $target = set($target); + } else { + $target = set( + int((get_max()*$target/100)+1) + ); + } + @output = to_percent($target); + $operation_found = 1; } else { - my $add = $ARGV[$i]; - $add =~ s/\\//; - push @output,$add; + push(@output,$arg); } } } else { @@ -305,29 +341,53 @@ if (scalar @ARGV) { ); } - open(my $fh,'>',$last_file); print $fh get_current(); close($fh); +print $_."\n" foreach(@output); if ($silent) { exit(); } elsif ($notify) { - # Don't output anything if the value didn't change - if ($current == get_current()) { - exit(); - } - my $concat; + my $concat = ''; foreach (@output) { $concat .= $_; } - system "notify-send --urgency=normal -i " - . "/usr/share/icons/Papirus-Dark-Grey/48x48/status/" - . "notification-display-brightness.svg -t " - . $duration - . ' "' - . $concat - . '"'; + #use Desktop::Notify; + #my $notify = Desktop::Notify->new(); + use Gtk2::Notify -init, "Backlight"; + my $notification = Gtk2::Notify::new('Backlight', $concat, '', "notification-display-brightness"); + #my $notification = $notify->create( + #'timeout' => $duration, + #'summary' => "Brightness", + #'body' => $concat, + #'hints' => { + ##'urgency' => 'NOTIFY_URGENCY_LOW', + #'x-canonical-private-synchronous' => 'blc', + #'value' => (split('%',$concat))[0] || 0 + #} + #); + #$notification->show; + #exit; + + $notification->set_hint_string('x-canonical-private-synchronous','blc'); + $notification->set_urgency('NOTIFY_URGENCY_LOW'); + $notification->set_hint_int32('value',(split('%',$concat))[0]); + $notification->set_timeout($duration); + $notification->show(); + exit; + print "notify-send --urgency=normal" + . ' --icon="notification-display-brightness"' + . ' --hint=string:x-canonical-private-synchronous:blc' + . ' --expire-time=' . $duration + . ' "Backlight"' + . ' "' . $concat . " '" . join("','", @output) . "'\""; + system "notify-send --urgency=normal" + . ' --icon="notification-display-brightness"' + . ' --hint=string:x-canonical-private-synchronous:blc' + . ' --hint=int:value:' . (split('%',$concat))[0] + . ' --expire-time=' . $duration + . ' "' . $concat . "\""; exit(); } else { print foreach @output;