#include    <stdio.h>
#include    <stdlib.h>
#include    <cdrfrb.h>
#include    <snd.h>
#include    "defs.h"

extern BOOL Input();
extern void wrtstr();
extern int  Sel_menu();
extern char *SPCSTR;

/* --------------------- EUP play 92.03.30 ------------------ */
extern void runeup();                             /* customaized by YAMA */
extern void stopeup();                            /* customaized by YAMA */
/* ---------------------------------------------------------- */

/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
extern void Dsp_vram();
extern UCHAR    vram[];
int cd_m_stat = FALSE;
/* ---------------------------------------------------------- */

typedef struct TIMEADRS CDTIME;

static short   cd_act=FALSE;
static int     type=0,strt=0,endt=0;
static int     vol=64,sts=0,no=0,acv=0;
static CDTIME  st,ed;
static CDTIME  *track=NULL;
/* ------------------ CD No Stop 91.10.18 Pumpkin ----------- */
int	cd_stop = TRUE;
/* ---------------------------------------------------------- */

int     CD_open(void)
{
    int     i;
    CDTIME  disc;

    if ( track == NULL && 
        (track = (CDTIME *)malloc(sizeof(CDTIME)*99)) == NULL )
        return 1;

    do {
        i = cdr_cdinfo(0,&type,&strt,&endt,track,&disc);
    } while ( i == CDERR7 );

/*********************************
    char tmp[80];
    sprintf(tmp,"%d:%d:%d",disc.min,disc.sec,disc.frame);
    wrtstr(tmp,30,1,0x15);
*********************************/

    if ( disc.frame-- <= 0 ) {
	disc.frame = 74;
	if ( disc.sec-- <= 0 ) {
	    disc.sec = 59;
	    if ( disc.min-- <= 0 )
		return 1;
	}
    }
    if ( i == 0 ) {
        track[endt].min = disc.min;
        track[endt].sec = disc.sec;
        track[endt].frame = disc.frame;
	acv = 1;
    }

    return i;
}
void	CD_play()
{
    int  i;
    char tmp[80],ttl[40];

/* --------------- debug   91.08.28 Pumpkin ---------------- */
    i = CDERR7;
    while (i == CDERR7) {
    	i = cdr_mphase(0,&sts,&no,&st,&ed);
    }
    if ( i != 0 ) {
	wrtstr("CDが正常にセットされていません ",30,1,0x12);
        return;
    }
/*********
    if ( cdr_mphase(0,&sts,&no,(char *)&st,(char *)&ed) != 0 ) {
	wrtstr("CDが正常にセットされていません?",30,1,0x12);
        return;
    }
********
/* ---------------------------------------------------------- */
    if ( sts != 0 )
        cdr_pause(0);

    if ( CD_open() != 0 ) {
	wrtstr("CD情報の読み取りに失敗しました",30,1,0x12);
	return;
    }

    tmp[0] = '\0'; sprintf(ttl,"曲番(1-%d) ",endt);
    if ( Input(tmp,ttl) != FALSE )
	return;
    if ( (no = atoi(tmp) - 1) < 0 || no >= endt ) no = 0;

/* ------------------ SIDEWORK 92.01.31 Pumpkin ----------------- */
/******
    SND_elevol_set(1,vol,vol);
******/
/* ---------------------------------------------------------- */
    if ( endt > 0 ) {
        for ( i=no ; i < endt && (track[i].min & 0x80) != 0 ; i++ );
        if ( cdr_mtplay(0,&(track[i]),&(track[endt])) != 0 )
	    wrtstr("正常に演奏出来ません",30,1,0x12);
    }
    if ( track != NULL )
	free(track);
    track = NULL;
    cd_act = TRUE;
}
void	CD_stop()
{
    cdr_mstop(0);
    if ( track != NULL )
	free(track);
    track = NULL;
    cd_act = FALSE;
}
void	CD_endof()
{
/* ------------------ CD No Stop 91.10.18 Pumpkin ----------- */
    int     i;

    i = CDERR7;
    while (i == CDERR7) {
    	i = cdr_mphase(0,&sts,&no,&st,&ed);
    }

    if ( cd_stop == TRUE && sts != 0)
        cdr_mstop(0);
/*****
    if ( cd_act != FALSE )
        cdr_mstop(0);
*****/
/* ---------------------------------------------------------- */
}
void	CD_vol()
{
    char    tmp[80];

/* ------------------ SIDEWORK 92.01.31 Pumpkin ------------- */
    SND_elevol_read(1,&vol,&vol);
/* ---------------------------------------------------------- */
    sprintf(tmp,"%d",vol);
    if ( Input(tmp,"音量(0-127) ") != FALSE )
	return;
    vol = atoi(tmp);
    SND_elevol_set(1,vol,vol);
}
void	CD_menu()
{
	static int  no=ERR;
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
     static char  *menu[]={
	 "P: CD Play      1. ＣＤ演奏する     ",
	 "S: CD Stop      2. ＣＤ演奏を止める ",
	"",
	 "V: Volume       3. CD音量を設定する ",
	"",
	 "E: Eup Play     4. EUPﾌｧｲﾙ演奏開始  ",
	 "T: Eup Stop     5. EUPﾌｧｲﾙ演奏停止  ",
	"",
	 "C: VC3 TV       6. VCARD3 TV表示    ",
	 "Z: VC3 TV Stop  7. VCARD3 TV停止    ",
	NULL,
	"1 ＣＤ演奏する     ",
	"2 ＣＤ演奏を止める ",
	"",
	"3 音量を設定する   ",
	"",
	"4 EUPﾌｧｲﾙ演奏開始  ",             /* customaized by YAMA */
	"5 EUP演奏停止      ",             /* customaized by YAMA */
	"",
	"6 VCARD3 TV表示    ",
	"7 VCARD3 TV停止    ",
	NULL };
	int m=0;

/**********
    static char *menu[]={
	"1 ＣＤ演奏する     ",
	"2 ＣＤ演奏を止める ",
	"",
	"3 音量を設定する   ",
	NULL };
**********/
/* ---------------------------------------------------------- */

    wrtstr(SPCSTR,30,1,0x1F);
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
	if (cd_m_stat != FALSE) m = 11;
	switch (Sel_menu(&menu[m],30,2,&no)) {
		case FALSE:
			break;
		case LEFT: 
			if (cd_m_stat == FALSE) {
	                    	Dsp_vram(vram);
				Cmds_PF4();
			}
			return;
		case RIGHT:
			if (cd_m_stat == FALSE) {
	                    	Dsp_vram(vram);
				Cmds_PF1();
			}
			return;
		default:
			return;
	}
/*****
    if ( Sel_menu(menu,30,2,&no) != FALSE )
	return;
*****/
/* ---------------------------------------------------------- */

    if ( no == 0 )
	CD_play();
    else if ( no == 1 )
	CD_stop();
    else if ( no == 2 )
	CD_vol();
/* --------------------- EUP play 92.03.30 ------------------ */
    else if ( no == 3 )               /* customaized by YAMA */
	runeup();
    else if ( no == 4 )               /* customaized by YAMA */
	stopeup();
/* ---------------------------------------------------------- */
    else if ( no == 5 )
	VC_on();
    else if ( no == 6 )
	VC_off();
}
