#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/alib_protos.h>
#include <intuition/intuition.h>
#include <iostream.h> 
#include <dos/dos.h>

#include "input/rawkeyinput.h"
#include "input/input.h"
#include "misc/systemdefs.h"


#define IDCMPSIGNAL (1 << (win->UserPort->mp_SigBit))

#pragma chip
static UWORD pointer[1]={0};
#pragma fast

BOOL RawKeyInput::StartUp(){
	oi_AddType("KeyDown",1,0,127);
	oi_AddType("KeyUp",2,128,255);
	gui_AddInfo("Key Values:");
	gui_AddInfo("Range = KeyDown, KeyUp");
	gui_AddInfo("~ -> \\ = 0->13 , 128->141");
	gui_AddInfo("q -> ] = 16->27 , 144->155");
	gui_AddInfo("a -> # = 32->42 , 160->170");
	gui_AddInfo("z -> / = 49->58 , 177->186");			
	return OmegaInput::StartUp();	// Initialise stuff from the base class as well
}

RawKeyInput::RawKeyInput(){
	char **array=AllocateValueTypeSpace(1);
	UINT *typeMap=GetTypeMap();
	oa_SetName("RawKey Input");
	oa_Author="T.Miles";
	oa_Version="1.0";
	op_Type=MAKE_TYPE('R','K');
	op_Configured=TRUE;
	array[0]="Key";
	typeMap[0]=1;
	win=NULL;
	gui_SetValueTypes();
}	

void RawKeyInput::RK_OpenWindow(){

	struct TagItem win_tags[]=
	{
		{WA_CustomScreen, (ULONG)coordScreen},
		{WA_Left,			0},
		{WA_Top,				0},
		{WA_Width,			200},
		{WA_Height,			100},
		{WA_IDCMP,			IDCMP_RAWKEY | IDCMP_INACTIVEWINDOW | IDCMP_ACTIVEWINDOW},
		{WA_Flags,			WFLG_DRAGBAR},
		{TAG_DONE,			NULL},
	};
    
	win=OpenWindowTagList(NULL, win_tags);
	SetWindowTitles(win,"Input Window",NULL);
	SetPointer(win,pointer,0,0,0,0);
}


void RawKeyInput::RK_CloseWindow(){
	if (win){
		CloseWindow(win);
		win=NULL;
	}
}

void RawKeyInput::OnStart(){
	RK_OpenWindow();
}	

void RawKeyInput::OnStop(){
	RK_CloseWindow();
}

// This should keep running until a quit signal is received
// Overridden from the base class as we need to deal with IDCMP msgs as well
void RawKeyInput::WaitForSignals(){
	struct IntuiMessage *msg;
	UBYTE values[2]={0,0};
	
   int wakeupmask;
   
   if (IsRunning()){
	   wakeupmask=Wait(IDCMPSIGNAL | INPUTSIGNAL | SIGBREAKF_CTRL_C);
 		if (wakeupmask & IDCMPSIGNAL){
  			while(msg = (struct IntuiMessage*)GetMsg(win->UserPort)){
 	 			switch(msg->Class){
 				case IDCMP_INACTIVEWINDOW:
	  	     		SetWindowTitles(win,"Cick In Me To Activate",NULL); 
	        		break;
	     		case IDCMP_ACTIVEWINDOW:
	     			SetWindowTitles(win,"Input Window",NULL);
	     			break;
  	    		case IDCMP_RAWKEY:
  	    			// Find out what track buffer to put it in and store it there
  	    			values[1]=msg->Code;
//  	    			cout << "Key: " << (UINT)values[1] << endl;
  	    			if (msg->Code<128)
  	    				values[0]=1;
  	    			else
  	    				values[0]=2;
  	    			// New way of entering values - need to have an array of
  	    			// (type,value) byte pairs
  	    			PutInTrackBuffer(2,&values[0]);
	        		break;
				case IDCMP_CLOSEWINDOW:
					oa_SetStatus(APP_STATUS_QUIT,TRUE);
					break;
				}
				ReplyMsg((struct Message*)msg);		// Say 'thank you'
			}
		}
		else
		if (wakeupmask & INPUTSIGNAL)
				DealWithSignals();
		else{
			if (wakeupmask & SIGBREAKF_CTRL_C)
				OnQuit();
		}
	}
	else
		OmegaInput::WaitForSignals();
	if (IsQuit())
		OnStop();
}
