chiark / gitweb /
7e0bb5038d365c79dd59e9ae26593837c85998b6
[profile] / bin / disorder-notify
1 #! /usr/bin/perl
2
3 sub notify ($$) {
4   my ($head, $body) = @_;
5
6   my $kid = fork;
7   defined $kid or return;
8   if (!$kid) {
9     open STDOUT, ">", "/dev/null";
10     exec "gdbus", "call", "-e",
11       "-d", "org.freedesktop.Notifications",
12       "-o", "/org/freedesktop/Notifications",
13       "-m", "org.freedesktop.Notifications.Notify", "--",
14       "DisOrder", "0", "audio-volume-high",
15       $head, $body, "[]", "{}", "2500";
16   }
17   waitpid $kid, 0;
18 }
19
20 for (;;) {
21   open my $log, "-|", "disorder", "log";
22   LINE: while (<$log>) {
23     chomp;
24     my @f = ();
25     my $q = my $t = undef;
26     my $e = 0;
27     my $j = -1;
28     for (my $i = 0; $i < length $_; $i++) {
29       my $ch = substr($_, $i, 1);
30       if ($e) {
31         if ($ch eq "n") { $ch = "\n"; }
32         $t .= $ch; $e = 0;
33       } elsif ($ch eq $q) {
34         push @f, $t; $q = $t = undef;
35       } elsif (defined $q) {
36         if ($ch eq "\\") { $e = 1; }
37         else { $t .= $ch; }
38       } elsif ($ch eq " ") {
39         push @f, $t if defined $t; $t = undef;
40       } elsif (!defined $t && ($ch eq '"' || $ch eq "'")) {
41         $t //= ""; $q = $ch; $j = $i;
42       } else {
43         $t //= ""; $t .= $ch;
44       }
45     }
46     defined $q and die "unmatched $q (pos $j) in: $_";
47     push @f, $t if defined $t;
48
49     my $what = $f[1];
50     if ($what eq "state") {
51       my $st = $f[2];
52       if ($st eq "disable_random") {
53         notify "DisOrder state", "Random play disabled";
54       } elsif ($st eq "enable_random") {
55         notify "DisOrder state", "Random play enabled";
56       } elsif ($st eq "disable_play") {
57         notify "DisOrder state", "Playing disabled";
58       } elsif ($st eq "enable_play") {
59         notify "DisOrder state", "Playing enabled";
60       } elsif ($st eq "pause") {
61         notify "DisOrder state", "Paused";
62       } elsif ($st eq "resume") {
63         notify "DisOrder state", "Resuming";
64       }
65     } elsif ($what eq playing) {
66       my $track = $f[2];
67       my %p;
68       for my $p ("artist", "album", "title") {
69         open my $f, "-|", "disorder", "part", $track, "display", $p;
70         chomp ($p{$p} = <$f>);
71         close $f;
72       }
73       if ($p{artist} =~ /^[A-Z]$/)
74         { $p{artist} = $p{album}; $p{album} = undef; }
75       elsif ($p{artist} eq "share" && $p{album} eq "disorder")
76         { next LINE; }
77       my $r = "$p{artist}: ‘$p{title}’";
78       if (defined $p{album}) { $r .= ", from ‘$p{album}’"; }
79       notify "Now playing", $r;
80     }
81   }
82   close $log;
83   sleep 5;
84 }