bin/disorder-notify: New hack to pop up notifications about DisOrder.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 21 Jul 2018 00:49:46 +0000 (01:49 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 21 Jul 2018 14:57:56 +0000 (15:57 +0100)
Useful now that it's mostly controlled from the keyboard.

Makefile
bin/disorder-notify [new file with mode: 0755]

index fd34e33..e1abd26 100644 (file)
--- 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 (executable)
index 0000000..7e0bb50
--- /dev/null
@@ -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;
+}