/*
*****************************************************************************
    TESTAPI.C   - Command test shell program for FMPAPI functions.
    Created by Mabel Sha
    Updated: 3/28/2002

*****************************************************************************
*/

#define _DEFINE_FMP_TYPES_
#include "fmp.h"
#include "rm84cmn.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>

/* for linux */
#if defined(__GNUC__)
#define INT		int
#define TCHAR		char
#define	DWORD		unsigned long
#define TEXT(X)		X
#define _tprintf	printf
#define _tcscmp		strcmp
#define _tcscpy		strcpy
#define _getts		gets
#define _ttoi		atoi
#endif

typedef DWORD COUNT;

/* For using unrefence arguments */
#define WASTEARG(X) (X)=(X)
//for FILE
typedef struct strthread{
	TCHAR *name;
	BOOL end;
}STRTHREAD;
STRTHREAD sthread;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret = 0;
int fd = 0;
//
static TCHAR command_buf[80] = {0};

typedef struct dispatcher {
    TCHAR *cmd;
    INT  (*proc)( INT argc, TCHAR **argv);
    TCHAR *helpstr;
} DISPATCHER;
//for TIMESEARCH function
typedef struct tagINFO{
	int    bFlag;
	BYTE	bDiscType;
}INFO;
INFO info;


int main(int argc, char **argv);
DISPATCHER *lex(INT *agc, TCHAR **agv);
TCHAR *gnext(TCHAR *p);
INT doEcho( INT agc, TCHAR **agv);
INT doQuit( INT agc, TCHAR **agv);
INT doHelp( INT agc, TCHAR **agv);
INT doLoad( INT agc, TCHAR **agv);
INT doUnload(INT agc, TCHAR **agv);
INT doFMPPlay(INT agc, TCHAR **agv);
INT doFMPPause(INT agc, TCHAR **agv);
INT doFMPStop(INT agc, TCHAR **agv);
INT doFMPClose(INT agc, TCHAR **agv);

INT doFMPGet(INT agc, TCHAR **agv);
INT doFMPSet(INT agc, TCHAR **agv);
INT doFMPAbout( INT agc, TCHAR **agv);
INT doFMPGetProfileWord( INT agc, TCHAR **agv);
INT doFMPWriteProfileWord( INT agc, TCHAR **agv);
INT doFMPProperty (INT agc, TCHAR **agv);

INT doFMPTrayOperation (INT agc, TCHAR **agv);
INT doFMPSetSource (INT agc, TCHAR **agv);
INT doFMPSetVisibleSource (INT agc, TCHAR **agv);
INT doFMPSetDestination (INT agc, TCHAR **agv);

INT doFMPSetVPort (INT agc, TCHAR **agv);

INT doDVDChapterPlay(INT agc, TCHAR **agv);
INT doDVDChapterSearch (INT agc, TCHAR **agv);
INT doDVDForwardScan  (INT agc, TCHAR **agv);
INT doDVDBackwardScan  (INT agc, TCHAR **agv);
INT doDVDSubPictureSelect (INT agc, TCHAR **agv);
INT doStopForResume (INT agc, TCHAR **agv);
INT doResume (INT agc, TCHAR **agv);
INT doDVDMenuCall (INT agc, TCHAR **agv);
INT doDVDUpperButtonSelect (INT agc, TCHAR **agv);
INT doDVDLowerButtonSelect (INT agc, TCHAR **agv);
INT doDVDRightButtonSelect (INT agc, TCHAR **agv);
INT doDVDLeftButtonSelect (INT agc, TCHAR **agv);
INT doDVDButtonActivate (INT agc, TCHAR **agv);
INT doDVDButtonSelectAndActivate (INT agc, TCHAR **agv);

INT doDVDAngleChange(INT agc, TCHAR **agv);
INT doDVDGoUp(INT agc, TCHAR **agv);
INT doDVDQueryAttribute(INT agc, TCHAR **agv);
INT doDVDStillOff(INT agc, TCHAR **agv);
INT doDVDStoreBookmark(INT agc, TCHAR **agv);
INT doDVDResumeBookmark(INT agc, TCHAR **agv);

INT doNextPGSearch (INT agc, TCHAR **agv);
INT doPrevPGSearch (INT agc, TCHAR **agv);
INT doAudioStreamChange (INT agc, TCHAR **agv);
INT doTitlePlay(INT agc, TCHAR **agv);
INT doTimeSearch(INT agc, TCHAR **agv);
INT doTimePlay(INT agc, TCHAR **agv);

INT doRepeatTitle(INT agc, TCHAR **agv);
INT doRepeatChapter(INT agc, TCHAR **agv);
INT doRepeatAB(INT agc, TCHAR **agv);
INT doClearRepeatAB(INT agc, TCHAR **agv);
// VCD
INT doSVCDReturnPGSearch (INT agc, TCHAR **agv);
INT doVCDDefaultPGSearch (INT agc, TCHAR **agv);
INT doVCDNumericSelections(INT agc, TCHAR **agv);
INT doFastForward (INT agc, TCHAR **agv);
INT doRewind(INT agc, TCHAR **agv);
//CDDA
INT doCDDAFastForward(INT agc, TCHAR **agv);
INT doCDDAGetNumberOfTracks(INT agc, TCHAR **agv);
INT doCDDAGetStatus(INT agc, TCHAR **agv);
INT doCDDAGetTrackDuration(INT agc, TCHAR **agv);
INT doCDDANextTrack(INT agc, TCHAR **agv);
INT doCDDAPlayTrack(INT agc, TCHAR **agv);
INT doCDDAPrevTrack(INT agc, TCHAR **agv);
INT doCDDARepeat(INT agc, TCHAR **agv);
INT doCDDARewind(INT agc, TCHAR **agv);

//
INT doDVD(INT agc, TCHAR **agv);
int TESTDVD (BYTE bLetter);

INT doVCD(INT agc, TCHAR **agv);
int TESTVCD (BYTE bLetter);

INT doSVCD(INT agc, TCHAR **agv);
int TESTSVCD (BYTE bLetter);

INT doCDDA(INT agc, TCHAR **agv);
int TESTCDDA (BYTE bLetter);

INT doFile(INT agc, TCHAR **agv);
void *Readdata(void* pthr);
DWORD Event_Notifications (DWORD dwContext, DWORD dwMsg, DWORD dwValue);
INT doSleep(INT agc, TCHAR **agv);


