From 6bdf3aad9e937a3d5718da832692f82b76f6ad67 Mon Sep 17 00:00:00 2001 Message-Id: <6bdf3aad9e937a3d5718da832692f82b76f6ad67.1716717908.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 21 Jul 2018 01:49:46 +0100 Subject: [PATCH] bin/disorder-notify: New hack to pop up notifications about DisOrder. Organization: Straylight/Edgeware From: Mark Wooding Useful now that it's mostly controlled from the keyboard. --- Makefile | 1 + bin/disorder-notify | 84 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 bin/disorder-notify diff --git a/Makefile b/Makefile index fd34e33..e1abd26 100644 --- a/Makefile +++ b/Makefile @@ -282,6 +282,7 @@ SCRIPTLINKS += un-backslashify-selection SCRIPTLINKS += xpra-start-xdummy SCRIPTLINKS += play-rawk SCRIPTLINKS += media-keys +SCRIPTLINKS += disorder-notify DOTCPP += .Xdefaults Xdefaults_DEFS = -DEMACSWD=$(call mdw-conf,emacs-width,77) diff --git a/bin/disorder-notify b/bin/disorder-notify new file mode 100755 index 0000000..7e0bb50 --- /dev/null +++ b/bin/disorder-notify @@ -0,0 +1,84 @@ +#! /usr/bin/perl + +sub notify ($$) { + my ($head, $body) = @_; + + my $kid = fork; + defined $kid or return; + if (!$kid) { + open STDOUT, ">", "/dev/null"; + exec "gdbus", "call", "-e", + "-d", "org.freedesktop.Notifications", + "-o", "/org/freedesktop/Notifications", + "-m", "org.freedesktop.Notifications.Notify", "--", + "DisOrder", "0", "audio-volume-high", + $head, $body, "[]", "{}", "2500"; + } + waitpid $kid, 0; +} + +for (;;) { + open my $log, "-|", "disorder", "log"; + LINE: while (<$log>) { + chomp; + my @f = (); + my $q = my $t = undef; + my $e = 0; + my $j = -1; + for (my $i = 0; $i < length $_; $i++) { + my $ch = substr($_, $i, 1); + if ($e) { + if ($ch eq "n") { $ch = "\n"; } + $t .= $ch; $e = 0; + } elsif ($ch eq $q) { + push @f, $t; $q = $t = undef; + } elsif (defined $q) { + if ($ch eq "\\") { $e = 1; } + else { $t .= $ch; } + } elsif ($ch eq " ") { + push @f, $t if defined $t; $t = undef; + } elsif (!defined $t && ($ch eq '"' || $ch eq "'")) { + $t //= ""; $q = $ch; $j = $i; + } else { + $t //= ""; $t .= $ch; + } + } + defined $q and die "unmatched $q (pos $j) in: $_"; + push @f, $t if defined $t; + + my $what = $f[1]; + if ($what eq "state") { + my $st = $f[2]; + if ($st eq "disable_random") { + notify "DisOrder state", "Random play disabled"; + } elsif ($st eq "enable_random") { + notify "DisOrder state", "Random play enabled"; + } elsif ($st eq "disable_play") { + notify "DisOrder state", "Playing disabled"; + } elsif ($st eq "enable_play") { + notify "DisOrder state", "Playing enabled"; + } elsif ($st eq "pause") { + notify "DisOrder state", "Paused"; + } elsif ($st eq "resume") { + notify "DisOrder state", "Resuming"; + } + } elsif ($what eq playing) { + my $track = $f[2]; + my %p; + for my $p ("artist", "album", "title") { + open my $f, "-|", "disorder", "part", $track, "display", $p; + chomp ($p{$p} = <$f>); + close $f; + } + if ($p{artist} =~ /^[A-Z]$/) + { $p{artist} = $p{album}; $p{album} = undef; } + elsif ($p{artist} eq "share" && $p{album} eq "disorder") + { next LINE; } + my $r = "$p{artist}: ‘$p{title}’"; + if (defined $p{album}) { $r .= ", from ‘$p{album}’"; } + notify "Now playing", $r; + } + } + close $log; + sleep 5; +} -- [mdw]