# include "Ximage.h"

int LoadOverlay (graphic, layout)
     Graphic graphic[];
     Layout  layout[];
{
  
  char line[17], *buffer, *buff, type[16], string[128];
  double x, y, dx, dy;
  int Nin, bytes, status;
  int i, N, NOBJECTS, Nobjects, Nstart, done;

  ALLOCATE (buffer, char, 65536);  /* space for 512 lines of 128 bytes */
  bzero (buffer, 65536);
  done = FALSE;

  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  

  status = read (layout[0].Ximage, line, 16); 
  line[16] = 0; 
  sscanf (line, "%*s %d", &N); 
  Nstart = layout[0].overlay[N].Nobjects;

  while (!done) {
    status = read (layout[0].Ximage, line, 16); 
    line[16] = 0; 
    
    if (!strncmp (line, "DONE", 4)) {
      done = TRUE;
      break;
    }

    if (!strncmp (line, "NLINES", 6)) {
      sscanf (line, "%*s %d", &Nin);
      buff = buffer;
      bytes = 128*Nin;
      fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
      while (bytes > 0) { 
	status = read (layout[0].Ximage, buff, bytes);
	if (status == 0) {  /* No more pipe */
	  fprintf (stderr, "error: pipe closed\n");
	  free (buffer);
	  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
	  return (FALSE);
	}
	if (status != -1) { /* pipe has data */
	  bytes -= status;
	  buff = (char *)(buff + status);
	}
      }
      fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  
      /* parse buffer data */
      NOBJECTS = layout[0].overlay[N].Nobjects + Nin;
      Nobjects = layout[0].overlay[N].Nobjects;
      REALLOCATE (layout[0].overlay[N].objects, Object, NOBJECTS);
      for (i = 0; i < Nin; i++) {
	sscanf (&buffer[i*128], "%s %lf %lf %lf %lf\n", type, &x, &y, &dx, &dy);

	if (strcasecmp (type, "TEXT") && strcasecmp (type, "LINE") && strcasecmp (type, "BOX") && strcasecmp (type, "CIRCLE")) {  /* skip */
	  fprintf (stderr, "don't know %s, skipping\n", type);
	  continue;
	}

	if (!strcasecmp (type, "TEXT")) { /* end of objects */
	  sscanf (&buffer[i*128], "%s %lf %lf %s\n", type, &x, &y, string);
	}
	
	strcpy (layout[0].overlay[N].objects[Nobjects].type, type);
	layout[0].overlay[N].objects[Nobjects].x = x;
	layout[0].overlay[N].objects[Nobjects].y = y;
	if (!strcmp (type, "TEXT")) {
	  layout[0].overlay[N].objects[Nobjects].dx = 0;
	  layout[0].overlay[N].objects[Nobjects].dy = 0;
	  layout[0].overlay[N].objects[Nobjects].text = strcreate (string);
	} else {
	  layout[0].overlay[N].objects[Nobjects].dx = dx;
	  layout[0].overlay[N].objects[Nobjects].dy = dy;
	}
	Nobjects++;
      }
      REALLOCATE (layout[0].overlay[N].objects, Object, MAX(Nobjects, 1));
      layout[0].overlay[N].Nobjects = Nobjects;
    }
  }

  free (buffer);
  REALLOCATE (layout[0].overlay[N].objects, Object, MAX (1, layout[0].overlay[N].Nobjects));
  OVERLAY[N] = TRUE;
  /*  Refresh (graphic, layout, 0); */
  if (USE_XWINDOW) {
    for (i = 0; i < NOVERLAYS; i++) {
      if (OVERLAY[i])
	PaintOverlay (graphic, layout, i);
    }
    PaintTickmarks (graphic, layout);
    XFlush (graphic[0].display);
  }
  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
  return (TRUE);

}


/* this routine is independent of the number of overlays */
