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.
21 7945ad34 2023-07-02 op use open ":std", ":encoding(UTF-8)";
25 7945ad34 2023-07-02 op use POSIX qw(:sys_wait_h setlocale LC_ALL);
26 508e5f5f 2022-07-10 op use Text::CharWidth qw(mbswidth);
27 508e5f5f 2022-07-10 op use IO::Poll qw(POLLIN);
28 508e5f5f 2022-07-10 op use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
29 508e5f5f 2022-07-10 op use Getopt::Long qw(:config bundling);
30 508e5f5f 2022-07-10 op use Pod::Usage;
35 508e5f5f 2022-07-10 op my $trim = "";
37 508e5f5f 2022-07-10 op my $pair_n = 1;
40 e9c1a558 2023-01-09 op my $current_song;
41 508e5f5f 2022-07-10 op my $playlist_cur;
42 508e5f5f 2022-07-10 op my $playlist_max;
48 508e5f5f 2022-07-10 op my $last_lines;
51 3c8ce7d5 2022-07-11 op return int(0.5 + shift);
55 508e5f5f 2022-07-10 op my ($a, $b) = @_;
56 508e5f5f 2022-07-10 op return $a > $b ? $a : $b;
60 3c8ce7d5 2022-07-11 op my $lines = shift;
62 42426670 2022-07-11 op my ($n, $idx, $cur) = (0, 0, -1);
64 42426670 2022-07-11 op open (my $fh, "-|", "amused", "show", "-p");
65 508e5f5f 2022-07-10 op while (<$fh>) {
68 42426670 2022-07-11 op $tmp[$idx] = $_;
72 e9c1a558 2023-01-09 op $current_song = s/^> //r;
76 42426670 2022-07-11 op $idx = ++$idx % $lines;
78 83438dc0 2022-07-13 op last if $cur != -1 && $n - $cur > int($lines/2) &&
79 42426670 2022-07-11 op $#tmp == $lines-1;
83 42426670 2022-07-11 op return ("Empty playlist.") unless @tmp;
85 42426670 2022-07-11 op # reorder the entries
87 42426670 2022-07-11 op my $len = $#tmp + 1;
88 6045cf87 2022-07-13 op $idx = $idx % $len;
89 42426670 2022-07-11 op for (1..$len) {
90 42426670 2022-07-11 op push @r, $tmp[$idx];
91 42426670 2022-07-11 op $idx = ++$idx % $len;
96 508e5f5f 2022-07-10 op sub playlist_numbers {
97 508e5f5f 2022-07-10 op my ($cur, $tot, $found) = (0, 0, 0);
98 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused", "show", "-p");
99 508e5f5f 2022-07-10 op while (<$fh>) {
101 508e5f5f 2022-07-10 op $cur++ unless $found;
102 508e5f5f 2022-07-10 op $found = 1 if m/^>/;
105 508e5f5f 2022-07-10 op return ($cur, $tot);
109 63dcef30 2022-07-13 op my ($pos, $dur, $mode);
111 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused", "status", "-f",
112 63dcef30 2022-07-13 op "status,time:raw,mode:oneline");
114 e9c1a558 2023-01-09 op <$fh> =~ m/([a-z]+) (.*)/;
115 e9c1a558 2023-01-09 op my ($status, $current_song) = ($1, $2);
117 508e5f5f 2022-07-10 op while (<$fh>) {
119 508e5f5f 2022-07-10 op $pos = s/position //r if m/^position /;
120 508e5f5f 2022-07-10 op $dur = s/duration //r if m/^duration /;
121 63dcef30 2022-07-13 op $mode = $_ if m/^repeat/;
124 e9c1a558 2023-01-09 op return ($status, $current_song, $pos, $dur, $mode);
127 508e5f5f 2022-07-10 op sub showtime {
128 508e5f5f 2022-07-10 op my $seconds = shift;
129 508e5f5f 2022-07-10 op my $str = "";
131 508e5f5f 2022-07-10 op if ($seconds > 3600) {
132 508e5f5f 2022-07-10 op my $hours = int($seconds / 3600);
133 508e5f5f 2022-07-10 op $seconds -= $hours * 3600;
134 86d7c366 2022-08-23 op $str = sprintf("%02d:", $hours);
137 508e5f5f 2022-07-10 op my $minutes = int($seconds / 60);
138 508e5f5f 2022-07-10 op $seconds -= $minutes * 60;
139 508e5f5f 2022-07-10 op $str .= sprintf "%02d:%02d", $minutes, $seconds;
144 508e5f5f 2022-07-10 op my ($str, $pstr) = @_;
145 508e5f5f 2022-07-10 op my $width = mbswidth($str);
146 508e5f5f 2022-07-10 op return $str if $width > $COLS;
147 1b135dc5 2022-07-26 op my $pre = round(($COLS - $width) / 2);
148 508e5f5f 2022-07-10 op my $lpad = $pstr x $pre;
149 1b135dc5 2022-07-26 op my $rpad = $pstr x ($COLS - $width - $pre);
150 508e5f5f 2022-07-10 op return ($lpad, $str, $rpad);
153 508e5f5f 2022-07-10 op sub offsets {
154 508e5f5f 2022-07-10 op my ($y, $x, $cur, $max) = @_;
155 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center(" $cur / $max ", '-');
156 508e5f5f 2022-07-10 op addstring $y, $x, "";
158 508e5f5f 2022-07-10 op my $p = COLOR_PAIR($pair_n);
161 508e5f5f 2022-07-10 op addstring $pre;
164 508e5f5f 2022-07-10 op addstring $c;
167 508e5f5f 2022-07-10 op addstring $post;
171 508e5f5f 2022-07-10 op sub progress {
172 508e5f5f 2022-07-10 op my ($y, $x, $pos, $dur) = @_;
174 508e5f5f 2022-07-10 op my $pstr = showtime $pos;
175 508e5f5f 2022-07-10 op my $dstr = showtime $dur;
177 508e5f5f 2022-07-10 op my $len = $COLS - length($pstr) - length($dstr) - 4;
178 127f376a 2022-07-11 op return if $len <= 0 or $dur <= 0;
179 3c8ce7d5 2022-07-11 op my $filled = round($pos * $len / $dur);
181 508e5f5f 2022-07-10 op addstring $y, $x, "$pstr [";
182 508e5f5f 2022-07-10 op addstring "#" x $filled;
183 508e5f5f 2022-07-10 op addstring " " x max($len - $filled, 0);
184 508e5f5f 2022-07-10 op addstring "] $dstr";
187 508e5f5f 2022-07-10 op sub show_status {
188 508e5f5f 2022-07-10 op my ($y, $x, $status) = @_;
189 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center($status, ' ');
190 508e5f5f 2022-07-10 op addstring $y, $x, $pre;
191 508e5f5f 2022-07-10 op addstring $c;
192 508e5f5f 2022-07-10 op addstring $post;
195 63dcef30 2022-07-13 op sub show_mode {
196 63dcef30 2022-07-13 op my ($y, $x, $mode) = @_;
197 63dcef30 2022-07-13 op my ($pre, $c, $post) = center($mode, ' ');
198 508e5f5f 2022-07-10 op addstring $y, $x, $pre;
199 508e5f5f 2022-07-10 op addstring $c;
200 508e5f5f 2022-07-10 op addstring $post;
205 508e5f5f 2022-07-10 op if ($LINES < 4 || $COLS < 20) {
206 508e5f5f 2022-07-10 op addstring "window too small";
211 508e5f5f 2022-07-10 op my $song_pad = "";
212 508e5f5f 2022-07-10 op my $longest = 0;
213 508e5f5f 2022-07-10 op $longest = max $longest, length($_) foreach @songs;
214 508e5f5f 2022-07-10 op if ($longest < $COLS) {
215 508e5f5f 2022-07-10 op $song_pad = " " x (($COLS - $longest)/2);
218 508e5f5f 2022-07-10 op my $line = 0;
220 508e5f5f 2022-07-10 op attron(A_BOLD) if m/^>/;
221 508e5f5f 2022-07-10 op addstring $line++, 0, $song_pad . $_;
225 508e5f5f 2022-07-10 op offsets $LINES - 4, 0, $playlist_cur, $playlist_max;
226 508e5f5f 2022-07-10 op progress $LINES - 3, 0, $time_cur, $time_dur;
227 e9c1a558 2023-01-09 op show_status $LINES - 2, 0, "$status $current_song";
228 63dcef30 2022-07-13 op show_mode $LINES - 1, 0, $mode;
233 508e5f5f 2022-07-10 op sub getsongs {
234 508e5f5f 2022-07-10 op $last_lines = $LINES;
235 42426670 2022-07-11 op @songs = excerpt $LINES - 4;
238 508e5f5f 2022-07-10 op sub getnums {
239 508e5f5f 2022-07-10 op ($playlist_cur, $playlist_max) = playlist_numbers;
243 508e5f5f 2022-07-10 op return unless defined $pfile;
245 508e5f5f 2022-07-10 op open(my $fh, ">", $pfile);
246 508e5f5f 2022-07-10 op open(my $ph, "-|", "amused", "show", "-p");
248 508e5f5f 2022-07-10 op print $fh $_ while (<$ph>);
252 508e5f5f 2022-07-10 op my $fh = shift;
253 508e5f5f 2022-07-10 op my $l = <$fh>;
254 508e5f5f 2022-07-10 op die "monitor quit" unless defined($l);
256 e9c1a558 2023-01-09 op $status = "playing" if $l =~ m/^play/;
257 e9c1a558 2023-01-09 op $status = "paused" if $l =~ m/^pause/;
258 e9c1a558 2023-01-09 op $status = "stopped" if $l =~ m/^stop/;
260 e9c1a558 2023-01-09 op ($time_cur, $time_dur) = ($1, $2) if $l =~ m/^seek (\d+) (\d+)/;
262 e9c1a558 2023-01-09 op $mode = $1 if $l =~ m/^mode (.*)/;
264 508e5f5f 2022-07-10 op getnums if $l =~ m/load|jump|next|prev/;
265 508e5f5f 2022-07-10 op getsongs if $l =~ m/load|jump|next|prev/;
269 508e5f5f 2022-07-10 op my ($ch, $key) = getchar;
270 9c72efd4 2022-07-13 op if (defined $key) {
271 9c72efd4 2022-07-13 op if ($key == KEY_BACKSPACE) {
272 3fc73666 2022-07-26 op system "amused", "seek", "0";
274 9c72efd4 2022-07-13 op } elsif (defined $ch) {
275 508e5f5f 2022-07-10 op if ($ch eq " ") {
276 508e5f5f 2022-07-10 op system "amused", "toggle";
277 508e5f5f 2022-07-10 op } elsif ($ch eq "<" or $ch eq "p") {
278 508e5f5f 2022-07-10 op system "amused", "prev";
279 508e5f5f 2022-07-10 op } elsif ($ch eq ">" or $ch eq "n") {
280 508e5f5f 2022-07-10 op system "amused", "next";
281 508e5f5f 2022-07-10 op } elsif ($ch eq ",") {
282 508e5f5f 2022-07-10 op system "amused", "seek", "-5";
283 508e5f5f 2022-07-10 op } elsif ($ch eq ".") {
284 508e5f5f 2022-07-10 op system "amused", "seek", "+5";
285 508e5f5f 2022-07-10 op } elsif ($ch eq "S") {
286 508e5f5f 2022-07-10 op system "amused show | sort -u | amused load";
287 508e5f5f 2022-07-10 op } elsif ($ch eq "R") {
288 508e5f5f 2022-07-10 op system "amused show | sort -R | amused load";
289 508e5f5f 2022-07-10 op } elsif ($ch eq "s") {
291 508e5f5f 2022-07-10 op } elsif ($ch eq "q") {
293 9c72efd4 2022-07-13 op } elsif ($ch eq "\cH") {
294 3fc73666 2022-07-26 op system "amused", "seek", "0"
300 508e5f5f 2022-07-10 op "p:s" => \$pfile,
301 508e5f5f 2022-07-10 op "t:s" => \$trim,
302 508e5f5f 2022-07-10 op ) or pod2usage(1);
304 87abdf60 2022-07-12 op my $mpid = open(my $monitor, "-|", "amused", "monitor")
305 508e5f5f 2022-07-10 op or die "can't spawn amused monitor";
307 7945ad34 2023-07-02 op setlocale(LC_ALL, "");
310 508e5f5f 2022-07-10 op use_default_colors;
311 508e5f5f 2022-07-10 op init_pair $pair_n, 250, -1;
313 508e5f5f 2022-07-10 op timeout 1000;
318 508e5f5f 2022-07-10 op my $poll = IO::Poll->new();
319 508e5f5f 2022-07-10 op $poll->mask(\*STDIN => POLLIN);
320 508e5f5f 2022-07-10 op $poll->mask($monitor => POLLIN);
322 936bd534 2022-08-13 op if (`uname` =~ "OpenBSD") {
323 936bd534 2022-08-13 op use OpenBSD::Pledge;
324 936bd534 2022-08-13 op use OpenBSD::Unveil;
326 936bd534 2022-08-13 op my $prog = `which amused`;
329 936bd534 2022-08-13 op unveil($prog, 'rx') or die "unveil $prog: $!";
330 936bd534 2022-08-13 op if (defined($pfile)) {
331 936bd534 2022-08-13 op unveil($pfile, 'wc') or die "unveil $pfile: $!";
332 936bd534 2022-08-13 op pledge qw(stdio wpath cpath tty proc exec) or die "pledge: $!";
334 936bd534 2022-08-13 op pledge qw(stdio tty proc exec) or die "pledge: $!";
340 e9c1a558 2023-01-09 op ($status, $current_song, $time_cur, $time_dur, $mode) = status;
343 3a0b3cb8 2022-07-10 op while ($run) {
344 e9c1a558 2023-01-09 op $poll->poll();
345 508e5f5f 2022-07-10 op hinput if $poll->events(\*STDIN) & POLLIN;
346 508e5f5f 2022-07-10 op hevent $monitor if $poll->events($monitor) & POLLIN;
348 508e5f5f 2022-07-10 op getsongs if $LINES != $last_lines;
356 508e5f5f 2022-07-10 op kill 'INT', $mpid;
365 508e5f5f 2022-07-10 op amused-monitor - curses interface for amused(1)
367 508e5f5f 2022-07-10 op =head1 SYNOPSIS
369 508e5f5f 2022-07-10 op B<amused-monitor> [B<-p> I<playlist>] [B<-t> I<string>]
371 508e5f5f 2022-07-10 op =head1 DESCRIPTION
373 508e5f5f 2022-07-10 op amused-monitor is a simple curses interface for amused(1).
375 508e5f5f 2022-07-10 op The following options are available:
379 508e5f5f 2022-07-10 op =item B<-p> I<playlist>
381 508e5f5f 2022-07-10 op Save the current playling queue to the file I<playlist> upon exit or
384 508e5f5f 2022-07-10 op =item B<-t> I<string>
386 508e5f5f 2022-07-10 op Trim out the given I<string> from every song in the playlist view.
390 508e5f5f 2022-07-10 op The following key-bindings are available:
394 9c72efd4 2022-07-13 op =item backspace or C-h
396 9c72efd4 2022-07-13 op Seek back to the beginning of the track.
400 508e5f5f 2022-07-10 op Toggle play/pause.
404 508e5f5f 2022-07-10 op Play previous song.
408 508e5f5f 2022-07-10 op Play next song.
412 508e5f5f 2022-07-10 op Seek backward by five seconds.
416 508e5f5f 2022-07-10 op Seek forward by five seconds.
420 508e5f5f 2022-07-10 op Randomize the playlist.
424 508e5f5f 2022-07-10 op Sort the playlist.
428 508e5f5f 2022-07-10 op Save the status to the file given with the B<-p> flag.
436 508e5f5f 2022-07-10 op =head1 SEE ALSO