1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
use 5.008001;
our $VERSION = '0.037';
package IO::LCDproc;
###############################################################################
package IO::LCDproc::Client;
@IO::LCDproc::Client::ISA = qw(IO::LCDproc);
use Carp;
use Fcntl;
use IO::Socket::INET;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
croak "No name for Client: $!" unless($params{name});
my $self = {};
$self->{name} = $params{name};
$self->{host} = $params{host} || "localhost";
$self->{port} = $params{port} || "13666";
$self->{cmd} = "client_set name {$self->{name}}\n";
$self->{screen} = undef;
bless ($self, $class);
return $self;
}
sub add {
my $self = shift;
$self->{screen} = shift;
$self->{screen}{client} = $self;
$self->{screen}{set} = "screen_set $self->{screen}{name} name {$self->{screen}{name}}\n";
$self->{screen}{set}.= "screen_set $self->{screen}{name} heartbeat $self->{screen}{heartbeat}\n";
}
sub connect {
my $self = shift;
$self->{lcd} = IO::Socket::INET->new(
Proto => "tcp", PeerAddr => "$self->{host}", PeerPort => "$self->{port}"
) or croak "Cannot connect to LCDproc port: $!";
$self->{lcd}->autoflush();
sleep 1;
}
sub initialize {
my $self = shift;
my $fh = $self->{lcd};
my $msgs;
print $fh "hello\n";
$msgs = <$fh>;
if($msgs =~ /lcd.+wid\s+(\d+)\s+hgt\s+(\d+)\s+cellwid\s+(\d+)\s+cellhgt\s+(\d+)/){
$self->{width} = $1;
$self->{height} = $2;
$self->{cellwidth} = $3;
$self->{cellheight} = $4;
} else {
croak "No stats reported...: $!";
}
fcntl( $fh, F_SETFL, O_NONBLOCK );
print $fh $self->{cmd};
print $fh $self->{screen}{cmd};
print $fh $self->{screen}{set};
foreach(@{$self->{screen}{widgets}}){
print $fh $_->{cmd};
}
}
sub answer {
my $self = shift;
my $fh = $self->{lcd};
my $answ;
$answ = <$fh>;
return $answ;
}
sub flushAnswers {
my $self = shift;
while ($self->answer()) {}
}
###############################################################################
package IO::LCDproc::Screen;
@IO::LCDproc::Screen::ISA = qw(IO::LCDproc);
use Carp;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
croak "No name for Screen: $!" unless($params{name});
my $self = {};
$self->{name} = $params{name};
$self->{heartbeat} = $params{heartbeat} || "on";
$self->{cmd} = "screen_add $self->{name}\n";
$self->{widgets} = undef;
bless ($self, $class);
return $self;
}
sub add {
my $self = shift;
foreach (@_){
push @{$self->{widgets}}, $_;
$_->{screen}=$self;
$_->{cmd} = "widget_add $_->{screen}{name} $_->{name} $_->{type}\n";
}
}
sub set_prio {
my $self = shift;
my $prio = shift;
my $fh = $self->{client}->{lcd};
print $fh "screen_set $self->{name} -priority $prio\n";
}
###############################################################################
package IO::LCDproc::Widget;
@IO::LCDproc::Client::ISA = qw(IO::LCDproc);
use Carp;
use overload '++' => \&xIncrement;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
croak "No name for Widget: $!" unless($params{name});
my $self = {};
$self->{name} = $params{name};
$self->{align} = $params{align} || "left";
$self->{type} = $params{type} || "string";
$self->{xPos} = $params{xPos} || "";
$self->{yPos} = $params{yPos} || "";
$self->{data} = $params{data} if( $params{data} );
bless ($self, $class);
return $self;
}
sub set {
my $self = shift;
my %params = @_;
$self->{xPos} = $params{xPos} if($params{xPos});
$self->{yPos} = $params{yPos} if($params{yPos});
$self->{data} = $params{data} if($params{data});
$self->{data} = " " x $self->{screen}{client}{width} if(length( $self->{data} ) < 1 );
my $fh = $self->{screen}->{client}->{lcd};
print $fh "widget_set $self->{screen}->{name} $self->{name} $self->{xPos} $self->{yPos} {" .
($self->{align} =~ /center/ ? $self->_center($self->{data}) :
($self->{align} =~ /right/ ? $self->_right($self->{data}) : $self->{data})
) . "}\n";
}
sub _center {
my $self = shift;
return(" " x (($self->{screen}{client}{width} - length($_[0]))/2) . $_[0]);
}
sub _right {
my $self = shift;
return(" " x (($self->{screen}{client}{width} - length($_[0]))) . $_[0]);
}
sub save {
my $self = shift;
$self->{saved} = $self->{data};
}
sub restore {
my $self = shift;
$self->{data} = $self->{saved};
$self->{saved} = "";
$self->set;
}
sub xIncrement {
my $self = shift;
$self->{xPos}++
}
1;
__END__
=head1 NAME
IO::LCDproc - Perl extension to connect to an LCDproc ready display.
=head1 SYNOPSIS
use IO::LCDproc;
my $client = IO::LCDproc::Client->new(name => "MYNAME");
my $screen = IO::LCDproc::Screen->new(name => "screen");
my $title = IO::LCDproc::Widget->new(
name => "date", type => "title"
);
my $first = IO::LCDproc::Widget->new(
name => "first", align => "center", xPos => 1, yPos => 2
);
my $second = IO::LCDproc::Widget->new(
name => "second", align => "center", xPos => 1, yPos => 3
);
my $third = IO::LCDproc::Widget->new(
name => "third", xPos => 1, yPos => 4
);
$client->add( $screen );
$screen->add( $title, $first, $second, $third );
$client->connect() or die "cannot connect: $!";
$client->initialize();
$title->set( data => "This is the title" );
$first->set( data => "First Line" );
$second->set( data => "Second line" );
$third->set( data => "Third Line" );
$client->flushAnswers();
=head1 DESCRIPTION
Follow the example above. Pretty straight forward. You create a client, assign a screen,
add widgets, and then set the widgets.
=head2 IO::LCDproc::Client
It is the back engine of the module. It generates the connection to a ready listening server.
=head3 METHODS
=over
=item new( name => 'Client_Name' [, host => $MYHOSTNAME] [, port => $MYPORTNUMBER] )
Constructor. Takes the following possible arguments (arguments must be given in key => value form):
host, port, and name. name is required.
=item add( I<SCREENREF> )
Adds the screens that will be attached to this client.
=item connect()
Establishes connection to LCDproc server (LCDd).
=item initialize()
Initializes client, screen and all the widgets with the server.
=item answer()
Reads an answer from the server
=item flushAnswers()
Flushes all answers from the server (should be called regulary if you don't need the answers)
=back
=head2 IO::LCDproc::Screen
=head3 METHODS
=over
=item new( name => 'MYNAME')
Constructor. Allowed options:
heartbeat => 1 or 0.
=item add( @WIDGETS )
Adds the given widgets to this screen.
=item set_prio( $prio )
Sets the screen priority with $prio one of
hidden The screen will never be visible
background The screen is only visible when no normal info screens exists
info normal info screen, default priority
foreground an active client
alert The screen has an important message for the user.
input The client is doing interactive input.
=back
=head2 IO::LCDproc::Widget
=head3 METHODS
=over
=item new( name => 'MYNAME' )
Constructor. Allowed arguments:
align (left, center, rigth), type (string, title, vbar, hbar, ...), xPos, yPos, data
=item set()
Sets the widget to the spec'd args. They may be given on the function call or the may be
pre specified.
xPos, yPos, data
=item save()
Saves current data to be user later.
=item restore()
Restore previously saved data. (Implicitly calls set)
=back
=head1 SEE ALSO
L<LCDd>
=head1 AUTHOR
Juan C. Muller, E<lt>[email protected]<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2005 by Juan C. Muller
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|