/* Timer.c - Timer support routines */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* |_o_o|\\ Copyright (c) 1987 The Software Distillery.  All Rights Reserved */
/* |. o.| || This program may not be distributed without the permission of   */
/* | .  | || the author.                                           BBS:      */
/* | o  | ||   John Toebes    Dave Baker                     (919)-471-6436  */
/* |  . |//                                                                  */
/* ======                                                                    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "handler.h"

int OpenTimer(global)
GLOBAL global;    
{
   int error;

   /* assumes that a msg port has been allocated */   

   if ((global->timerpkt = (struct TimerPacket *)
                CreateExtIO(global->port, sizeof(struct TimerPacket)))== NULL)
     return(1);

   global->timerpkt->tm_req.tr_node.io_Message.mn_Node.ln_Name = 
                                         (char *)&(global->timerpkt->tm_pkt);
   global->timerpkt->tm_pkt.dp_Link =
                                 &(global->timerpkt->tm_req.tr_node.io_Message);
   global->timerpkt->tm_pkt.dp_Port = global->port;

   if ((global->systimepkt = (struct timerequest *)
      CreateExtIO(global->port, sizeof(struct timerequest)))== NULL)
      return(1);

   error = OpenDevice(TIMERNAME, UNIT_MICROHZ, 
                         (struct IORequest *)&(global->timerpkt->tm_req), 0);

   /* Fill in the packet we will use for getting the system time */
   global->systimepkt->tr_node.io_Device =
                                     global->timerpkt->tm_req.tr_node.io_Device;
   global->systimepkt->tr_node.io_Unit   =
                                     global->timerpkt->tm_req.tr_node.io_Unit;
   return(error);
}

void GetDateStamp(global, date)
GLOBAL global;
struct DateStamp *date;
{
   register struct timerequest *tr = global->systimepkt;

   tr->tr_node.io_Command = TR_GETSYSTIME;
   tr->tr_node.io_Flags   = IOF_QUICK;
   DoIO((struct IORequest *)tr);
   MakeDateStamp(&tr->tr_time, date);
}

void CloseTimer(global)
GLOBAL global;
{
   if (global->timerpkt != NULL)
      {
      CloseDevice((struct IORequest *)&(global->timerpkt->tm_req));
      DeleteExtIO((struct IORequest *)global->timerpkt);
      global->timerpkt = NULL;
      }

   if (global->systimepkt != NULL)
      {
      DeleteExtIO((struct IORequest *)global->systimepkt);
      global->systimepkt = NULL;
      }
}

void PostTimerReq (global)
GLOBAL global;
{
   /* Fill in the timer packet values */
   /* that is the fields required for the timer device timerequest struct */
   /* and the necessary fields of the DosPacket struct                    */
   /* nothing like using 35 meg of store to accomplish a simple task      */
   /* oh well ! this is a 68K machine right ?                             */ 
   /* some of them get trampled on so fill them all */

   if (global->timerpkt != NULL)
      {
      global->timerpkt->tm_req.tr_node.io_Command = TR_ADDREQUEST;
      global->timerpkt->tm_req.tr_time.tv_secs = 2; 
      global->timerpkt->tm_req.tr_time.tv_micro = 0;

      global->timerpkt->tm_pkt.dp_Type = ACTION_TIMER;
      
      /* Async IO so we don't sleep here for the msg */
     
      SendIO((struct IORequest *)&global->timerpkt->tm_req);
      }
}
