# include "Ximage.h"
# define STRCONST(A) ((int)(0x1000000*A[0] + 0x10000*A[1] + 0x100*A[2] + 0x1*A[3]))
# define DEBUG 0

static int ActiveCursor = FALSE;

int CheckPipe (Graphic *graphic, Layout *layout) {

  int status;
  char buffer[20];

  /* attempt to read 4 bytes from stdin */
  bzero (buffer, 20);
  status = read (layout[0].Ximage, buffer, 4);
  buffer[4] = 0;

  switch (status) {
  case -1:
    /* no input from pipe: continue */
    return (TRUE);
    break;

  case 0:
    fprintf (stderr, "pipe has died!\n");
    return (FALSE);
    break;

  case 4:
    if (DEBUG) fprintf (stderr, "got signal %s from pipe\n", buffer);
    break;

  default:
    fprintf (stderr, "weird signal: too many or few bytes!  %d\n", status);
    return (TRUE);
    break;
  }
  
  /* In Active Cursor mode, we can only receive the NCUR signal */
  if (ActiveCursor) {
    if (!strcmp (buffer, "NCUR")) {
      ActiveCursor = FALSE;
      return (2);
    }
    fprintf (stderr, "unknown signal (AC) %s\n", buffer);
    return (TRUE);
  }

  /* If Active Cursor mode is off, we can receive other signal */
  if (!strcmp (buffer, "QUIT")) {
    if (DEBUG) fprintf (stderr, "Goodbye!\n");
    return (FALSE);
  }
  
  if (!strcmp (buffer, "READ")) {
    status = NewPicture (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "CURS")) {
    ActiveCursor = TRUE;
    status = cursor (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "NCUR")) {
    fprintf (stderr, "error: not in active cursor mode\n");
    return (TRUE);
  }

  if (!strcmp (buffer, "ERAS")) {
    status = EraseOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "LOAD")) {
    status = LoadOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "TICK")) {
    status = LoadTickmarks (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "SAVE")) {
    status = SaveOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "PSIT")) {
    /** this one needs work : ConvertPixmapN fails for !USE_XWINDOW */
    status = PSit (graphic, layout, 0);
    write (layout[0].Ximage, "DONE", 4);
    return (status);
  }

  if (!strcmp (buffer, "PSRW")) {
    /** this one needs work : ConvertPixmapN fails for !USE_XWINDOW */
    status = PSit (graphic, layout, 1);
    write (layout[0].Ximage, "DONE", 4);
    return (status);
  }

  if (!strcmp (buffer, "JPEG")) {
    status = JPEGit24 (graphic, layout);
    write (layout[0].Ximage, "DONE", 4);
    return (status);
  }

  if (!strcmp (buffer, "RSIZ")) {
    status = Resize (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "CSVE")) {
    status = CSaveOverlay (graphic, layout);
    return (status);
  }

  if (!strcmp (buffer, "CENT")) {
    status = Center (graphic, layout);
    return (status);
  }

  /* is this no longer used ?
  if (!strcmp (buffer, "NPIX")) {
    sprintf (buffer, "NPIX:   %8d", layout[0].Npixels);
    write (layout[0].Ximage, buffer, 16);
    return (TRUE);
  }
  */

  fprintf (stderr, "unknown signal %s\n", buffer);

  return (TRUE);

}


    