DISPATCHER cmds[] =
    {
    { TEXT("HELP"),             doHelp,                 TEXT("HELP           : print help")},
    { TEXT("ECHO"),             doEcho,                 TEXT("ECHO           : echo command")},
    { TEXT("QUIT"),             doQuit,                 TEXT("QUIT           : exit shell")},
    { TEXT("LOAD"),             doLoad,                 TEXT("LOAD           : load driver")},
    { TEXT("UNLOAD"),           doUnload,               TEXT("UNLOAD         : unload driver") },
    { TEXT("PLAY"),             doFMPPlay,              TEXT("PLAY           : FMPPlay ")},
    { TEXT("PAUSE"),            doFMPPause,             TEXT("PAUSE          : FMPPause ")},
    { TEXT("STOP"),             doFMPStop,              TEXT("STOP           : FMPStop ")},
    { TEXT("CLOSE"),            doFMPClose,             TEXT("CLOSE          : FMPClose ")},

    { TEXT("GET"),              doFMPGet,               TEXT("GET            : FMPGet") },
    { TEXT("SET"),              doFMPSet,               TEXT("SET            : FMPSet") },
    { TEXT("ABOUT"),            doFMPAbout,             TEXT("ABOUT          : Version and Copyright Notice") },
    { TEXT("GETPROFILE"),       doFMPGetProfileWord,    TEXT("GETPROFILE     : FMPGetProfileWord") },
    { TEXT("WRITEPROFILE"),     doFMPWriteProfileWord,  TEXT("WRITEPROFILE   : FMPWriteProfileWord") },
    { TEXT("PROPERTY"),         doFMPProperty,          TEXT("PROPERTY       : FMPProperty") },

    { TEXT("TRAY"),             doFMPTrayOperation,     TEXT("TRAY           : FMPTray") },
    { TEXT("SETSOURCE"),        doFMPSetSource,         TEXT("SETSOURCE      : FMPSetSource") },
    { TEXT("SETVISIBLESOURCE"), doFMPSetVisibleSource,  TEXT("SETVISIBLESOURCE : FMPSetVisibleSource") },
    { TEXT("SETDESTINATION"),   doFMPSetDestination,    TEXT("SETDESTINATION : FMPSetDestination") },

    { TEXT("SETVPORT"),         doFMPSetVPort,          TEXT("SETVPORT       : FMPSetVideoPortDimensions") },

    { TEXT("CHAPTERPLAY"),      doDVDChapterPlay,       TEXT("CHAPTERPLAY    : DVD Chapter Play") },
    { TEXT("CHAPTERSEARCH"),    doDVDChapterSearch,     TEXT("CHAPTERSEARCH  : DVD Chapter Search") },
    { TEXT("FS"),               doDVDForwardScan,       TEXT("FS             : DVD ForwardScan")},
    { TEXT("BS"),               doDVDBackwardScan,      TEXT("BS             : DVD BackwardScan")},
    { TEXT("SUBPICTURE"),       doDVDSubPictureSelect,  TEXT("SUBPICTURE     : SubPictureStreamChange")},
    { TEXT("STOPFORRESUME"),    doStopForResume,        TEXT("STOPFORRESUME  : StopForResume")},
    { TEXT("RESUME"),           doResume,               TEXT("RESUME         : Resume")},
    { TEXT("MENU"),             doDVDMenuCall,          TEXT("MENU           : MenuCall")},
    { TEXT("UBS"),              doDVDUpperButtonSelect, TEXT("UBS            : DVDUpperButtonSelect")},
    { TEXT("LOWERBS"),          doDVDLowerButtonSelect, TEXT("LOWERBS        : DVDLowerButtonSelect")},
    { TEXT("RBS"),              doDVDRightButtonSelect, TEXT("RBS            : DVDRightButtonSelect")},
    { TEXT("LEFTBS"),           doDVDLeftButtonSelect,  TEXT("LEFTBS         : DVDLeftButtonSelect")},
    { TEXT("BA"),               doDVDButtonActivate,    TEXT("BA             : DVDButtonActivate")},
    { TEXT("BSA"),              doDVDButtonSelectAndActivate,TEXT("BSA       : DVDButtonSelectAndActivate")},

    { TEXT("ANGLECHANGE"),      doDVDAngleChange,       TEXT("ANGLECHANGE    : DVD Anglechange") },
    { TEXT("GOUP"),             doDVDGoUp,              TEXT("GOUP           : DVD Go Up") },
    { TEXT("DVDQATT"),          doDVDQueryAttribute,    TEXT("DVDQATT        : DVD Query Attribute") },
    { TEXT("STILLOFF"),         doDVDStillOff,          TEXT("STILLOFF       : DVD Still Off") },
    { TEXT("STOREBOOKMARK"),    doDVDStoreBookmark,     TEXT("STOREBOOKMARK  : DVD StoreBookmark") },
    { TEXT("RESUMEBOOKMARK"),   doDVDResumeBookmark,    TEXT("RESUMEBOOKMARK : DVD ResumeBookmark") },

    { TEXT("NEXTPG"),           doNextPGSearch,         TEXT("NEXTPG         : NextPGSearch")},
    { TEXT("PREVPG"),           doPrevPGSearch,         TEXT("PREVPG         : PrevPGSearch")},
    { TEXT("AUDIOSTREAM"),      doAudioStreamChange,    TEXT("AUDIOSTREAM    : AudioStreamChange")},
    { TEXT("TITLEPLAY"),        doTitlePlay,            TEXT("TITLEPLAY      : Title Play") },
    { TEXT("TIMESEARCH"),       doTimeSearch,           TEXT("TIMESEARCH     : Time Search") },
    { TEXT("TIMEPLAY"),         doTimePlay,             TEXT("TIMEPLAY       : Time Play") },

    { TEXT("REPEATTITLE"),      doRepeatTitle,          TEXT("REPEATTITLE    : RepeatTitle") },
    { TEXT("REPEATCHAPTER"),    doRepeatChapter,        TEXT("REPEATCHAPTER  : RepeatChapter") },
    { TEXT("REPEATAB"),         doRepeatAB,             TEXT("REPEATAB       : RepeatAB") },
    { TEXT("CLEARREPEATAB"),    doClearRepeatAB,        TEXT("CLEARREPEATAB  : ClearRepeatAB") },

    { TEXT("RETURNPG"),         doSVCDReturnPGSearch,   TEXT("RETURNPG       : SVCDReturnPGSearch")},
    { TEXT("DEFAULTPG"),        doVCDDefaultPGSearch,   TEXT("DEFAULTPG      : VCD/SVCD DefaultPGSearch")},
    { TEXT("NUMSEL"),           doVCDNumericSelections, TEXT("NUMSEL         : VCDNumericSelections") },
    { TEXT("FF"),               doFastForward,          TEXT("FF             : VCD/SVCD FastForward") },
    { TEXT("RW"),               doRewind,               TEXT("RW             : VCD/SVCD Rewind") },

    { TEXT("CFF"),              doCDDAFastForward ,     TEXT("CFF            : CDDAFastForward") },
    { TEXT("CGETTRACKS"),       doCDDAGetNumberOfTracks,TEXT("CGETTRACKS     : CDDAGetNumberOfTracks") },
    { TEXT("CSTATUS"),          doCDDAGetStatus ,       TEXT("CSTATUS        : CDDAGetStatus") },
    { TEXT("CGETDURATION"),     doCDDAGetTrackDuration, TEXT("CGETDURATION   : CDDAGetTrackDuration") },
    { TEXT("CNEXT"),            doCDDANextTrack,        TEXT("CNEXT          : CDDANextTrack") },
    { TEXT("CPLAY"),            doCDDAPlayTrack,        TEXT("CPLAY          : CDDAPlayTrack") },
    { TEXT("CPREV"),            doCDDAPrevTrack,        TEXT("CPREV          : CDDAPrevTrack") },
    { TEXT("CREPEAT"),          doCDDARepeat,           TEXT("CREPEAT        : CDDARepeat") },
    { TEXT("CRW"),              doCDDARewind,           TEXT("CRW            : CDDARewind") },

    { TEXT("DVD"),              doDVD,                  TEXT("DVD            : DVD player")},
    { TEXT("VCD"),              doVCD,                  TEXT("VCD            : VCD player")},
    { TEXT("SVCD"),             doSVCD,                 TEXT("SVCD           : SVCD player")},
    { TEXT("CDDA"),             doCDDA,                 TEXT("CDDA           : CDDA player")},
    { TEXT("FILE"),             doFile,                 TEXT("FILE           : File Playback") },
    { TEXT("SLEEP"),            doSleep,                TEXT("SLEEP          : Sleep command") },
    { NULL }
    };

TCHAR *gnext(TCHAR *p);
TCHAR read_buf[32768];
DWORD res=0;
BOOL mpaused=FALSE;
BYTE BookMark[1024*10];


// Main function for Linux
int main(int argc, char **argv)
{
    DISPATCHER *pcmd;
    INT     agc = 0;
    TCHAR *agv[20];
    INT i;

    while( (pcmd = lex( &agc, &agv[0])) )
    {
        i = pcmd->proc(agc, &agv[0]);
   }
   return 0;

}


