/* PROGRAM : AutoIconOpen.c
   AUTHOR  : Tony (**all at sea in C**) Wills.
   DATE    : 15 FEB 87.
   VERSION : 1.2
   PURPOSE : To fool WorkBench into thinking it is recieving mouse inputs
             that select & open icons.
             Original application was to automatically open your disk icon
             on bootup (Could be easily expanded to click open a file
             of instructions as well).  For use in our NewsDisk
             (=newsletter on a disk, like JumpDisk and Amigazine).
   USE :
   1) Install "AutoIconOpen" in your Workbench (or other bootable disk)
      "c:" directory.
   2) Put the command "AutoIconOpen" into your startup-sequence after
      "LoadWB". You may possibly need a slight delay (use "Wait" command)
      in between, to ensure that Workbench is up and going before this runs
   3) Boot your disk and this program will then blindly try and double
      click on whatever disk icon is sitting in the upper right hand corner
      (that is the 'default' position for the first disk icon) of your
      screen.
   NB The disk icon that normally resides in the upper right hand corner
      of your screen will be that of df0: if thats the only disk inserted
      at LoadWB time.  If you have something in df1: Workbench will put
      it's icon in that position with that of df0: below it.  Hence this
      program will open df1: instead of df0:.

   This is a very simplistic program, and ignores all sorts of conventions
   that might otherwise ensure it will work in many different environments.
   (Just like the examples in the Amiga Technical manuals !!!, which this
    program is based on.)
   1) It assumes that the screen is 640x200 on startup
   2) It assumes there is a cli window in front with it's bottom corner
      at 640/400. It resizes this window to 540x200
   3) This is expected to expose the disk icons residing in the top right
      hand corner of the screen.
   4) It opens the icon in the top righthand position by sending a double
      click sequence.
   5) All done, so closes input.device and exits.

   Bugs, oversights, and possible improvements :
   1) Doesn't allocate anything in public address space so won't work on
      future hardware with a MMU that stops programs getting at others
      address space - I'm sure that this is the least of this programs
      problems!
   2) I thought I was working on a 640x200 screen but have to move twice
      as far vertically as I would expect!
   3) Could string all events together using ie_NextEvent pointers, but not
      much point for this application.
   4) If some twit moves the mouse during the execution of this program,
      then who knows where it will end up.  Perhaps stringing all the
      events together (as in 3. above) would ensure these spurious
      movements didn't interfere.

   Revisions : 8 JUN 87
        : Inserted CreatePort, so the the system had somewhere to reply
   to messages, which we then ignore!  Lettuce C (Aka Lattice C) has this
   CreatePort thing but it's not a standard system call, so couldn't add
   this to the Assembler version - case of not enough documentation and not
   knowing what I'm doing anyway.
        Added bit to blat pointer into top left corner (0,0) first (it's
   normally there on bootup anyway) just to make sure we get where we want
   to go with relative pointer movement (haven't got around to pinning
   down absolute pointer positioning commands).  And don't go hard into
   bottom right corner (640,400) but stop at (638,398) to ensure we really
   are sitting on the sizing gadget (or where it should be).
   It now works under AmigaDOS 1.1 and 1.2.
*/

#include <exec/types.h>
#include <exec/io.h>
#include <devices/input.h>
#include <exec/devices.h>
#include <devices/inputevent.h>

#include <exec/ports.h>

struct MsgPort    *inputDevPort;
struct IOStdReq   *input_request_block;
struct InputEvent  SimulatedEvent;

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

