File: sats

package info (click to toggle)
mpdtoys 0.25.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 104 kB
  • sloc: perl: 844; sh: 58; makefile: 19
file content (89 lines) | stat: -rwxr-xr-x 1,583 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl
use strict;
use warnings;
use Audio::MPD q{0.19.0};
use Getopt::Long;

=head1 NAME

sats - mpd stop after this song

=head1 SYNOPSIS

sats [-d] [-n num] [host]

=head1 DESCRIPTION

B<sats> is an acronym for Stop After This Song. It will wait for playback
of the currently playing song to finish, and then tell mpd to stop playing.

If the hostname is omitted, the MPD_HOST environment variable will be used.

=head1 OPTIONS

=over 4

=item -d

Daemonize rather than waiting in the foreground for the song to stop playing.

=item -n num

Stop after B<num> songs (default is 1).

=back

=head1 AUTHOR

Copyright 2007 Joey Hess <joey@kitenet.net>

Licensed under the GNU GPL version 2 or higher.

http://kitenet.net/~joey/code/mpdtoys

=cut

my $num=1;
my $daemon=0;
GetOptions(
	"n=i" => \$num,
	"d" => \$daemon,
) || usage();

sub usage {
	die "Usage: sats [-d] [-n num] [host]\n";
}

if (@ARGV) {
	$ENV{MPD_HOST}=shift;
}

my $mpd=Audio::MPD->new(conntype => "reuse");

my $song=$mpd->current;
if ($mpd->status->state ne 'play') {
	die "no song is currently playing\n";
}

if ($daemon) {
	eval q{use Proc::Daemon};
	Proc::Daemon::Init();
	# daemonising closed the connection to mpd
	$mpd=Audio::MPD->new(conntype => "reuse");
}

# Polling is evil, it could look at the seek position, and sleep until the
# end. But that would break if something seeked in the song..

while (sleep 1) {
	last if $mpd->status->state ne 'play';
	my $current=$mpd->current;
	if ($current->id != $song->id) {
		$num--;
		if ($num == 0) {
			$mpd->stop;
			exit;
		}
		$song=$current;
	}
}