/*
*/
#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 6

Widget toplevel, main_wnd, form, magtape, apply, menubar;

char *labels[NDRIVES]={"tape 1","tape 2","tape 3","tape 4","tape 5","tape 6"};

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 *) null2;
#ifdef DEBUG
  printf("done_cb called %ld %ld %ld\n", (long)null1, (long) null2,unitNo);
#endif
  cbcount[unitNo]++;
  /*
  if(cbcount[unitNo]<(99+unitNo))
  {
    st=XsmagTapeCommand((XsMagTapeWidget) magtape, unitNo, TAPE_FORWARD, BLOCKLEN(200,0,0));
  }
  else
  {
    st=XsmagTapeCommand((XsMagTapeWidget) magtape, 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;
  int 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);
#ifdef DEBUG
  printf("manage done\n");
#endif

  n=0;
  XtSetArg(wargs[n], XtNtapeLabels, labels); n++;
  XtSetArg(wargs[n], XtNnDrives, NTAPES); n++;

  magtape=XtCreateManagedWidget("magtape", XsmagTapeWidgetClass, main_wnd,
       wargs, n);
  XtAddCallback(magtape, XtNdone, done_cb, NULL);

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

  first=1;
  unitNo=0;

AGAIN:

  status=XtAppPending(app);

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

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

