/* 
	Based on AutoIconOpen.c by Tony (**all at sea in C**) Wills 15 FEB 87, V1.2
	reference: http://aminet.net/package/util/batch/AutoIconOpen
	
	This version modified by Stone Oakvalley Studios to automate ExoticRipper 3.3 during SOAMC= ENDGAME
	Arguments added for x,y :-) by Waxhead very late at night, haha. All done during 2-3 June 2018
	
	In fact, this little gem of re-compiled software can be used in many other automation procedures :-)
	
	Additionally the original source code (at least for OS3.0) we need to change
	SimulatedEvent.ie_Code               = IECODE_LBUTTON;
		into
	SimulatedEvent.ie_Code               = IECODE_LBUTTON | IECODE_UP_PREFIX;
		otherwise the gadgetpress was stuck.
	
	Compiled now with Lattice C v5.03 
	
	This is BTW my first c compiled source/program on Amiga ever!  Imagine a source from 1987 made Stone Oakvalley edit, tweak
	and compile it 31 years later!
*/

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

struct MsgPort *inputDevPort;
struct IOStdReq *input_request_block;
struct InputEvent SimulatedEvent;
extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();

main(argc,argv)
int argc;
char *argv[];
{
	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;

	/* Move Mouse to x,y click mouse button DOWN */
	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                  = atoi(argv[1]);
	SimulatedEvent.ie_Y                  = atoi(argv[2]);
	DoIO(input_request_block);
	
	/* Keep mouse postion, but release mouse button UP to activate the correct response */
	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                  = 0;
	SimulatedEvent.ie_Y                  = 0;
	DoIO(input_request_block);	

	CloseDevice(input_request_block);
}
