

;*
;* InputDevice example
; *
; * This example swaps the function of the left and right mouse buttons
; * The Code is just the wrapper that installs and removes the
; * input.device handler that does the work.
; *
; * The handler is written in assembly code since it is important that
; * handlers be as fast as possible while processing the input events.
 
; * Assambly files ram:inputswap.o and ram:inputhandler.o
; * Blink ram:inputswap.o ram:inputhandler.o to inputswap

		include	"baselibs.i"
		include	"baselibs1.i"
		include	"baseexec.i"
		include	"baseio.i"
		include	"intuition.i"
		include	"baseintuition.i"
		include	"exec/nodes.i"
		include	"exec/lists.i"
		include	"exec/io.i"
		include	"exec/interrupts.i"
		include "exec/memory.i"
		include "devices/input.i"

	#define	NULL,0
	#define	LF,10

	DATAAREA
	{

	structset	mywin
	{
	sw	0,0,124,10
	sb	0,1
	sl	CLOSEWINDOW
	sl	WINDOWDRAG|WINDOWCLOSE|SIMPLE_REFRESH|NOCAREREFRESH
        sa	NULL,NULL
	sa	NameString
	sa	NULL,NULL
	sw	0,0,0,0
	sw	WBENCHSCREEN
	};

	structset	inputHandler,IS_SIZE
NameString	dc.b	"Button Swap",0
	}
	extern	VOID,ButtonSwap;

	variables
	CPTR	_IntuitionBase;


	main()

	CPTR	inputReqBlk;
	CPTR	inputPort;
	int	error;
	{

    func	inputPort,=,CreatePort,NULL,NULL;
 	if	inputPort,==,0,1
   	{
	printf	<"no create inputport",LF>
	exit	0
	}
	ifend	1

            func	inputReqBlk,=,CreateExtIO,inputPort,IOSTD_SIZE
		if	inputReqBlk,==,0,2
            	{
		printf	<"no create inputReqBlk",LF>
		deleteport	inputPort
		exit	0
		}
		ifend	2
                func	error,=,OpenDevice,"input.device",NULL,inputReqBlk,NULL;
		if	error,!=,0,3
                {
			printf	<"no open input.device",LF>
			deleteExtIO	inputReqBlk;
			deleteport	inputPort;
			exit	0
		}
		ifend	3

                   structput	 inputHandler,->,IS_CODE,=&,ButtonSwap;		
                   structput	 inputHandler,->,IS_DATA,=,NULL;
                   structput	 inputHandler,->,LN_PRI,=,100;
                   structput	 inputHandler,->,LN_NAME,=,NameString;
                   structput	inputReqBlk,->,IO_DATA,=&,inputHandler;
                   structput	inputReqBlk,->,IO_COMMAND,=,IND_ADDHANDLER;
                    DoIO	inputReqBlk;

                    ROUTINE	WaitForUser;

                   structput	inputReqBlk,->,IO_DATA,=,inputHandler;
                   structput	inputReqBlk,->,IO_COMMAND,=,IND_REMHANDLER;
                    DoIO	inputReqBlk;

        CloseDevice	inputReqBlk;
        DeleteExtIO	inputReqBlk
        DeletePort	inputPort;
    
	}

;*
; * This routine opens a window and waits for the one event that
; * can happen (CLOSEWINDOW)  This is just to let the user play with
; * the swapped buttons and then close the program...
; */
	VOID WaitForUser
	CPTR	win
	CPTR	userport
	CPTR	msg

    {
    func	_IntuitionBase,=,OpenLibrary,intuition.library,0
	if	_IntuitionBase,!=,0,11
    {
        func	win,=,OpenWindow,mywin
	if	win,!=,0,12
        {
	    structput	userport,=,win,->,wd_UserPort;
            WaitPort	userport;
	    func	msg,=,GetMsg,userport;
            ReplyMsg	msg;

            CloseWindow	win;
        }
	ifend	12
        CloseLibrary	_IntuitionBase;
    }
	ifend	11
    }
	end