/*
 *
 *	DISCLAIMER:
 *
 *	This program is provided as a service to the programmer
 *	community to demonstrate one or more features of the Amiga
 *	personal computer.  These code samples may be freely used
 *	for commercial or noncommercial purposes.
 * 
 * 	Commodore Electronics, Ltd ("Commodore") makes no
 *	warranties, either expressed or implied, with respect
 *	to the program described herein, its quality, performance,
 *	merchantability, or fitness for any particular purpose.
 *	This program is provided "as is" and the entire risk
 *	as to its quality and performance is with the user.
 *	Should the program prove defective following its
 *	purchase, the user (and not the creator of the program,
 *	Commodore, their distributors or their retailers)
 *	assumes the entire cost of all necessary damages.  In 
 *	no event will Commodore be liable for direct, indirect,
 *	incidental or consequential damages resulting from any
 *	defect in the program even if it has been advised of the 
 *	possibility of such damages.  Some laws do not allow
 *	the exclusion or limitation of implied warranties or
 *	liabilities for incidental or consequential damages,
 *	so the above limitation or exclusion may not apply.
 *
 */

/* keyboard.c */

/* sample program to demonstrate direct communications with the keyboard,
 * won't work unless input device is disabled, so that keyboard can
 * be accessed individually.  (It will compile and it will run, but
 * this program will get some of the keyboard's inputs,  and the input
 * device will steal the rest... no guarantee that F1 Key can break it out.
 *
 * To try the program, if run under the AmigaDOS CLI, strike any key, then
 * hit return.  (You won't see any responses until each return key... DOS
 * is sitting on the input stream with its input editor as well as the
 * input device.)  By rapidly hitting F1 then Return several times, 
 * eventually you can generate a hex 50 that exits the program.  This
 * program is provided for those who are taking over the machine.  It
 * is not intended as a general purpose keyboard interface under DOS.
 *
 * Demo is more effective if you make the CLI window smaller, then
 * click the select button in the (Workbench) space below the CLI
 * window.  Then no console will be receiving the keystrokes and
 * more of them will be reported to the startup CLI window.   Most
 * users will leave the keyboard device attached to the input device
 * and install a handler in the input stream instead of doing it
 * this way.
 *
 * Author:  Rob Peck, 12/1/85
 *
 * This code may be freely utilized in the creation of programs for the Amiga. 
 */

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

#define F1KEY 0x50

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

SHORT error;

struct IOStdReq *keyreq;
struct MsgPort *keyport;
struct InputEvent *keydata;	/* pointer into the returned data area
				   where an input event has been sent */
BYTE keybuffer[sizeof( struct InputEvent )];

main()
{
	keyport = CreatePort(0,0);
	if(keyport == 0) { printf("\nError during CreatePort"); 
			   exit(-1); 
			 }		
	keyreq = CreateStdIO(keyport);	
		/* make an io request block for 
		 * communicating with the keyboard */
	if(keyreq == 0) { printf("\nError during CreateStdIO");
			  DeletePort(keyport);
			  exit(-2); 
			}
	error = OpenDevice("keyboard.device",0,keyreq,0);
			/* open the device for access */

	if (error != 0) { printf("\nCan't open keyboard!"); 
			  ReturnMemoryToSystem();
			  exit(-100); 
			}
	keyreq->io_Length = sizeof(struct InputEvent);	
	/* read one event each time we go back to the keyboard */

	keyreq->io_Data = (APTR)keybuffer;	
	/* show where to put the data when read */

	keydata = (struct InputEvent *)keybuffer;

	keyreq->io_Command = KBD_READEVENT;	/* get an event!! */


	for(;;)		/* FOREVER */
	{
	printf("\n Ready to retrieve another key\n");
	DoIO( keyreq );
	if(keydata->ie_Code == F1KEY) break;	
	printf("\n Raw key found this time was %lx",keydata->ie_Code);
	}
	printf("\nFINALLY found an F1 key!!!   Exiting...");
	ReturnMemoryToSystem();	/* can't get here because of FOREVER,
				 * but if user provides an exit..... */
}		

ReturnMemoryToSystem()   
{
	DeleteStdIO(keyreq);
	DeletePort(keyport);
	return(0);
}
