/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* |_o_o|\\ Copyright (c) 1990 The Software Distillery.  All Rights Reserved.*/
/* |. o.| || This program may not be distributed without the permission of   */
/* | .  | || the authors.                                                    */
/* | o  | ||                                                                 */
/* |  . |// Written by Doug Walker                                           */
/* ======          BBS:(919)-471-6436      VOICE:(919)-467-4764              */

/*                 BIX: djwalker           USENET: ...!mcnc!rti!sas!walker   */
/*                 405 B3 Gooseneck Dr, Cary, NC 27513, USA                  */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * * * * * * * * INCLUDE FILES * * * * * * * * * * * */

#include "netkeys.h"

struct DChannel *chan;

struct MsgPort *lisport;

int InitDevice(void);
int TermDevice(void);

void _main(char *);

void _main(x)
char *x;
{
   struct MsgPort  *inputDevPort = NULL;
   struct IOStdReq *inputRequestBlock = NULL;
   struct InputEvent ie;
   ULONG rc;

   if (
       InitDevice()                                               ||

       ((inputDevPort = CreatePort(0,0)) == NULL)                           ||

       ((inputRequestBlock = 
               CreateIOReq(inputDevPort, sizeof(struct IOStdReq))) == NULL) ||

      OpenDevice("input.device",0,(struct IORequest *)inputRequestBlock,0))
   {
      goto abort;
   }

   inputRequestBlock->io_Command = IND_WRITEEVENT;
   inputRequestBlock->io_Flags   = 0;
   inputRequestBlock->io_Length  = IESZ;
   inputRequestBlock->io_Data    = (APTR)&ie;

   while(1)
   {
      rc = Wait(SIGBREAKF_CTRL_C | (1 << chan->port.mp_SigBit));

      if(rc & SIGBREAKF_CTRL_C)          /* Received ctrl-c */
         goto abort;

      if(DRead(chan, (char *)&ie, IESZ) != IESZ)
         goto abort;

      ie.ie_TimeStamp.tv_secs = 
      ie.ie_TimeStamp.tv_micro = 0;
      ie.ie_NextEvent = NULL;
      DoIO(inputRequestBlock);
   }

abort:
   TermDevice();

   if (inputRequestBlock != NULL)
   {
      if (inputRequestBlock->io_Device != NULL)
         CloseDevice((struct IORequest *)inputRequestBlock);
      DeleteIOReq(inputRequestBlock);
   }

   if (inputDevPort != NULL)  DeletePort(inputDevPort);
}

int InitDevice(void)
{
   struct MsgPort *myport;
   struct Message *msg;

   chan = NULL;
   lisport = NULL;

   myport = &(((struct Process *)FindTask(0L))->pr_MsgPort);
   lisport = DListen(PORT_NETKEYS);
   do
   {
      WaitPort(myport);
   }
   while(!(msg=GetMsg(myport)));

   ReplyMsg(msg);

   if(!lisport) return(1);

   Wait(1<<lisport->mp_SigBit);
   
   if(!(chan = (struct DChannel *)DAccept(lisport)))
      return(1);

   return(0);
}

int TermDevice(void)
{
   if(chan)
   {
      DClose(chan);
      chan = NULL;
   }

   if(lisport)
   {
      DUnListen(lisport);
      lisport = NULL;
   }

   return(0);
}

void MemCleanup(void);
void MemCleanup(){}
