#!/usr/bin/perl -w # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License, # version 2. use lib ("../server/lib", "../server/lib/CPAN", "../server/lib/CPAN/arch/" . $Config::Config{archname} ); use strict; use Socket; use Time::HiRes qw(usleep); my $server_ip = "127.0.0.1"; my $server_port = 1069; my %ircodes = ( play => "32", up => "0b", left => "4b", right => "cb", down => "8b", pip => "f6", back => "0e", fwd => "6e", rec => "43", stop => "c2", pause => "b2", disp => "03", sleep => "b3", guide => "b6", ); my $lines = " ". " "; my $starttime = time(); socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!"; my $ipaddr = inet_aton($server_ip); my $portaddr = sockaddr_in($server_port, $ipaddr); die "can't fork: $!" unless defined(my $kidpid = fork()); if ($kidpid) { while (1) { send_ir(); } } else { while (1) { receive_stuff(); } } sub receive_stuff { my $msg; recv(SOCKET, $msg, 2047, 0) or die "recv: $!"; my $op = substr($msg,0,1); if ($op eq "l") { show_display (substr($msg,18)); } } sub show_display { my $cmd = shift; my @cmds = split(//, $cmd); my $pos = 0; while (($#cmds-1)) { my $command = ord(shift(@cmds)); if ($command == 3) { substr($lines,$pos,1) = shift(@cmds); $pos++; } elsif ($command == 2) { my $realcommand = unpack("B*", shift(@cmds)); if (substr($realcommand,0,1) == '1') { # move cursor my $newpos = ord(pack("B*","0".substr($realcommand,1))); $pos=$newpos; } } } printf(" ,--------------------------------------.\n"); printf("|".substr($lines,0,40)."|\n"); printf("|".substr($lines,64,40)."|\n"); printf(" `--------------------------------------'\n"); } sub send_ir { my $code = <>; chomp($code); my $ircode = $ircodes{$code} or next(); my $msg = "i\0____\377\20\0\0\367_......"; my $elaptime = time() - $starttime; my $elapticks = int($elaptime * 625_000); substr($msg,2,4) = pack("N",$elapticks); substr($msg,11,1) = pack("H*",$ircode); send(SOCKET,$msg,0,$portaddr) == length($msg) or die "cannot send: $!"; }