working notifications for blc

Not ideal. Requires manual installation of Gtk2::Notify which is no
longer packaged by Debian and has broken dependencies. Need to migrate
to a better notification library.

Migration was initially made to Gtk2::Notify because it allows for
replacing the content of an existing notification. No newer libraries
appear to support this in Perl.

This was the first "substantial" Perl program I've ever written; it may
become the first which is re-written in a better supported language.
This commit is contained in:
John Mertz 2021-12-31 11:48:24 -05:00 committed by John Mertz
parent 04f1af06e6
commit b8cbbb330b
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
1 changed files with 180 additions and 120 deletions

View File

@ -1,5 +1,68 @@
#!/usr/bin/perl -w #!/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 # Files containing current and max brightness values
my $cur_file = "/sys/class/backlight/intel_backlight/brightness"; my $cur_file = "/sys/class/backlight/intel_backlight/brightness";
my $max_file = "/sys/class/backlight/intel_backlight/max_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 sub to_percent
{ {
my $value = shift; my $value = shift;
if ($value eq "Permission Denied") {
return $value;
} else {
$value = int($value/get_max()*100); $value = int($value/get_max()*100);
return $value . '%'; return $value . '%';
}
} }
sub get_offset sub get_offset
@ -47,10 +106,9 @@ sub get_min
sub writable sub writable
{ {
if (! -w $cur_file) { if (! -w $cur_file) {
return 0; die "You don't have permission to write $cur_file\n";
} else {
return 1;
} }
return 1;
} }
sub increment sub increment
@ -66,8 +124,6 @@ sub increment
print $c $target; print $c $target;
close $c; close $c;
return $target; return $target;
} else {
return "Permission Denied";
} }
} }
@ -84,8 +140,6 @@ sub decrement
print $c $target; print $c $target;
close $c; close $c;
return $target; return $target;
} else {
return "Permission Denied";
} }
} }
@ -108,8 +162,6 @@ sub set
} }
} }
return $value; return $value;
} else {
return "Permission Denied";
} }
} }
@ -135,9 +187,9 @@ Actions:
= VALUE Set backlight to specific value. VALUE greater than = VALUE Set backlight to specific value. VALUE greater than
100 will be treated as absolute value. VALUE eqaul to 100 will be treated as absolute value. VALUE eqaul to
or less than 100 will be treated as a percentage or less than 100 will be treated as a percentage
++ Increment by 1% + Increment by 1%
+= VALUE Increment by VALUE percent += VALUE Increment by VALUE percent
-- Decrement by 1% - Decrement by 1%
-= VALUE Decrement by VALUE percent -= VALUE Decrement by VALUE percent
Actions corrected to 1% or 100% if over or under. All actions Actions corrected to 1% or 100% if over or under. All actions
@ -187,61 +239,79 @@ Only one \'\\' is removed per block. eg.
my $current = get_current(); my $current = get_current();
my (@output, $target, $silent, $notify); my (@output, $target, $silent, $notify);
my $operation_found = 0;
if (scalar @ARGV) { if (scalar @ARGV) {
for (my $i=0;$i<scalar @ARGV;$i++) { while ($arg = shift) {
if ($ARGV[$i] eq '++') { if ($arg eq '%') {
push @output,int(get_current()/get_max()*100);
} elsif ($arg eq '^') {
push @output,get_max();
} elsif ($arg eq '==') {
push @output,get_current();
} elsif ($arg eq '--help') {
help();
print " (see --HELP).\n\n";
exit();
} elsif ($arg eq '--HELP') {
help();
advanced();
exit();
} elsif ($arg eq '--silent') {
$silent = 1;
} elsif ($arg =~ /^--notify/) {
$notify = 1;
if ($arg =~ /=[0-9]+$/) {
$duration = $arg;
$duration =~ s/.*=([0-9]+)/$1/;
} else {
$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 "+= <value> is not a number ($offset)\n";
}
for (my $j=0;$j<$offset;$j++) {
$target = increment(); $target = increment();
@output = to_percent($target); @output = to_percent($target);
last;
} elsif ($ARGV[$i] eq '+=') {
for (my $i=0;$i<scalar @ARGV;$i++) {
if ($ARGV[$i] eq '+=') {
if (defined $ARGV[($i+1)]) {
$count = $ARGV[($i+1)];
for (my $j=0;$j<$count;$j++) {
$target = increment();
if ($target eq
"Permission Denied") {
last;
} }
} $operation_found = 1;
@output = to_percent($target); } elsif ($arg eq '-') {
unshift(@ARGV,1);
unshift(@ARGV,'-=');
} elsif ($arg =~ /^-=/) {
my $offset;
if ($arg =~ m/^-=(.+)$/) {
$offset = $1;
} else { } else {
@output = $offset = shift || die "-= without accompanying offset value\n";
("No value after $ARGV[$i]");
} }
last; unless ($offset =~ /^\d+$/) {
die "-= <value> is not a number ($offset)\n";
} }
} for (my $j=0;$j<$offset;$j++) {
last;
} elsif ($ARGV[$i] eq '--') {
$target = decrement(); $target = decrement();
@output = to_percent($target); @output = to_percent($target);
last;
} elsif ($ARGV[$i] eq '-=') {
for (my $i=0;$i<scalar @ARGV;$i++) {
if ($ARGV[$i] eq '-=') {
if (defined $ARGV[($i+1)]) {
$count = $ARGV[($i+1)];
for (my $j=0;$j<$count;$j++) {
$target = decrement();
if ($target eq
"Permission Denied") {
last;
} }
} $operation_found = 1;
@output = to_percent($target); } elsif ($arg =~ m/^=/) {
my $value;
if ($arg =~ m/^=(.+)$/) {
$target = $1;
} else { } else {
@output = $target = shift || die "= without accompanying absolute value\n";
("No value after $ARGV[$i]");
} }
last; unless ($target =~ /^\d+$/) {
die "= <value> is not a number ($target)\n";
} }
}
last;
} elsif ($ARGV[$i] eq '=') {
$target = $ARGV[($i+1)];
if (defined $target) {
if ($target < 1) { if ($target < 1) {
$target = set(get_min()); $target = set(get_min());
} elsif ($target > get_max()) { } elsif ($target > get_max()) {
@ -253,44 +323,10 @@ if (scalar @ARGV) {
int((get_max()*$target/100)+1) int((get_max()*$target/100)+1)
); );
} }
if ($target eq "Permission Denied") {
@output = $target;
} else {
@output = to_percent($target); @output = to_percent($target);
} $operation_found = 1;
last;
} else { } else {
@output = ("No value after $ARGV[$i]"); push(@output,$arg);
last;
}
} elsif ($ARGV[$i] eq '%') {
push @output,int(get_current()/get_max()*100);
} elsif ($ARGV[$i] eq '^') {
push @output,get_max();
} elsif ($ARGV[$i] eq '==') {
push @output,get_current();
} elsif ($ARGV[$i] eq '--help') {
help();
print " (see --HELP).\n\n";
exit();
} elsif ($ARGV[$i] 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];
$duration =~ s/.*=([0-9]+)/$1/;
} else {
$duration = 200;
}
} else {
my $add = $ARGV[$i];
$add =~ s/\\//;
push @output,$add;
} }
} }
} else { } else {
@ -305,29 +341,53 @@ if (scalar @ARGV) {
); );
} }
open(my $fh,'>',$last_file); open(my $fh,'>',$last_file);
print $fh get_current(); print $fh get_current();
close($fh); close($fh);
print $_."\n" foreach(@output);
if ($silent) { if ($silent) {
exit(); exit();
} elsif ($notify) { } elsif ($notify) {
# Don't output anything if the value didn't change my $concat = '';
if ($current == get_current()) {
exit();
}
my $concat;
foreach (@output) { foreach (@output) {
$concat .= $_; $concat .= $_;
} }
system "notify-send --urgency=normal -i " #use Desktop::Notify;
. "/usr/share/icons/Papirus-Dark-Grey/48x48/status/" #my $notify = Desktop::Notify->new();
. "notification-display-brightness.svg -t " use Gtk2::Notify -init, "Backlight";
. $duration my $notification = Gtk2::Notify::new('Backlight', $concat, '', "notification-display-brightness");
. ' "' #my $notification = $notify->create(
. $concat #'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(); exit();
} else { } else {
print foreach @output; print foreach @output;