/* input.c
   created by John Schultz, 10-Sep-89
   
*/

#include <exec/types.h>
#include <exec/io.h>  
#include <exec/ports.h>
#include <exec/interrupts.h>  
#include <proto/exec.h>
#include <proto/graphics.h>
#include <devices/input.h>
#include <devices/inputevent.h>
#include "input.h"

unsigned short volatile raw; /* raw key value */

struct MsgPort indevport;
struct MsgPort * indevportptr;
struct IOStdReq * inreqblockptr;
struct Interrupt inhandler;
struct InputEvent * eventp;

extern void totalinput();

int createti(void) {

  indevportptr = CreatePort("indevport",0);
  if (!indevportptr) {
    return 0;
  }

  inreqblockptr = CreateStdIO((struct MsgPort *)indevportptr);
  if (!inreqblockptr) {
   DeletePort(indevportptr); 
   return 0;
  }

  inhandler.is_Data = (APTR)&raw;
  inhandler.is_Code = totalinput;
  inhandler.is_Node.ln_Pri = 51; /* One ahead of Intuition */

  if (OpenDevice("input.device",0L,
                 (struct IORequest *)inreqblockptr,0L)) {
    DeletePort(indevportptr);
    DeleteStdIO(inreqblockptr);
    return 0; 
  }

  inreqblockptr->io_Command = IND_ADDHANDLER;
  inreqblockptr->io_Data    = (APTR)&inhandler;
  
  if (DoIO((struct IORequest *)inreqblockptr)) {
    DeletePort(indevportptr);
    DeleteStdIO(inreqblockptr);
    CloseDevice((struct IORequest *)inreqblockptr);
    return 0;
  }

  GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  if(!GfxBase) {
    DeletePort(indevportptr);
    DeleteStdIO(inreqblockptr);
    CloseDevice((struct IORequest *)inreqblockptr);
    return 0;
  }
  FreeSprite(0);

  CloseLibrary((struct Library *)GfxBase);

  return 1;  

} /* end createti */

void deleteti(void) {

  if ((indevportptr) && (inreqblockptr)) {
    inreqblockptr->io_Command = IND_REMHANDLER;
    inreqblockptr->io_Data = (APTR)&inhandler;
    DoIO((struct IORequest *)inreqblockptr);
    CloseDevice((struct IORequest *)inreqblockptr);
    DeleteStdIO(inreqblockptr);
    DeletePort(indevportptr);
  } /* end if */

} /* END deleteti */


/* END input.c */
