#include        <conio.h>       /* for getch, kbhit */
#include        <stdio.h>       /* for malloc */
#include        <dos.h>         /* for delay() */
#include	<allegro.h>	/* for show_mouse(screen) */
#include	<unistd.h>

#define EXTERN
#include        "..\include\globals.h"

#define         PPI     0
#define         RHI     1

extern double trcorrtog;

double DACVOLTAGE = -4.2;
unsigned char *Colors;
FILE *deb, *sray, *ftogp;
DISPLAY *remdisp;

main(argc,argv)
char    *argv[];   
int     argc;
   {
   char         c;
   int          tape,skip=0, hold = 0;
   double	zsave;
   DWELL   *dwell;   
   RADAR   *radar;
   DISPLAY *disp;
   float   *prods;
   int     type=PPI,dispnum='1',dchange=0,pmode=0;   
   int	oraycnt = 0, result = 0, count = 0;
   char tpath[80];
   
   if((prods = (float *)malloc(sizeof(float) * 1200 * 8)) == NULL)
      {puts("not enough memory"); exit(0);}

   if((dwell = (DWELL *)malloc(sizeof(DWELL))) == NULL)
      {puts("not enough memory"); exit(0);}

   if((disp = (DISPLAY *)malloc(sizeof(DISPLAY))) == NULL)
      {puts("not enough memory"); exit(0);}

   if((radar = (RADAR *)malloc(sizeof(RADAR))) == NULL)
      {puts("not enough memory"); exit(0);}

   /* read in the display configuration file */
   readdisplay("",disp);

   if(argc < 2)
      	{
     	printf("Use: %s <remote_disk>\n",argv[0]);
      	exit(0);
      	}
   while(!(result = read_remdisk(dwell,radar, argv[1])))
   	{
      	usleep(10000);
	printf("Looking for throwa.way and temp.ray: %d \r", count++);
	if(count > 300)
		{
		printf("\n\nCannot find files.  Exiting\n");
		exit(0);
		}
	}

   /* do this AFTER init disk */
   if(!init_display(radar,dwell,disp)) {closeall(); exit(-1);}
   
   disp->recording = 1;  /* the word "play" will appear on display */
   zsave = disp->rgpp; 	/* /(3*1.414); */
   disp->rgpp =zsave;
   disp->type |=2; /* Put on the grid bit */
   disp->type |=4; /* Put on the VP bit */

   do
      {

      c = kbhit() ? toupper(getch()) : 1; /* get keyboard input (1 is benign) */
      
      if(c != 1)
      	{

      /* if c = 0 and there is already is another character, then */ 
      /* a function key was hit */
      if(c == 0) 
	 {
	 c = getch() - 0x3B;    /* parse the function key */
	 if(c < 6  || (c < 8 && dwell->header.dataformat == DATA_POL1))
	    {
	    disp->displayparm = c;  
	    selections(disp);
	    }
	 if(c == 8 || c == 9)
	 	{
		strcpy(tpath, argv[1]);
		strcat(tpath, "\\wrtoggle.trg");
		ftogp = fopen(tpath, "w");
		fwrite(dwell, sizeof(DWELL), 1, ftogp);
		fclose(ftogp);
		}
	 c = 1;        /* reset c to benign value */
	 }

      if(dwell->header.dataformat != DATA_POL1 && disp->displayparm > 5)
	 {
	 disp->displayparm = 0;  
	 selections(disp);
	 disp->displayparm = 5;  
	 selections(disp);
	 }

      if(c == ' '){skip = 1 - skip; hold = 1-hold;} 

      if(c == '1')   disp->rgpp *= 2.0;
      if(c == '2')   disp->rgpp /= 2.0;

      if(c == '3')   disp->type ^= 2;   /* flip the grid bit */
      if(c == '4')   disp->type ^= 4;   /* vertical profile bit */

      if(c == 'F')   disp->fakeangles = 1 - disp->fakeangles;
      if(c == 'T')   {disp->threshold += 0.05; if(disp->threshold > .99) disp->threshold=0.0;}
      if(c == 'U')   disp->threshold = 0.0;
      if(c == 'X') trcorrtog = trcorrtog == 1? 0: 1; /* Toggle TR correction */ 
      if(c == 'G') savescr(dwell->header.time); /* make a gif file */
      
      if(c == 'P' && disp->type != PPI)              {disp->type = PPI; dchange = 1; c = dispnum;}
      if(c == 'R' && disp->type != RHI)              {disp->type = RHI; dchange = 1; c = dispnum;}
      if(c == 'H')getchar();			/* Pause display */
      if(c >= '0' && c <= '9' && c != dispnum) dchange = 1;
      
      }
      
      result = 0;
      
      
      while(!(result = read_remdisk(dwell,radar, argv[1])))
      		{
		show_mouse(screen);
		mousecolor(disp);
		usleep(100);
		}
      disp->recording = remdisp->recording;

      if(oraycnt != dwell->header.ray_count)
        {
	oraycnt = dwell->header.ray_count;
        products(dwell,radar,prods);  /* compute scientific radar parameters */
        display(dwell,disp,prods);  /* write one dwell to display */
	}
      }while(c != 'Q' && c != 27);
   closeall();
   }

void closeall(void)
   {
   close_display();
   close_disk();
   close_tape();
   }

   




