/*******************************
**
**UsbPlug.c
**
**************************************/


#define MWINCLUDECOLORS

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

#define SCREEN_WIDTH 600     
#define SCREEN_HEIGHT 800

#define CM_USB_PC	1
#define CM_USB_EBR	2
#define CM_USB_PLUG_IN	108


GR_WINDOW_ID wid;
GR_GC_ID gc;
GR_FONT_ID font;

int Usb_Plug = 0;



/*************************************************************************
**			Function:Usb_Plug_In
**=========================================================================
**
**	Description:Check the USB Cable
**	param : no
**	return : 0
****************************************************************************/
int Usb_Plug_In()
{
	int pio_fd;
	int Usb_plug=0;
	int fpid = open("/dev/pvi_ioc",O_RDONLY|O_NONBLOCK);
	ioctl(fpid,CM_USB_PLUG_IN,&Usb_plug);
	close(fpid);
	
	if(Usb_plug){		//connect to pc;
		umount2("/mnt/ext1",0);
		umount2("/mnt/ext2",0);
		pio_fd = open("/dev/pvi_ioc",O_RDONLY | O_NONBLOCK);
		if(Usb_Plug) return 0;
		ioctl(pio_fd,CM_USB_PC,0);
		close(pio_fd);
		Usb_Plug = 1;
		GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
		GrText(wid,gc,50,350,"connect",-1,GR_TFASCII);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	}
	else if(Usb_Plug){
		Usb_Plug = 0;
		pio_fd = open("/dev/pvi_ioc",O_RDONLY | O_NONBLOCK);
		ioctl(pio_fd,CM_USB_EBR,0);
		close(pio_fd);
		GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
		GrText(wid,gc,50,350,"Hello",-1,GR_TFASCII);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	}
	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:		
			GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
			GrText(wid,gc,50,350,"Hello",-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();
			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);
		Usb_Plug_In();
	}	
	return 0;
}