#include	<stdlib.h>
#include	<string.h>
#include	<stdarg.h>
#include	"display.h"

DSP_work_type DSP_work = {NULL, NULL};
bool_t	DSP_isDOS = TRUE;
static	bool_t DSP_isInstall = FALSE;

int     DSP_init(int page0, ...)
{
    int EGB_init(char *, int);
    int		page1 = 0;
    va_list	ap;
    
    va_start(ap, page0);
    if (DSP_work.EGB == NULL) {
	DSP_work.EGB = (char *) malloc(EgbWorkSize);
	if (DSP_work.EGB == NULL)
	    return 1;
	EGB_init(DSP_work.EGB, EgbWorkSize);
    }
    
    DSP_resolution(0, page0);
    DSP_writePage(0);
    if(page0 == 10) {
	DSP_displayStart(2, 2, 2);
	DSP_displayStart(3, 320, 240);
	DSP_displayStart(1, 0, 0);
    }
    if(page0 < 12) { 
	page1 = va_arg(ap, int);
	if(DSP_resolution(1, page1)) return 1;
	DSP_writePage(1);
	if(page1 == 10) {
	    DSP_displayStart(2, 2, 2);
	    DSP_displayStart(3, 320, 240);
	    DSP_displayStart(1, 0, 0);
	}
    }
    DSP_displayPage(ON, ON);
    
    if (DSP_work.MOS == NULL) {
	DSP_work.MOS = (char *) malloc(MosWorkSize);
	if (DSP_work.MOS == NULL)
	    return 1;
	MOS_start(DSP_work.MOS, MosWorkSize);
    }	
    
    MOS_resolution(0, page0);
    if(page1) MOS_resolution(1, page1);
    if(page0 == 3 || page0 == 12) {
	MOS_horizon(0, 639);
	MOS_vertical(0, 479);
    }
    MOS_writePage(0);

    DSP_isInstall = TRUE;
    
    va_end(ap);
    return 0;
}

int     DSP_end()
{
    if (DSP_work.MOS != NULL) {
	MOS_end();
	free(DSP_work.MOS);
	DSP_work.MOS = NULL;
    } else return 1;
    if (DSP_work.EGB != NULL) {
	if(DSP_isDOS == TRUE) screenterm(DSP_work.EGB);
	free(DSP_work.EGB);
	DSP_work.EGB = NULL;
    } else return 1;

    DSP_isInstall = FALSE;

    return 0;
}

bool_t DSP_install(void)
{
    return DSP_isInstall;
}
