/**********************************************
**
**Suspend.c
**
**
*************************************************/

#define MWINCLUDECOLORS

#include "nano-X.h"

#include <stdio.h>

#define SCREEN_WIDTH	600
#define SCREEN_HEIGHT	800

int SuspendTime = 0;

GR_WINDOW_ID wid;

GR_GC_ID gc;

GR_FONT_ID font;

/*************************************************************************
**			Function:SuspendMode
**=========================================================================
**
**	Description:Suspend Mode
**	param : no
**	return : 0
****************************************************************************/
int SuspendMode()
{
	if(SuspendTime < 10){
		return 1;
	}
	else{
		GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
		GrText(wid,gc,50,350,"Suspend Mode",-1,GR_TFASCII);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
		sleep(2);
		GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
		system("/lib/modules/suspend.sh");
		SuspendTime = 0;
	}
	return 0;
}

int ShowSuspedMode()
{
	GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
	GrText(wid,gc,50,350,"Normal Mode",-1,GR_TFASCII);
	GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);		
}

/*************************************************************************
**			Function:event_handler
**=========================================================================
**
**	Description:responding the event which select by GrSelectEvents()
**	param : event
**	return : 0
****************************************************************************/

int event_handler(GR_EVENT *event)
{	
	switch(event->type)
	{
		case GR_EVENT_TYPE_EXPOSURE:
			SuspendTime = 0;
		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:		//
			SuspendTime = 0;
		break;
		
		default:
		break;
	}
	return 0;
}

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,36);
	GrSetGCFont(gc,font);
	GrSetGCUseBackground(gc, GR_TRUE);
	GrSetGCForeground(gc,BLACK);
	GrSetGCBackground(gc,WHITE);
	while(1){
		GrCheckNextEvent(&event);
		event_handler(&event);
		SuspendMode();
		SuspendTime++;
		sleep(1);
		ShowSuspedMode();
	}	
	return 0;
}

