
 /*****************************************************************
  *                                                               *
  *  BSP.c  -  "BackSpace inserter", or, "how to make life hard"  *
  *                                                               *
  *  written by F.van Leeuwen from original source from Rob Peck  *
  *                                                               *
  *  This is Public Domain.  Give it to anyone.  Please copy it!  *
  *  Spread it!  You may even try to sell it if someone's stupid  *
  *  enough to pay money for it! Please don't leave this header!  *
  *                                                               *
  *  The author of this program will NOT be responsible for any   *
  *  direct or indirect damage caused by this program, so use     *
  *  it with care! Switch it off before editing important text!   *
  *                                                               *
  *****************************************************************/

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/interrupts.h>
#include <devices/input.h>
#include <exec/devices.h>
#include <devices/inputevent.h>

#define TIME__OUT 100
#define F1KEYUP   0xD0
#define BSPP      0x41
#define BSPR      0xC1

long finish=0;

struct Library *DosBase, *OpenLibrary();

struct MsgPort *inputDevPort;
struct IOStdReq *inputRequestBlock;
struct Interrupt handlerStuff;

struct InputEvent dummyEvent;

extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();

struct MemEntry me[10];

struct InputEvent *myhandler(ev, mydata)
   struct InputEvent *ev;
   struct MemEntry *mydata[];
{
   long i,j;

/********************************************************************
 *   if (ev->ie_Class == IECLASS_RAWKEY && ev->ie_Code == F1KEYUP)  *
 *      finish=1;                                                   *
 ***************** Does not work...  (Don't know why.) **************/

   if (ev->ie_Class == IECLASS_TIMER)
   {
      i=(i+1)%TIME__OUT;
      if (i==TIME__OUT-2)
      {  ev->ie_Class = IECLASS_RAWKEY;
         ev->ie_SubClass = 0;
         ev->ie_Code =  BSPP;             /* BACKSPACE PRESSED */
         ev->ie_Qualifier = 0;
      }
      else if (i==TIME__OUT-1)
      {  ev->ie_Class = IECLASS_RAWKEY;
         ev->ie_SubClass = 0;
         ev->ie_Code = BSPR;              /* BACKSPACE RELEASED */
         ev->ie_Qualifier = 0;
      }
   }
   return ev;
}

extern VOID HandlerInterface(); /* Call 'myhandler' from a non-C caller */

main()
{  DosBase = (struct DosBase *) OpenLibrary("dos.library",0L);
   if (DosBase == NULL) exit (6L);

   /* init dummy event, this is what we will feed to other handlers
    * while this handler is active */
   
   dummyEvent.ie_Class = IECLASS_NULL;   /* no event happened */
   dummyEvent.ie_NextEvent = NULL;       /* only this one in the chain */
   
   inputDevPort = CreatePort(0L,0L);
   if(inputDevPort == NULL) exit(10L);
   inputRequestBlock = CreateStdIO(inputDevPort);     
   if(inputRequestBlock == 0L) { DeletePort(inputDevPort); exit(20L); }

   handlerStuff.is_Data = (APTR)&me[0];
   handlerStuff.is_Code = HandlerInterface;
   handlerStuff.is_Node.ln_Pri = 51;
   OpenDevice("input.device",0L,inputRequestBlock,0L);

   inputRequestBlock->io_Command = IND_ADDHANDLER;
   inputRequestBlock->io_Data = (APTR)&handlerStuff;
   DoIO(inputRequestBlock);

   Delay(6000L);         /* Main loop (non-idle!) about 2 minutes */

   inputRequestBlock->io_Command = IND_REMHANDLER;
   inputRequestBlock->io_Data = (APTR)&handlerStuff;
   DoIO(inputRequestBlock);

   CloseDevice(inputRequestBlock);
   DeleteStdIO(inputRequestBlock);
   DeletePort(inputDevPort);
   CloseLibrary(DosBase);
   puts("*** Your Amiga was alive! ***");
}