DISPATCHER *lex(INT *agc, TCHAR **agv)      /*__fn__*/
{
    TCHAR *cmd,*p;
    DISPATCHER *pcmds = &cmds[0];

    *agc = 0;    
    _tprintf(TEXT("\n:-> "));
    if (!_getts(command_buf))
        return NULL;

    p = cmd = &command_buf[0];

    /* Put the destination in uppercase */
   // p = _tcsupr(p);
    while ( (p = gnext(p) ))
    {
       if (*p == '"')
           *agv++ = (p+1);
       else
           *agv++ = p;
       *agc += 1;
    }

    while (pcmds->cmd)
    {
        if (_tcscmp(pcmds->cmd,cmd) == 0)
            return (pcmds);
        pcmds++;
    }

    /* No match return ??? */
    return (&cmds[0]);
 }

/* Null term the current token and return a pointer to the next */
TCHAR *gnext(TCHAR *p)                                            /*__fn__*/
{
    TCHAR termat;

    /* Look for space or the end of a quoted string */
    if (*p == '"')
        termat = '"';
    else
        termat = ' ';

    p++;
    while (*p)
    {
        if (*p == termat)
        {
            *p++ = '\0';    /* Null it and look at next */
            break;
        }
        p++;
    }

    if (!*p)                /* All done */
        p = NULL;

    return (p);
}

INT doEcho( INT agc, TCHAR **agv)                                  /*__fn__*/
{
    INT i;
    for (i = 0; i < agc; i++)
        _tprintf(TEXT(":%s:\n"),*agv++);
    return (1);
}

INT doQuit( INT agc, TCHAR **agv)                                /*__fn__*/
{
   WASTEARG(agc);
   WASTEARG(agv);
    _tprintf(TEXT(" Good Bye \n"));
   exit(0);
   return 0;
}

INT doHelp(INT agc, TCHAR **agv)                             /*__fn__*/
{
DISPATCHER *pcmds = &cmds[0];
TCHAR garbage[20];
COUNT nprinted = 0;

    WASTEARG(agc);
    WASTEARG(agv);

    while (pcmds->cmd)
    {
        _tprintf(TEXT("%s\n"), pcmds->helpstr);
        if (nprinted++ > 20)
        {
            _tprintf(TEXT("Press -cr-\n"));
            _getts(garbage);
            nprinted = 0;
        }
        pcmds++;
    }
    return (1);
}

INT doLoad( INT agc, TCHAR **agv)                                  /*__fn__*/
{
    int c=0;
    info.bDiscType=0;
	if (agc != 1)
	{
	        _tprintf(TEXT(" LOAD [Driver Number]\n"));
                _tprintf(TEXT(" 4(NO_DRIVE) 0(PRIMARY_MASTER) 2(SECOND_MASTER) \n"));

	    return(0);
	}
        if ( _ttoi(agv[0])== 4)
        {
                res = MPEGDriverEntry(NO_DRIVE) ;
        }
        else
        {
                c = (int) _ttoi (agv[0]);
                printf("%s",agv[0]);
                res = MPEGDriverEntry((BYTE)c);
        }

        _tprintf(TEXT("LOAD returned %ld\n"),res);
        return (0);
}

INT doUnload (INT agc, TCHAR **agv)                                    /*__fn__*/
{

       /* if (sthread.name){
                free(sthread.name);

               // sthread.end = TRUE;
               } */
      //  free(sthread.name);
        res = MPEGDriverUnload();
         _tprintf(TEXT(" UNLOAD returned %ld\n"), res);
        return(0);
}

INT doFMPPlay(INT agc, TCHAR **agv)                                    /*__fn__*/
{
        DWORD offset;
       // printf("MUTE off\n");
        FMPSet (FMPI_MUTE, 0);
        if (mpaused == TRUE){
                FMPPlay();
                mpaused =FALSE;}

        else if(fd){
                FMPStop();
                offset=lseek (fd,0,SEEK_SET);
               // printf("offset:%ld\n",offset);
                res = FMPPlay();
                }
        else {
                res = FMPPlay();}

        _tprintf(TEXT(" FMPPlay returned %ld\n"), res);
        return(0);
}

INT doFMPPause(INT agc, TCHAR **agv)                                    /*__fn__*/
{
	res = FMPPause();
	mpaused=TRUE;
	_tprintf(TEXT(" FMPPause returned %ld\n"), res);
    return(0);
}

INT doFMPStop(INT agc, TCHAR **agv)                                    /*__fn__*/
{
        DWORD offset;
        res = FMPStop();
        if (fd){
                offset=lseek (fd,0,SEEK_END);
              //  printf(TEXT("lseek:%ld\n"),offset);
                }
        _tprintf(TEXT(" FMPStop returned %ld\n"), res);
        return(0);
}

INT doFMPClose(INT agc, TCHAR **agv)                                    /*__fn__*/
{
        if (fd !=0){
                sthread.end=TRUE;
                ret=pthread_join(tid, NULL);
                printf("thread join finished: %d\n",ret);
               // FMPStop ();
                close(fd);
                fd = 0;
                 free(sthread.name);
                 res = FMPClose();}
        else {
                res = FMPClose();
                printf("DVD Close:%ld\n",res);
                }

       // res = FMPClose();

        _tprintf(TEXT(" FMPClose returned %ld\n"), res);
    return(0);

}

INT doFMPGet (INT agc, TCHAR **agv)
{
        if (agc != 1)
        {
                _tprintf(TEXT(" GET [Index]\n"));
                return (0);

        }

        res = FMPGet((DWORD)_ttoi(agv[0]));
        _tprintf(TEXT("FMPGet returned %ld\n"), res);

        return(0);

}


INT doFMPSet (INT agc, TCHAR **agv)
{
        if(agc != 2)
        {
                _tprintf(TEXT(" SET [Index] [Value]\n"));
                return (0);
        }

        res = FMPSet((DWORD)_ttoi(agv[0]), (DWORD)_ttoi(agv[1]));

        _tprintf(TEXT(" FMPSet returned  %ld\n"), res);
        return(0);

}

INT doFMPAbout(INT agc, TCHAR **agv)                                    /*__fn__*/
{
        TCHAR	sVersion[128],
                sDate[128],
                sCr[128];

        res = FMPAbout(sVersion, sDate, sCr);
        _tprintf(TEXT(" Version : %s \n"),sVersion);
        _tprintf(TEXT(" Build on : %s \n"),sDate);
        _tprintf(TEXT(" %s\n"), sCr);

        _tprintf(TEXT(" FMPAbout returned %ld\n"), res);
      return(0);
}
INT doFMPGetProfileWord( INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT(" GETPROFILE [index]\n"));
		return 0;
	}
	res = FMPGetProfileWord ((DWORD)_ttoi(agv[0]));
	_tprintf (TEXT("FMPGetProfileWord returned %ld\n"), res);
	return 0;
}

INT doFMPWriteProfileWord( INT agc, TCHAR **agv)
{
	if (agc != 2)
	{
		_tprintf(TEXT(" WRITEPROFILE [Index] [Value]\n"));
		return 0;
	}
	res = FMPWriteProfileWord ((DWORD)_ttoi(agv[0]), (DWORD)_ttoi(agv[1]));
	_tprintf (TEXT("FMPWriteProfileWord returned %ld\n"), res);
	return 0;
}

INT doFMPProperty (INT agc, TCHAR **agv)
{

	 DWORD sizeOut, fVal;

	if (FMPProperty (KSPROPERTY_TYPE_GET,
		   BOARDINFO_SET, ebiDeviceId, 0, &fVal, sizeof(fVal), &sizeOut) == 0)
    {
		if(sizeOut!=sizeof(fVal))
			return 0;
		res=fVal;
    }
	_tprintf (TEXT("FMPProperty returned %ld\n"), res);
	return 0;
}

