/* esuom - my first screen hack! */
/* Sean Riddle  7/87 */
/* Based on RKM Vol I, Pg 3-108, input.device IND_ADDHANDLER sample prog */
/* OKC ACE BBS: (405) 631-9040 */

#include <intuition/intuition.h>
#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>

struct IntuitionBase *IntuitionBase;
struct IntuiMessage *GetMsg();
struct IntuiMessage *msg;
struct Window *OpenWindow();
struct Window *Window;

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

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

struct MemEntry me[10];

struct NewWindow nw = {
   50,50,
   73,10,
   -1,-1,
   CLOSEWINDOW,
   WINDOWCLOSE|ACTIVATE|WINDOWDRAG,
   NULL,
   NULL,
   "Esuom",
   NULL,
   NULL,
   0,0,0,0,
   WBENCHSCREEN
};

USHORT pointer[]=
{
0x0000,0x0000,

0x0000,0x0000,
0x0000,0x0000,
0x0000,0x0000,
0x0000,0x0000,
0x0000,0x0000,
0x0000,0x0000,
0x0000,0x0100,
0x0100,0x0280,
0x0380,0x0640,
0x01C0,0x0320,
0x00E0,0x0196,
0x0076,0x00C9,
0x003E,0x0061,
0x001E,0x0031,
0x003E,0x0061,
0x003E,0x007F,
0x0000,0x003F,
0x0000,0x0000,

0x0000,0x0000
};

void cleanup(); /* must declare since it doesn't return an int */

/* my original input handler - too slow, caused stack overflows
   unless stack was approx 100000!
   rewritten in ASM - see InterfaceHandler.asm

struct InputEvent *myhandler(ev,mydata)
struct InputEvent *ev;
struct MemEntry *mydata[];
{
   if(ev->ie_Class==IECLASS_RAWMOUSE) {
      ev->ie_X=-ev->ie_X;
      ev->ie_Y=-ev->ie_Y;
      if(ev->ie_Code==IECODE_LBUTTON)
         ev->ie_Code=IECODE_RBUTTON;
      else if(ev->ie_Code==IECODE_RBUTTON)
         ev->ie_Code=IECODE_LBUTTON;
      if(ev->ie_Code==IECODE_RBUTTON+IECODE_UP_PREFIX)
         ev->ie_Code=IECODE_LBUTTON+IECODE_UP_PREFIX;
      else if(ev->ie_Code==IECODE_LBUTTON+IECODE_UP_PREFIX)
         ev->ie_Code=IECODE_RBUTTON+IECODE_UP_PREFIX;
   }
   return(ev);
}
*/

extern VOID HandlerInterface(); /* our input handler */

void main(argc,argv)
int argc;
char *argv;
{
   ULONG class;

   printf("Esuom - 7/15/87 by Sean Riddle - OKC ACE BBS (405) 631-9040\n");
   if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0)))
      cleanup("Intuitionless.");
   if(!(Window=(struct Window *)OpenWindow(&nw)))
      cleanup("Window stuck.");
   SetPointer(Window,pointer,18,16,-15,-14);
   if(!(inputDevPort=CreatePort(0,0)))
      cleanup("No port in sight.");
   if(!(inputRequestBlock=CreateStdIO(inputDevPort)))
      cleanup("IO not created.");
   handlerStuff.is_Data=(APTR)&me[0];
   handlerStuff.is_Code=HandlerInterface;
   handlerStuff.is_Node.ln_Pri=51;  /* puts handler before Intuition() */
   if(OpenDevice("input.device",0,inputRequestBlock,0))
      cleanup("No input.device.");
   inputRequestBlock->io_Command=IND_ADDHANDLER;
   inputRequestBlock->io_Data=(APTR)&handlerStuff;
   DoIO(inputRequestBlock);
   for(;;) {
      Wait(1<<Window->UserPort->mp_SigBit);
      if(msg=(struct IntuiMessage *)GetMsg(Window->UserPort)) {
         class=msg->Class;
         ReplyMsg(msg);
         if(class==CLOSEWINDOW)
            break;
      }
   }
   inputRequestBlock->io_Command=IND_REMHANDLER;
   inputRequestBlock->io_Data=(APTR)&handlerStuff;
   DoIO(inputRequestBlock);
   cleanup("Mouse interface corrected....");
}

void cleanup(text) /* get rid of everything we've gotten */
char *text;
{
   printf("%s\n",text);

   if(inputRequestBlock) {
      if (inputRequestBlock->io_Device)
         CloseDevice(inputRequestBlock);
      DeleteStdIO(inputRequestBlock);
   }
   if(inputDevPort)
      DeletePort(inputDevPort);
   if(Window) {
      ClearPointer(Window);
      CloseWindow(Window);
   }
   if(IntuitionBase)
      CloseLibrary(IntuitionBase);
   exit(0);
}

