/***************************************************************************

   'Mouse' was written to move the mouse pointer via keyboard commands.
   It was derived from various display hacks found on CompuServe.

   Usage:   Mouse                : Displays current pointer coordinates
            Mouse X Y            : Move pointer to X Y


   'Mouse' is Public Domain.  Please feel free to pass it anywhere
   you want.

   Comments should be sent to:   Denny Jenkins
                                 5226 Greensedge Way
                                 Columbus, Ohio  43220

                                 CompuServe ID: 70003,2374


****************************************************************************/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <lattice/stdio.h>
#include <devices/input.h>

#define  Coordinates  "Mouse: Coordinates are %d, %d\n"
#define  NoIntuition  "Error: Loading Intuition\n"
#define  NoWindow     "Error: Opening window\n"
#define  NoPort       "Error: Opening port\n"
#define  NoRequest    "Error: Creating I/O request\n"
#define  NoInput      "Error: Opening input.device\n"
#define  InvalidX     "Error: XCoordinate cannot be greater than 640\n"
#define  InvalidY     "Error: YCoordinate cannot be greater than 400\n"
#define  Usage        "Usage: Mouse XCoordinate YCoordinate\n"

struct Window *w=NULL, *OpenWindow();
struct Screen *s;

struct NewWindow nw = {
   0,0,3,10,
   -1,-1,
   NULL,
   NULL,
   NULL, NULL,
   NULL,
   NULL, NULL,
   0,0,0,0,
   WBENCHSCREEN
};

ULONG    IntuitionBase, OpenLibrary();
struct   MsgPort      *ioPort, *CreatePort();
struct   IOStdReq     *ioReq,  *CreateStdIO();
struct   InputEvent   Mouse;
void     OpenStuff(), CloseStuff();

main(ac, av)
int ac;
char *av[];
{
   OpenStuff();
   if (ac == 1) {
      printf (Coordinates,s->MouseX,s->MouseY*2);
      CloseStuff(NULL);
   }
   if (!(ac == 3))                                 CloseStuff (Usage);
   if (atoi(av[1]) > 640)                          CloseStuff (InvalidX);
   if (atoi(av[2]) > 400)                          CloseStuff (InvalidY);
   Mouse.ie_X = atoi (av[1]) - (s->MouseX);
   Mouse.ie_Y = atoi (av[2]) - (s->MouseY)*2;
   DoIO(ioReq);
   CloseStuff(NULL);
}

void OpenStuff()
{
   if (!(IntuitionBase = OpenLibrary("intuition.library",0L)))
                                                   CloseStuff (NoIntuition);
   if (!(ioPort = CreatePort("Mouse",NULL)))       CloseStuff (NoPort);
   if (!(ioReq = CreateStdIO(ioPort)))             CloseStuff (NoRequest);
   if (OpenDevice("input.device",0,ioReq,0))       CloseStuff (NoInput);
   if (!(w = OpenWindow(&nw)))                     CloseStuff (NoWindow);
   s = w->WScreen;

   ioReq->io_Command           = IND_WRITEEVENT;
   ioReq->io_Flags             = 0;
   ioReq->io_Length            = sizeof(struct InputEvent);
   ioReq->io_Data              = (APTR) &Mouse;
   Mouse.ie_NextEvent          = NULL;
   Mouse.ie_Class              = IECLASS_RAWMOUSE;
   Mouse.ie_TimeStamp.tv_secs  = 0;
   Mouse.ie_TimeStamp.tv_micro = 0;
   Mouse.ie_Code               = IECODE_NOBUTTON;
   Mouse.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
}

void CloseStuff(Message)
char *Message;
{
   if (ioReq)
                        if (ioReq->io_Device)
                        CloseDevice(ioReq);
                            DeleteStdIO(ioReq);
   if (ioPort)
                        DeletePort(ioPort);
   if (w)               CloseWindow(w);
   if (IntuitionBase)   CloseLibrary(IntuitionBase);
   if (Message)         fprintf (stderr, Message);
   _exit(!!Message);
}
