update Perl sample scripts

This commit is contained in:
marschap
2006-04-30 17:13:44 +00:00
parent 7494d6cfc8
commit ee6e26e934
4 changed files with 693 additions and 177 deletions
+176 -28
View File
@@ -9,6 +9,7 @@
# 2001, David Glaude # 2001, David Glaude
# 2002, Jonathan Oxer # 2002, Jonathan Oxer
# 2002, Rene Wagner <reenoo@gmx.de> # 2002, Rene Wagner <reenoo@gmx.de>
# 2006, Peter Marschall
# #
# This file is free software; you can redistribute it and/or modify # This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -25,6 +26,9 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# #
use 5.005;
use strict;
use Getopt::Std;
use IO::Socket; use IO::Socket;
use Fcntl; use Fcntl;
@@ -33,26 +37,58 @@ use Fcntl;
############################################################ ############################################################
# Path to `fortune' program. # Path to `fortune' program.
$FORTUNE = "fortune"; my $FORTUNE = "fortune";
# Host which runs lcdproc daemon (LCDd) # Host which runs lcdproc daemon (LCDd)
$HOST = "localhost"; my $SERVER = "localhost";
# Port on which LCDd listens to requests # Port on which LCDd listens to requests
$PORT = "13666"; my $PORT = "13666";
############################################################ ############################################################
# End of user configurable parts # End of user configurable parts
############################################################ ############################################################
my $progname = $0;
$progname =~ s#.*/(.*?)$#$1#;
# declare functions
sub update_text($);
sub error($@);
sub usage($);
## main routine ##
my %opt = ();
# get options #
if (getopts('F:s:p:hV', \%opt) == 0) {
usage(1);
}
# check options
usage(0) if ($opt{h});
if ($opt{V}) {
print STDERR $progname ." version 1.1\n";
exit(0);
}
# check number of arguments
usage(1) if ($#ARGV >= 0);
# set variables
$SERVER = defined($opt{s}) ? $opt{s} : $SERVER;
$PORT = defined($opt{p}) ? $opt{p} : $PORT;
$FORTUNE = defined($opt{F}) ? $opt{F} : $FORTUNE;
# Connect to the server... # Connect to the server...
$remote = IO::Socket::INET->new( my $remote = IO::Socket::INET->new(
Proto => "tcp", Proto => 'tcp',
PeerAddr => $HOST, PeerAddr => $SERVER,
PeerPort => $PORT, PeerPort => $PORT,
) )
|| die "Cannot connect to LCDproc port\n"; or error(1, "cannot connect to LCDd daemon at $SERVER:$PORT");
# Make sure our messages get there right away # Make sure our messages get there right away
$remote->autoflush(1); $remote->autoflush(1);
@@ -66,13 +102,11 @@ print $remote "hello\n";
my $lcdresponse = <$remote>; my $lcdresponse = <$remote>;
#print $lcdresponse; #print $lcdresponse;
# Turn off blocking mode... # Turn off blocking mode...
fcntl($remote, F_SETFL, O_NONBLOCK); fcntl($remote, F_SETFL, O_NONBLOCK);
# Set up some screen widgets... # Set up some screen widgets...
print $remote "client_set name {fortune.pl}\n"; print $remote "client_set name {$progname}\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "screen_add fortune\n"; print $remote "screen_add fortune\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
@@ -85,25 +119,25 @@ $lcdresponse = <$remote>;
print $remote "widget_add fortune text scroller\n"; print $remote "widget_add fortune text scroller\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
update_text($remote);
&update_text();
# Forever, we should do stuff... # Forever, we should do stuff...
while (1) while (1)
{ {
# Handle input... (spew it to the console) # Handle input... (spew it to the console)
# Also, certain keys scroll the display # Also, certain keys scroll the display
while(defined($input = <$remote>)) { while (defined(my $input = <$remote>)) {
#print $input; #print $input;
# Make sure we handle each line... # Make sure we handle each line...
@lines = split(/\n/, $input); my @lines = split(/\n/, $input);
foreach $line (@lines)
{ foreach my $line (@lines) {
if ( $line =~ /^success$/ ) { next; } next if ( $line =~ /^success$/ );
if($line =~ /^ignore (\S)/) # Update just after disappearing
{ # Update just after disappearing
&update_text(); if ($line =~ /^ignore (\S)/) {
update_text($remote);
} }
} }
} }
@@ -112,17 +146,131 @@ while(1)
sleep 1; sleep 1;
} }
close ($remote) || die "close: $!"; close($remote) or error(1, "close() failed: $!");
exit; exit;
sub update_text {
# Grab some text. # update text on LCD
$text = `$FORTUNE` || die "\n$0: Error running `$FORTUNE'.\nPlease check that the path is correct.\n\n"; sub update_text($)
@lines = split(/\n/, $text); {
$text = join(" / ", @lines); my $remote = shift;
my $text;
# Grab some text
$text = `$FORTUNE` || error(1, "error running `$FORTUNE'.\nPlease check that the path is correct.");
# replace new-line by slashes
$text =~ s,\n, / ,g;
# Now, show a fortune... # Now, show a fortune...
print $remote "widget_set fortune text 1 2 20 4 v 16 {$text}\n"; print $remote "widget_set fortune text 1 2 20 4 v 16 {$text}\n";
my $lcdresponse = <$remote>; $text = <$remote>;
} }
## print out error message and eventually exit ##
# Synopsis: error($status, $message)
sub error($@)
{
my $status = shift;
my @msg = @_;
print STDERR $progname . ": " . join(" ", @msg) . "\n";
exit($status) if ($status);
}
## print out usage message and exit ##
# Synopsis: usage($status)
sub usage($)
{
my $status = shift;
print STDERR "Usage: $progname [<options>]\n";
if (!$status) {
print STDERR " where <options> are\n" .
" -s <server> connect to <server> (default: $SERVER)\n" .
" -p <port> connect to <port> on <server> (default: $PORT)\n" .
" -F <fortune> use <fortune> as fortune program (default: $FORTUNE)\n" .
" -h show this help page\n" .
" -V display version number\n";
}
else {
print STDERR "For help, type: $progname -h\n";
}
exit($status);
}
__END__
=pod
=head1 NAME
fortune.pl -- display fortune messages on the LCD
=head1 SYNOPSIS
B<fortune.pl>
[B<-s> I<server>]
[B<-p> I<port>]
[B<-F> I<fortune>]
[B<-h>]
[B<-V>]
=head1 DESCRIPTION
B<fortune.pl> is a small example client for LCDd, the lcdproc server.
It connects to the LCDd daemon and displays fortune cookies as delivered
by the well known fortune program.
=head1 OPTIONS
=over 4
=item B<-s> I<server>
Connect to the LCDd daemon at host I<server> instead of the default C<localhost>.
=item B<-p> I<port>
Use port I<port> when connecting to the LCDd server instead of the default
LCDd port C<13666>.
=item B<-F> I<fortune>
Use I<fortune> as the program generation fortune messages instead of the
default C<fortune>
=item B<-h>
Display a short help page and exit.
=item B<-V>
Display fortune.pl's version number and exit.
=back
=head1 SEE ALSO
L<fortune(6)>,
L<LCDd(8)>
=head1 AUTHORS
fortune.pl was written by various members of the LCDproc project team;
this manual page was written by Peter Marschall.
=cut
# EOF
+180 -42
View File
@@ -1,5 +1,8 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
use 5.005;
use strict;
use Getopt::Std;
use IO::Socket; use IO::Socket;
use Fcntl; use Fcntl;
@@ -15,6 +18,7 @@ use Fcntl;
# 2001, Jarda Benkovsky # 2001, Jarda Benkovsky
# 2002, Jonathan Oxer # 2002, Jonathan Oxer
# 2002, Rene Wagner <reenoo@gmx.de> # 2002, Rene Wagner <reenoo@gmx.de>
# 2006, Peter Marschall
# #
# This file is free software; you can redistribute it and/or modify # This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -37,16 +41,16 @@ use Fcntl;
############################################################ ############################################################
# Host which runs lcdproc daemon (LCDd) # Host which runs lcdproc daemon (LCDd)
$HOST = "localhost"; my $SERVER = "localhost";
# Port on which LCDd listens to requests # Port on which LCDd listens to requests
$PORT = "13666"; my $PORT = "13666";
# Path to `ping' command # Path to `ping' command
$PING = "ping"; my $PING = "ping";
# list of hosts to be ping'ed # list of hosts to be ping'ed
@MACHINES = ("lcdproc.omnipotent.net", my @MACHINES = ("lcdproc.omnipotent.net",
"www.linux.org", "www.linux.org",
"www.daemonnews.org", "www.daemonnews.org",
"127.0.0.1", "127.0.0.1",
@@ -59,14 +63,45 @@ $PING = "ping";
# End of user configurable parts # End of user configurable parts
############################################################ ############################################################
my $progname = $0;
$progname =~ s#.*/(.*?)$#$1#;
# declare functions
sub error($@);
sub usage($);
## main routine ##
my %opt = ();
# get options #
if (getopts('F:s:p:P:hV', \%opt) == 0) {
usage(1);
}
# check options
usage(0) if ($opt{h});
if ($opt{V}) {
print STDERR $progname ." version 1.1\n";
exit(0);
}
# check number of arguments
#usage(1) if ($#ARGV >= 0);
# set variables
$SERVER = defined($opt{s}) ? $opt{s} : $SERVER;
$PORT = defined($opt{p}) ? $opt{p} : $PORT;
$PING = defined($opt{P}) ? $opt{P} : $PING;
@MACHINES = ($#ARGV >= 0) ? @ARGV : @MACHINES;
# Connect to the server... # Connect to the server...
$remote = IO::Socket::INET->new( my $remote = IO::Socket::INET->new(
Proto => "tcp", Proto => 'tcp',
PeerAddr => $HOST, PeerAddr => $SERVER,
PeerPort => $PORT, PeerPort => $PORT,
) )
|| die "Cannot connect to LCDproc port\n"; or error(1, "cannot connect to LCDd daemon at $SERVER:$PORT");
# Make sure our messages get there right away # Make sure our messages get there right away
$remote->autoflush(1); $remote->autoflush(1);
@@ -78,15 +113,13 @@ print $remote "hello\n";
# server even if there isn't meant to be one. If you don't, you may find # server even if there isn't meant to be one. If you don't, you may find
# your program crashes after running for a while when the buffers fill up: # your program crashes after running for a while when the buffers fill up:
my $lcdresponse = <$remote>; my $lcdresponse = <$remote>;
print $lcdresponse; #print $lcdresponse;
# Turn off blocking mode... # Turn off blocking mode...
fcntl($remote, F_SETFL, O_NONBLOCK); fcntl($remote, F_SETFL, O_NONBLOCK);
# Set up some screen widgets... # Set up some screen widgets...
print $remote "client_set name {Test Client (Perl)}\n"; print $remote "client_set name {$progname}\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "screen_add pings\n"; print $remote "screen_add pings\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
@@ -103,43 +136,148 @@ $lcdresponse = <$remote>;
print $remote "widget_set pings one 1 2 {Checking machines...}\n"; print $remote "widget_set pings one 1 2 {Checking machines...}\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
while (1) {
my %pingstatus = (); # pingstatus{host} = 1 <=> host is up
my @list;
my ($machine, %down, %up, $i, $list);
while(1)
{
#print "Main loop...\n"; #print "Main loop...\n";
# Handle input... (just spew it to the console) # Handle input... (just spew it to the console)
while(defined($line = <$remote>)) { while (defined(my $line = <$remote>)) {
if ( $line =~ /^success$/ ) { next; } next if ($line =~ /^success$/o);
print $line; print $line;
} }
undef %down; foreach my $machine (@MACHINES) {
undef %up;
foreach $machine (@MACHINES)
{
`$PING -c 1 $machine`; `$PING -c 1 $machine`;
if($?) { $down{$machine} = 1; } $pingstatus{$machine} = ($?) ? 0 : 1;
else { $up{$machine} = 1; }
}
$i = 0; $list = "";
foreach $machine (keys %up)
{
$i++;
$list .= "$machine, ";
}
print $remote "widget_set pings one 1 2 {Machines Up: $i}\n";
my $lcdresponse = <$remote>;
$i = 0; $list = "";
foreach $machine (keys %down)
{
$i++;
$list .= "$machine, ";
}
print $remote "widget_set pings two 1 3 {Down ($i): $list}\n";
$lcdresponse = <$remote>;
} }
close ($remote) || die "close: $!"; # count reachablei (up) machines
@list = grep { $pingstatus{$_} == 1 } @MACHINES;
print $remote "widget_set pings one 1 2 {Machines Up: ",scalar(@list),"}\n";
my $lcdresponse = <$remote>;
# count unreachable (down) machines
@list = grep { $pingstatus{$_} == 0 } @MACHINES;
print $remote "widget_set pings two 1 3 {Down (",scalar(@list),"): ",join(", ", @list),"}\n";
$lcdresponse = <$remote>;
# wait a bit
sleep(1);
}
close($remote) or error(1, "close() failed: $!");
exit; exit;
## print out error message and eventually exit ##
# Synopsis: error($status, $message)
sub error($@)
{
my $status = shift;
my @msg = @_;
print STDERR $progname . ": " . join(" ", @msg) . "\n";
exit($status) if ($status);
}
## print out usage message and exit ##
# Synopsis: usage($status)
sub usage($)
{
my $status = shift;
print STDERR "Usage: $progname [<options>] [<host> ...]\n";
if (!$status) {
print STDERR " where <options> are\n" .
" -s <server> connect to <server> (default: $SERVER)\n" .
" -p <port> connect to <port> on <server> (default: $PORT)\n" .
" -P <ping> use <ping> as the ping cmmand (default: $PING)\n" .
" -h show this help page\n" .
" -V display version number\n";
}
else {
print STDERR "For help, type: $progname -h\n";
}
exit($status);
}
__END__
=pod
=head1 NAME
iosock.pl -- show reachability of hosts and display it on the LCD
=head1 SYNOPSIS
B<iosock.pl>
[B<-s> I<server>]
[B<-p> I<port>]
[B<-P> I<ping>]
[B<-h>]
[B<-V>]
[I<host> ...]
=head1 DESCRIPTION
B<iosock.pl> is a small example client for LCDd, the lcdproc server.
It tries to ping the servers given as parameters and displays on the LCD
whether they are reachable or not.
If no hosts are given as parameters a built-in list of default machines will be checked.
=head1 OPTIONS
=over 4
=item B<-s> I<server>
Connect to the LCDd daemon at host I<server> instead of the default C<localhost>.
=item B<-p> I<port>
Use port I<port> when connecting to the LCDd server instead of the default
LCDd port C<13666>.
=item B<-P> I<ping>
Use I<ping> as the command to check whether the remote hosts are reachable
instead of the default C<ping>.
Any I<ping> given using this options must understand the C<-c 1>
command line option of the default C<ping>.
=item B<-h>
Display a short help page and exit.
=item B<-V>
Display iosock.pl's version number and exit.
=back
=head1 SEE ALSO
L<LCDd(8)>
=head1 AUTHORS
iosock.pl was written by various members of the LCDproc project team;
this manual page was written by Peter Marschall.
=cut
# EOF
+199 -59
View File
@@ -29,6 +29,9 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# #
use 5.005;
use strict;
use Getopt::Std;
use IO::Socket; use IO::Socket;
use Fcntl; use Fcntl;
@@ -37,38 +40,63 @@ use Fcntl;
############################################################ ############################################################
# Host which runs lcdproc daemon (LCDd) # Host which runs lcdproc daemon (LCDd)
$HOST = "localhost"; my $SERVER = "localhost";
# Port on which LCDd listens to requests # Port on which LCDd listens to requests
$PORT = "13666"; my $PORT = "13666";
# These define the visible part of the file...
$top = 3; # How far from the end of the file should we start?
$lines = 3; # How many lines to display?
$left = 1; # Left/right scrolling position,
$width = 20; # and number of characters per line to show
############################################################ ############################################################
# End of user configurable parts # End of user configurable parts
############################################################ ############################################################
$slow = 1; # Should we pause after the current frame? # These define the visible part of the file...
my $top = 3; # How far from the end of the file should we start?
my $lines = 3; # How many lines to display?
my $left = 1; # Left/right scrolling position,
my $width = 20; # and number of characters per line to show
if($#ARGV < 0)
{ my $progname = $0;
print "Usage: tail.pl file\n"; $progname =~ s#.*/(.*?)$#$1#;
exit;
# declare functions
sub error($@);
sub usage($);
## main routine ##
my %opt = ();
# get options #
if (getopts('F:s:p:hV', \%opt) == 0) {
usage(1);
} }
$filename = shift @ARGV;
# check options
usage(0) if ($opt{h});
if ($opt{V}) {
print STDERR $progname ." version 1.1\n";
exit(0);
}
# check number of arguments
usage(1) if ($#ARGV != 0);
# set variables
$SERVER = defined($opt{s}) ? $opt{s} : $SERVER;
$PORT = defined($opt{p}) ? $opt{p} : $PORT;
my $filename = shift @ARGV;
my $slow = 1; # Should we pause after the current frame?
# Connect to the server... # Connect to the server...
$remote = IO::Socket::INET->new( my $remote = IO::Socket::INET->new(
Proto => "tcp", Proto => 'tcp',
PeerAddr => $HOST, PeerAddr => $SERVER,
PeerPort => $PORT, PeerPort => $PORT,
) )
|| die "Cannot connect to LCDproc port\n"; or error(1, "cannot connect to LCDd daemon at $SERVER:$PORT");
# Make sure our messages get there right away # Make sure our messages get there right away
$remote->autoflush(1); $remote->autoflush(1);
@@ -80,15 +108,22 @@ print $remote "hello\n";
# server even if there isn't meant to be one. If you don't, you may find # server even if there isn't meant to be one. If you don't, you may find
# your program crashes after running for a while when the buffers fill up. # your program crashes after running for a while when the buffers fill up.
my $lcdresponse = <$remote>; my $lcdresponse = <$remote>;
print $lcdresponse; #print $lcdresponse;
# get width & height from server's greet message
if ($lcdresponse =~ /\bwid\s+(\d+)\b/) {
$width = 0 + $1;
}
if ($lcdresponse =~ /\bhgt\s+(\d+)\b/) {
$lines = (0 + $1) - 1;
$top = $lines;
}
# Turn off blocking mode... # Turn off blocking mode...
fcntl($remote, F_SETFL, O_NONBLOCK); fcntl($remote, F_SETFL, O_NONBLOCK);
# Set up some screen widgets... # Set up some screen widgets...
print $remote "client_set name {Tail}\n"; print $remote "client_set name {$progname}\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "screen_add tail\n"; print $remote "screen_add tail\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
@@ -106,68 +141,69 @@ print $remote "widget_add tail 3 string\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
# NOTE: You have to ask LCDd to send you keys you want to handle # NOTE: You have to ask LCDd to send you keys you want to handle
print $remote "client_add_key E\n"; print $remote "client_add_key Left\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "client_add_key F\n"; print $remote "client_add_key Right\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "client_add_key G\n"; print $remote "client_add_key Up\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "client_add_key H\n"; print $remote "client_add_key Down\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
# Forever, we should do stuff... # Forever, we should do stuff...
while(1) while (1) {
{
# Handle input... (spew it to the console) # Handle input... (spew it to the console)
# Also, certain keys scroll the display # Also, certain keys scroll the display
while(defined($input = <$remote>)) { while(defined(my $input = <$remote>)) {
if ( $input =~ /^success$/ ) { next; } next if ($input =~ /^success$/o);
print $input;
#print $input;
$slow = -10; $slow = -10;
# Make sure we handle each line... # Make sure we handle each line...
@lines = split(/\n/, $input); my @lines = split(/\n/, $input);
foreach $line (@lines) foreach my $line (@lines) {
{ if ($line =~ /^key (\S+)/) { # Keypresses are useful.
if($line =~ /^key (\S)/) # Keypresses are useful. my $key = $1;
{
$key = $1; if ($key eq 'Down') {
if($key eq "G") { $top++; } $top++;
if($key eq "H")
{
if($top > $lines) {$top--; }
} }
if($key eq "F") { $left++; } elsif ($key eq 'Up') {
if($key eq "E") $top-- if ($top > $lines);
{ }
if($left > 1) {$left--; } elsif ($key eq 'Right') {
$left++;
}
elsif ($key eq 'Left') {
$left-- if ($left > 1);
} }
} }
} }
} }
# Now, show what the file contains, if anything... # Now, show what the file contains, if anything...
if( -f $filename ) if ( -f $filename ) {
{
# Grab some text. # Grab some text.
$right = $left + $width - 1; my $right = $left + $width - 1;
$text = `tail -$top $filename | head -$lines | cut -c$left-$right`; my $text = `tail -$top $filename | head -$lines | cut -c$left-$right`;
@lines = split(/\n/, $text); my @lines = split(/\n/, $text);
# Now, display that text... # Now, display that text...
for($i=0; $i<$lines; $i++) for (my $i = 0; $i < $lines; $i++) {
{ my $j = $i + 1;
$j = $i + 1; my $k = $i + 2;
$k = $i + 2;
if($#lines < $i) { $line = " "; } # Avoid blank lines # Avoid blank lines
else { $line = $lines[$i]; } my $line = ($#lines < $i) ? " " : $lines[$i];
if(!($line =~ /\S/)) { $line = " ";} $line = " " if ($line =~ /^\s*$/o);
print $remote "widget_set tail $j 1 $k {$line}\n"; print $remote "widget_set tail $j 1 $k {$line}\n";
my $lcdresponse = <$remote>; my $lcdresponse = <$remote>;
} }
} }
else # If the file is unreadable, show an error else { # If the file is unreadable, show an error
{
print $remote "widget_set tail 1 1 2 {$filename}\n"; print $remote "widget_set tail 1 1 2 {$filename}\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "widget_set tail 2 1 3 {doesn't exist.}\n"; print $remote "widget_set tail 2 1 3 {doesn't exist.}\n";
@@ -186,5 +222,109 @@ while(1)
# is inactive, it gradually decreases update frequency. # is inactive, it gradually decreases update frequency.
} }
close ($remote) || die "close: $!"; close ($remote) or error(1, "close() failed");
exit; exit;
## print out error message and eventually exit ##
# Synopsis: error($status, $message)
sub error($@)
{
my $status = shift;
my @msg = @_;
print STDERR $progname . ": " . join(" ", @msg) . "\n";
exit($status) if ($status);
}
## print out usage message and exit ##
# Synopsis: usage($status)
sub usage($)
{
my $status = shift;
print STDERR "Usage: $progname [<options>] <file>\n";
if (!$status) {
print STDERR " where <options> are\n" .
" -s <server> connect to <server> (default: $SERVER)\n" .
" -p <port> connect to <port> on <server> (default: $PORT)\n" .
" -h show this help page\n" .
" -V display version number\n";
}
else {
print STDERR "For help, type: $progname -h\n";
}
exit($status);
}
__END__
=pod
=head1 NAME
tail.pl -- show tail of a file on LCD
=head1 SYNOPSIS
B<tail.pl>
[B<-s> I<server>]
[B<-p> I<port>]
[B<-h>]
[B<-V>]
=head1 DESCRIPTION
B<tail.pl> is a small example client for LCDd, the lcdproc server.
It shows the tail of th file given as parameter on the LCD.
If the LCD supports keys you can use the keys mapped to C<Up>, C<Down>,
C<Left> and C<Right> to scroll the contents shown on the screen.
=head1 OPTIONS
=over 4
=item B<-s> I<server>
Connect to the LCDd daemon at host I<server> instead of the default C<localhost>.
=item B<-p> I<port>
Use port I<port> when connecting to the LCDd server instead of the default
LCDd port C<13666>.
=item B<-h>
Display a short help page and exit.
=item B<-V>
Display tail.pl's version number and exit.
=back
=head1 SEE ALSO
L<tail(1)>,
L<LCDd(8)>
=head1 AUTHORS
tail.pl was written by various members of the LCDproc project team;
this manual page was written by Peter Marschall.
=cut
# EOF
+134 -44
View File
@@ -38,6 +38,9 @@
# (although it seems buggy.. oops! :) # (although it seems buggy.. oops! :)
use 5.005;
use strict;
use Getopt::Std;
use IO::Socket; use IO::Socket;
use Fcntl; use Fcntl;
@@ -46,27 +49,58 @@ use Fcntl;
############################################################ ############################################################
# Host which runs lcdproc daemon (LCDd) # Host which runs lcdproc daemon (LCDd)
$HOST = "localhost"; my $SERVER = "localhost";
# Port on which LCDd listens to requests # Port on which LCDd listens to requests
$PORT = "13666"; my $PORT = "13666";
# Path to command which controls XMMS/X11AMP # Path to command which controls XMMS/X11AMP
$XMMS = "xmms"; my $XMMS = "xmms";
# Commands to set to previous / next song # Commands to set to previous / next song
$XMMS_FORWARD = "$XMMS --fwd"; my $XMMS_FORWARD = "$XMMS --fwd";
$XMMS_REWIND = "$XMMS --rew"; my $XMMS_REWIND = "$XMMS --rew";
$XMMS_PLAY_PAUSE = "$XMMS --play-pause"; my $XMMS_PLAY_PAUSE = "$XMMS --play-pause";
############################################################ ############################################################
# End of user configurable parts # End of user configurable parts
############################################################ ############################################################
my $progname = $0;
$progname =~ s#.*/(.*?)$#$1#;
# declare functions
sub error($@);
sub usage($);
## main routine ##
my %opt = ();
# get options #
if (getopts('s:p:hV', \%opt) == 0) {
usage(1);
}
# check options
usage(0) if ($opt{h});
if ($opt{V}) {
print STDERR $progname ." version 1.1\n";
exit(0);
}
# check number of arguments
usage(1) if ($#ARGV >= 0);
# set variables
$SERVER = defined($opt{s}) ? $opt{s} : $SERVER;
$PORT = defined($opt{p}) ? $opt{p} : $PORT;
# Connect to the server... # Connect to the server...
$remote = IO::Socket::INET->new( my $remote = IO::Socket::INET->new(
Proto => "tcp", Proto => 'tcp',
PeerAddr => $HOST, PeerAddr => $SERVER,
PeerPort => $PORT, PeerPort => $PORT,
) )
|| die "Cannot connect to LCDproc port\n"; || die "Cannot connect to LCDproc port\n";
@@ -83,13 +117,11 @@ print $remote "hello\n";
my $lcdresponse = <$remote>; my $lcdresponse = <$remote>;
#print $lcdresponse; #print $lcdresponse;
# Turn off blocking mode... # Turn off blocking mode...
fcntl($remote, F_SETFL, O_NONBLOCK); fcntl($remote, F_SETFL, O_NONBLOCK);
# Set up some screen widgets... # Set up some screen widgets...
print $remote "client_set name {X11AMP test}\n"; print $remote "client_set name {X11AMP}\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
print $remote "screen_add x11amp\n"; print $remote "screen_add x11amp\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
@@ -113,93 +145,151 @@ print $remote "client_add_key Enter\n";
$lcdresponse = <$remote>; $lcdresponse = <$remote>;
while(1) while(1) {
{
# Handle input... # Handle input...
while(defined($line = <$remote>)) { while (defined(my $line = <$remote>)) {
@items = split(" ", $line); my @items = split(/ /, $line);
$command = shift @items; my $command = shift @items;
# Use input to change songs... # Use input to change songs...
if($command eq "key") { if ($command eq 'key') {
my $key = shift @items; my $key = shift @items;
if($key eq "Left") { if ($key eq 'Left') {
system($XMMS_REWIND); system($XMMS_REWIND);
} }
elsif($key eq "Right") { elsif ($key eq 'Right') {
system($XMMS_FORWARD); system($XMMS_FORWARD);
} }
elsif($key eq "Enter") { elsif ($key eq 'Enter') {
system($XMMS_PLAY_PAUSE); system($XMMS_PLAY_PAUSE);
} }
} }
# And ignore everything else # And ignore everything else
elsif($command eq "connect") { elsif ($command eq 'connect') {
} }
elsif($command eq "listen") { elsif ($command eq 'listen') {
} }
elsif($command eq "ignore") { elsif ($command eq 'ignore') {
} }
elsif($command eq "success") { elsif ($command eq 'success') {
} }
else { else {
if($line =~ /\S/) {print "Huh? $line\n";} error(0, "Huh?", $line)
if ($line !~ /^\s*$/o);
} }
} }
# wait a bit
sleep 1; sleep 1;
} }
close ($remote) || die "close: $!"; close ($remote) or error(1, "close() error");
exit; exit;
## print out error message and eventually exit ##
# Synopsis: error($status, $message)
sub error($@)
{
my $status = shift;
my @msg = @_;
print STDERR $progname . ": " . join(" ", @msg) . "\n";
exit($status) if ($status);
}
## print out usage message and exit ##
# Synopsis: usage($status)
sub usage($)
{
my $status = shift;
print STDERR "Usage: $progname [<options>]\n";
if (!$status) {
print STDERR " where <options> are\n" .
" -s <server> connect to <server> (default: $SERVER)\n" .
" -p <port> connect to <port> on <server> (default: $PORT)\n" .
" -h show this help page\n" .
" -V display version number\n";
}
else {
print STDERR "For help, type: $progname -h\n";
}
exit($status);
}
__END__ __END__
=pod =pod
=head1 NAME =head1 NAME
x11amp - LCDproc client controlling XMMS/X11AMP x11amp.pl - LCDproc client controlling XMMS/X11AMP
=head1 SYNOPSIS =head1 SYNOPSIS
B<x11amp.pl> B<x11amp.pl>
[B<-s> I<server>]
[B<-p> I<port>]
[B<-h>]
[B<-V>]
=head1 DESCRIPTION =head1 DESCRIPTION
x11amp is a simple client for LCDproc which controls an XMMS/X11AMP B<x11amp.pl> is a simple client for LCDproc which controls an XMMS/X11AMP
MP3 player from the keyboard controlled by LCDproc. MP3 player from the keyboard controlled by LCDproc.
It can only rewind to the previous song previous song, skip forward It can only rewind to the previous song previous song, skip forward
to the next song and switch between pause and play. to the next song and switch between pause and play.
=head1 REQUIRES =head1 OPTIONS
Perl >= 5.005, IO::Socket, Fcntl =over 4
These are all available on CPAN: http://www.cpan.org/. =item B<-s> I<server>
=head1 DISCLAMER Connect to the LCDd daemon at host I<server> instead of the default C<localhost>.
This program is free software; you can redistribute it and/or modify =item B<-p> I<port>
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but Use port I<port> when connecting to the LCDd server instead of the default
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY LCDd port C<13666>.
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details. =item B<-h>
Display a short help page and exit.
=item B<-V>
Display x11amp's version number and exit.
=back
=head1 SEE ALSO
L<xmms(6)>,
L<LCDd(8)>
=head1 AUTHORS
x11amp.pl was written by various members of the LCDproc project team;
this manual page was written by Peter Marschall.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=head1 BUGS =head1 BUGS
Yes, there might be some. Please report any one you find to LCDproc's mailing list. Yes, there might be some. Please report any one you find to LCDproc's mailing list.
See the website for more information. See the website for more information.
=head1 WEBSITE =head1 WEBSITE
Visit B<http://www.lcdproc.org/> for more infos and the lastest version. Visit B<http://www.lcdproc.org/> for more infos and the lastest version.