| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Audio::MPD q{0.19.0}; |
| 5 |
use Getopt::Long; |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
sats - mpd stop after this song |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
sats [-d] [-n num] [host] |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
B<sats> is an acronym for Stop After This Song. It will wait for playback |
| 18 |
of the currently playing song to finish, and then tell mpd to stop playing. |
| 19 |
|
| 20 |
If the hostname is omitted, the MPD_HOST environment variable will be used. |
| 21 |
|
| 22 |
=head1 OPTIONS |
| 23 |
|
| 24 |
=over 4 |
| 25 |
|
| 26 |
=item -d |
| 27 |
|
| 28 |
Daemonize rather than waiting in the foreground for the song to stop playing. |
| 29 |
|
| 30 |
=item -n num |
| 31 |
|
| 32 |
Stop after B<num> songs (default is 1). |
| 33 |
|
| 34 |
=back |
| 35 |
|
| 36 |
=head1 AUTHOR |
| 37 |
|
| 38 |
Copyright 2007 Joey Hess <[email protected]> |
| 39 |
|
| 40 |
Licensed under the GNU GPL version 2 or higher. |
| 41 |
|
| 42 |
http://kitenet.net/~joey/code/mpdtoys |
| 43 |
|
| 44 |
=cut |
| 45 |
|
| 46 |
my $num=1; |
| 47 |
my $daemon=0; |
| 48 |
GetOptions( |
| 49 |
"n=i" => \$num, |
| 50 |
"d" => \$daemon, |
| 51 |
) || usage(); |
| 52 |
|
| 53 |
sub usage { |
| 54 |
die "Usage: sats [-d] [-n num] [host]\n"; |
| 55 |
} |
| 56 |
|
| 57 |
if (@ARGV) { |
| 58 |
$ENV{MPD_HOST}=shift; |
| 59 |
} |
| 60 |
|
| 61 |
my $mpd=Audio::MPD->new(conntype => "reuse"); |
| 62 |
|
| 63 |
my $song=$mpd->current; |
| 64 |
if ($mpd->status->state ne 'play') { |
| 65 |
die "no song is currently playing\n"; |
| 66 |
} |
| 67 |
|
| 68 |
if ($daemon) { |
| 69 |
eval q{use Proc::Daemon}; |
| 70 |
Proc::Daemon::Init(); |
| 71 |
# daemonising closed the connection to mpd |
| 72 |
$mpd=Audio::MPD->new(conntype => "reuse"); |
| 73 |
} |
| 74 |
|
| 75 |
# Polling is evil, it could look at the seek position, and sleep until the |
| 76 |
# end. But that would break if something seeked in the song.. |
| 77 |
|
| 78 |
while (sleep 1) { |
| 79 |
last if $mpd->status->state ne 'play'; |
| 80 |
my $current=$mpd->current; |
| 81 |
if ($current->id != $song->id) { |
| 82 |
$num--; |
| 83 |
if ($num == 0) { |
| 84 |
$mpd->stop; |
| 85 |
exit; |
| 86 |
} |
| 87 |
$song=$current; |
| 88 |
} |
| 89 |
} |