/** timeout.c
*
*   Roll-yer-own Delay() function.
*
**/
#include <exec/exec.h>
#include <devices/timer.h>
#include <functions.h>

#define TRSIZE ((long) sizeof(struct timerequest))

TimeOut(ticks)
long ticks;
{
   long secs, micros;
   struct timerequest *Timereq = NULL;
   struct MsgPort *Timerport = 0L;

   if (ticks == 0L) return;

   Timerport = CreatePort(0L, 0L);
   if (Timerport == NULL) goto cleanup;

   Timereq = (struct timerequest *) AllocMem(TRSIZE, MEMF_PUBLIC | MEMF_CLEAR);
   if (Timereq == NULL) goto cleanup;

   if (OpenDevice(TIMERNAME, UNIT_VBLANK, Timereq, 0L) != NULL) goto cleanup;
/*
*  Set up timer request.
*/
   secs = ticks / 50L;
   micros = (ticks % 50L) * 20000L;

   Timereq->tr_node.io_Message.mn_ReplyPort = Timerport;
   Timereq->tr_node.io_Command = TR_ADDREQUEST;
   Timereq->tr_node.io_Flags = 0;
   Timereq->tr_node.io_Error = 0;
   Timereq->tr_time.tv_secs = secs;
   Timereq->tr_time.tv_micro = micros;
/*
*   Time out
*/
   SendIO(Timereq);
   Wait(1L << Timerport->mp_SigBit);
/*
*  Handle timer events.
*/
   GetMsg(Timerport);
/*
*   Clean up
*/
cleanup:
   if (Timereq) {
      if (Timereq->tr_node.io_Message.mn_ReplyPort) CloseDevice(Timereq);
      FreeMem(Timereq, TRSIZE);
   }
   if (Timerport) DeletePort(Timerport);

   return;
}
