fix .ogg info update
[soepkiptng.git] / soepkiptng_console
blobf2f0fac6907cc2c9e2f9f7fd9174be1a56f58753
1 #!/usr/bin/perl
2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 # $Id$
5 ############################################################################
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License, version 2, as
8 # published by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # A copy of the GNU General Public License is available on the World Wide Web
16 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
17 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 ############################################################################
21 use integer;
22 use Cwd 'abs_path';
23 use Getopt::Std;
24 use IO::Socket;
25 use Term::ReadKey;
27 # find program directory
28 $_ = $0;
29 while(-l) {
30 my $l = readlink or die "readlink $_: $!\n";
31 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
33 m|(.*)/|;
34 my $progdir = abs_path($1);
36 require "$progdir/soepkiptng.lib";
38 getopts('c:');
40 read_configfile(\%conf, $opt_c);
42 while(!defined($sock = opensocket())) {
43 sleep 1;
46 ReadMode 3;
48 print "space=pause n=next\n";
50 $| = 1;
51 $song = -1;
52 for(;;) {
53 $sock->print("status\n");
54 $_ = <$sock>;
55 if(length == 0) {
56 while(!defined($sock = opensocket())) {
57 sleep 1;
59 $song = -1;
60 next;
62 /song=(\d+)/;
63 if($1 != $song) {
64 $song = $1;
65 printf " %-74s\r", songinfo();
67 /time=(\d+)/;
68 if($1 != $t) {
69 $t = $1;
70 printf "\r%2d:%02d\r", $t / 60, $t % 60;
73 # wait for 0.1s or until a key is pressed
74 while($_ = lc ReadKey 0.1) {
75 if($_ eq "n") {
76 kill_song();
77 } elsif($_ eq " ") {
78 $sock->print("pausetoggle\n");
83 sub songinfo()
85 local *F;
87 open F, $conf{statusfile} or return "unknown";
88 <F>; <F>; <F>; <F>; <F>; <F>; <F>; <F>;
89 chop (($ar, $t, $al, $tr, $len) = <F>);
90 close F;
92 return sprintf "/%d:%02d %.25s - %.25s (%s%.20s)",
93 $len / 60, $len % 60,
94 $ar, $t, $tr? "[$tr] ":"", $al;
97 sub opensocket()
99 my $sleeptime = 1000;
100 local *F;
102 for(;;) {
103 open F, $conf{statusfile} or do {
104 warn "$conf{statusfile}: $!\n";
105 return undef;
107 <F>; <F>; <F>; <F>;
108 chop(my $host = <F>);
109 close F;
111 $s = IO::Socket::INET->new("$host:2222") and last;
112 warn "$host:2222: $!\n";
113 sleep $sleeptime / 1000;
114 $sleeptime = $sleeptime * 11 / 10 unless $sleeptime > 60000;
116 $s->autoflush(1);
117 $response = <$s>; #discard greeting
118 return $s;