00001 #!/usr/bin/perl
00002 # mplayernetwork startup script by Mark Musone, mmusone at shatterit dot com
00003 # instructions by Robert Kulagowski, rkulagow at rocketmail dot com
00004 # To use, you must install Net::Server::Fork first
00005 # perl -MCPAN -e shell
00006 # cpan>install Net::Server::Fork
00007 # cpan>exit
00008 # This script is designed to be called from the mythexplorer-settings.txt
00009 # file.
00010 # To use this script, ensure that you have done a
00011 # chmod a+x mplayernetwork.pl and copy it to /usr/local/bin
00012 # Then, edit the mythexplorer-settings.txt file and replace any
00013 # /usr/local/bin/mplayer {options} with /usr/local/bin/mplayernetwork.pl %s
00014
00015 package MyPackage;
00016 use FileHandle;
00017 #use strict;
00018 # use vars qw(@ISA);
00019 use Net::Server::Fork; # any personality will do
00020
00021 $| = 1;
00022 sub CHLD_handler
00023 {
00024 print "got handler!\n";
00025 exit(0);
00026 }
00027
00028
00029 $SIG{'CHLD'} = 'CHLD_handler';
00030 $SIG{'PIPE'} = 'CHLD_handler';
00031
00032 $pid = open(WRITEME, "| /usr/local/bin/mplayer -nolirc -display :0.0 -slave '$ARGV[0]' ");
00033 #$pid = open(WRITEME, "| cat ");
00034 WRITEME->autoflush(1);
00035
00036 @ISA = qw(Net::Server::Fork);
00037 MyPackage->run();
00038 exit;
00039 ### over-ridden subs below
00040 sub process_request {
00041
00042 my $self = shift;
00043 eval {
00044 ## local $SIG{ALRM} = sub { die "Timed Out!\n" };
00045 local $SIG{PIPE} = sub { $self->server_close() };
00046 local $SIG{CHLD} = sub { $self->server_close() };
00047 my $timeout = 30; # give the user 30 seconds to type a line
00048 my $previous_alarm = alarm($timeout);
00049 WRITEME->autoflush(1);
00050 while( <STDIN> ){
00051 s/\r$
00052 $status = print WRITEME $_;
00053 if(!$status || $_ =~ /quit$/)
00054 {
00055 print "closing server\n";
00056 {
00057 close(WRITEME);
00058 system("killall mplayer");
00059 $self->server_close();
00060 }
00061 }
00062 return;
00063 alarm($timeout);
00064 }
00065 alarm($previous_alarm);
00066 };
00067 if( $@=~/timed out/i ){
00068 print STDOUT "Timed Out.\r\n";
00069 return;
00070 }
00071 }
00072
00073 1;