/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>

#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/Scale.h>
#include <Xm/Label.h>
#include <Xm/Protocols.h>
#include <Xm/ScrollBar.h>
#include <Xm/RowColumn.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Xm/ToggleBG.h>
#include <Xm/MessageB.h>
#include "MagTape.h"


void quit_callback();

int n;
Arg wargs[1000];

#define NTAPES 4

Widget toplevel, main_wnd, form, mag_wnd[NTAPES], mag_form[NTAPES], magtapes[NTAPES], apply, menubar;

char *label[NTAPES];
char *geo[NTAPES];

XtErrorHandler MKXtDefaultError(String message)
{
  fprintf(stderr, "Error: %s\n", message);
  exit(-1);
}

static XtAppContext app;

static void maindestroy_callback(Widget w, XtPointer client_data, XtPointer call_data)
{
  exit(0);
}

static int cbcount[NTAPES];

static void done_cb(Widget w, void *null1, void *null2)
{
  int st;
  long unitNo=(long) null1;
#ifdef DEBUG
  printf("done_cb called %ld %ld\n", (long)null1, (long) null2);
#endif
  cbcount[unitNo]++;
  if(cbcount[unitNo]<(unitNo*11+7))
  {
    st=XsmagTapeCommand((XsMagTapeWidget) magtapes[unitNo], TAPE_FORWARD, BLOCKLEN(200,0,0));
  }
  else
  {
    st=XsmagTapeCommand((XsMagTapeWidget) magtapes[unitNo], TAPE_REWIND, 0.0);
    cbcount[unitNo]=0;
  }
#ifdef DEBUG
  printf("tape command %ld: %d\n",unitNo,st);
#endif
}

void main(argc, argv)
int argc;
char **argv;
{
  int local_argc;
  int first;
  long unitNo;
  int i,st;
  XmString s[20];
  XEvent event;
  int status;

  toplevel=XtAppInitialize(&app,"UAKK", NULL, 0, &argc, argv, NULL, NULL, 0);

#ifdef DEBUG
//XtSetErrorHandler(MKXtDefaultError);
#endif

#ifdef DEBUG
  printf("create window\n");
#endif
  /*
	Create the window stuff:
  */
  n=0;
  XtSetArg(wargs[n], XtNtitle, "GIER Simulator"); n++;
  XtSetArg(wargs[n], XtNgeometry, "+10+10"); n++;
  main_wnd = XmCreateMainWindow(toplevel, "main", wargs, n);
#ifdef DEBUG
  printf("manage\n");
#endif
  XtManageChild(main_wnd);
  XtRealizeWidget(toplevel);
#ifdef DEBUG
  printf("manage done\n");
#endif

  for(unitNo=0;unitNo<NTAPES;unitNo++)
  {
#ifdef DEBUG
    printf("create magtape %ld\n",unitNo);
#endif
    n=0;
    XtSetArg(wargs[n], XtNtitle, "MagTape"); n++;
    geo[unitNo] = malloc(100);
    sprintf(geo[unitNo], "247x350+%ld+40", unitNo*253+50);
    XtSetArg(wargs[n], XtNgeometry, geo[unitNo]); n++;
    mag_wnd[unitNo] = XtAppCreateShell("mag", "mag", topLevelShellWidgetClass, XtDisplay(toplevel), wargs, n);

    n=0;
    mag_form[unitNo]=XtCreateManagedWidget("mag_form", xmFormWidgetClass, mag_wnd[unitNo], wargs, n);

    n=0;
    XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); n++;
    XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); n++;
    XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); n++;
    XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); n++;
    XtSetArg(wargs[n], XmNwidth, 247); n++;
    XtSetArg(wargs[n], XmNheight, 350); n++;

    label[unitNo] = malloc(100);
    sprintf(label[unitNo], "Tape %ld", unitNo);
    XtSetArg(wargs[n], XtNtapeLabel, label[unitNo]); n++;
    magtapes[unitNo]=XtCreateManagedWidget("magtape", XsmagTapeWidgetClass, mag_form[unitNo],
       wargs, n);
    cbcount[unitNo]=0;
  XtAddCallback(magtapes[unitNo], XtNdone, done_cb, (void *) unitNo);
    XtRealizeWidget(mag_wnd[unitNo]);
  }

#ifdef DEBUG
  printf("realize\n");
#endif
#ifdef DEBUG
  printf("main loop\n");
#endif

  first=1;
  unitNo=0;

AGAIN:

  status=XtAppPending(app);

  if(status)
  {
AGAIN2:
#ifdef DEBUG
    printf("AGAIN2a: status=%d\n", status);
#endif
    XtAppProcessEvent(app, status);
    if(first)
    {
      st=XsmagTapeCommand((XsMagTapeWidget) magtapes[unitNo], TAPE_FORWARD, BLOCKLEN(200,0,0));
      unitNo++;
      if(unitNo==NTAPES) first=0;
    }

    goto AGAIN;
  }
  else
  {
#ifdef DEBUG
    printf("AGAIN:\n");
#endif
    goto AGAIN;
  }
//status=XtIMAll;
//goto AGAIN2;
}

