7 use Getopt::Long qw{:config gnu_compat bundling
8 require_order no_getopt_compat};
9 use POSIX qw{:errno_h :fcntl_h :sys_wait_h};
13 (my $PROG = $0) =~ s:^.*/::;
15 sub mumble ($) { print STDERR ";; $_[0]\n" if $DEBUG; }
18 my $file = $CONFFILE // "$ENV{HOME}/.disorder/passwd", my $conf;
20 if (-f $file || defined $CONFFILE)
21 { $conf = load_config $file; }
23 $conf = { connect => ["-unix", "/var/lib/disorder/private/socket"],
24 username => ["root"], password => ["hunoz"] };
26 return connect_to_server %$conf;
31 my $STATE = "off"; # `off', `on', `killed'
32 my $WANT = "off"; # `off', `on', `pause'
33 my $STOPTIME = undef; # if $WANT eq "pause"
38 my $kid = waitpid -1, WNOHANG;
41 mumble "player exited (st = $?)";
42 $PID = undef; $STATE = "off";
47 $SIG{TERM} = $SIG{INT} = sub {
48 kill "TERM", $PID if defined $PID;
52 sub start () { $WANT = "on"; $STOPTIME = undef; }
55 if ($WANT eq "on") { $WANT = "pause"; $STOPTIME = $NOW + $PAUSETIME; }
60 ##mumble "state = $STATE ($PID)";
61 ##mumble "want = $WANT ($STOPTIME <=> $NOW)";
63 if ($WANT eq "pause" && $NOW >= $STOPTIME) {
64 $WANT = "off"; $STOPTIME = undef;
65 mumble "pause time up: stopping" if $STATE eq "on";
68 if ($WANT eq "on" && $STATE eq "off") {
70 if (!$kid) { exec @ARGV; }
71 $STATE = "on"; $PID = $kid;
72 mumble "player wanted but not running: started pid $kid";
73 } elsif ($WANT eq "off" && $STATE eq "on") {
76 mumble "player running but not wanted: killed pid $PID";
83 my $pause, my $track = 0;
85 my $rdin = ""; vec($rdin, fileno($sk), 1) = 1;
86 my $buffer = "", my @lines = ();
90 my $r = send_command $sk, "playing";
94 fcntl $sk, F_SETFL, (fcntl $sk, F_GETFL, 0) | O_NONBLOCK;
97 if (!$sk) { mumble "eof from server"; last WATCH; }
101 $nfd = select my $rdout = $rdin, undef, undef,
102 defined($STOPTIME) ? $STOPTIME - $NOW : 60;
104 if ($@ && $@->errno == EINTR) { next SEL; }
105 elsif ($@) { mumble "error from select: " . $@->errno; last WATCH; }
108 eval { print $sk "."; flush $sk; };
109 if ($@) { mumble "error from write: " . $@->errno; last WATCH; }
114 eval { $n = sysread $sk, $b, 4096; };
115 if ($@ && $@->errno == EAGAIN) { last READ; }
116 elsif ($@ && $@->errno == EINTR) { next READ; }
117 elsif ($@) { mumble "error from read: " . $@->errno; last WATCH; }
118 elsif (!$n) { close $sk; $sk = undef; last READ; }
119 else { $buffer .= $b; }
122 @lines = split /\n/, $buffer, -1;
123 $buffer = pop @lines;
126 for my $line (@lines) {
127 my @f = split_fields $line;
128 if ($f[1] eq "state") {
129 if ($f[2] eq "pause") { mumble "paused"; $pause = 1; }
130 elsif ($f[2] eq "resume") { mumble "unpaused"; $pause = 0; }
131 } elsif ($f[1] eq "playing") { mumble "track started"; $track = 1; }
132 elsif ($f[1] eq "completed") { mumble "track finished"; $track = 0; }
137 if ($track && !$pause) { start; } else { pause; }
144 print $fh "usage: $PROG [-d] [-u CONFIG] [--] COMMAND ARGS...\n";
151 Command-line options:
152 -h, --help Show this help text
153 -u, --user-config Set user configuration file
159 "h|help" => sub { help; exit 0; },
160 "d|debug" => \$DEBUG,
161 "u|user-config=s" => \$CONFFILE
163 @ARGV > 0 or $bad = 1;
164 if ($bad) { usage *STDERR; exit 2; }
167 eval { watch_status; }; mumble "watcher exited: $@";