/* ConvertTest.c ***********************************************************
*
*	ConvertTest ---	Convert the RAWKEY events coming in from a window.
*
*	Author --------	Olaf 'Olsen' Barthel, ED Electronic Design Hannover
*			Brabeckstrasse 35
*			D-3000 Hannover 71
*
*			Federal Republic of Germany
*			
***************************************************************************/

#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <exec/memory.h>
#include <stdio.h>

#include "KeyDef.h"

	/* Aztec uses buffered IO, Dump() will print the contents of
	 * the string as soon as the call comes.
	 */

#define Dump(s) Write(Output(),s,strlen(s))

	/* Some forward declarations. */

extern struct Library	*OpenLibrary();
extern struct Window	*OpenWindow();
extern struct Message	*GetMsg();

	/* Global Intuition stuff. */

struct IntuitionBase	*IntuitionBase;
struct GfxBase		*GfxBase;
struct Window		*Window;
struct IntuiMessage	*Message;

	/* A new window. */

struct NewWindow NewWindow =
{
	0,0,
	280,10,
	-1,-1,
	CLOSEWINDOW | RAWKEY | INACTIVEWINDOW | ACTIVEWINDOW,
	WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH | RMBTRAP,
	(struct Gadget *) NULL,
	(struct Image  *) NULL,
	(UBYTE *)"ConvertTest, ED Hannover",
	(struct Screen *) NULL,
	(struct BitMap *) NULL,
	0,0,
	0,0,
	WBENCHSCREEN
};

	/* Finish(ReturnCode) :
	 *
	 *	Closes what there is to close and waves goodbye.
	 */

void
Finish(ReturnCode)
long ReturnCode;
{
		/* No more conversion today. */

	KeyConvertExit();

		/* Say goodnight, window. */

	if(Window)
	{
		while(Message = (struct IntuiMessage *)GetMsg(Window -> UserPort))
			ReplyMsg(Message);

		CloseWindow(Window);
	}

		/* Adieu, Intuition. */

	if(IntuitionBase)
	{
		if(ReturnCode)
			DisplayBeep(NULL);

		CloseLibrary(IntuitionBase);
	}

		/* Arrivederci Graphics. */

	if(GfxBase)
		CloseLibrary(GfxBase);

		/* Hey DOS, wake up, holiday's over. */

	exit(ReturnCode);
}

	/* Begin() :
	 *
	 *	That's where it begins, open what there is to open.
	 */

void
Begin()
{
	if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",LIBRARY_VERSION)))
		Finish(20);

	if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",LIBRARY_VERSION)))
		Finish(20);

	if(!(Window = (struct Window *)OpenWindow(&NewWindow)))
		Finish(20);

	if(!KeyConvertInit())
		Finish(20);
}

	/* main() :
	 *
	 *	Main routines are always fun.
	 */

void
main(argc,argv)
long argc;
char *argv[];
{
	ULONG Class;		/* RAWKEY or CLOSEWINDOW? */
	USHORT Code;		/* Which code? */
	UBYTE *TxtPtr,Tmp;	/* Some text. */
	BOOL IsShifted;		/* User pressed shift? */

	register short i,j;	/* Simple loop counters. */

		/* Started via Workbench? We need a CLI window! */

	if(!argc)
		exit(20);

		/* To give a demonstration of the ToUpper()
		 * function we will echo the arguments the
		 * user called us with.
		 */

	for(i = 0 ; i < argc ; i++)
	{
		for(j = 0 ; j < strlen(argv[i]) ; j++)
		{
			argv[i][j] = ToUpper(argv[i][j]);
		}

		printf("Arg %d is \"%s\"\n",argc,argv[i]);
	}

		/* Startup */

	Begin();

		/* Ad infinitum. */

	FOREVER
	{
			/* Oh what a nice program. */

		WaitPort(Window -> UserPort);

			/* Don't get me wrong. */

		Class = Code = NULL;

			/* Any mail for us? */

		if(Message = (struct IntuiMessage *)GetMsg(Window -> UserPort))
		{
			Class	= Message -> Class;
			Code	= Message -> Code;

				/* Convert it before you reply it! */

			TxtPtr = (UBYTE *)ConvertToBuffer(Message);

			ReplyMsg(Message);
		}

			/* "It's all over now, baby blue..." */

		if(Class == CLOSEWINDOW)
			break;

			/* Deselected? How unkind! */

		if(Class == INACTIVEWINDOW)
			SetWindowTitles(Window,"Click here to convert",-1);

			/* Selected? Fine! */

		if(Class == ACTIVEWINDOW)
			SetWindowTitles(Window,"Type anything you want",-1);

			/* There's a text coming in. */

		if(TxtPtr)
		{
				/* Just in case... */

			IsShifted = FALSE;

				/* Could be a special key, couldn't it? */

			Tmp = TxtPtr[0];

				/* Shifted? */

			if(Tmp >= KC_FKEY1 && Tmp <= (KC_CURSORLEFT | KC_SHIFT))
			{
				if(Tmp & KC_SHIFT)
					Dump("SHIFT + ");

				Tmp &= ~KC_SHIFT;
			}

				/* "Excuse me, are you aliens?" */

			switch(Tmp)
			{
				case KC_CURSORUP	:	Dump("KC_CURSORUP\n");
								TxtPtr[0] = 0;
								break;

				case KC_CURSORDOWN	:	Dump("KC_CURSORDOWN\n");
								TxtPtr[0] = 0;
								break;

				case KC_CURSORRIGHT	:	Dump("KC_CURSORRIGHT\n");
								TxtPtr[0] = 0;
								break;

				case KC_CURSORLEFT	:	Dump("KC_CURSORLEFT\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY1:			Dump("KC_FKEY1\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY2:			Dump("KC_FKEY2\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY3:			Dump("KC_FKEY3\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY4:			Dump("KC_FKEY4\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY5:			Dump("KC_FKEY5\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY6:			Dump("KC_FKEY6\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY7:			Dump("KC_FKEY7\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY8:			Dump("KC_FKEY8\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY9:			Dump("KC_FKEY9\n");
								TxtPtr[0] = 0;
								break;

				case KC_FKEY10:			Dump("KC_FKEY10\n");
								TxtPtr[0] = 0;
								break;

				case KC_HELP:			Dump("KC_HELP\n");
								TxtPtr[0] = 0;
								break;
			}

				/* If we can handle it, print it. */

			if(TxtPtr[0])
				Dump(TxtPtr);
		}
	}

		/* It's really over now. */

	Finish(0);
}
