fix .ogg info update
[soepkiptng.git] / soepkiptng_eventd
blobcf90633eb20a2d4d685714aac286e64c99b0b1c2
1 #!/usr/bin/perl
3 ############################################################################
4 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
5 # $Id$
6 ############################################################################
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License, version 2, as
9 # published by the Free Software Foundation.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # A copy of the GNU General Public License is available on the World Wide Web
17 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
18 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20 ############################################################################
22 use integer;
23 use Cwd 'abs_path';
24 use Errno;
25 use Fcntl;
26 use Getopt::Std;
27 use IO::File;
28 use IO::Socket;
30 $opt_m = "$ENV{HOME}/.mplayer.fifo";
32 # find program directory
33 $_ = $0;
34 while(-l) {
35 my $l = readlink or die "readlink $_: $!\n";
36 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
38 m|(.*)/|;
39 my $progdir = abs_path($1);
41 require "$progdir/soepkiptng.lib";
43 getopts('c:dFm:');
45 read_configfile(\%conf, $opt_c);
47 if(!$opt_d) {
48 if(!$opt_F) {
49 fork && exit;
50 setpgrp;
52 chdir "/";
53 open STDIN, "</dev/null";
54 open STDOUT, ">/dev/null";
55 open STDERR, ">&STDOUT";
58 sub EV_KEY () {0x1;}
59 sub KEY_NEXTSONG () {163;}
60 sub KEY_PLAYPAUSE () {164;}
61 sub KEY_PREVIOUSSONG () {165;}
63 sub handle_event($) {
64 my ($event) = @_;
65 ($sec, $usec, $type, $code, $value) = unpack "llSSl", $event;
67 printf "%10d.%06d type=%d code=%d value%d\n", $sec, $usec, $type, $code, $value
68 if $opt_d;
70 next unless $type == EV_KEY && $value == 1;
71 printf "key %d\n", $code
72 if $opt_d;
74 if($code == KEY_NEXTSONG || $code == 90) {
75 print "kill_song();\n" if $opt_d;
76 kill_song();
77 } elsif($code == KEY_PLAYPAUSE || $code == 91) {
78 mplayer_cmd("pause") || pause_toggle();
79 } elsif($code == 116) {
80 mplayer_cmd("quit");
84 sub pause_toggle()
86 local *F;
88 print "pause_toggle()\n" if $opt_d;
89 open F, $conf{statusfile} or do {
90 warn "$conf{statusfile}: $!\n";
91 return undef;
93 <F>; <F>; <F>; <F>;
94 chop(my $host = <F>);
95 close F;
97 my $s = IO::Socket::INET->new("$host:2222") or do {
98 warn "$host:2222: $!\n";
99 return undef;
101 $s->autoflush(1);
102 <$s>; #discard greeting
103 $s->print("pausetoggle\n");
104 <$s>;
105 $s->close();
108 sub mplayer_cmd()
110 local *M;
111 sysopen M, $opt_m, O_WRONLY|O_NONBLOCK
112 or return undef;
113 fcntl M, F_SETFL, 0;
114 print M "$_[0]\n";
115 close M;
116 return 1;
119 sub check_nodes()
121 local *D;
123 if(opendir D, "/dev/input") {
124 foreach(grep { /^event\d+$/ } readdir D) {
125 next if $fd{$_};
126 if($fd{$_} = new IO::File "/dev/input/$_", "r") {
127 print "opened /dev/input/$_\n" if $opt_d;
128 $fd{$_}->blocking(0);
129 vec($rin,$fd{$_}->fileno(),1) = 1;
130 } else {
131 print "/dev/input/$_: $!\n" if $opt_d;
132 delete $fd{$_};
135 closedir D;
139 my $tcheck;
140 for(;;) {
141 my $rout;
143 if(time > $tcheck) {
144 check_nodes();
145 $tcheck = time + 10;
147 if(select($rout=$rin, undef, undef, 10)) {
148 foreach(keys %fd) {
149 if(vec($rout, $fd{$_}->fileno(),1)) {
150 my $event;
151 my $r = $fd{$_}->sysread($event, 16);
152 print "read $r bytes from $_\n" if $opt_d;
153 if($r == 16) {
154 handle_event($event);
155 } elsif(! $!{EAGAIN} && ! $!{EINTR}) {
156 $fd{$_}->close();
157 delete $fd{$_};