#!/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){
    $val = getc;
    if($val eq "b"){
	&bounce;
    }
    elsif($val eq "d"){
	&dance;
    }
    elsif($val eq "q"){
	exit(0);
    }
    elsif($val eq "c"){
	&xlib'x_clear_area($xlib'root_window, 0, 0, 0, $xlib'rootw, 
			   $xlib'rooth);
    }
    elsif($val eq "p"){
	$cl = rand(20);
	int($cl);
	$xp = rand($xlib'rootw);
	$yp = rand($xlib'rooth);
	&drawpent($cl, $xp, $yp);
    }
    elsif($val eq "s"){
	&sfunc;
    }
    else{
	eval("&$val"."func;");
    }
}

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


sub drawpent {
    local($color, $x, $y) = @_;
    if(fork()){
	return;
    }
    
    &drawline($color, $x, $y, ($x +20), ($y -40));
    &drawline($color, $x+20, $y-40, $x+40, $y);
    &drawline($color, $x+40, $y, $x, $y-30);
    &drawline($color, $x, $y-30, $x+40, $y-30);
    &drawline($color, $x+40, $y-30, $x, $y);
    exit;
}


sub bounce{
    if(fork()){
	return;
    }
    $e = .66 + rand(1)/3.0;
    srand(time);
    $x = 0;
    $y = 0;
    $vx = (rand(12) +rand(2))/2;
    $vy = rand(1);
    
    $c = rand(20);
    if($c == 4 || $c == 11){$c++;}
    int($c);
    $| = 1;
    while(($vy > .015 || $vy < -.015) || $y != $xlib'rooth){
	$ox = $x;
	$oy = $y;
	$x = ($x + $vx);
	$y = ($y + $vy);
	
	&drawline($c, $ox, $oy, $x, $y);
	if($x > $xlib'rootw){$vx = -$vx;$x = $xlib'rootw;$vx *= $e;}
	if($x < 0) { $vx = -$vx;$x = 0;$vx *= $e;}
	
	if($y > $xlib'rooth){$vy = -$vy;$y = $xlib'rooth;$vy *= $e;}
	if($y < 0) { $vy = -$vy;$y = 0;$vy *= $e;}
	$vy += .03;
	if($y > ($xlib'rooth + 50)){exit(1);}
    }
    exit(0);
}

sub afunc {
    if(fork()){
	return;
    }
    $xcent = $xlib'rootw/2-3+rand(6);
    $ycent = $xlib'rooth/2-3+rand(6);
    for $r (11..360){
	$a = $r*3.14/180;
	&drawline(rand(20), $xcent*cos($a)+$xcent, $ycent*sin($a)+$ycent, $xcent-$xcent*cos($a), $ycent-$ycent*sin($a));
    }
    exit;
}

sub efunc{
    
}
sub sfunc {
    if(fork()){
	return;
    }
    srand(time);
    $center = rand($xlib'rooth);
    $center2 = rand($xlib'rootw);
    $size = $center > $xlib'rooth-$center ? $xlib'rooth-$center:$center;
    for $r (0..10){
	$a = $r*3.14/180;
	&drawline(1, $size*cos($a)+$center2, $size*sin($a)+$center, $center2-$size*cos($a), $center-$size*sin($a));
    }

    for $r (11..360){
	$a = $r*3.14/180;
	&drawline(1, $size*cos($a)+$center2, $size*sin($a)+$center, $center2-$size*cos($a), $center-$size*sin($a));
	$a = ($r-10)*3.14/180;
	&drawline(0, $size*cos($a)+$center2, $size*sin($a)+$center, $center2-$size*cos($a), $center-$size*sin($a));
    }
    exit(0);
}


sub dance {
    if(fork()){
	return;
    }
    $xstart = ($xlib'rootw)/2 + 100;
    $ystart = ($xlib'rooth)/2 +100;
    $xend = $xstart + 200;
    $yend = $ystart +200;
    $co = 8;
    $otime = time;
    while(time < $otime+6){
	&drawline($co, $xstart, $ystart, $xend, $yend);
	$xstart += (rand(50))-25;
	$ystart += (rand(50))-25;
	$xend += (rand(50))-25;
	$yend += (rand(50))-25;
	$co += (rand(2)-1);
	if($xstart < 0){$xstart += 50;}
	if($xstart > $xlib'rootw){$xstart -= 50;}
	if($xend < 0){$xend += 50;}
	if($xend > $xlib'rooth){$xend -= 50;}
	if($ystart < 0){$ystart += 50;}
	if($ystart > $xlib'rooth){$ystart -= 50;}
	if($yend < 0){$yend += 50;}
	if($yend > $xlib'rooth){$yend -= 50;}
	if($co < 0){$co = $co +10;}
	if($co > 20){$co = $co -10;}
    }
    exit(0);
}




