#!/afs/athena/contrib/perl/p

$sgttyb_t   = 'C4 S';
$winsz_t = "S S S S";  # rows,cols, xpixel, ypixel
$default_num_cols = 80; # if can't be gotten from ioctl...
$TIOCGETP = 0x40067408;
$TIOCSETP = 0x80067409;
$TIOCGWINSZ = 0x40087468;
$RAW = 0x20;
$ECHO = 0x8;

sub start {
#    system 'stty raw -echo';

    ioctl(STDIN,$TIOCGETP,$sgttyb) || die "Can't ioctl TIOCGETP: $!";
    @tty_buf = unpack($sgttyb_t,$sgttyb);
    $tty_buf[4] |= $RAW;
    $tty_buf[4] &= ~$ECHO;
    $sgttyb = pack($sgttyb_t,@tty_buf);
    ioctl(STDIN,$TIOCSETP,$sgttyb) || die "Can't ioctl TIOCSETP: $!";
}

$SIG{'INT'} = 'sighandler';
$SIG{'HUP'} = 'sighandler';
$SIG{'QUIT'} = 'sighandler';


do "./xbase.perl";

print &xlib'x_open_display($ENV{'DISPLAY'});
foreach $col (0..20){
$mygc[$col] = &xlib'x_create_gc($xlib'root_window,$xlib'defines{'GCForeground'}
| $xlib'defines{'GCBackground'}, $col, $xlib'root_white);
}

print &xlib'x_clear_area($xlib'root_window, 0, 0, 0, $xlib'rootw, $xlib'rooth);
print("About to begin\n");
$x = $y =100;
&start;
print("Beginning\n");
while(1){
    $found = 1;
    if($found){
	$val = getc;
	if($val eq "\033"){
	    $val = getc;
	    if($val eq "["){
		$dir = getc;
		if($dir eq "A"){
		    $vy -=.1;
		}
		elsif($dir eq "B"){
		    $vy += .1;
		}
		elsif($dir eq "C"){
		    $vx+=.1;
		}
		elsif($dir eq "D"){
		    $vx-=.1;
		}
	    }
	}
	elsif($val eq "q"){
	    exit(0);
	}
    }
    $ox = $x;
    $oy = $y;
    $x += $vx;
    $y += $vy;
    print("Drewline $ox $oy --> $x $y\r\n");
    &drawline(3, $ox, $oy, $x, $y);
}

sub drawline {
    local($color, $x, $y, $nx, $ny) = @_;
print &xlib'x_line($xlib'root_window, $mygc[$color], 0, $x, $y, $nx, $ny);
}
sub sighandler {
    print("Quitting cleanly... \n");
    exit(0);
}
