#include <stdio.h>

#include "DeviceToolKits/Input.h"
#ifndef  TESTPROTO
#define  NO_PRAGMAS  1
#endif
#include "DeviceToolKits/proto/Input.h"
#include "DeviceToolKits/InputBase.h"

typedef  void  (*VoidFunc)();

DTInput_t            Unit0;
struct   Interrupt   handler_setup;
struct   InputEvent  event_copy;
int                  found_key;
int                  event_count;

#ifdef   MANX
APTR                 GetA4();
void                 ManxHandler();

#asm
   xdef     _ManxHandler
   xdef     _GetA4
   xref     _ehandler

_GetA4:
   move.l   a4,d0
   rts

_ManxHandler:
   movem.l  d2-d7/a2-a6,-(sp)
   move.l   a1,a4
   move.l   a0,-(sp)
   jsr      _ehandler
   addq.l   #4,sp
   movem.l  (sp)+,d2-d7/a2-a6
   rts
#endasm
#endif

#ifdef   SAS
APTR  __saveds __asm ehandler(register __a0 struct InputEvent *events,
                              register __a1 struct InputEvent *copy)
#endif
#ifdef   MANX
APTR  ehandler(events)
struct   InputEvent  *events;
#endif
{
   register struct   InputEvent  *ep, *laste;

   event_count++;

   for (ep = events, laste = NULL; ep != NULL; ep = ep->ie_NextEvent) {
      if ((ep->ie_Class == IECLASS_RAWKEY) &&
         (ep->ie_Code == 0x45) &&
         (ep->ie_Qualifier & IEQUALIFIER_LCOMMAND)) {
         if (laste == NULL) {
            events = ep->ie_NextEvent;
         }
         else {
            laste->ie_NextEvent = ep->ie_NextEvent;
         }
         found_key = 1;
#ifdef   SAS
         *copy = *ep;
#endif
#ifdef   MANX
         event_copy = *ep;
#endif
      }
      else {
         laste = ep;
      }
   }
   return ((APTR) events);
}

main(argc,argv)
int   argc;
char  *argv[];
{
   long  status;

#ifdef   TESTSHARED
/*  Open the Input library  */

   DTInputOpen(DTINPUTREV);
#endif

/*  Try to initialize unit 0 of the Input  */

   if ((Unit0 = DTInputCreate(&status)) == NULL) {
      fprintf(stderr,"Error initializing unit 0 = %ld.\n",status);
      fflush(stderr);
      goto cleanup;
   }

/*  Initialze the input event handler  */

   found_key = 0;
   event_count = 0;
   handler_setup.is_Node.ln_Pri = 51;
#ifdef   SAS
   handler_setup.is_Code = (VoidFunc) ehandler;
   handler_setup.is_Data = (APTR) &event_copy;
#endif
#ifdef   MANX
   handler_setup.is_Code = (VoidFunc) ManxHandler;
   handler_setup.is_Data = (APTR) GetA4();
#endif

   printf("Press <Left-Amiga Escape> to exit.\n");

/*  Set up the input event handler  */

   if ((status = DTInputAddHandler(Unit0,&handler_setup)) != 0) {
      fprintf(stderr,"Error adding handler = %ld,%ld.\n",
               status,Unit0->in_error);
      fflush(stderr);
      goto cleanup;
   }

/*  Wait for the LeftAmiga-Escape sequence to be pressed  */

   while (TRUE) {
      if (found_key) {
         break;
      }
      Delay(15L);
   }

   printf("Key sequence hit.\n");
   fflush(stdout);

/*  Remove up the input event handler  */

   if ((status = DTInputRemHandler(Unit0,&handler_setup)) != 0) {
      fprintf(stderr,"Error removing handler = %ld,%ld.\n",
               status,Unit0->in_error);
      fflush(stderr);
      goto cleanup;
   }

/*  Free the stuff  */

cleanup:
   DTInputFree(Unit0);
#ifdef   TESTSHARED
   DTInputClose();
#endif
}

