# include "Ximage.h"

void DefineLayout (Layout *layout, Graphic *graphic, int argc, char **argv) {

  int i, status, N;
  struct sockaddr_un Address;
  char temp[100];

  DEBUG = FALSE;
  if ((N = get_argument (argc, argv, "-debug"))) {
    remove_argument(N, &argc, argv);
    DEBUG = TRUE;
  }

  /** initiate connection with mana **/
  if (!DEBUG) {	
    if (argc < 2) {
      fprintf (stderr, "socket path not specified\n");
      exit (0);
    }
    strcpy (Address.sun_path, argv[1]);
    Address.sun_family = AF_UNIX;
    layout[0].Ximage = socket (AF_UNIX, SOCK_STREAM, 0);
    status = connect (layout[0].Ximage, (struct sockaddr *) &Address, (socklen_t) sizeof (Address));
    if (status < 0) {
      fprintf (stderr, "kii - unsuccessful connection: %d\n", status);
      exit (0);
    }
    fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
    sprintf (temp, "rm -f %s", argv[1]);
    system (temp);
  }

  PositionPictures (layout, graphic);
  
  /* get the display colors */
  MakeColormap (graphic, layout, argc, argv);

  /** set up a bunch of default things **/
  layout[0].X = layout[0].Y = 0;
  layout[0].expand = 1;
  layout[0].zero = 0;
  layout[0].range = 1;
  layout[0].coords.Npolyterms = 0;
  for (i = 0; i < NOVERLAYS; i++) {
    layout[0].overlay[i].Nobjects = 0;
    ALLOCATE (layout[0].overlay[i].objects, Object, 1);  /* allocate so later free will not crash! */
    OVERLAY[i] = FALSE;
  }
  layout[0].matrix.size = 0; /* a flag to show there is no data in the matrix */

  MOVE_POINTER = TRUE;
  DECIMAL_DEG  = TRUE;
  ALLOCATE (layout[0].matrix.buffer, char, 1);  /* allocate so later free will not crash! */
  ALLOCATE (layout[0].picture.data, char, 1);   /* allocate so later free will not crash! */
  ALLOCATE (layout[0].cmapbar.data, char, 1);   /* allocate so later free will not crash! */
  ALLOCATE (layout[0].zoom.data, char, 1);      /* allocate so later free will not crash! */

  CreatePicture (layout, graphic);
  CreateColorbar (layout, graphic);
  CreateZoom (layout, graphic, 0, 0); 
}


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

  /* cmap = the mapping of value to color in kii
     colormap = the set of pixel -> color representation in X */