INT doFMPTrayOperation (INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT(" TRAY [tray code]\n"));
		return 0;
	}
	res = FMPTrayOperation ((DWORD)_ttoi(agv[0]));
	_tprintf (TEXT("FMPTrayOperation returned %ld\n"), res);
	return 0;
}

INT doFMPSetSource (INT agc, TCHAR **agv)
{
	if (agc != 2)
	{
		_tprintf(TEXT(" SETSOURCE [Width] [Height]\n"));
		return 0;
	}

	res = FMPSetSource (0,0,(DWORD)_ttoi(agv[0]), (DWORD)_ttoi(agv[1]));
	_tprintf (TEXT("FMPSetSource returned %ld\n"), res);
	return 0;
}

INT doFMPSetVisibleSource (INT agc, TCHAR **agv)
{
	if (agc != 2)
	{
		_tprintf(TEXT(" SETVISIBLESOURCE [Width] [Height]\n"));
		return 0;
	}
	FMPSet(FMPI_OVERLAY_FLAGS, 1);
	res = FMPSetVisibleSource ( 0, 0, (DWORD)_ttoi(agv[0]), (DWORD)_ttoi(agv[1]));

	_tprintf (TEXT("FMPSetVisibleSource returned %ld\n"), res);
	return 0;

}


INT doFMPSetDestination (INT agc, TCHAR **agv)
{
	if (agc != 4)
	{
		_tprintf(TEXT(" SETDESTINATION [Left] [Top] [Width] [Height]\n"));
		return 0;
	}

	FMPSet(FMPI_OVERLAY_FLAGS, 2);  //VIDEO_TV_DEST_ENABLE 0x0002
	res = FMPSetDestination ((DWORD)_ttoi(agv[0]), (DWORD)_ttoi(agv[1]),(DWORD)_ttoi(agv[2]), (DWORD)_ttoi(agv[3]));
	_tprintf (TEXT("FMPSetDestination returned %ld\n"), res);
	return 0;

}
INT doFMPSetVPort (INT agc, TCHAR **agv)
{
       if (agc != 2)
        {
                _tprintf(TEXT(" SETVPORT [WIDTH] [HEIGHT]\n"));
                return (0);
        }
        //return FMPSetVideoPortDimensions((DWORD) _ttoi(agv[0]), (DWORD) _ttoi(agv[1]));
        res = FMPSetVideoPortDimensions((DWORD) _ttoi(agv[0]), (DWORD) _ttoi(agv[1]));
        _tprintf (TEXT("FMPSetVideoPortDimensions returned %ld\n"), res);
        return 0;


}


INT doDVDChapterPlay (INT agc, TCHAR **agv)
{
	if (agc != 2)
	{
		_tprintf(TEXT(" CHAPTERPLAY [TitleNumber] [ChapterNumber]\n"));
		return (0);

	}

	res = ChapterPlay((BYTE)_ttoi(agv[0]), (WORD)_ttoi(agv[1]));

	_tprintf(TEXT(" CHAPTERPLAY returned %ld\n"), res);
    return(0);

}

INT doDVDChapterSearch (INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT(" CHAPTERSEARCH [ChapterNumber]\n"));
		return (0);

	}
	res = ChapterSearch((WORD)_ttoi(agv[0]));
	_tprintf(TEXT(" CHAPTERSEARCH returned  %ld\n"), res);
    return(0);

}

INT doDVDForwardScan (INT agc, TCHAR **agv)
{
	if(agc != 2)
	{
		_tprintf(TEXT(" FORWARDSCAN [speed] [fast]\n"));
		return (0);
	}

	res = ForwardScan((BYTE)_ttoi(agv[0]), (BYTE)_ttoi(agv[1]));

	_tprintf(TEXT(" DVDForwardScan returned  %ld\n"), res);
    return(0);

}


INT doDVDBackwardScan (INT agc, TCHAR **agv)
{
	if(agc != 2)
	{
		_tprintf(TEXT(" BACKWARDSCAN [speed] [fast]\n"));
		return (0);
	}

	res = BackwardScan((BYTE)_ttoi(agv[0]), (BYTE)_ttoi(agv[1]));

	_tprintf(TEXT(" DVDBackwardScan returned  %ld\n"), res);
    return(0);

}

INT doDVDSubPictureSelect (INT agc, TCHAR **agv)
{
	if(agc != 2)
	{
		_tprintf(TEXT(" SUBPICTURE [STREAM ID] [ON/OFF]\n"));
		return (0);
	}

	res = SubPictureStreamChange((BYTE)_ttoi(agv[0]), (BYTE)_ttoi(agv[1]));

	_tprintf(TEXT(" DVDSubPicture returned  %ld\n"), res);
    return(0);

}

INT doStopForResume (INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" STOPFORRESUME\n"));
		return (0);
	}

	res = StopForResume();

	_tprintf(TEXT(" StopForResume returned  %ld\n"), res);
    return(0);

}

INT doResume (INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" RESUME\n"));
		return (0);
	}

	res = Resume();

	_tprintf(TEXT(" Resume returned  %ld\n"), res);
    return(0);

}

INT doDVDMenuCall (INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT(" MENU [MenuNumber]\n"));
		return (0);

	}

	res = MenuCall((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" DVDMenucall returned  %ld\n"), res);
    return(0);

}


INT doDVDLeftButtonSelect (INT agc, TCHAR **agv)
{

	res = LeftButtonSelect();

	_tprintf(TEXT(" DVDLeftButtonSelect returned  %ld\n"), res);
    return(0);
}

INT doDVDRightButtonSelect (INT agc, TCHAR **agv)
{

	res = RightButtonSelect();

	_tprintf(TEXT(" DVDRightButtonSelect returned  %ld\n"), res);
    return(0);
}

INT doDVDUpperButtonSelect (INT agc, TCHAR **agv)
{

	res = UpperButtonSelect();

	_tprintf(TEXT(" DVDUpperButtonSelect returned  %ld\n"), res);
    return(0);
}

INT doDVDLowerButtonSelect (INT agc, TCHAR **agv)
{

	res = LowerButtonSelect();

	_tprintf(TEXT(" DVDLowerButtonSelect returned  %ld\n"), res);
    return(0);
}


INT doDVDButtonActivate (INT agc, TCHAR **agv)
{

	res = ButtonActivate();

	_tprintf(TEXT(" DVDButtonActivate returned  %ld\n"), res);
    return(0);
}


INT doDVDButtonSelectAndActivate (INT agc, TCHAR **agv)
{

	if (agc != 1)
	{
		_tprintf(TEXT(" BSA [Button num]\n"));
		return (0);

	}


	res = ButtonSelectAndActivate((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" DVDButtonSelectAndActivate returned  %ld\n"), res);
    return(0);
}

INT doDVDAngleChange (INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT(" ANGLECHANGE [Angle]\n"));
		return (0);

	}

	res = AngleChange((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" DVDAngleChange returned  %ld\n"), res);
    return(0);

}

INT doDVDGoUp (INT agc, TCHAR **agv)
{
	if (agc != 0)
	{
		_tprintf(TEXT(" GOUP \n"));
		return (0);

	}

	res = GoUp();

	_tprintf(TEXT(" GOUP returned  %ld\n"), res);
    return(0);

}

