decode_argv(Encode::FB_CROAK);
binmode(STDOUT, ":utf8");
-use IO::Socket::INET6;
-use IO::Socket::UNIX;
use Getopt::Long qw(:config gnu_getopt);
use lib "$FindBin::Bin";
use MPDHacks;
-my $host = $ENV{MPD_HOST} // "localhost";
-my $port = $ENV{MPD_PORT} // 6600;
my $sock;
-# Submit a command to the MPD server; each argument to this function
-# is quoted and sent as a single argument to MPD.
-sub mpd_exec {
- my $cmd = join(' ', map { MPD::escape } @_);
-
- print $sock "$cmd\n";
-}
-
# Returns a hash reference containing all tracks in the current play queue.
# The hash keys are filenames.
sub get_tracks_in_play_queue {
my %matches;
my $entry;
- mpd_exec("playlistinfo");
+ MPD::exec("playlistinfo");
while (<$sock>) {
last if /^OK/;
die($_) if /^ACK/;
my ($plname) = @_;
my @files;
- mpd_exec("listplaylist", $plname);
+ MPD::exec("listplaylist", $plname);
while (<$sock>) {
last if /^OK/;
die($_) if /^ACK/;
}
sub print_usage {
- my $fh = $_[1] // *STDERR;
+ my ($fh) = (@_, *STDERR);
print $fh "Usage: $0 [options] playlist\n";
- print "Try $0 --help for more information.\n" unless (@_ > 0);
+ print $fh "Try $0 --help for more information.\n" unless (@_ > 0);
}
sub print_help {
}
GetOptions(
- 'host|h=s' => \$host,
- 'port|p=s' => \$port,
+ 'host|h=s' => \$MPD::host,
+ 'port|p=s' => \$MPD::port,
'V|version' => sub { print_version(); exit },
'H|help' => sub { print_help(); exit },
print_usage(); exit 1
};
-# Connect to MPD.
-if ($host =~ /^[@\/]/) {
- $host =~ s/^@/\0/;
- $sock = new IO::Socket::UNIX(Type => SOCK_STREAM(), Peer => $host);
-} else {
- $sock = new IO::Socket::INET6(PeerAddr => $host,
- PeerPort => $port,
- Proto => 'tcp');
-}
-$sock or die "failed to connect to MPD: $!";
-binmode($sock, ":utf8");
-
-if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
- die "MPD failed to announce version: $!";
-}
+$sock = MPD::connect();
# Retrieve the current play queue and target play queue.
my $current = get_tracks_in_play_queue();
my $target = get_playlist_files($ARGV[0]);
-mpd_exec("command_list_begin");
+MPD::exec("command_list_begin");
for (my $i = 0; $i < @$target; $i++) {
my $f = $target->[$i];
my $ids = $current->{$f}->{Id};
delete $current->{$f} unless (keys %$ids > 0);
if (defined $id) {
- mpd_exec("moveid", $id, $i);
+ MPD::exec("moveid", $id, $i);
} else {
- mpd_exec("addid", $f, $i);
+ MPD::exec("addid", $f, $i);
}
}
foreach (keys %$current) {
my $ids = $current->{$_}->{Id};
foreach (keys %$ids) {
- mpd_exec("deleteid", $_);
+ MPD::exec("deleteid", $_);
}
}
-mpd_exec("command_list_end");
+MPD::exec("command_list_end");
while (<$sock>) {
last if /^OK$/;
die($_) if /^ACK/;