]> git.draconx.ca Git - fvwmconf.git/blob - scripts/mpdexec.pl
mpdmenu: Use a specialized script to send playid commands to MPD.
[fvwmconf.git] / scripts / mpdexec.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright © 2012 Nick Bowler
4 #
5 # Simple program to send a command to MPD.  The result is printed to standard
6 # output.
7 #
8 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
9 # This is free software: you are free to do what the fuck you want to.
10 # There is NO WARRANTY, to the extent permitted by law.
11
12 use strict;
13
14 use utf8;
15 use encoding 'utf8';
16 use IO::Socket::INET6;
17
18 my $host = $ENV{MPD_HOST} // "localhost";
19 my $port = $ENV{MPD_PORT} // 6600;
20
21 my $sock = new IO::Socket::INET6(
22         PeerAddr => $host,
23         PeerPort => $port,
24         Proto    => 'tcp',
25 ) or die "failed to connect to MPD: $!";
26 binmode($sock, ":utf8");
27
28 if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
29         die "MPD failed to announce version: $!";
30 }
31
32 print $sock join(' ', @ARGV), "\n";
33 while (<$sock>) {
34         last if (/^OK/);
35         print;
36         exit 1 if (/^ACK/);
37 }
38
39 print $sock "close\n";
40 close $sock;