From ee6e26e93458a4696e6716585cd8dc8224f02f58 Mon Sep 17 00:00:00 2001 From: marschap Date: Sun, 30 Apr 2006 17:13:44 +0000 Subject: [PATCH] update Perl sample scripts --- clients/examples/fortune.pl | 208 ++++++++++++++++++++++++---- clients/examples/iosock.pl | 216 +++++++++++++++++++++++------ clients/examples/tail.pl | 268 +++++++++++++++++++++++++++--------- clients/examples/x11amp.pl | 178 ++++++++++++++++++------ 4 files changed, 693 insertions(+), 177 deletions(-) diff --git a/clients/examples/fortune.pl b/clients/examples/fortune.pl index 408abc1..c9b6f7d 100755 --- a/clients/examples/fortune.pl +++ b/clients/examples/fortune.pl @@ -9,6 +9,7 @@ # 2001, David Glaude # 2002, Jonathan Oxer # 2002, Rene Wagner +# 2006, Peter Marschall # # 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 @@ -25,6 +26,9 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # +use 5.005; +use strict; +use Getopt::Std; use IO::Socket; use Fcntl; @@ -33,26 +37,58 @@ use Fcntl; ############################################################ # Path to `fortune' program. -$FORTUNE = "fortune"; +my $FORTUNE = "fortune"; # Host which runs lcdproc daemon (LCDd) -$HOST = "localhost"; +my $SERVER = "localhost"; # Port on which LCDd listens to requests -$PORT = "13666"; +my $PORT = "13666"; ############################################################ # 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... -$remote = IO::Socket::INET->new( - Proto => "tcp", - PeerAddr => $HOST, +my $remote = IO::Socket::INET->new( + Proto => 'tcp', + PeerAddr => $SERVER, 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 $remote->autoflush(1); @@ -66,13 +102,11 @@ print $remote "hello\n"; my $lcdresponse = <$remote>; #print $lcdresponse; - # Turn off blocking mode... fcntl($remote, F_SETFL, O_NONBLOCK); - # Set up some screen widgets... -print $remote "client_set name {fortune.pl}\n"; +print $remote "client_set name {$progname}\n"; $lcdresponse = <$remote>; print $remote "screen_add fortune\n"; $lcdresponse = <$remote>; @@ -85,26 +119,26 @@ $lcdresponse = <$remote>; print $remote "widget_add fortune text scroller\n"; $lcdresponse = <$remote>; - -&update_text(); +update_text($remote); # Forever, we should do stuff... -while(1) +while (1) { # Handle input... (spew it to the console) # Also, certain keys scroll the display - while(defined($input = <$remote>)) { + while (defined(my $input = <$remote>)) { #print $input; # Make sure we handle each line... - @lines = split(/\n/, $input); - foreach $line (@lines) - { - if ( $line =~ /^success$/ ) { next; } - if($line =~ /^ignore (\S)/) # Update just after disappearing - { - &update_text(); - } + my @lines = split(/\n/, $input); + + foreach my $line (@lines) { + next if ( $line =~ /^success$/ ); + + # Update just after disappearing + if ($line =~ /^ignore (\S)/) { + update_text($remote); + } } } @@ -112,17 +146,131 @@ while(1) sleep 1; } -close ($remote) || die "close: $!"; +close($remote) or error(1, "close() failed: $!"); exit; -sub update_text { - # Grab some text. - $text = `$FORTUNE` || die "\n$0: Error running `$FORTUNE'.\nPlease check that the path is correct.\n\n"; - @lines = split(/\n/, $text); - $text = join(" / ", @lines); + +# update text on LCD +sub update_text($) +{ +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... 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 []\n"; + if (!$status) { + print STDERR " where are\n" . + " -s connect to (default: $SERVER)\n" . + " -p connect to on (default: $PORT)\n" . + " -F use 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 +[B<-s> I] +[B<-p> I] +[B<-F> I] +[B<-h>] +[B<-V>] + + +=head1 DESCRIPTION + +B 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 + +Connect to the LCDd daemon at host I instead of the default C. + +=item B<-p> I + +Use port I when connecting to the LCDd server instead of the default +LCDd port C<13666>. + +=item B<-F> I + +Use I as the program generation fortune messages instead of the +default C + +=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, +L + + +=head1 AUTHORS + +fortune.pl was written by various members of the LCDproc project team; +this manual page was written by Peter Marschall. + +=cut + +# EOF + diff --git a/clients/examples/iosock.pl b/clients/examples/iosock.pl index 03c55cd..3f74a42 100755 --- a/clients/examples/iosock.pl +++ b/clients/examples/iosock.pl @@ -1,5 +1,8 @@ #!/usr/bin/perl -w +use 5.005; +use strict; +use Getopt::Std; use IO::Socket; use Fcntl; @@ -15,6 +18,7 @@ use Fcntl; # 2001, Jarda Benkovsky # 2002, Jonathan Oxer # 2002, Rene Wagner +# 2006, Peter Marschall # # 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 @@ -37,16 +41,16 @@ use Fcntl; ############################################################ # Host which runs lcdproc daemon (LCDd) -$HOST = "localhost"; +my $SERVER = "localhost"; # Port on which LCDd listens to requests -$PORT = "13666"; +my $PORT = "13666"; # Path to `ping' command -$PING = "ping"; +my $PING = "ping"; # list of hosts to be ping'ed -@MACHINES = ("lcdproc.omnipotent.net", +my @MACHINES = ("lcdproc.omnipotent.net", "www.linux.org", "www.daemonnews.org", "127.0.0.1", @@ -59,14 +63,45 @@ $PING = "ping"; # 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... -$remote = IO::Socket::INET->new( - Proto => "tcp", - PeerAddr => $HOST, +my $remote = IO::Socket::INET->new( + Proto => 'tcp', + PeerAddr => $SERVER, 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 $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 # your program crashes after running for a while when the buffers fill up: my $lcdresponse = <$remote>; -print $lcdresponse; - +#print $lcdresponse; # Turn off blocking mode... fcntl($remote, F_SETFL, O_NONBLOCK); - # Set up some screen widgets... -print $remote "client_set name {Test Client (Perl)}\n"; +print $remote "client_set name {$progname}\n"; $lcdresponse = <$remote>; print $remote "screen_add pings\n"; $lcdresponse = <$remote>; @@ -103,43 +136,148 @@ $lcdresponse = <$remote>; print $remote "widget_set pings one 1 2 {Checking machines...}\n"; $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"; + # Handle input... (just spew it to the console) - while(defined($line = <$remote>)) { - if ( $line =~ /^success$/ ) { next; } + while (defined(my $line = <$remote>)) { + next if ($line =~ /^success$/o); print $line; } - undef %down; - undef %up; - foreach $machine (@MACHINES) - { + foreach my $machine (@MACHINES) { `$PING -c 1 $machine`; - if($?) { $down{$machine} = 1; } - else { $up{$machine} = 1; } + $pingstatus{$machine} = ($?) ? 0 : 1; } - $i = 0; $list = ""; - foreach $machine (keys %up) - { - $i++; - $list .= "$machine, "; - } - print $remote "widget_set pings one 1 2 {Machines Up: $i}\n"; + + # 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>; - $i = 0; $list = ""; - foreach $machine (keys %down) - { - $i++; - $list .= "$machine, "; - } - print $remote "widget_set pings two 1 3 {Down ($i): $list}\n"; + + # 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) || die "close: $!"; +close($remote) or error(1, "close() failed: $!"); 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 [] [ ...]\n"; + if (!$status) { + print STDERR " where are\n" . + " -s connect to (default: $SERVER)\n" . + " -p connect to on (default: $PORT)\n" . + " -P use 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 +[B<-s> I] +[B<-p> I] +[B<-P> I] +[B<-h>] +[B<-V>] +[I ...] + + +=head1 DESCRIPTION + +B 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 + +Connect to the LCDd daemon at host I instead of the default C. + +=item B<-p> I + +Use port I when connecting to the LCDd server instead of the default +LCDd port C<13666>. + +=item B<-P> I + +Use I as the command to check whether the remote hosts are reachable +instead of the default C. + +Any I given using this options must understand the C<-c 1> +command line option of the default C. + +=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 + + +=head1 AUTHORS + +iosock.pl was written by various members of the LCDproc project team; +this manual page was written by Peter Marschall. + +=cut + +# EOF + diff --git a/clients/examples/tail.pl b/clients/examples/tail.pl index 564aa9c..1958843 100755 --- a/clients/examples/tail.pl +++ b/clients/examples/tail.pl @@ -29,6 +29,9 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # +use 5.005; +use strict; +use Getopt::Std; use IO::Socket; use Fcntl; @@ -37,38 +40,63 @@ use Fcntl; ############################################################ # Host which runs lcdproc daemon (LCDd) -$HOST = "localhost"; +my $SERVER = "localhost"; # Port on which LCDd listens to requests -$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 +my $PORT = "13666"; ############################################################ # 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) -{ - print "Usage: tail.pl file\n"; - exit; + +my $progname = $0; + $progname =~ s#.*/(.*?)$#$1#; + +# 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... -$remote = IO::Socket::INET->new( - Proto => "tcp", - PeerAddr => $HOST, +my $remote = IO::Socket::INET->new( + Proto => 'tcp', + PeerAddr => $SERVER, 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 $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 # your program crashes after running for a while when the buffers fill up. 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... fcntl($remote, F_SETFL, O_NONBLOCK); - # Set up some screen widgets... -print $remote "client_set name {Tail}\n"; +print $remote "client_set name {$progname}\n"; $lcdresponse = <$remote>; print $remote "screen_add tail\n"; $lcdresponse = <$remote>; @@ -106,68 +141,69 @@ print $remote "widget_add tail 3 string\n"; $lcdresponse = <$remote>; # 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>; -print $remote "client_add_key F\n"; +print $remote "client_add_key Right\n"; $lcdresponse = <$remote>; -print $remote "client_add_key G\n"; +print $remote "client_add_key Up\n"; $lcdresponse = <$remote>; -print $remote "client_add_key H\n"; +print $remote "client_add_key Down\n"; $lcdresponse = <$remote>; # Forever, we should do stuff... -while(1) -{ +while (1) { # Handle input... (spew it to the console) # Also, certain keys scroll the display - while(defined($input = <$remote>)) { - if ( $input =~ /^success$/ ) { next; } - print $input; + while(defined(my $input = <$remote>)) { + next if ($input =~ /^success$/o); + + #print $input; $slow = -10; # Make sure we handle each line... - @lines = split(/\n/, $input); - foreach $line (@lines) - { - if($line =~ /^key (\S)/) # Keypresses are useful. - { - $key = $1; - if($key eq "G") { $top++; } - if($key eq "H") - { - if($top > $lines) {$top--; } + my @lines = split(/\n/, $input); + foreach my $line (@lines) { + if ($line =~ /^key (\S+)/) { # Keypresses are useful. + my $key = $1; + + if ($key eq 'Down') { + $top++; } - if($key eq "F") { $left++; } - if($key eq "E") - { - if($left > 1) {$left--; } + elsif ($key eq 'Up') { + $top-- if ($top > $lines); + } + elsif ($key eq 'Right') { + $left++; + } + elsif ($key eq 'Left') { + $left-- if ($left > 1); } } } } # Now, show what the file contains, if anything... - if( -f $filename ) - { + if ( -f $filename ) { # Grab some text. - $right = $left + $width - 1; - $text = `tail -$top $filename | head -$lines | cut -c$left-$right`; - @lines = split(/\n/, $text); + my $right = $left + $width - 1; + my $text = `tail -$top $filename | head -$lines | cut -c$left-$right`; + my @lines = split(/\n/, $text); + # Now, display that text... - for($i=0; $i<$lines; $i++) - { - $j = $i + 1; - $k = $i + 2; - if($#lines < $i) { $line = " "; } # Avoid blank lines - else { $line = $lines[$i]; } - if(!($line =~ /\S/)) { $line = " ";} - print $remote "widget_set tail $j 1 $k {$line }\n"; + for (my $i = 0; $i < $lines; $i++) { + my $j = $i + 1; + my $k = $i + 2; + + # Avoid blank lines + my $line = ($#lines < $i) ? " " : $lines[$i]; + $line = " " if ($line =~ /^\s*$/o); + + print $remote "widget_set tail $j 1 $k {$line}\n"; 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"; $lcdresponse = <$remote>; print $remote "widget_set tail 2 1 3 {doesn't exist.}\n"; @@ -177,14 +213,118 @@ while(1) } # And wait a little while before we show stuff again. - if($slow > 0) {sleep 1; $slow++;} - elsif($slow > 4) {sleep 2; $slow++;} - elsif($slow > 64) {sleep 4; } - else {$slow++;} + if ($slow > 0) { sleep 1; $slow++; } + elsif ($slow > 4) { sleep 2; $slow++; } + elsif ($slow > 64) { sleep 4; } + else { $slow++; } # The "slow" thing just lets us have a better response time # while the user is pressing keys... But while the user # is inactive, it gradually decreases update frequency. } -close ($remote) || die "close: $!"; +close ($remote) or error(1, "close() failed"); 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 [] \n"; + if (!$status) { + print STDERR " where are\n" . + " -s connect to (default: $SERVER)\n" . + " -p connect to on (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 +[B<-s> I] +[B<-p> I] +[B<-h>] +[B<-V>] + + +=head1 DESCRIPTION + +B 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, C, +C and C to scroll the contents shown on the screen. + + +=head1 OPTIONS + +=over 4 + +=item B<-s> I + +Connect to the LCDd daemon at host I instead of the default C. + +=item B<-p> I + +Use port I 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, +L + + +=head1 AUTHORS + +tail.pl was written by various members of the LCDproc project team; +this manual page was written by Peter Marschall. + +=cut + +# EOF + diff --git a/clients/examples/x11amp.pl b/clients/examples/x11amp.pl index 4d9b7c8..13460df 100755 --- a/clients/examples/x11amp.pl +++ b/clients/examples/x11amp.pl @@ -38,6 +38,9 @@ # (although it seems buggy.. oops! :) +use 5.005; +use strict; +use Getopt::Std; use IO::Socket; use Fcntl; @@ -46,27 +49,58 @@ use Fcntl; ############################################################ # Host which runs lcdproc daemon (LCDd) -$HOST = "localhost"; +my $SERVER = "localhost"; # Port on which LCDd listens to requests -$PORT = "13666"; +my $PORT = "13666"; # Path to command which controls XMMS/X11AMP -$XMMS = "xmms"; +my $XMMS = "xmms"; # Commands to set to previous / next song -$XMMS_FORWARD = "$XMMS --fwd"; -$XMMS_REWIND = "$XMMS --rew"; -$XMMS_PLAY_PAUSE = "$XMMS --play-pause"; +my $XMMS_FORWARD = "$XMMS --fwd"; +my $XMMS_REWIND = "$XMMS --rew"; +my $XMMS_PLAY_PAUSE = "$XMMS --play-pause"; ############################################################ # 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... -$remote = IO::Socket::INET->new( - Proto => "tcp", - PeerAddr => $HOST, +my $remote = IO::Socket::INET->new( + Proto => 'tcp', + PeerAddr => $SERVER, PeerPort => $PORT, ) || die "Cannot connect to LCDproc port\n"; @@ -83,13 +117,11 @@ print $remote "hello\n"; my $lcdresponse = <$remote>; #print $lcdresponse; - # Turn off blocking mode... fcntl($remote, F_SETFL, O_NONBLOCK); - # Set up some screen widgets... -print $remote "client_set name {X11AMP test}\n"; +print $remote "client_set name {X11AMP}\n"; $lcdresponse = <$remote>; print $remote "screen_add x11amp\n"; $lcdresponse = <$remote>; @@ -113,93 +145,151 @@ print $remote "client_add_key Enter\n"; $lcdresponse = <$remote>; -while(1) -{ +while(1) { # Handle input... - while(defined($line = <$remote>)) { - @items = split(" ", $line); - $command = shift @items; + while (defined(my $line = <$remote>)) { + my @items = split(/ /, $line); + my $command = shift @items; + # Use input to change songs... - if($command eq "key") { + if ($command eq 'key') { my $key = shift @items; - if($key eq "Left") { + if ($key eq 'Left') { system($XMMS_REWIND); } - elsif($key eq "Right") { + elsif ($key eq 'Right') { system($XMMS_FORWARD); } - elsif($key eq "Enter") { + elsif ($key eq 'Enter') { system($XMMS_PLAY_PAUSE); } } # 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 { - if($line =~ /\S/) {print "Huh? $line\n";} + error(0, "Huh?", $line) + if ($line !~ /^\s*$/o); } } + # wait a bit sleep 1; - } -close ($remote) || die "close: $!"; +close ($remote) or error(1, "close() error"); 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 []\n"; + if (!$status) { + print STDERR " where are\n" . + " -s connect to (default: $SERVER)\n" . + " -p connect to on (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 -x11amp - LCDproc client controlling XMMS/X11AMP +x11amp.pl - LCDproc client controlling XMMS/X11AMP =head1 SYNOPSIS B +[B<-s> I] +[B<-p> I] +[B<-h>] +[B<-V>] + =head1 DESCRIPTION -x11amp is a simple client for LCDproc which controls an XMMS/X11AMP +B is a simple client for LCDproc which controls an XMMS/X11AMP MP3 player from the keyboard controlled by LCDproc. It can only rewind to the previous song previous song, skip forward 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 -=head1 DISCLAMER +Connect to the LCDd daemon at host I instead of the default C. -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. +=item B<-p> I -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. +Use port I 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 x11amp's version number and exit. + +=back + + +=head1 SEE ALSO + +L, +L + + +=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 Yes, there might be some. Please report any one you find to LCDproc's mailing list. See the website for more information. + =head1 WEBSITE Visit B for more infos and the lastest version.