bin/wakey.zsh: Use `$SECONDS' rather than `$EPOCHREALTIME'.
[profile] / bin / disorder-notify
index 5c9e12d..84c702f 100755 (executable)
@@ -3,22 +3,53 @@
 sub notify ($$) {
   my ($head, $body) = @_;
 
+  $body =~ s:\&:&:g;
+  $body =~ s:\<:&lt;:g;
+  $body =~ s:\>:&gt;:g;
   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, "[]", "{}", "5000";
+    exec "notify-send",
+      "-c", "DisOrder", "-i", "audio-volume-high", "-t", "5000",
+      $head, $body;
   }
   waitpid $kid, 0;
 }
 
+sub cmd (@) {
+  my @args = @_;
+  open my $f, "-|", "disorder", @args;
+  chomp (my @r = <$f>);
+  close $f;
+  if (wantarray) { return @r; }
+  elsif (@r == 1) { return $r[0]; }
+  else { return "??? multiple lines"; }
+}
+
+sub now_playing (;$) {
+  my ($track) = @_;
+  if (!defined $track) {
+    my @r = cmd "playing";
+    if ($r[0] =~ /^track\s+(.*)$/) { $track = $1; }
+    else { return; }
+  }
+  my %p;
+  for my $p ("artist", "album", "title")
+    { $p{$p} = cmd "part", $track, "display", $p; }
+  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 "DisOrder: now playing", $r;
+}
+
 for (;;) {
   open my $log, "-|", "disorder", "log";
+  my $startp = 1;
+  my $stateinfo = undef;
   LINE: while (<$log>) {
     chomp;
     my @f = ();
@@ -47,36 +78,32 @@ for (;;) {
     push @f, $t if defined $t;
 
     my $what = $f[1];
-    if ($what eq "state") {
+    if ($what eq "volume" && $startp) {
+      $startp = 0;
+      notify "DisOrder state", "Connected: $startinfo";
+      now_playing;
+    } elsif ($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;
+      my $msg;
+      my $np = 0;
+      if ($st eq "disable_random") { $msg = "random play disabled"; }
+      elsif ($st eq "enable_random") { $msg = "random play enabled"; }
+      elsif ($st eq "disable_play") { $msg = "playing disabled"; }
+      elsif ($st eq "enable_play") { $msg = "playing enabled"; }
+      elsif ($st eq "pause") { $msg = "paused"; }
+      elsif ($st eq "resume") { $msg = "playing"; $np = 1; }
+      else { next LINE; }
+      if (!$startp) {
+       notify "DisOrder state", ucfirst $msg;
+       now_playing if $np;
+      } else {
+       if (defined $startinfo) { $startinfo .= "; " . $msg; }
+       else { $startinfo = $msg; }
       }
-      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;
+    } elsif ($what eq "scratched") {
+      notify "DisOrder state", "Scratched playing track";
+    } elsif ($what eq "playing") {
+      now_playing $f[2];
     }
   }
   close $log;