/******************************************
**
** KeyFunction.c
**
**
****************************************************/

#define MWINCLUDECOLORS

#include "nano-X.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>


#define SCREEN_WIDTH 600     
#define SCREEN_HEIGHT 800

#define Asc_Top_Key  63490
#define Asc_Bottom_Key  63491
#define Asc_Right_Key  63489
#define Asc_Left_Key  63488
#define Asc_Enter_Key  48
#define Asc_F1_Key  49

#define Asc_F2_Key  50
#define Asc_F3_Key  51
#define Asc_F4_Key  52
#define Asc_Volplus_Key	256				//v+
//#define Asc_Volnega_Key 128				//v-	


GR_WINDOW_ID wid;
GR_GC_ID gc;
GR_FONT_ID font;



/*************************************************************************
**			Function:event_handler
**=========================================================================
**
**	Description:responding the event which select by GrSelectEvents()
**	param :  event
**	return : 0
****************************************************************************/
int event_handler(GR_EVENT *event)
{
	int ret = 0;
	switch(event->type)
	{
		case GR_EVENT_TYPE_EXPOSURE:
			GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
			GrText(wid,gc,0,400,"Wait Press Any Key",-1,GR_TFASCII);
			GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
		break;
		
		case GR_EVENT_TYPE_CLOSE_REQ:
			printf("close ok.\n");
			GrDestroyFont(font);
			GrDestroyWindow(wid);
			GrDestroyGC(gc);
			GrClose();
			exit (0);
		break;
		
		case GR_EVENT_TYPE_KEY_DOWN:
		switch(event->keystroke.ch){
			
			case Asc_Top_Key:		//top
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Top Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_Right_Key:		//right
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Right Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_Bottom_Key:		//bottom
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Bottom Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_Left_Key:		//left
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Left Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_Enter_Key:		//enter
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Enter Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_F2_Key:		//F2
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"F2 Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_F3_Key:		//F3
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"F3 Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
				
			case Asc_F4_Key:		//F4
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"F4 Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_F1_Key:		//F1
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"F1 Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_Volplus_Key:		//V+
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Vol+ Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			case Asc_Volnega_Key:		//V-
				GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
				GrText(wid,gc,0,400,"Vol- Key Press",-1,GR_TFASCII);
				GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			break;
			
			
			default:
			break;
		}
		break;
		
		default:
		break;
	}
}

int main(void)
{
	GR_EVENT event;
	
	if (GrOpen() < 0) {
       		fprintf (stderr, "GrOpen failed");
        	return 1;
	}
	wid = GrNewWindowEx(GR_WM_PROPS_APPWINDOW,NULL,GR_ROOT_WINDOW_ID, 0, 0, SCREEN_WIDTH,SCREEN_HEIGHT,WHITE);
	GrSelectEvents(wid, GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_EXPOSURE|GR_EVENT_MASK_KEY_DOWN);
	GrMapWindow(wid);
	gc = GrNewGC();
	font = GrCreateFont("mfont.mbf",0, NULL);
	GrSetFontSize(font,32);
	GrSetGCFont(gc,font);
	GrSetGCUseBackground(gc, GR_TRUE);
	GrSetGCForeground(gc,BLACK);
	GrSetGCBackground(gc,WHITE);
	
	while(1){
		GrCheckNextEvent(&event);
		event_handler(&event);
	}
	return 0;
}