INT doDVDQueryAttribute(INT agc, TCHAR **agv)
{
/*DVDI_TT_SRPTI                           0x0001
 DVDI_AST_ATR                             0x0002
 DVDI_SPST_ATR                            0x0003
 DVDI_SPRM                                0x0004
 DVDI_VIDEO_MODE                          0x0005
 DVDI_CURRENT_UOPS                        0x000B
*/
	DWORD res, i;
	DWORD arg1, arg2;
	TT_SRPTI tt;
	AST_ATR ast;
	SPST_ATR spst;
	VIDEO_MODE vmode;


	arg1 = 	(DWORD)_ttoi(agv[0]);
        arg2 = 	(DWORD)_ttoi(agv[1]);



	if(agc != 2)
	{
		_tprintf(TEXT(" DVDQATT [FLAG] [ARG]\n"));
               	return (0);
	}
        if(arg1 == DVDI_CURRENT_UOPS)
	{
        	res = (DWORD)DVDQueryAttribute((DWORD)_ttoi(agv[0]), (DWORD)NULL);
        }

	if(arg1 == DVDI_TT_SRPTI)
	{
		res = DVDQueryAttribute((DWORD)_ttoi(agv[0]), (DWORD)&tt);
	}


	if(arg1 == DVDI_AST_ATR)
	{
		res = DVDQueryAttribute((DWORD)_ttoi(agv[0]), (DWORD)&ast);
	}


	if(arg1 == DVDI_SPST_ATR)
	{
		res = DVDQueryAttribute((DWORD)_ttoi(agv[0]), (DWORD) &spst);
	}


	if(arg1 == DVDI_VIDEO_MODE)
	{
		res = DVDQueryAttribute((DWORD)_ttoi(agv[0]), (DWORD) &vmode);
	}




	if(arg1 == DVDI_SPRM)
		res = DVDQueryAttribute((DWORD)_ttoi(agv[0]), (DWORD)_ttoi(agv[1]));


	if(arg1 == DVDI_SPRM)
		_tprintf(TEXT("Attribute returned : %ld\n"), res);


	if(arg1 == DVDI_TT_SRPTI)
	{
		_tprintf(TEXT("Attribute returned TT_SRPTI\n"));
		_tprintf(TEXT("Number of Titles = %d\n"), tt.bTT_SRP_Ns );

		for(i=0; i< tt.bTT_SRP_Ns; i++)
		{
			_tprintf(TEXT("Title %ld\n"), i+1 );

			_tprintf(TEXT("Number of Angles = %d\n"),  tt.bALG_Ns[i] );
			_tprintf(TEXT("Number of Part_of_Titles = %d\n"),  tt.wPTT_Ns[i] );


		}

	}



	if(arg1 ==DVDI_AST_ATR)
	{

		_tprintf(TEXT("Attribute returned AST_ATR\n"));
		_tprintf(TEXT("Number of Audio Streams available = %d\n"), ast.bAST_Ns );
		for(i=0; i< ast.bAST_Ns; i++)
		{
			_tprintf(TEXT("Audio stream %ld\n"), i+1 );

			if(ast.bAvailable[i] == TRUE)
			{
				_tprintf(TEXT("Audio stream is available in current chain\n") );
			}else
				_tprintf(TEXT("Audio stream is not available in current chain\n") );


			_tprintf(TEXT("Audio Coding Mode for stream is ") );

			switch(ast.bAudioCodingMode[i]) {
			case 0x00:
				_tprintf(TEXT("Dolby AC-3\n"));
				break;
			case 0x02:
				_tprintf(TEXT("MPEG-1\n"));
				break;
			case 0x03:
				_tprintf(TEXT("MPEG-2 with extension\n"));
				break;
			case 0x04:
				_tprintf(TEXT("Linear PCM audio\n"));
				break;
			case 0x06:
				_tprintf(TEXT("DTS\n"));
				break;
			case 0x07:
				_tprintf(TEXT("SDDS\n"));
				break;
			default:
				break;
			}


			if(ast.bMultichannelExtenstion[i] == TRUE)
			{
				_tprintf(TEXT("Audio stream has multichannel extension\n") );
			}else
				_tprintf(TEXT("Audio stream does not have multichannel extension\n") );


			if(ast.bAudioType[i] == 0)
			{
				_tprintf(TEXT("Audio type for stream %ld : Not specified\n"), i );
			}else
				_tprintf(TEXT("Audio type for stream %ld : Language\n"), i );


			if(ast.bAudioApplicationMode[i] == 0x00)
				_tprintf(TEXT("Audio Application Mode for stream is Not specified\n") );
			if(ast.bAudioApplicationMode[i] == 0x01)
				_tprintf(TEXT("Audio Application Mode for stream is Karaoke mode\n") );
			if(ast.bAudioApplicationMode[i] == 0x02)
				_tprintf(TEXT("Audio Application Mode for stream is Surround mode\n") );
			if(ast.bAudioApplicationMode[i] == 0x03)
				_tprintf(TEXT("Audio Application Mode for stream is reserved\n") );


			 _tprintf(TEXT("Audio stream quantization is %d\n"),  ast.bQuantization[i] );

			 _tprintf(TEXT("Audio stream frequency is %d\n"),  ast.bfs[i] );



			if(ast.bNumberOfAudioChannels[i] == 0x00)
				_tprintf(TEXT("Audio stream  has 1 channel (mono)\n") );
			if(ast.bNumberOfAudioChannels[i] == 0x01)
				_tprintf(TEXT("Audio stream  has 2 channels\n") );
			if(ast.bNumberOfAudioChannels[i] == 0x02)
				_tprintf(TEXT("Audio stream  has 3 channels \n") );
			if(ast.bNumberOfAudioChannels[i] == 0x03)
				_tprintf(TEXT("Audio stream  has 4 channels \n") );
			if(ast.bNumberOfAudioChannels[i] == 0x04)
				_tprintf(TEXT("Audio stream  has 5 channels \n") );
			if(ast.bNumberOfAudioChannels[i] == 0x05)
				_tprintf(TEXT("Audio stream  has 6 channels \n") );
			if(ast.bNumberOfAudioChannels[i] == 0x06)
				_tprintf(TEXT("Audio stream  has 7 channels \n") );
			if(ast.bNumberOfAudioChannels[i] == 0x07)
				_tprintf(TEXT("Audio stream  has 8 channels \n") );


			 _tprintf(TEXT("Audio stream language code is %d\n"), ast.wLanguageCode[i] );

			 _tprintf(TEXT("Audio stream language code extension is %d\n"), ast.wLanguageCodeExtension[i] );

			 _tprintf(TEXT("Audio stream application extension is %d\n\n"), ast.bApplicationExtension[i] );

		}
	}


	if(arg1 ==DVDI_SPST_ATR)
	{

		_tprintf(TEXT("Attribute returned SPST_ATR\n"));
		_tprintf(TEXT("Number of Subpicture Streams available = %d\n"), spst.bSPST_Ns );

		for(i=0; i< spst.bSPST_Ns; i++)
		{

			_tprintf(TEXT("Subpicture stream %ld\n"), i+1 );
			if(spst.bAvailable[i] == TRUE)
			{
				_tprintf(TEXT("Subpicture stream is available in the current program chain\n") );
			}else
				_tprintf(TEXT("Subpicture stream is not available in the current program chain\n") );


			if(spst.bSubpictureType[i] == 0)
			{
				_tprintf(TEXT("Subpicture type is not specified\n") );
			}else
				_tprintf(TEXT("Subpicture type is Language\n") );


			 _tprintf(TEXT("Subpicture Language Code is %d\n"), spst.wLanguageCode[i] );
			 _tprintf(TEXT("Subpicture Language Code Extension is %d\n\n"), spst.wLanguageCodeExtension[i] );

		}
	}



	if(arg1 ==DVDI_VIDEO_MODE)
	{

		_tprintf(TEXT("Attribute returned VIDEO_MODE\n"));


		if(vmode.bAspectRatio == 0x00)
			_tprintf(TEXT("Aspect Ratio is 4:3\n") );
		else if(vmode.bAspectRatio == 0x03)
			_tprintf(TEXT("Aspect Ratio is 16:9\n") );
		else
			_tprintf(TEXT("Aspect Ratio is reserved\n") );


		if(vmode.bDisplayMode == 0x00)
			_tprintf(TEXT("Display Mode is Both Pan-scan and Letterbox\n") );
		else if(vmode.bDisplayMode == 0x01)
			_tprintf(TEXT("Display Mode is only Pan-scan\n") );
		else if(vmode.bDisplayMode == 0x02)
			_tprintf(TEXT("Display Mode is only Letterbox\n") );
		else if(vmode.bDisplayMode == 0x03)
			_tprintf(TEXT("Display Mode is Not specified\n") );


	}





	_tprintf(TEXT(" DVDQueryAttribute returned  %ld\n"), res);
    return(0);


}

