3 # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 use POSIX ":sys_wait_h";
23 use Text::CharWidth qw(mbswidth);
24 use IO::Poll qw(POLLIN);
25 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
26 use Getopt::Long qw(:config bundling);
47 return int(0.5 + shift);
52 return $a > $b ? $a : $b;
58 my ($n, $idx, $cur) = (0, 0, -1);
60 open (my $fh, "-|", "amused", "show", "-p");
67 $idx = ++$idx % $lines;
69 last if $cur != -1 && $n - $cur > round($lines/2) &&
74 return ("Empty playlist.") unless @tmp;
86 sub playlist_numbers {
87 my ($cur, $tot, $found) = (0, 0, 0);
88 open (my $fh, "-|", "amused", "show", "-p");
99 my ($pos, $dur, $repeat);
101 open (my $fh, "-|", "amused", "status", "-f",
102 "status,time:raw,repeat:oneline");
107 $pos = s/position //r if m/^position /;
108 $dur = s/duration //r if m/^duration /;
109 $repeat = $_ if m/^repeat/;
112 return ($status, $pos, $dur, $repeat);
119 if ($seconds > 3600) {
120 my $hours = int($seconds / 3600);
121 $seconds -= $hours * 3600;
122 $str = sprintf("%02:", $hours);
125 my $minutes = int($seconds / 60);
126 $seconds -= $minutes * 60;
127 $str .= sprintf "%02d:%02d", $minutes, $seconds;
132 my ($str, $pstr) = @_;
133 my $width = mbswidth($str);
134 return $str if $width > $COLS;
135 my $pre = ($COLS - $width) / 2;
136 my $lpad = $pstr x $pre;
137 my $rpad = $pstr x ($COLS - $width - $pre + 1);
138 return ($lpad, $str, $rpad);
142 my ($y, $x, $cur, $max) = @_;
143 my ($pre, $c, $post) = center(" $cur / $max ", '-');
144 addstring $y, $x, "";
146 my $p = COLOR_PAIR($pair_n);
160 my ($y, $x, $pos, $dur) = @_;
162 my $pstr = showtime $pos;
163 my $dstr = showtime $dur;
165 my $len = $COLS - length($pstr) - length($dstr) - 4;
166 return if $len <= 0 or $dur <= 0;
167 my $filled = round($pos * $len / $dur);
169 addstring $y, $x, "$pstr [";
170 addstring "#" x $filled;
171 addstring " " x max($len - $filled, 0);
176 my ($y, $x, $status) = @_;
177 my ($pre, $c, $post) = center($status, ' ');
178 addstring $y, $x, $pre;
184 my ($y, $x, $repeat) = @_;
185 my ($pre, $c, $post) = center($repeat, ' ');
186 addstring $y, $x, $pre;
193 if ($LINES < 4 || $COLS < 20) {
194 addstring "window too small";
201 $longest = max $longest, length($_) foreach @songs;
202 if ($longest < $COLS) {
203 $song_pad = " " x (($COLS - $longest)/2);
208 attron(A_BOLD) if m/^>/;
209 addstring $line++, 0, $song_pad . $_;
213 offsets $LINES - 4, 0, $playlist_cur, $playlist_max;
214 progress $LINES - 3, 0, $time_cur, $time_dur;
215 show_status $LINES - 2, 0, $status;
216 show_repeat $LINES - 1, 0, $repeat;
222 $last_lines = $LINES;
223 @songs = excerpt $LINES - 4;
227 ($playlist_cur, $playlist_max) = playlist_numbers;
231 ($status, $time_cur, $time_dur, $repeat) = status;
235 return unless defined $pfile;
237 open(my $fh, ">", $pfile);
238 open(my $ph, "-|", "amused", "show", "-p");
240 print $fh $_ while (<$ph>);
246 die "monitor quit" unless defined($l);
248 getnums if $l =~ m/load|jump|next|prev/;
249 getsongs if $l =~ m/load|jump|next|prev/;
253 my ($ch, $key) = getchar;
256 system "amused", "toggle";
257 } elsif ($ch eq "<" or $ch eq "p") {
258 system "amused", "prev";
259 } elsif ($ch eq ">" or $ch eq "n") {
260 system "amused", "next";
261 } elsif ($ch eq ",") {
262 system "amused", "seek", "-5";
263 } elsif ($ch eq ".") {
264 system "amused", "seek", "+5";
265 } elsif ($ch eq "S") {
266 system "amused show | sort -u | amused load";
267 } elsif ($ch eq "R") {
268 system "amused show | sort -R | amused load";
269 } elsif ($ch eq "s") {
271 } elsif ($ch eq "q") {
274 } elsif (defined $key) {
284 my $mpid = open(my $monitor, "-|", "amused", "monitor")
285 or die "can't spawn amused monitor";
290 init_pair $pair_n, 250, -1;
296 my $poll = IO::Poll->new();
297 $poll->mask(\*STDIN => POLLIN);
298 $poll->mask($monitor => POLLIN);
301 my $tbefore = clock_gettime(CLOCK_MONOTONIC);
310 my $now = clock_gettime(CLOCK_MONOTONIC);
311 my $elapsed = $now - $tbefore;
314 $time_cur += round($elapsed)
315 if $status =~ m/^playing/;
318 ($status, $time_cur, $time_dur, $repeat) = status
321 hinput if $poll->events(\*STDIN) & POLLIN;
322 hevent $monitor if $poll->events($monitor) & POLLIN;
324 getsongs if $LINES != $last_lines;
341 amused-monitor - curses interface for amused(1)
345 B<amused-monitor> [B<-p> I<playlist>] [B<-t> I<string>]
349 amused-monitor is a simple curses interface for amused(1).
351 The following options are available:
355 =item B<-p> I<playlist>
357 Save the current playling queue to the file I<playlist> upon exit or
360 =item B<-t> I<string>
362 Trim out the given I<string> from every song in the playlist view.
366 The following key-bindings are available:
384 Seek backward by five seconds.
388 Seek forward by five seconds.
392 Randomize the playlist.
400 Save the status to the file given with the B<-p> flag.