/* :ts=8 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "general.h"
#include "globals.h"
#include "intui.h"
#include "timer.h"

static Boolean_T	Timer_Enabled 	= FALSE;
static Boolean_T	Timer_Expired 	= FALSE;
static Boolean_T	Initialize_Time = TRUE;

static ULONG		Stop_Seconds	= 0;
static ULONG		Stop_Micros	= 0;

static
void Enable_IntuiTicks(Boolean_T On)
/************************************************************************/
/*                                                                      */
/* Enable or disable generation of IDCMP_INTUITICKS messages.		*/
/*                                                                      */
/************************************************************************/
{
    long	Status;
    ULONG 	IDCMP_Flags;


    IDCMP_Flags = Windows[W_Main].Window->IDCMPFlags;

    if (On) IDCMP_Flags |= IDCMP_INTUITICKS;
    else    IDCMP_Flags &= ~IDCMP_INTUITICKS;

    if (ModifyIDCMP(Windows[W_Main].Window, IDCMP_Flags) == NULL) {

    	Display_Error_Message("Couldn't modify IDCMP port\n");
	CloseStuff();
	exit(3);

    } /* if */

} /* Enable_IntuiTicks */

Boolean_T Handle_IntuiTick_Message(struct IntuiMessage *Msg)
/************************************************************************/
/*                                                                      */
/* This function should be called on receipt of an INTUITICK message.	*/
/*                                                                      */
/* It will do the following:						*/
/*                                                                      */
/* 1: If the timer isn't enabled, then disable intuiticks (we shouldn't	*/
/*    get these if the timer isn't enabled).				*/
/*                                                                      */
/* 2: If 'Initialize_Time' is TRUE, then the timer has been started,	*/
/*    but we still need to compute stop time for the timer, so do this	*/
/*    and return.							*/
/*                                                                      */
/* 3: Otherwise, check if the timer has expired. In this case disable	*/
/*    IntuiTicks, set 'Timer_Expired' to TRUE, and return TRUE.		*/
/*                                                                      */
/************************************************************************/
{
    if (!Timer_Enabled) {
        Enable_IntuiTicks(FALSE);
	return(FALSE);
    }

    if (Initialize_Time) {

	/* Compute stop time based on current time from message */

        Stop_Micros += Msg->Micros;
        Stop_Seconds += Msg->Seconds;

        if (Stop_Micros >= 1000000) {

	    Stop_Micros -= 1000000;
	    Stop_Seconds += 1;

        } /* if */
	Initialize_Time = FALSE;
	return(FALSE);
    }
    
    if (Msg->Seconds > Stop_Seconds ||
	(Msg->Seconds == Stop_Seconds && Msg->Micros > Stop_Micros)) {

	/* Timer has expired.			*/	

	Timer_Expired = TRUE;
        Timer_Enabled = FALSE;
        Enable_IntuiTicks(FALSE);

	return(TRUE);

    }  /* if */

    return(FALSE);

} /* Handle_IntuiTick_Message */

void Start_Timer(unsigned long Seconds, unsigned long Micros)
/************************************************************************/
/*                                                                      */
/* Start the timer.							*/
/*                                                                      */
/************************************************************************/
{
    Stop_Micros  = Micros;
    Stop_Seconds = Seconds;

    if (!Timer_Enabled) Enable_IntuiTicks(TRUE);
    Timer_Enabled   = TRUE;
    Initialize_Time = TRUE;	/* Compute stop time at first tick */
    Timer_Expired   = FALSE;

} /* Start_Timer */

void Stop_Timer()
/************************************************************************/
/*                                                                      */
/* Stop the timer.							*/
/*                                                                      */
/************************************************************************/
{
    if (Timer_Enabled) Enable_IntuiTicks(FALSE);
    Timer_Enabled = FALSE;
    Timer_Expired = FALSE;

} /* Stop_Timer */

Boolean_T Check_Timer()
/************************************************************************/
/*                                                                      */
/* Return TRUE if timer has expired. 					*/
/*                                                                      */
/************************************************************************/
{
    return(Timer_Expired);

} /* Check_Timer */