INT doDVDStillOff(INT agc, TCHAR **agv)
{
        if (agc != 0)
        {
                _tprintf(TEXT(" STILLOFF\n"));
                return (0);
        }

        res = StillOff();

        _tprintf(TEXT(" DVDStillOff returned  %ld\n"), res);
    return(0);

}

INT doDVDStoreBookmark(INT agc, TCHAR **agv)
{
	res = StoreBookmark (BookMark);
	FMPStop();
	FMPClose();
	_tprintf(TEXT("StoreBookmark returned %ld\n"), res);

    return(0);
}
INT doDVDResumeBookmark(INT agc, TCHAR **agv)
{
	//TEST(2);
        TESTDVD (2);
	FMPPlay();
	res = ResumeBookmark (BookMark);

	_tprintf(TEXT("ResumeBookmark returned %ld\n"), res);
	return(0);
}

INT doNextPGSearch (INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" NEXTPGSEARCH\n"));
		return (0);
	}

	res = NextPGSearch();

	_tprintf(TEXT(" NextPGSearch returned  %ld\n"), res);
    return(0);
}

INT doPrevPGSearch (INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" PREVPGSEARCH\n"));
		return (0);
	}

	res = PrevPGSearch();

	_tprintf(TEXT(" PrevPGSearch returned  %ld\n"), res);
    return(0);
}
INT doAudioStreamChange (INT agc, TCHAR **agv)
{
	if(agc != 1)
	{
		_tprintf(TEXT(" AUDIOSTREAM [STREAM ID]\n"));
		return (0);
	}

	res = AudioStreamChange((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" AudioSteamChange returned  %ld\n"), res);
    return(0);

}
INT doTitlePlay (INT agc, TCHAR **agv)                                    /*__fn__*/
{
	if (agc != 1)
	{
		_tprintf(TEXT(" TITLEPLAY [TitleNumber]\n"));
	    return(0);
	}

	res = TitlePlay((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" TitlePlay returned %ld\n"), res);
    return(0);
}
INT doTimeSearch (INT agc, TCHAR **agv)
{
	BYTE bHour;BYTE Hours10,Hours1;
	BYTE bMinute; BYTE Minutes10, Minutes1;
	BYTE bSecond; BYTE Seconds10, Seconds1;
	DWORD TimeCode;
	if (info.bDiscType == FMPC_DVD_VIDEO)
	{
		if (agc != 3)
		{
			_tprintf(TEXT(" TIMESEARCH [Hours] [Minutes] [Seconds]\n"));
			return (0);

		}
    bHour = (BYTE)_ttoi(agv[0]);
	bMinute = (BYTE)_ttoi(agv[1]);
	bSecond = (BYTE)_ttoi(agv[2]);
	Hours10 = bHour/10;
	Hours1 = bHour%10;
	Minutes10 = bMinute/10;
	Minutes1 = bMinute%10;
	Seconds10 = bSecond/10;
	Seconds1 = bSecond%10;
	TimeCode = (Hours10<<4)|(Hours1<<0)|(Minutes10<<12)
			|(Minutes1<<8)|(Seconds10<<20)|(Seconds1<<16);

	res = TimeSearch((TCHAR *) TimeCode);}

	if (info.bDiscType == FMPC_SVCD)
	{
		if (agc != 1)
		{
			_tprintf(TEXT(" TIMESEARCH [seconds]\n"));
			return (0);
		}
		res = TimeSearch((TCHAR*)_ttoi(agv[0]));
	}
	_tprintf(TEXT(" TimeSearch returned  %ld\n"), res);
    return(0);

}

INT doTimePlay (INT agc, TCHAR **agv)
{

	BYTE bTitleNumber;
	BYTE bHour;BYTE Hours10,Hours1;
	BYTE bMinute; BYTE Minutes10, Minutes1;
	BYTE bSecond; BYTE Seconds10, Seconds1;
	DWORD TimeCode;
	if (info.bDiscType == FMPC_DVD_VIDEO)
	{

		if (agc != 4)
		{
			_tprintf(TEXT(" TIMEPLAY [Title] [Hours] [Minutes] [Seconds]\n"));
			return (0);

		}
    bTitleNumber = (BYTE)_ttoi(agv[0]);
	bHour = (BYTE)_ttoi(agv[1]);
	bMinute = (BYTE)_ttoi(agv[2]);
	bSecond = (BYTE)_ttoi(agv[3]);
	Hours10 = bHour/10;
	Hours1 = bHour%10;
	Minutes10 = bMinute/10;
	Minutes1 = bMinute%10;
	Seconds10 = bSecond/10;
	Seconds1 = bSecond%10;
	TimeCode = (Hours10<<4)|(Hours1<<0)|(Minutes10<<12)
			|(Minutes1<<8)|(Seconds10<<20)|(Seconds1<<16);

	res = TimePlay(bTitleNumber,(TCHAR *) TimeCode);}

	_tprintf(TEXT(" TimePlay returned  %ld\n"), res);
    return(0);
}

INT doRepeatTitle(INT agc, TCHAR **agv)
{
	if (agc != 0)
	{
		_tprintf(TEXT(" REPEATTITLE\n"));
		return (0);
        }
	res = RepeatTitle();
	_tprintf(TEXT(" RepeatTitle returned  %ld\n"), res);
    return(0);
}

INT doRepeatChapter(INT agc, TCHAR **agv)
{
        if (agc != 0)
        {
                _tprintf(TEXT(" REPEATCHAPTER\n"));
                return (0);
        }
        res = RepeatChapter();
        _tprintf(TEXT(" RepeatChapter returned  %ld\n"), res);
    return(0);

}

INT doRepeatAB(INT agc, TCHAR **agv)
{
	if (agc != 0)
	{
		_tprintf(TEXT(" REPEATAB\n"));
		return (0);

	}

	res = RepeatAB();

	_tprintf(TEXT(" RepeatAB returned  %ld\n"), res);
    return(0);

}

INT doClearRepeatAB(INT agc, TCHAR **agv)
{
	res = ClearRepeatAB();

	_tprintf(TEXT(" ClearRepeatAB returned  %ld\n"), res);
    return(0);
}

INT doSVCDReturnPGSearch (INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" RETURNPG\n"));
		return (0);
	}

	res = ReturnPGSearch();

	_tprintf(TEXT(" SVCDReturnPGSearch returned  %ld\n"), res);
    return(0);

}
INT doVCDDefaultPGSearch (INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" DEFAULTPG\n"));
		return (0);
	}

	res = DefaultPGSearch();

	_tprintf(TEXT(" VCDDefaultPGSearch returned  %ld\n"), res);
    return(0);

}

INT doVCDNumericSelections (INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT(" NUMSEL [Selection]\n"));
	    return(0);
	}

	res = NumericSelections((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" VCDNumericSelections returned  %ld\n"), res);
    return(0);
}


INT doFastForward (INT agc, TCHAR **agv)
{
	if(agc != 1)
	{
		_tprintf(TEXT(" FF [Speed]\n"));
		return (0);
	}

	res = FastForward((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" Fastforward returned  %ld\n"), res);
    return(0);

}


