/***************************
**
**  RTC test
**
*******************************/


#define MWINCLUDECOLORS

#include "nano-X.h"

#include <stdio.h>
#include <linux/rtc.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#define CM_PWR			112
#define SCREEN_WIDTH		600
#define SCREEN_HEIGHT		800

GR_WINDOW_ID wid;

GR_GC_ID gc;

GR_FONT_ID font;

/*
struct rtc_time {
        int tm_sec;
        int tm_min;
        int tm_hour;
        int tm_mday;
        int tm_mon;
        int tm_year;
        int tm_wday;
        int tm_yday;
        int tm_isdst;
};
*/

int fd_rtc = -1;

int auto_off(void)
{
	struct rtc_time now_time;	
	struct rtc_time wake_up_time;

	ioctl(fd_rtc,RTC_RD_TIME,&wake_up_time);		//read time
	
	printf("new time is : %d :%d :%d \n",wake_up_time.tm_hour,wake_up_time.tm_min,wake_up_time.tm_sec);

	
//	ioctl(fd_rtc,RTC_ALM_SET,&new_time);		//set time;
	
//	ioctl(rtc_fd,RTC_AIE_ON,0);		//set Alarm interrupt on
	
	wake_up_time.tm_min += 1;
	
	if(wake_up_time.tm_min>59){
		wake_up_time.tm_hour+= (wake_up_time.tm_min/60);
		wake_up_time.tm_min = wake_up_time.tm_min%60;
		if(wake_up_time.tm_hour>23){
			wake_up_time.tm_mday += (wake_up_time.tm_hour/24);
			wake_up_time.tm_hour = wake_up_time.tm_hour%24;
			if(wake_up_time.tm_mday>28){
				switch(wake_up_time.tm_mon)
				{
					case 0:
					case 2:
					case 4:
					case 6:
					case 7:
					case 9:
						if(wake_up_time.tm_mday>31){
							wake_up_time.tm_mday=1;
							wake_up_time.tm_mon+=1;
						}
					break;	
					case 11:
						if(wake_up_time.tm_mday>31){
							wake_up_time.tm_mday=1;
							wake_up_time.tm_mon=0;
							wake_up_time.tm_year+=1;
						}
					break;	
					case 3:	
					case 5:	
					case 8: 
					case 10:
						if(wake_up_time.tm_mday>30){
							wake_up_time.tm_mday=1;
							wake_up_time.tm_mon+=1;
						}
					break;
					case 1: 
						if((wake_up_time.tm_year+1900)%4==0){
							if(wake_up_time.tm_mday>29){
								wake_up_time.tm_mday=1;
								wake_up_time.tm_mon+=1;
							}
						}else{
							if(wake_up_time.tm_mday>28){
								wake_up_time.tm_mday=1;
								wake_up_time.tm_mon+=1;
							}
						}
					break;
				}
			}
		}
	}
	ioctl(fd_rtc,RTC_ALM_SET,&wake_up_time);
	
	ioctl(fd_rtc,RTC_AIE_ON,0);
	
	system("/lib/modules/suspend.sh");
	
	ioctl(fd_rtc,RTC_AIE_OFF,0);

	ioctl(fd_rtc,RTC_RD_TIME,&now_time);
	
	close(fd_rtc);
	
	printf("new time is : %d :%d :%d \n",now_time.tm_hour,now_time.tm_min,now_time.tm_sec);
	
	if((now_time.tm_year==wake_up_time.tm_year)&&(now_time.tm_mon==wake_up_time.tm_mon)&&
		   (now_time.tm_mday==wake_up_time.tm_mday)&&(now_time.tm_hour==wake_up_time.tm_hour)&&
		   (now_time.tm_min==wake_up_time.tm_min))
		{
			int fpid = open("/dev/pvi_ioc",O_RDONLY|O_NONBLOCK);
			printf("Auto off\n");
			sleep(2);
			GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
			GrText(wid, gc,10,300,"system  auto off", -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);
			GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
			GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			ioctl(fpid,CM_PWR,0);
			close(fpid);
			system("reboot");
		}
		
	return 0;
	
}


int event_header(GR_EVENT *event)
{
	switch(event->type)
	{	
		case GR_EVENT_TYPE_EXPOSURE:
			GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
			GrText(wid, gc,10,300,"system will auto off after 1 minute!", -1, GR_TFASCII);
			GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
		break;
		
		case GR_EVENT_TYPE_CLOSE_REQ:
			GrDestroyFont(font);
			GrDestroyWindow(wid);
			GrDestroyGC(gc);
			GrClose();
		break;
		
		default:
		break;
	}
	return 0;
}

int main(int argc,char *argv[])
{	
	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);
	GrMapWindow(wid);
	gc = GrNewGC();
	
	font = GrCreateFont("mfont.mbf",0, NULL);
	GrSetFontSize(font,24);
	GrSetGCFont(gc,font);
	GrSetGCUseBackground(gc, GR_TRUE);
	GrSetGCForeground(gc,BLACK);
	GrSetGCBackground(gc,WHITE);
	
	
	fd_rtc = open("/dev/misc/rtc",O_RDONLY | O_NONBLOCK);
	
	if(fd_rtc < 0) {
		printf("error \n");
		return 1;
	}
	
	while(1)
	{
		GrCheckNextEvent(&event);
		event_header(&event);
		auto_off();
	}
	return 0;
}