1 508e5f5f 2022-07-10 op #!/usr/bin/env perl
3 508e5f5f 2022-07-10 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
5 508e5f5f 2022-07-10 op # Permission to use, copy, modify, and distribute this software for any
6 508e5f5f 2022-07-10 op # purpose with or without fee is hereby granted, provided that the above
7 508e5f5f 2022-07-10 op # copyright notice and this permission notice appear in all copies.
9 508e5f5f 2022-07-10 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 508e5f5f 2022-07-10 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 508e5f5f 2022-07-10 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 508e5f5f 2022-07-10 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 508e5f5f 2022-07-10 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 508e5f5f 2022-07-10 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 508e5f5f 2022-07-10 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 508e5f5f 2022-07-10 op use POSIX ":sys_wait_h";
23 508e5f5f 2022-07-10 op use Text::CharWidth qw(mbswidth);
24 508e5f5f 2022-07-10 op use IO::Poll qw(POLLIN);
25 508e5f5f 2022-07-10 op use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
26 508e5f5f 2022-07-10 op use Getopt::Long qw(:config bundling);
27 508e5f5f 2022-07-10 op use Pod::Usage;
32 508e5f5f 2022-07-10 op my $trim = "";
34 508e5f5f 2022-07-10 op my $pair_n = 1;
37 508e5f5f 2022-07-10 op my $playlist_cur;
38 508e5f5f 2022-07-10 op my $playlist_max;
44 508e5f5f 2022-07-10 op my $last_lines;
47 3c8ce7d5 2022-07-11 op return int(0.5 + shift);
51 508e5f5f 2022-07-10 op my ($a, $b) = @_;
52 508e5f5f 2022-07-10 op return $a > $b ? $a : $b;
56 3c8ce7d5 2022-07-11 op my $lines = shift;
58 42426670 2022-07-11 op my ($n, $idx, $cur) = (0, 0, -1);
60 42426670 2022-07-11 op open (my $fh, "-|", "amused", "show", "-p");
61 508e5f5f 2022-07-10 op while (<$fh>) {
64 42426670 2022-07-11 op $tmp[$idx] = $_;
65 42426670 2022-07-11 op $cur = $n if m/^>/;
67 42426670 2022-07-11 op $idx = ++$idx % $lines;
69 83438dc0 2022-07-13 op last if $cur != -1 && $n - $cur > int($lines/2) &&
70 42426670 2022-07-11 op $#tmp == $lines-1;
74 42426670 2022-07-11 op return ("Empty playlist.") unless @tmp;
76 42426670 2022-07-11 op # reorder the entries
78 42426670 2022-07-11 op my $len = $#tmp + 1;
79 6045cf87 2022-07-13 op $idx = $idx % $len;
80 42426670 2022-07-11 op for (1..$len) {
81 42426670 2022-07-11 op push @r, $tmp[$idx];
82 42426670 2022-07-11 op $idx = ++$idx % $len;
87 508e5f5f 2022-07-10 op sub playlist_numbers {
88 508e5f5f 2022-07-10 op my ($cur, $tot, $found) = (0, 0, 0);
89 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused", "show", "-p");
90 508e5f5f 2022-07-10 op while (<$fh>) {
92 508e5f5f 2022-07-10 op $cur++ unless $found;
93 508e5f5f 2022-07-10 op $found = 1 if m/^>/;
96 508e5f5f 2022-07-10 op return ($cur, $tot);
100 63dcef30 2022-07-13 op my ($pos, $dur, $mode);
102 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused", "status", "-f",
103 63dcef30 2022-07-13 op "status,time:raw,mode:oneline");
104 508e5f5f 2022-07-10 op my $status = <$fh>;
105 508e5f5f 2022-07-10 op chomp $status;
106 508e5f5f 2022-07-10 op while (<$fh>) {
108 508e5f5f 2022-07-10 op $pos = s/position //r if m/^position /;
109 508e5f5f 2022-07-10 op $dur = s/duration //r if m/^duration /;
110 63dcef30 2022-07-13 op $mode = $_ if m/^repeat/;
113 63dcef30 2022-07-13 op return ($status, $pos, $dur, $mode);
116 508e5f5f 2022-07-10 op sub showtime {
117 508e5f5f 2022-07-10 op my $seconds = shift;
118 508e5f5f 2022-07-10 op my $str = "";
120 508e5f5f 2022-07-10 op if ($seconds > 3600) {
121 508e5f5f 2022-07-10 op my $hours = int($seconds / 3600);
122 508e5f5f 2022-07-10 op $seconds -= $hours * 3600;
123 508e5f5f 2022-07-10 op $str = sprintf("%02:", $hours);
126 508e5f5f 2022-07-10 op my $minutes = int($seconds / 60);
127 508e5f5f 2022-07-10 op $seconds -= $minutes * 60;
128 508e5f5f 2022-07-10 op $str .= sprintf "%02d:%02d", $minutes, $seconds;
133 508e5f5f 2022-07-10 op my ($str, $pstr) = @_;
134 508e5f5f 2022-07-10 op my $width = mbswidth($str);
135 508e5f5f 2022-07-10 op return $str if $width > $COLS;
136 1b135dc5 2022-07-26 op my $pre = round(($COLS - $width) / 2);
137 508e5f5f 2022-07-10 op my $lpad = $pstr x $pre;
138 1b135dc5 2022-07-26 op my $rpad = $pstr x ($COLS - $width - $pre);
139 508e5f5f 2022-07-10 op return ($lpad, $str, $rpad);
142 508e5f5f 2022-07-10 op sub offsets {
143 508e5f5f 2022-07-10 op my ($y, $x, $cur, $max) = @_;
144 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center(" $cur / $max ", '-');
145 508e5f5f 2022-07-10 op addstring $y, $x, "";
147 508e5f5f 2022-07-10 op my $p = COLOR_PAIR($pair_n);
150 508e5f5f 2022-07-10 op addstring $pre;
153 508e5f5f 2022-07-10 op addstring $c;
156 508e5f5f 2022-07-10 op addstring $post;
160 508e5f5f 2022-07-10 op sub progress {
161 508e5f5f 2022-07-10 op my ($y, $x, $pos, $dur) = @_;
163 508e5f5f 2022-07-10 op my $pstr = showtime $pos;
164 508e5f5f 2022-07-10 op my $dstr = showtime $dur;
166 508e5f5f 2022-07-10 op my $len = $COLS - length($pstr) - length($dstr) - 4;
167 127f376a 2022-07-11 op return if $len <= 0 or $dur <= 0;
168 3c8ce7d5 2022-07-11 op my $filled = round($pos * $len / $dur);
170 508e5f5f 2022-07-10 op addstring $y, $x, "$pstr [";
171 508e5f5f 2022-07-10 op addstring "#" x $filled;
172 508e5f5f 2022-07-10 op addstring " " x max($len - $filled, 0);
173 508e5f5f 2022-07-10 op addstring "] $dstr";
176 508e5f5f 2022-07-10 op sub show_status {
177 508e5f5f 2022-07-10 op my ($y, $x, $status) = @_;
178 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center($status, ' ');
179 508e5f5f 2022-07-10 op addstring $y, $x, $pre;
180 508e5f5f 2022-07-10 op addstring $c;
181 508e5f5f 2022-07-10 op addstring $post;
184 63dcef30 2022-07-13 op sub show_mode {
185 63dcef30 2022-07-13 op my ($y, $x, $mode) = @_;
186 63dcef30 2022-07-13 op my ($pre, $c, $post) = center($mode, ' ');
187 508e5f5f 2022-07-10 op addstring $y, $x, $pre;
188 508e5f5f 2022-07-10 op addstring $c;
189 508e5f5f 2022-07-10 op addstring $post;
194 508e5f5f 2022-07-10 op if ($LINES < 4 || $COLS < 20) {
195 508e5f5f 2022-07-10 op addstring "window too small";
200 508e5f5f 2022-07-10 op my $song_pad = "";
201 508e5f5f 2022-07-10 op my $longest = 0;
202 508e5f5f 2022-07-10 op $longest = max $longest, length($_) foreach @songs;
203 508e5f5f 2022-07-10 op if ($longest < $COLS) {
204 508e5f5f 2022-07-10 op $song_pad = " " x (($COLS - $longest)/2);
207 508e5f5f 2022-07-10 op my $line = 0;
209 508e5f5f 2022-07-10 op attron(A_BOLD) if m/^>/;
210 508e5f5f 2022-07-10 op addstring $line++, 0, $song_pad . $_;
214 508e5f5f 2022-07-10 op offsets $LINES - 4, 0, $playlist_cur, $playlist_max;
215 508e5f5f 2022-07-10 op progress $LINES - 3, 0, $time_cur, $time_dur;
216 508e5f5f 2022-07-10 op show_status $LINES - 2, 0, $status;
217 63dcef30 2022-07-13 op show_mode $LINES - 1, 0, $mode;
222 508e5f5f 2022-07-10 op sub getsongs {
223 508e5f5f 2022-07-10 op $last_lines = $LINES;
224 42426670 2022-07-11 op @songs = excerpt $LINES - 4;
227 508e5f5f 2022-07-10 op sub getnums {
228 508e5f5f 2022-07-10 op ($playlist_cur, $playlist_max) = playlist_numbers;
231 508e5f5f 2022-07-10 op sub getstatus {
232 63dcef30 2022-07-13 op ($status, $time_cur, $time_dur, $mode) = status;
236 508e5f5f 2022-07-10 op return unless defined $pfile;
238 508e5f5f 2022-07-10 op open(my $fh, ">", $pfile);
239 508e5f5f 2022-07-10 op open(my $ph, "-|", "amused", "show", "-p");
241 508e5f5f 2022-07-10 op print $fh $_ while (<$ph>);
245 508e5f5f 2022-07-10 op my $fh = shift;
246 508e5f5f 2022-07-10 op my $l = <$fh>;
247 508e5f5f 2022-07-10 op die "monitor quit" unless defined($l);
249 508e5f5f 2022-07-10 op getnums if $l =~ m/load|jump|next|prev/;
250 508e5f5f 2022-07-10 op getsongs if $l =~ m/load|jump|next|prev/;
254 508e5f5f 2022-07-10 op my ($ch, $key) = getchar;
255 9c72efd4 2022-07-13 op if (defined $key) {
256 9c72efd4 2022-07-13 op if ($key == KEY_BACKSPACE) {
257 3fc73666 2022-07-26 op system "amused", "seek", "0";
259 9c72efd4 2022-07-13 op } elsif (defined $ch) {
260 508e5f5f 2022-07-10 op if ($ch eq " ") {
261 508e5f5f 2022-07-10 op system "amused", "toggle";
262 508e5f5f 2022-07-10 op } elsif ($ch eq "<" or $ch eq "p") {
263 508e5f5f 2022-07-10 op system "amused", "prev";
264 508e5f5f 2022-07-10 op } elsif ($ch eq ">" or $ch eq "n") {
265 508e5f5f 2022-07-10 op system "amused", "next";
266 508e5f5f 2022-07-10 op } elsif ($ch eq ",") {
267 508e5f5f 2022-07-10 op system "amused", "seek", "-5";
268 508e5f5f 2022-07-10 op } elsif ($ch eq ".") {
269 508e5f5f 2022-07-10 op system "amused", "seek", "+5";
270 508e5f5f 2022-07-10 op } elsif ($ch eq "S") {
271 508e5f5f 2022-07-10 op system "amused show | sort -u | amused load";
272 508e5f5f 2022-07-10 op } elsif ($ch eq "R") {
273 508e5f5f 2022-07-10 op system "amused show | sort -R | amused load";
274 508e5f5f 2022-07-10 op } elsif ($ch eq "s") {
276 508e5f5f 2022-07-10 op } elsif ($ch eq "q") {
278 9c72efd4 2022-07-13 op } elsif ($ch eq "\cH") {
279 3fc73666 2022-07-26 op system "amused", "seek", "0"
285 508e5f5f 2022-07-10 op "p:s" => \$pfile,
286 508e5f5f 2022-07-10 op "t:s" => \$trim,
287 508e5f5f 2022-07-10 op ) or pod2usage(1);
289 87abdf60 2022-07-12 op my $mpid = open(my $monitor, "-|", "amused", "monitor")
290 508e5f5f 2022-07-10 op or die "can't spawn amused monitor";
294 508e5f5f 2022-07-10 op use_default_colors;
295 508e5f5f 2022-07-10 op init_pair $pair_n, 250, -1;
297 508e5f5f 2022-07-10 op timeout 1000;
302 508e5f5f 2022-07-10 op my $poll = IO::Poll->new();
303 508e5f5f 2022-07-10 op $poll->mask(\*STDIN => POLLIN);
304 508e5f5f 2022-07-10 op $poll->mask($monitor => POLLIN);
306 508e5f5f 2022-07-10 op my $tick = 0;
307 508e5f5f 2022-07-10 op my $tbefore = clock_gettime(CLOCK_MONOTONIC);
314 3a0b3cb8 2022-07-10 op while ($run) {
315 508e5f5f 2022-07-10 op $poll->poll(0.25);
316 508e5f5f 2022-07-10 op my $now = clock_gettime(CLOCK_MONOTONIC);
317 508e5f5f 2022-07-10 op my $elapsed = $now - $tbefore;
318 508e5f5f 2022-07-10 op if ($elapsed > 1) {
319 508e5f5f 2022-07-10 op $tbefore = $now;
320 3c8ce7d5 2022-07-11 op $time_cur += round($elapsed)
321 508e5f5f 2022-07-10 op if $status =~ m/^playing/;
324 63dcef30 2022-07-13 op getstatus unless $tick++ % 8;
326 508e5f5f 2022-07-10 op hinput if $poll->events(\*STDIN) & POLLIN;
327 508e5f5f 2022-07-10 op hevent $monitor if $poll->events($monitor) & POLLIN;
329 508e5f5f 2022-07-10 op getsongs if $LINES != $last_lines;
337 508e5f5f 2022-07-10 op kill 'INT', $mpid;
346 508e5f5f 2022-07-10 op amused-monitor - curses interface for amused(1)
348 508e5f5f 2022-07-10 op =head1 SYNOPSIS
350 508e5f5f 2022-07-10 op B<amused-monitor> [B<-p> I<playlist>] [B<-t> I<string>]
352 508e5f5f 2022-07-10 op =head1 DESCRIPTION
354 508e5f5f 2022-07-10 op amused-monitor is a simple curses interface for amused(1).
356 508e5f5f 2022-07-10 op The following options are available:
360 508e5f5f 2022-07-10 op =item B<-p> I<playlist>
362 508e5f5f 2022-07-10 op Save the current playling queue to the file I<playlist> upon exit or
365 508e5f5f 2022-07-10 op =item B<-t> I<string>
367 508e5f5f 2022-07-10 op Trim out the given I<string> from every song in the playlist view.
371 508e5f5f 2022-07-10 op The following key-bindings are available:
375 9c72efd4 2022-07-13 op =item backspace or C-h
377 9c72efd4 2022-07-13 op Seek back to the beginning of the track.
381 508e5f5f 2022-07-10 op Toggle play/pause.
385 508e5f5f 2022-07-10 op Play previous song.
389 508e5f5f 2022-07-10 op Play next song.
393 508e5f5f 2022-07-10 op Seek backward by five seconds.
397 508e5f5f 2022-07-10 op Seek forward by five seconds.
401 508e5f5f 2022-07-10 op Randomize the playlist.
405 508e5f5f 2022-07-10 op Sort the playlist.
409 508e5f5f 2022-07-10 op Save the status to the file given with the B<-p> flag.
417 508e5f5f 2022-07-10 op =head1 SEE ALSO