Commit | Line | Data |
---|---|---|
6bdf3aad MW |
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", | |
0990cf69 | 15 | $head, $body, "[]", "{}", "5000"; |
6bdf3aad MW |
16 | } |
17 | waitpid $kid, 0; | |
18 | } | |
19 | ||
994838b7 MW |
20 | sub cmd (@) { |
21 | my @args = @_; | |
22 | open my $f, "-|", "disorder", @args; | |
23 | chomp (my @r = <$f>); | |
24 | close $f; | |
25 | if (wantarray) { return @r; } | |
26 | elsif (@r == 1) { return $r[0]; } | |
27 | else { return "??? multiple lines"; } | |
28 | } | |
29 | ||
f1b1fa59 MW |
30 | sub now_playing ($) { |
31 | my ($track) = @_; | |
32 | my %p; | |
33 | for my $p ("artist", "album", "title") | |
34 | { $p{$p} = cmd "part", $track, "display", $p; } | |
35 | if ($p{artist} =~ /^[A-Z]$/) | |
36 | { $p{artist} = $p{album}; $p{album} = undef; } | |
37 | elsif ($p{artist} eq "share" && $p{album} eq "disorder") | |
38 | { next LINE; } | |
39 | my $r = "$p{artist}: ‘$p{title}’"; | |
40 | if (defined $p{album}) { $r .= ", from ‘$p{album}’"; } | |
41 | notify "DisOrder: now playing", $r; | |
42 | } | |
43 | ||
6bdf3aad MW |
44 | for (;;) { |
45 | open my $log, "-|", "disorder", "log"; | |
46 | LINE: while (<$log>) { | |
47 | chomp; | |
48 | my @f = (); | |
49 | my $q = my $t = undef; | |
50 | my $e = 0; | |
51 | my $j = -1; | |
52 | for (my $i = 0; $i < length $_; $i++) { | |
53 | my $ch = substr($_, $i, 1); | |
54 | if ($e) { | |
55 | if ($ch eq "n") { $ch = "\n"; } | |
56 | $t .= $ch; $e = 0; | |
57 | } elsif ($ch eq $q) { | |
58 | push @f, $t; $q = $t = undef; | |
59 | } elsif (defined $q) { | |
60 | if ($ch eq "\\") { $e = 1; } | |
61 | else { $t .= $ch; } | |
62 | } elsif ($ch eq " ") { | |
63 | push @f, $t if defined $t; $t = undef; | |
64 | } elsif (!defined $t && ($ch eq '"' || $ch eq "'")) { | |
65 | $t //= ""; $q = $ch; $j = $i; | |
66 | } else { | |
67 | $t //= ""; $t .= $ch; | |
68 | } | |
69 | } | |
70 | defined $q and die "unmatched $q (pos $j) in: $_"; | |
71 | push @f, $t if defined $t; | |
72 | ||
73 | my $what = $f[1]; | |
74 | if ($what eq "state") { | |
75 | my $st = $f[2]; | |
76 | if ($st eq "disable_random") { | |
77 | notify "DisOrder state", "Random play disabled"; | |
78 | } elsif ($st eq "enable_random") { | |
79 | notify "DisOrder state", "Random play enabled"; | |
80 | } elsif ($st eq "disable_play") { | |
81 | notify "DisOrder state", "Playing disabled"; | |
82 | } elsif ($st eq "enable_play") { | |
83 | notify "DisOrder state", "Playing enabled"; | |
84 | } elsif ($st eq "pause") { | |
85 | notify "DisOrder state", "Paused"; | |
86 | } elsif ($st eq "resume") { | |
87 | notify "DisOrder state", "Resuming"; | |
88 | } | |
ccd96c4f | 89 | } elsif ($what eq "playing") { |
f1b1fa59 | 90 | now_playing $f[2]; |
6bdf3aad MW |
91 | } |
92 | } | |
93 | close $log; | |
94 | sleep 5; | |
95 | } |