script to copy all songs from a playlist to an mp3 player
[soepkiptng.git] / soepkiptng_eventd
blob60ed06ee8fdb02c8a875d7976e395da6ad3586e8
1 #!/usr/bin/perl
3 ############################################################################
4 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
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 Errno;
24 use Fcntl;
25 use Getopt::Std;
26 use IO::File;
27 use IO::Socket;
29 $opt_m = "$ENV{HOME}/.mplayer.fifo";
31 # find program directory
32 $_ = $0;
33 while(-l) {
34 my $l = readlink or die "readlink $_: $!\n";
35 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
37 m|(.*)/|;
38 my $progdir = abs_path($1);
40 require "$progdir/soepkiptng.lib";
42 getopts('c:dFm:');
44 read_configfile(\%conf, $opt_c);
46 if(!$opt_d) {
47 if(!$opt_F) {
48 fork && exit;
49 setpgrp;
51 chdir "/";
52 open STDIN, "</dev/null";
53 open STDOUT, ">/dev/null";
54 open STDERR, ">&STDOUT";
57 sub EV_KEY () {0x1;}
58 sub KEY_NEXTSONG () {163;}
59 sub KEY_PLAYPAUSE () {164;}
60 sub KEY_PREVIOUSSONG () {165;}
62 sub handle_event($) {
63 my ($event) = @_;
64 ($sec, $usec, $type, $code, $value) = unpack "llSSl", $event;
66 printf "%10d.%06d type=%d code=%d value%d\n", $sec, $usec, $type, $code, $value
67 if $opt_d;
69 next unless $type == EV_KEY && $value == 1;
70 printf "key %d\n", $code
71 if $opt_d;
73 if($code == KEY_NEXTSONG || $code == 90) {
74 print "kill_song();\n" if $opt_d;
75 kill_song();
76 } elsif($code == KEY_PLAYPAUSE || $code == 91) {
77 mplayer_cmd("pause") || pause_toggle();
78 } elsif($code == 116) {
79 mplayer_cmd("quit");
83 sub pause_toggle()
85 local *F;
87 print "pause_toggle()\n" if $opt_d;
88 open F, $conf{statusfile} or do {
89 warn "$conf{statusfile}: $!\n";
90 return undef;
92 <F>; <F>; <F>; <F>;
93 chop(my $host = <F>);
94 close F;
96 my $s = IO::Socket::INET->new("$host:2222") or do {
97 warn "$host:2222: $!\n";
98 return undef;
100 $s->autoflush(1);
101 <$s>; #discard greeting
102 $s->print("pausetoggle\n");
103 <$s>;
104 $s->close();
107 sub mplayer_cmd()
109 local *M;
110 sysopen M, $opt_m, O_WRONLY|O_NONBLOCK
111 or return undef;
112 fcntl M, F_SETFL, 0;
113 print M "$_[0]\n";
114 close M;
115 return 1;
118 sub check_nodes()
120 local *D;
122 if(opendir D, "/dev/input") {
123 foreach(grep { /^event\d+$/ } readdir D) {
124 next if $fd{$_};
125 if($fd{$_} = new IO::File "/dev/input/$_", "r") {
126 print "opened /dev/input/$_\n" if $opt_d;
127 $fd{$_}->blocking(0);
128 vec($rin,$fd{$_}->fileno(),1) = 1;
129 } else {
130 print "/dev/input/$_: $!\n" if $opt_d;
131 delete $fd{$_};
134 closedir D;
138 my $tcheck;
139 for(;;) {
140 my $rout;
142 if(time > $tcheck) {
143 check_nodes();
144 $tcheck = time + 10;
146 if(select($rout=$rin, undef, undef, 10)) {
147 foreach(keys %fd) {
148 if(vec($rout, $fd{$_}->fileno(),1)) {
149 my $event;
150 my $r = $fd{$_}->sysread($event, 16);
151 print "read $r bytes from $_\n" if $opt_d;
152 if($r == 16) {
153 handle_event($event);
154 } elsif(! $!{EAGAIN} && ! $!{EINTR}) {
155 $fd{$_}->close();
156 delete $fd{$_};