/**********************************
*
*UsbCtrl.c
*
************************************/

#define MWINCLUDECOLORS

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

#include <dirent.h>
#include <sys/stat.h>
#include <sys/mount.h>

#include <ctype.h>

#define SCREEN_WIDTH 600     
#define SCREEN_HEIGHT 800

#define CM_PWR		112
#define CM_USB_PLUG_IN	108
#define CM_SD_IN	117

GR_WINDOW_ID wid;
GR_GC_ID gc;
GR_FONT_ID font;

int IsPulgSD = 0;


#define MOUNTFLAG (MS_MGC_VAL | MS_NOSUID | MS_NODEV |MS_SYNCHRONOUS)

#define UMOUNTFLAG (MS_MGC_VAL | MS_NOSUID | MS_NODEV | MS_SYNCHRONOUS | MS_REMOUNT)

char *mount_data = "iocharset=utf8,fscontext=system_u:object_r:removable_t,managed";


/***********************************************************
		function :ShowInfoUsb
*============================================================
**	Description:Display whether  mount the SD
**	param :no
**	return  0
****************************************************************/
int ShowInfoUsb()
{
	GrSetGCForeground(gc,BLACK);
	
	GrSetGCBackground(gc,WHITE);
	
	GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
	
	if(IsPulgSD)
		GrText(wid,gc,100,450,"Mount the SD",-1,GR_TFASCII);
	else GrText(wid,gc,100,450,"Umount the SD",-1,GR_TFASCII);
	
	GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	
	return 0;	
}

/***********************************************************
		function :Mount_SD
*============================================================
**	Description:mount or umount SD Card
**	param :0:umount, 1:mount
**	return  ok=>0 or 1
****************************************************************/

int Mount_SD(int mountflag)
{
	char path[30] = {0};
	int loop = 0;
	
	while(loop < 10){
		loop++;
		if(access("/dev/discs/disc0/part1",F_OK)){
			printf("wait SD\n");
		}
		else break;
	}
	if(mountflag){		//mount
		 sprintf(path,"/dev/discs/disc0/part%d",1);
		 if(mount(path,"/mnt/ext2","vfat",MOUNTFLAG,mount_data) == 0)
		 	return 0;
		 else {
		 	sprintf(path,"/dev/discs/disc0/part%d",4);
		 	if (mount(path,"/mnt/ext2","vfat",MOUNTFLAG,mount_data) == 0)
		 		return 0;
		 }
	}
	else{		//umount
		if(!IsPulgSD) return 0;
		sprintf(path,"/dev/discs/disc0/part%d",1);
		 if(mount(path,"/mnt/ext2","vfat",UMOUNTFLAG,mount_data) == 0){
		 	return 0;
		 }
		 else{
		 	sprintf(path,"/dev/discs/disc0/part%d",4);
			 if(mount(path,"/mnt/ext2","vfat",UMOUNTFLAG,mount_data) == 0)
		 		return 0;
			 
		 }
	}
	return 1;	
}
/*************************************************************************
**			Function:SD_State
**=========================================================================
**
**	Description:Get the State of the SD Card
**	param : no
**	return : 0
****************************************************************************/
int SD_State()
{
	int SD_value=0;
	int pio_fd = open("/dev/pvi_ioc",O_RDONLY | O_NONBLOCK);
	ioctl(pio_fd,CM_SD_IN,&SD_value);
	close(pio_fd);
	if(SD_value != IsPulgSD){		//sd¿¨×´Ì¬¸Ä±ä
		if(SD_value == 1){		//pulg in
			Mount_SD(1);
		}
		else{
			Mount_SD(0);
		}
		IsPulgSD = SD_value;
		ShowInfoUsb();
	}
	return 0;	
}

/*************************************************************************
**			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:
			ShowInfoUsb();
		break;
		
		case GR_EVENT_TYPE_CLOSE_REQ:
			printf("close ok.\n");
			GrDestroyFont(font);
			GrDestroyWindow(wid);
			GrDestroyGC(gc);
			GrClose();
			exit (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);
	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);
		SD_State();
		ShowInfoUsb();
	}	
	return 0;
}