/*
*/
#include <stdio.h>
#include <stdlib.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, 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,200));
  }
  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++;
  geo[unitNo] = malloc(100);
  sprintf(geo[unitNo], "247x350+%ld+40", unitNo*253+20);
  XtSetArg(wargs[n], XtNgeometry, geo[unitNo]); n++;
  main_wnd = XmCreateMainWindow(toplevel, "main", wargs, n);
#ifdef DEBUG
  printf("manage\n");
#endif
  XtManageChild(main_wnd);
#ifdef DEBUG
  printf("manage done\n");
#endif

  form=XtCreateManagedWidget("form", xmFormWidgetClass, main_wnd, wargs, n);

  for(unitNo=0;unitNo<NTAPES;unitNo++)
  {
#ifdef DEBUG
  printf("create magtape %ld\n",unitNo);
#endif
  cbcount[unitNo]=0;
  n=0;
  if(unitNo==0)
  {
    XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); n++;
  }
  else
  {
    XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
    XtSetArg(wargs[n], XmNleftWidget, magtapes[unitNo-1]); n++;
  }
  if(unitNo==(NTAPES-1))
  {
    XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); n++;
  }
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); n++;
  XtSetArg(wargs[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  label[unitNo] = malloc(100);
  sprintf(label[unitNo], "Tape %ld", unitNo);
  XtSetArg(wargs[n], XtNtapeLabel, label[unitNo]); n++;
  magtapes[unitNo]=XtCreateManagedWidget("magtape", XsmagTapeWidgetClass, form,
       wargs, n);
  XtAddCallback(magtapes[unitNo], XtNdone, done_cb, (void *) unitNo);
  while(!XsmagTapeIdle((XsMagTapeWidget)magtapes[unitNo]))
  {
#ifdef DEBUG
    printf("wait for idle unit %ld\n",unitNo);
#endif
  }
  }

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

  first=1;

AGAIN:

  status=XtAppPending(app);

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

    goto AGAIN;
  }
  status=XtIMAll;
  goto AGAIN2;
}

