/*MusicTimer.c
   Copyright (c) 1990,1991,1992 by Thomas E. Janzen
   All Rights Reserved

   THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
   BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
   CHANGES FOR FREE DISTRIBUTION.  NO TITLE TO AND OWNERSHIP OF THE
   SOFTWARE IS HEREBY TRANSFERRED.  THOMAS E. JANZEN ASSUMES NO 
   RESPONSBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
   
   Thomas E. Janzen
   58A School St. Apt. 2-L
   Hudson, MA 01749
   (508)562-1295
*/
/*
**  FACILITY:
**
**	AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
**	compiled with SAS/C V5.10b
**
**  ABSTRACT:
**
**	MusicTimer.c manages the timer device.
**	Time is kept in a double floating-point number in seconds.
**	Notes starting times are noted and when their length has passed
**	they are stopped.
**
**  AUTHORS: Thomas E. Janzen
**
**  CREATION DATE:	26-MAR-1990
**
**  MODIFICATION HISTORY:
**    DATE	NAME	DESCRIPTION
**    8 DEC 91 T. Janzen conform to SAS/C 5.10b remove extern from functs
**  4 Jan 92 TEJ  last changes for 2.0
**--
*/

#include "exec/types.h"
#include "exec/nodes.h"
#include "exec/lists.h"
#include "exec/memory.h"
#include "exec/interrupts.h"
#include "exec/ports.h"
#include "exec/libraries.h"
#include "exec/tasks.h"
#include "exec/io.h"
#include "exec/devices.h"
#include "devices/timer.h"
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/exec.h>
#include <proto/mathffp.h>
#include <proto/intuition.h>
#include "Window.h"
#include "MusicTimer.h"

struct Library *TimerBase;
struct timerequest *tr;

extern struct IORequest *CreateExtIO ();
extern struct timerequest *CreateTimer (ULONG unit);
int DeleteTimer(struct timerequest *);

struct timerequest *CreateTimer (ULONG unit)
{
	int error;
	struct MsgPort *timerport;
	struct timerequest *timermsg;

	timerport = CreatePort (NULL, 0);
	if(timerport == NULL) 
   {
		return (NULL);
	}
	timermsg = (struct timerequest *)
		CreateExtIO (timerport, sizeof (struct timerequest));
	if(timermsg == NULL)
   {
		return NULL;
	}
	error = OpenDevice (TIMERNAME, unit, (struct IORequest *)timermsg, 0L);
	if (error != 0)
   {
		DeleteTimer (timermsg);
		return NULL;
	}
	return timermsg;
}

int StartTimer (void)
{
   /* 
   ** start the timer at the beginning of the program
   */
	tr = CreateTimer (UNIT_MICROHZ);
	if (tr == 0) 
   {
      return -1;
   }
   TimerBase = (struct Library *)tr->tr_node.io_Device;
	return 0;
}

int GetSysTime (struct timeval *tv)
{	
   /* 
   ** get the time in a structure
   */
	tr->tr_node.io_Command = TR_GETSYSTIME;
	DoIO ((struct IORequest *)tr);
	*tv = tr->tr_time;
	return 0;
}

void RemoveTimer (void) 
{	
   /* 
   ** delete the timer at end of program 
   */
	DeleteTimer (tr);
   return;
}

int DeleteTimer (struct timerequest *tr)
{
	struct MsgPort *tp;
	if (tr != 0)
   {
		tp = tr->tr_node.io_Message.mn_ReplyPort;
		if (tp != 0)
      {
			DeletePort (tp);
		}
		if (tr) CloseDevice ((struct IORequest *)tr);
		DeleteExtIO ((struct IORequest *)tr, sizeof (struct timerequest));
	}
	return 0;
}
