/* $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/timer/RCS/timer.c,v 1.1 90/12/11 13:31:58 mar Exp Locker: brlewis $ */

#include <stdio.h>
#include <sys/time.h>
#include <X11/Intrinsic.h>
#include "Wc/WcCreate.h"
#include <X11/StringDefs.h>

extern void AriRegisterAthena ();
void localErrorHandler(), resetCB(), tick();

/* Definition of the Application resources structure. */
typedef struct _XLoginResources {
  int startTime;
  char *command;
} XLoginResources;

/* Command line options table.  Only resources are entered here...there is a
 * pass over the remaining options after XtParseCommand is let loose. 
 */
static XrmOptionDescRec options[] = {
  {"-start",	"*startTime",		XrmoptionSepArg,	NULL},
  {"-command",	"*command",		XrmoptionSepArg,	NULL},
};

/* The structure containing the resource information for the
 * Xlogin application resources.
 */
#define Offset(field) (XtOffset(XLoginResources *, field))

static XtResource my_resources[] = {
  {"startTime", XtCInterval, XtRInt, sizeof(int),
     Offset(startTime), XtRImmediate, (caddr_t) 300},
  {"command", XtCFile, XtRString, sizeof(String),
     Offset(command), XtRImmediate, "/bin/true"},
};

#undef Offset


/*
 * Globals.
 */
unsigned int seconds, maxseconds;
Widget appShell;
XLoginResources resources;
static Widget clock_widget = NULL;
Pixel fgclock, bgclock;

/******************************************************************************
*   MAIN function
******************************************************************************/

void
main(argc, argv)
     int argc;
     char* argv[];
{   
  XtAppContext app;
  Arg args[1];
  int i;

  /*
   *  Intialize Toolkit creating the application shell, and get
   *  application resources.
   */
  appShell = XtInitialize ("timer", "Timer",
			   options, XtNumber(options),
			   &argc, argv);
  app = XtWidgetToApplicationContext(appShell);
  XtAppSetErrorHandler(app, localErrorHandler);
  XtGetApplicationResources(appShell, (caddr_t) &resources, 
			    my_resources, XtNumber(my_resources),
			    NULL, (Cardinal) 0);
  WcRegisterCallback(app, "resetCB", resetCB, NULL);

  AriRegisterAthena ( app );
  WcWidgetCreation ( appShell );
  XtRealizeWidget ( appShell );

  XtAddTimeOut(1000, tick, NULL);
  seconds = maxseconds = resources.startTime;
  XtMainLoop ( );
}


void resetCB()
{
  Arg args[2];
  int i=0;

  /* return foreground/background color to normal */
  XtSetArg(args[i], XtNforeground, fgclock); i++;
  XtSetArg(args[i], XtNbackground, bgclock); i++;
  XtSetValues(clock_widget, args, i);

  /* give user more time depending on how long the timer has been going */
  maxseconds += maxseconds - seconds;
  seconds = maxseconds;
}


void tick()
{
    Arg args[3];
    char buf[256];
    int i=0;

    if (clock_widget == NULL) {
        clock_widget = WcFullNameToWidget(appShell, "*display");

	/* get original foreground/background colors */
        XtSetArg(args[i], XtNforeground, &fgclock); i++;
	XtSetArg(args[i], XtNbackground, &bgclock); i++;
	XtGetValues(clock_widget, args, i);
	i=0;
    }

    sprintf(buf, " %2d:%02d ", seconds/60, seconds%60);
    XtSetArg(args[i], XtNlabel, buf); i++;
    if (seconds < 30) {
        /* flash to indicate time running out */
        if (seconds%2) {
	  XtSetArg(args[i], XtNforeground, fgclock); i++;
	  XtSetArg(args[i], XtNbackground, bgclock); i++;
	} else {
	  XtSetArg(args[i], XtNforeground, bgclock); i++;
	  XtSetArg(args[i], XtNbackground, fgclock); i++;
	}
    }
    XtSetValues(clock_widget, args, i);
    if (seconds == 0) {
	XFlush(XtDisplay(appShell));
	system(resources.command);
	exit(0);
    }
    XtAddTimeOut(1000, tick, NULL);
    seconds--;
}


/* Called from within the toolkit */
void localErrorHandler(s)
String s;
{
    fprintf(stderr, "Timer X error: %s\n", s);
    exit(1);
}