INT doRewind (INT agc, TCHAR **agv)
{
	if(agc != 1)
	{
		_tprintf(TEXT(" RW [Speed]\n"));
		return (0);
	}

	res = Rewind((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" Rewind returned  %ld\n"), res);
    return(0);

}
INT doCDDAFastForward(INT agc, TCHAR **agv)
{
        if(agc !=1)
	{
		_tprintf(TEXT(" CFF [Speed]\n"));
		return (0);
	}

	res = CDDAFastForward((DWORD)_ttoi(agv[0]));

	_tprintf(TEXT(" CDDAFastForward returned  %ld\n"), res);
    return(0);
}
INT doCDDAGetNumberOfTracks(INT agc, TCHAR **agv)
{
        DWORD val=0;
        res = CDDAGetNumberOfTracks((BYTE*)&val);
        if( res==0 ){
            printf(TEXT (" Number of Tracks : %ld\n"),val);
             _tprintf(TEXT(" CDDAGetNumberOfTracks returned  %ld\n"), res);
            }

    return(0);
}
INT doCDDAGetStatus(INT agc, TCHAR **agv)
{
        DWORD mode=0, track=0,sec=0;
        res = CDDAGetStatus((DWORD*) &mode, (BYTE*) &track, (DWORD*) &sec);
        if (res==0){
                printf(" Current mode status: %ld\n", mode);
                printf(" Current track: %ld\n", track);
                printf(" Current position in seconds: %ld\n", sec);
                _tprintf(TEXT(" CDDAGetStatus returned  %ld\n"), res);
                }
    return(0);
}

INT doCDDAGetTrackDuration(INT agc, TCHAR **agv)
{
        DWORD nsec=0;
        if(agc != 1)
	{
		_tprintf(TEXT(" CGETDURATION [TrackNumber] \n"));
		return (0);
	}
        res = CDDAGetTrackDuration((BYTE)_ttoi(agv[0]),(DWORD*)&nsec);
        if (res==0){
                printf (" Time for audio %s track in seconds: %ld\n", agv[0], nsec);
                _tprintf(TEXT(" CDDAGetTrackDuration returned  %ld\n"), res);
        }
    return(0);

}

INT doCDDANextTrack(INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" CNEXT \n"));
		return (0);
	}

	res = CDDANextTrack();

	_tprintf(TEXT(" CDDANextTrack returned  %ld\n"), res);
    return(0);
}
INT doCDDAPlayTrack(INT agc, TCHAR **agv)
{
         if(agc != 3)
	{
		_tprintf(TEXT(" CPLAY [Track] [Min] [Sec]\n"));
		return (0);
	}
        res = CDDAPlayTrack((DWORD)_ttoi(agv[0]),(DWORD)_ttoi(agv[1]),(DWORD)_ttoi(agv[2]));
        _tprintf(TEXT(" CDDAPlayTrack returned  %ld\n"), res);
    return(0);
}

INT doCDDAPrevTrack(INT agc, TCHAR **agv)
{
	if(agc != 0)
	{
		_tprintf(TEXT(" CPREV \n"));
		return (0);
	}

	res = CDDAPrevTrack();

	_tprintf(TEXT(" CDDAPrevTrack returned  %ld\n"), res);
    return(0);
}
INT doCDDARepeat(INT agc, TCHAR **agv)
{
       if(agc !=1)
	{
		_tprintf(TEXT(" CREPEAT [Mode]\n"));
		return (0);
	}

	res = CDDARepeat((DWORD)_ttoi(agv[0]));

	_tprintf(TEXT(" CDDARepeat returned  %ld\n"), res);
    return(0);
}

INT doCDDARewind(INT agc, TCHAR **agv)
{
        if(agc !=1)
	{
		_tprintf(TEXT(" CRW [Speed]\n"));
		return (0);
	}

	res = CDDARewind((DWORD)_ttoi(agv[0]));

	_tprintf(TEXT(" CDDARewind returned  %ld\n"), res);
    return(0);
}

INT doDVD (INT agc, TCHAR **agv)
{
	if (agc != 1)
	{
		_tprintf(TEXT("DVD [Driver Number 0 or 2]\n"));
	    return(0);
	}

        res = TESTDVD((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" DVD returned %ld\n"), res);
    return(0);

}

int TESTDVD (BYTE bLetter)
{
	FMP_OPENSTRUCT FMPOpenStruct;

	FMPOpenStruct.dwStructSize		= sizeof (FMP_OPENSTRUCT);
	FMPOpenStruct.bDriverNumber		= bLetter;
	FMPOpenStruct.dwSize			= 32*1024;
	FMPOpenStruct.dwCount			= 8;
	FMPOpenStruct.pCallback			= Event_Notifications;
	FMPOpenStruct.dwContext			= 0;
	FMPOpenStruct.sFileName			= NULL;
	FMPOpenStruct.dwFlags			= FMPF_DVD;
	FMPOpenStruct.bDiscContent		= FMPC_DVD_VIDEO;
	FMPOpenStruct.bFileSystem		= 0x00;

	info.bDiscType=FMPC_DVD_VIDEO;//FMPOpenStruct.bDiscContent;
	return FMPOpenDiscPlayback (&FMPOpenStruct);
}

INT doVCD (INT agc, TCHAR **agv)                                    /*__fn__*/
{
	if (agc != 1)
	{
		_tprintf(TEXT("VCD [Driver Number 0 or 2]\n"));
	    return(0);
	}

        res = TESTVCD((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" VCD returned %ld\n"), res);
    return(0);

}

int TESTVCD (BYTE bLetter)
{
	FMP_OPENSTRUCT FMPOpenStruct;

	FMPOpenStruct.dwStructSize		= sizeof (FMP_OPENSTRUCT);
	FMPOpenStruct.bDriverNumber		= bLetter;
	FMPOpenStruct.dwSize			= 32*1024;
	FMPOpenStruct.dwCount			= 8;
	FMPOpenStruct.pCallback			= 0;//Event_Notifications;
	FMPOpenStruct.dwContext			= 0;
	FMPOpenStruct.sFileName			= NULL;
	FMPOpenStruct.dwFlags			= FMPF_SYSTEM;
	FMPOpenStruct.bDiscContent		= FMPC_VCD;
	FMPOpenStruct.bFileSystem		= 0x00;

	return FMPOpenDiscPlayback (&FMPOpenStruct);
}
INT doSVCD (INT agc, TCHAR **agv)                                    /*__fn__*/
{
	if (agc != 1)
	{
		_tprintf(TEXT("SVCD [Driver Number 0 or 2]\n"));
	    return(0);
	}

        res = TESTSVCD((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" SVCD returned %ld\n"), res);
    return(0);

}

int TESTSVCD (BYTE bLetter)
{
	FMP_OPENSTRUCT FMPOpenStruct;

	FMPOpenStruct.dwStructSize		= sizeof (FMP_OPENSTRUCT);
	FMPOpenStruct.bDriverNumber		= bLetter;
	FMPOpenStruct.dwSize			= 32*1024;
	FMPOpenStruct.dwCount			= 8;
	FMPOpenStruct.pCallback			= 0;//Event_Notifications;
	FMPOpenStruct.dwContext			= 0;
	FMPOpenStruct.sFileName			= NULL;
	FMPOpenStruct.dwFlags			= FMPF_SYSTEM;
	FMPOpenStruct.bDiscContent		= FMPC_SVCD;
	FMPOpenStruct.bFileSystem		= 0x00;
        info.bDiscType=FMPC_SVCD;
	return FMPOpenDiscPlayback (&FMPOpenStruct);
}

INT doCDDA (INT agc, TCHAR **agv)                                    /*__fn__*/
{
	if (agc != 1)
	{
		_tprintf(TEXT("CDDA [Driver Number 0 or 2]\n"));
	    return(0);
	}

        res = TESTCDDA((BYTE)_ttoi(agv[0]));

	_tprintf(TEXT(" CDDA returned %ld\n"), res);
    return(0);

}