main()
{
  if ((inputDevPort = CreatePort(0,0)) == NULL) exit(-1);
  if ((input_request_block = CreateStdIO(inputDevPort)) == 0) exit (-2);
  if (OpenDevice("input.device",0,input_request_block,0) != 0) exit(-1);

input_request_block->io_Command = IND_WRITEEVENT;
input_request_block->io_Flags   = 0;
input_request_block->io_Length  = sizeof(struct InputEvent);
input_request_block->io_Data    = (APTR)&SimulatedEvent;

/* Blat pointer into top, lefthand corner, doesn't really matter where
   we start from. I thought IECLASS_POINTERPOS or setting ie_Qualifier to
   0 might give me absolute positioning - but didn't seem to do what I
   expected.  Press left button when you get there */
SimulatedEvent.ie_NextEvent          = NULL;
SimulatedEvent.ie_Class              = IECLASS_RAWMOUSE;
SimulatedEvent.ie_TimeStamp.tv_secs  = 0;
SimulatedEvent.ie_TimeStamp.tv_micro = 0;
SimulatedEvent.ie_Code               = IECODE_NOBUTTON;
SimulatedEvent.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
SimulatedEvent.ie_X                  = -640;
SimulatedEvent.ie_Y                  = -512; /*Might be on a PAL screen*/

DoIO(input_request_block);

/* Blat pointer into bottom, righthand corner, Press left button when you
   get there */
SimulatedEvent.ie_NextEvent          = NULL;
SimulatedEvent.ie_Class              = IECLASS_RAWMOUSE;
SimulatedEvent.ie_TimeStamp.tv_secs  = 0;
SimulatedEvent.ie_TimeStamp.tv_micro = 0;
SimulatedEvent.ie_Code               = IECODE_LBUTTON;
SimulatedEvent.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
SimulatedEvent.ie_X                  = 638;
SimulatedEvent.ie_Y                  = 398;

DoIO(input_request_block);

/* Move left 100 while holding left button down, and release button - ie
   resize the CLI window I expect to be there - If nothing's there then
   it doesn't really matter!  */
SimulatedEvent.ie_NextEvent          = NULL;
SimulatedEvent.ie_Class              = IECLASS_RAWMOUSE;
SimulatedEvent.ie_TimeStamp.tv_secs  = 0;
SimulatedEvent.ie_TimeStamp.tv_micro = 0;
SimulatedEvent.ie_Code               = IECODE_LBUTTON | IECODE_UP_PREFIX;
SimulatedEvent.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
SimulatedEvent.ie_X                  = -100;
SimulatedEvent.ie_Y                  = 0;

DoIO(input_request_block);

/* Move to where I expect disk icon to be (595,22), but note I seem to
   have to move to (595,45) as though the vertical resolution is double
   what I think it is! */
SimulatedEvent.ie_NextEvent          = NULL;
SimulatedEvent.ie_Class              = IECLASS_RAWMOUSE;
SimulatedEvent.ie_TimeStamp.tv_secs  = 0;
SimulatedEvent.ie_TimeStamp.tv_micro = 0;
SimulatedEvent.ie_Code               = IECODE_NOBUTTON;
SimulatedEvent.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
SimulatedEvent.ie_X                  = 55;
SimulatedEvent.ie_Y                  = -355;

DoIO(input_request_block);

/* Press left button without moving */
SimulatedEvent.ie_NextEvent          = NULL;
SimulatedEvent.ie_Class              = IECLASS_RAWMOUSE;
SimulatedEvent.ie_TimeStamp.tv_secs  = 0;
SimulatedEvent.ie_TimeStamp.tv_micro = 0;
SimulatedEvent.ie_Code               = IECODE_LBUTTON;
SimulatedEvent.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
SimulatedEvent.ie_X                  = 0;
SimulatedEvent.ie_Y                  = 0;

DoIO(input_request_block);

/* Press left button again. Note I didn't release button in between but it
   doesn't seem to matter! */
SimulatedEvent.ie_NextEvent          = NULL;
SimulatedEvent.ie_Class              = IECLASS_RAWMOUSE;
SimulatedEvent.ie_TimeStamp.tv_secs  = 0;
SimulatedEvent.ie_TimeStamp.tv_micro = 0;
SimulatedEvent.ie_Code               = IECODE_LBUTTON;
SimulatedEvent.ie_Qualifier          = IEQUALIFIER_RELATIVEMOUSE;
SimulatedEvent.ie_X                  = 0;
SimulatedEvent.ie_Y                  = 0;

DoIO(input_request_block);

/* All done so pack up and go home */
CloseDevice(input_request_block);
}
/* Ok, you're right, it's the most over documented, painful program that
you've ever seen!  But it's only my second attempt at C programs for the
Amiga, and it does actually do what I want. */
