/*********************************
**
**PlayMusic.c
**
****************************************/


#define MWINCLUDECOLORS

#include "nano-X.h"

#include <stdio.h>

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <linux/soundcard.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>

#include "mad.h"

#include "madlld.h"

#define SCREEN_WIDTH 600     
#define SCREEN_HEIGHT 800

#define Asc_Volplus_Key	256				//v+
#define Asc_Volnega_Key 128				//v-

int gCurVol = 1;			//volume

pthread_t pthread_id;
GR_WINDOW_ID wid;
GR_GC_ID gc;
GR_FONT_ID font;

/*************************************************************************
**			Function:pthread_funPlay
**=========================================================================
**
**	Description:Play the music for ever
**	param :arg = NULL
**	return : 0
****************************************************************************/
void pthread_funPlay(void *arg)
{
	while(1){
		play_mp3("/mnt/ext1/Take_Me_To_Your_Heart.mp3");
	}	
	pthread_exit(NULL);
}

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

int event_handler(GR_EVENT *event)
{
	char VolIcon[20] = {0};
	int ret = 0;
	switch(event->type)
	{
		case GR_EVENT_TYPE_EXPOSURE:
			GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
			GrText(wid,gc,250,100,"Start Play",-1,GR_TFASCII);
			GrText(wid,gc,100,520,"Volume:",-1,GR_TFASCII);
			
			sprintf(VolIcon,"pic/vol_set%d.bmp",gCurVol);
			GrDrawImageFromFile(wid,gc,180,500,240,64,VolIcon,0);
			GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			VolumeSet(gCurVol);
			
			ret = pthread_create(&pthread_id,NULL,pthread_funPlay,NULL);
			if(ret) {
				printf("pthread_crate  Play error\n");
				exit(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:
		switch(event->keystroke.ch){
			case Asc_Volplus_Key:
				if(gCurVol < 9) gCurVol++;
				else gCurVol = 9;
				VolumeSet(gCurVol);

				sprintf(VolIcon,"pic/vol_set%d.bmp",gCurVol);
				GrClearArea(wid,180,500,240,64,0);
				GrDrawImageFromFile(wid,gc,180,500,240,64,VolIcon,0);
				GrShowWindow(wid,gc,180,500,240,64);

			break;
			
			case Asc_Volnega_Key:
				if(gCurVol >0) gCurVol--;
				else gCurVol = 0;
				VolumeSet(gCurVol);
				
				sprintf(VolIcon,"pic/vol_set%d.bmp",gCurVol);
				GrClearArea(wid,180,500,240,64,0);
				GrDrawImageFromFile(wid,gc,180,500,240,64,VolIcon,0);
				GrShowWindow(wid,gc,180,500,240,64);

			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;
}