int TESTCDDA (BYTE bLetter)
{
	FMP_OPENSTRUCT FMPOpenStruct;

	FMPOpenStruct.dwStructSize		= sizeof (FMP_OPENSTRUCT);
	FMPOpenStruct.bDriverNumber		= bLetter;
	FMPOpenStruct.dwSize			= 32*1024;
	FMPOpenStruct.dwCount			= 8;
	FMPOpenStruct.pCallback			= 0;
	FMPOpenStruct.dwContext			= 0;
	FMPOpenStruct.sFileName			= NULL;
	FMPOpenStruct.dwFlags			= FMPF_SYSTEM;
	FMPOpenStruct.bDiscContent		= FMPC_CDDA;
	FMPOpenStruct.bFileSystem		= 0x00;

	return FMPOpenDiscPlayback (&FMPOpenStruct);
}

INT doFile(INT agc, TCHAR **agv)
{
        int streamtype;
        sthread.end=FALSE;
	if (agc!=2)
        {
               fprintf
        (
            stderr,
            "Syntax is FILE <streamtype> <filename> \n"
    	    "<streamtype=t (transport) d (dvd) s (system)\n\n"
    	    "Example: FILE s /samplempeg/movie.mpg\n"
        );
                return(0);
        }
        printf(TEXT("streamtype: %s\n"),agv[0]);
        switch (agv[0][0])
        {
        case 't':
            streamtype=FMPF_TRANSPORT;
            break;

        case 'd':
            streamtype=FMPF_DVD;
            break;

        case 's':
            streamtype=FMPF_SYSTEM;
            break;

        default:
            printf
            (
                "Syntax is playfile <streamtype> <filename>\n"
                "<streamtype=t (transport) d (dvd) s (system)>\n"
            );
            return (0);
        }
        sthread.name =  malloc(sizeof(char)*strlen(agv[1]));
        //printf("%s\n",agv[1]);
        strcpy(sthread.name,agv[1]);
        printf("%s\n", sthread.name);
                fd=open(sthread.name,O_RDONLY);
                if (fd ==-1){
                perror ("open failed");

        }

	FMPOpen (streamtype, 32*1024, 8, NULL, 0);
	FMPPause();
	ret=pthread_create(&tid,NULL,&Readdata,&sthread);
        FMPPlay();

	_tprintf(TEXT(" FILE returned \n"));
  	return(0);


}
void * Readdata(void *pthr)
{
	STRTHREAD* ps = (STRTHREAD*) pthr;
        FMP_BUFFER	FMPBuf;
	BOOL feeded 	= TRUE;
        int n;
	//int fd;

      /* printf("file is: %s\n",ps->name);
         fd=open(ps->name,O_RDONLY);
         if (fd ==-1)
                perror ("open failed"); */
         ps->end=FALSE;
	memset (&FMPBuf, 0, sizeof (FMP_BUFFER));

	printf("Hello, you are in the FILE thread\n");
        do{
		if( feeded == TRUE){
			FMPGetBuffer (&FMPBuf, TRUE);
			FMPBuf.dwBufferSize=32*1024;
		       // printf("Got a buffer\n");
                }
		n=read(fd,FMPBuf.pBuffer,FMPBuf.dwBufferSize);

		if (n == -1 || n==0){
			feeded = FALSE;
                        //printf("Got data\n");

		}
		else{
			FMPBuf.dwDataSize = n;
			FMPPush(&FMPBuf);
			feeded = TRUE;
		}
	}while(ps->end==FALSE);
	if(feeded == FALSE){
		FMPBuf.dwBufferSize=32*1024;
		FMPBuf.dwDataSize = n;
		FMPPush(&FMPBuf);
        }
        //close(fd);
        printf("Bye, you exit the FILE thread\n");
        return (0);

}
//----------------------------------------------------------------------------//
// Event_Notifications - This function receives events from the graph manager //
//----------------------------------------------------------------------------//
DWORD Event_Notifications (DWORD dwContext, DWORD dwMsg, DWORD dwValue)
{
	switch (dwMsg)
	{
        case FMPM_ERROR:
		printf("Event: FMPM Error\n");
		break;
	case FMPM_STARVATION:
		printf("Event: FMPM Starvation\n");
		break;
	case FMPM_EOS:
		printf("Event: FMPM Eos\n");
		break;
	case FMPM_DVDROM_NOT_READY:
		printf("Event: DVDROM not ready\n");
		break;
	case FMPM_DVDROM_ERR:
		printf("Event: DVDROM error\n");
		break;
	case FMPM_SVCD_PSD_END:
		printf("Event: SVCD PSD End\n");
		break;
	case FMPM_TRICK_MODE_CHANGE:
		printf("Event: TRICK MODE change\n");
		break;
	case FMPM_DVD_FATAL_ERROR:
		printf("Event: DVD fatal error\n");
		break;
	case FMPM_DVD_END_PLAYBACK:
		printf("Event: DVD end playback\n");
		break;
	case FMPM_REGION_MISMATCH:
		printf("Event: Play back region mismatch\n");
		break;

	case FMPM_DVD_ANGLE_CHANGE:
		printf("Event: DVD Angle Change\n");
		break;

	case FMPM_DVD_ANGLES_BLOCK:
		printf("Event: DVD Angles Block Change\n");
		break;

	case FMPM_DVD_AUDIO_STREAM_CHANGE:
		printf("Event: DVD Audio Stream Change\n");
		break;
	case FMPM_DVD_BUTTONS_CHANGE:
		printf("Event: DVD Buttons Change\n");
		break;

	case FMPM_DVD_BUTTON_CHANGE:
		printf("Event: DVD Button Change\n");
		break;

	case FMPM_DVD_PROGRAM_START:
		printf("Event: DVD Program Start\n");
		break;

	/*case FMPM_DVD_CURRENT_TIME:
		{
			printf (TEXT("Event: DVD Current Time %2x:%2x:%2x:%x\r\n"),
						 ((unsigned char *)(&dwValue))[0],
						 ((unsigned char *)(&dwValue))[1],
						 ((unsigned char *)(&dwValue))[2],
						 ((unsigned char *)(&dwValue))[3]);
		}
		break;*/
	case FMPM_DVD_DOMAIN_CHANGE:
		printf("Event: DVD Domain Change\n");
		break;

	case FMPM_DVD_NO_FP_PGC:
		printf("Event: DVD No FP PGC\n");
		break;

	case FMPM_DVD_PARENTAL_LEVEL_CHANGE:
		printf("Event: DVD Parental Level Change\n");
		break;

	case FMPM_DVD_PLAYBACK_STOPPED:
		printf("Event: DVD Playback Stopped\n");
		break;

	case FMPM_DVD_STILL_OFF:
		printf("Event: DVD Still Off\n");
		break;

	case FMPM_DVD_STILL_ON:
		printf("Event: DVD Still On\n");
		break;

	case FMPM_DVD_SUBPICTURE_STREAM_CHANGE:
		printf("Event: DVD Subpic Stream Change\n");
		break;

	case FMPM_DVD_TITLE_CHANGE:
		printf("Event: DVD Title Change\n");
		break;

	case FMPM_DVD_VALID_UOPS_CHANGE:
		printf("Event: DVD VALID UOPS Change\n");
		break;
        default:

		break;

	}
	return 0;
}
INT doSleep(INT agc, TCHAR **agv)
{
        if (agc!=1)
        {
               fprintf
        (
            stderr,
            "Syntax is SLEEP <seconds> \n"

    	    "Example: SLEEP 20 \n"
        );
                return(0);
        }

       sleep((BYTE)_ttoi(agv[0]));


	_tprintf(TEXT(" SLEEP returned \n"));
    return(0);
}


