#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <mos.h>
#include <egb.h>
#include <guidbg.h>

/* EIN(TM) >> */
#include "eintm.h"
/* << EIN(TM) */

char	*guiEgbPtr ;			/*	EGB のワークアドレス	*/


int DentaWinId = -1;

static int ShellCallFunc(int apId,int messId,int info,int data);
static void SetShellFunc(void);
static void ShellSetResolution(void);

extern void DentaInitialize(void);

int main(void)
{
	extern int APL_init() ;
	MMICTRL ct;

	ct.page0=SCREEN16|SCREENIGNORE; /* Page0の画面モード */
	ct.page1=SCREENUNUSED;          /* Page1の画面モード */
	ct.writePage=0;                 /* 書き込みページは0  */
	ct.displayPage=SCREENAVAILABLE; /* 表示ページは,これしかない */
	ct.priority=0;                  /* 優先表示ページもPage0 */
	ct.mode=SCREENAVAILABLE;        /* ビット/ピクセルのことらしい */
	ct.width=SCREENEXPAND;          /* VRAM 1line分のバイト数だそうだ. */

	ct.size=0;                      /* LIBがメモリを勝手に初期化するらしい */
	ct.ptr=NULL;

	ct.asize=0;                     /* アプリ管轄のメモリなんて使うと面倒だ */
	ct.aptr=NULL;

	ct.fr.lupx=0;                   /* 画面枠は自動再設定だけど一応 */
	ct.fr.lupy=0;                   /* 未初期化の値を渡すのは気持ち悪いぞ */
	ct.fr.rdwx=639;
	ct.fr.rdwy=479;

	ct.move.lupx=-16384;            /* ウィンドウ移動枠だ */
	ct.move.lupy=     0;
	ct.move.rdwx= 16383;
	ct.move.rdwy= 16383;

	ct.white=15;
	ct.black=8;
	ct.gray=7;
	ct.xor=7;

	if (MMI_Open(&ct) == NOERR)
	{
		DentaInitialize();
		if (APL_init() == NOERR)
		{
			MMI_SendMessage(DentaWinId,MM_WAKE,0);
			MMI_ExecSystem();
		}
	}

	MMI_Close() ;
	return 0;
}


/* EIN(TM) >> */
static void EinTmSetup(void);
static FRAME ResumeFrame;
static char ResumeId[]="%%% YSDENTA %%%";
static char ResumeTitle[]="YSDENTA V0.2";
/* << EIN(TM) */

int APL_init()
{
	extern MMIINIT	initDataDENTAGUS ;
	register int	ret ;

	guiEgbPtr = MMI_GetEgbPtr() ;

	if ((ret = MMI_initHyper()) < 0)
		return ret ;
	if ((ret = MMI_initDialogL40()) < 0)
		return ret ;
	if ((ret = MMI_initWindowL40()) < 0)
		return ret ;
	if ((ret = MMI_initMessageL40()) < 0)
		return ret ;
	if ((ret = MMI_initButtonL40()) < 0)
		return ret ;
	if ((ret = MMI_initDrawButtonL40()) < 0)
		return ret ;

	if ((ret = MMI_Init(&initDataDENTAGUS)) < 0)
		return ret ;

	SetShellFunc();
	/* EIN(TM) >> */
	EinTmSetup();
	/* << EIN(TM) */

	MMI_SendMessage(MMI_GetBaseObj(), MM_SHOW, 0) ;
	return NOERR ;
}

/* EIN(TM) >> */
static void EinTmSetup(void)
{
	char work[RSMWORKSIZE];	// リジュームマネージャのワークエリア
	char buf[1024];
	char *ptr;
	int size,x,y;
	HYPER hyp;

	EIN_rsmInit( work, ResumeId );
	EIN_rsmBufSet( work, buf, 1023 );
	size = EIN_rsmLoad( work );

	if ( size > 0 ){
		if ( ((ptr = strstr(buf,"\nICON: ")) != NULL ) &&
			 (sscanf(ptr,"\nICON: %d %d\n", &x, &y) > 1 ) ){
			MMI_SendMessage( DentaWinId, MM_GETHYPER, 1, &hyp );
			ResumeFrame.lupx = x;
			ResumeFrame.lupy = y;
			ResumeFrame.rdwx = x+(hyp.fr.rdwx-hyp.fr.lupx);
			ResumeFrame.rdwy = y+(hyp.fr.rdwy-hyp.fr.lupy);

			if ( x<0 ){
				x=0;
			}
			if ( y<0 ){
				y=0;
			}
			MMI_SendMessage( DentaWinId, MM_MOVE, 1, &ResumeFrame ) ;
		}
	}
}

void SaveResumeFile(void)
{
	char	work[RSMWORKSIZE];
	char	buf[1024];
	char	aplpath[128];
	HYPER	hyp;
	register int ret;
	
	MMI_SendMessage( DentaWinId, MM_GETHYPER, 1, &hyp );
	
	if ( (ResumeFrame.lupx  != hyp.fr.lupx) ||
	     (ResumeFrame.lupy  != hyp.fr.lupy) ){

		EIN_rsmInit( work, ResumeId);
		EIN_rsmBufSet( work, buf, 1023 );
		MMI_CallMessage( MMI_GetApliId(), GM_QUERYID, QM_PATH, (int)aplpath );
		EIN_rsmBufPrintf( work, "APLPATH: %s", aplpath );
		EIN_rsmBufPrintf( work, "TITLE:   %s", ResumeTitle);
		EIN_rsmBufCat( work, "COMMENT: その名の通り電卓(^_^;)");
		EIN_rsmBufPrintf( work, "ICON: %d %d", hyp.fr.lupx, hyp.fr.lupy );
		EIN_rsmBufTail( work );
		ret = EIN_rsmSave( work );

		ResumeFrame.lupx = hyp.fr.lupx;
		ResumeFrame.lupy = hyp.fr.lupy;
		ResumeFrame.rdwx = hyp.fr.rdwx;
		ResumeFrame.rdwy = hyp.fr.rdwy;
	}
}
/* << EIN(TM) */




static void SetShellFunc(void)
{
	int bas,app;

	bas=MMI_GetBaseObj();
	app=MMI_GetApliId();
	MMI_SendMessage(bas,MM_SETEXEC,1,ShellCallFunc);
	MMI_CallMessage(app,GM_TITLE,(int)"YS電卓",0);
}

static int ShellCallFunc(int apId,int messId,int info,int data)
{
	EVENT ev;
	static int dummy;

	switch(messId)
	{
	case GM_POSTSCRCHG:
		ev.what=EVEXEC;        /* 関数起動イベント */
		ev.shift=0;            /* 引き数の数だ */
		ev.info=(int)ShellSetResolution; /* ShellSetResolutionを呼んでくれ */
		ev.data=(int)&dummy;   /* NULlだと気持ち悪いぞ(アドレス渡しらしい) */
		ev.time=MOS_getTime(); /* 時刻は、なんか知らんがこうらしいぞ */
		MMI_SetEvnt(&ev);      /* イベントキューに積み上げるぞ */
		break;
	case GM_WAKE:
		MMI_SendMessage(DentaWinId,MM_WAKE,0);
		break;
	case GM_SLEEP:
		MMI_SendMessage(DentaWinId,MM_SLEEP,0);
		break;
	case GM_SHOW:
		break;
	case GM_QUIT:
		/* EIN(TM) >> */
		SaveResumeFile();
		/* << EIN(TM) */
		return NOERR;
	}
	return ILLEGAL_FUNCTION;
}

static void ShellSetResolution(void)
{
	EGB_tmenuPalette2(MMI_GetEgbPtr());
}
