#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <egb.h>
#include <guidbg.h>

char	*guiEgbPtr ;			/*	EGB のワークアドレス	*/

int	DialogID = -1 ;
int	MessageID[2] = -1 ;
int	IconID = -1 ;

#include <eintm.h>
//#define DEBUG

/*= リジューム用設定=================================================*/
#define RSM_BUFSIZE (300)
FRAME	RSM_FRAME;
char	RSMID[]="%%% m %%%";
char	RSMTITLE[]="m                               By 機拡";

int exitFuncRet = ILLEGAL_FUNCTION ;
unsigned int beforeMem = 0;
//short count = 0;
char mes1[] = "888,888,888 -> 888,888,888\0\0";
char mes2[] = "+888,888,888\0\0";

int userFunc(apliId, messId, info, data)
int	apliId;
int	messId;
int	info;
int	data;
{
	register int	ret;

	ret = ILLEGAL_FUNCTION;

	switch(messId)
	{
		case	GM_QUIT :
			MMI_SendMessage( IconID, MM_EXEC, 2, 0, 0 );
			ret = exitFuncRet ;
			break;
	}

	return(ret);
}

void main()
{
	static MMICTRL mmi ={
				SCREEN16 | SCREENIGNORE,	// ページ0側解像度
				SCREENUNUSED,				// ページ1側解像度
				0, 							// 書き込みページ
				SCREENAVAILABLE, 			// 表示ページ
				0, 							// 表示プライオリティ
				SCREENAVAILABLE,			// 色数
				SCREENEXPAND, 				// VRAMの横の長さ
				0, 							// メモリ領域の大きさ
				NULL, 						// メモリ領域のアドレス
				0, 							// ユーザ領域の大きさ
				NULL, 						// ユーザ領域のアドレス
				0, 0, 						// 画面枠	lupx,lupy
				0, 0,						//			rdwx,rdwy
				-16384, -16384,				// 移動枠	lupx,lupy
				16383, 16383,				//			rdwx,rdwy
				15, 						// 白色
				8,  						// 黒色
				7,  						// 灰色
				6	 						// 反転色
	};

	extern int APL_init() ;
	extern int APL_end() ;

	/*	初期化処理	*/
	if (MMI_Open( &mmi ) == NOERR)
	{
		/*	初期化に成功すればメインループに入る.	*/
		if (APL_init() == NOERR){
			MMI_ExecSystem() ;
			APL_end();
			}
	}

	/*	終了処理	*/
	MMI_Close() ;

}

int APL_init()
{
	extern MMIINIT	initDataMGUI ;

	register int	ret ;

	/*	EGB ワークアドレスの取得.	*/
	guiEgbPtr = MMI_GetEgbPtr() ;

	/*	ハイパ型部品の初期化			*/
	if ((ret = MMI_initHyper()) < 0)
		return ret ;
	/*	ダイアログ型部品の初期化		*/
	if ((ret = MMI_initDialogL40()) < 0)
		return ret ;
	/*	メッセージ型部品の初期化		*/
	if ((ret = MMI_initMessageL40()) < 0)
		return ret ;
	/*	ボタン型部品の初期化			*/
	if ((ret = MMI_initButtonL40()) < 0)
		return ret ;
	/*	アイコンボタン型部品の初期化	*/
	if ((ret = MMI_initIconL40()) < 0)
		return ret ;

	/*	背景データの初期化						*/

	/*	データの登録		*/
	if ((ret = MMI_Init(&initDataMGUI)) < 0)
		return ret ;

	/*----------------------------------------------------------------*/
	/*  リジューム読み込み (※のついた処理は必ず行ってください)       */
	/*----------------------------------------------------------------*/
	char	work[RSMWORKSIZE];	// リジュームマネージャのワークエリア
	char	buf[RSM_BUFSIZE];
	char	*ptr;
	int		size, x, y;
	HYPER	hyp;

	// リジュームマネージャの初期化
	EIN_rsmInit( work, RSMID );
	// 読み込みバッファ設定
	EIN_rsmBufSet( work, buf, RSM_BUFSIZE );
	// リジューム情報読み込み
	size = EIN_rsmLoad( work );
	#ifdef DEBUG
	printf("load RSM_SIZE:%d\n",size);
	#endif
	if ( size > 0 ){
		// 表示位置
		if ( ((ptr = strstr(buf,"\nPANEL: ")) != NULL ) &&
			 (sscanf(ptr,"\nPANEL: %d %d\n", &x, &y) > 1 ) ){
			// 枠座標を得る
			MMI_SendMessage( DialogID, MM_GETHYPER, 1, &hyp );
			RSM_FRAME.lupx = x;
			RSM_FRAME.lupy = y;
			RSM_FRAME.rdwx = x+(hyp.fr.rdwx-hyp.fr.lupx);
			RSM_FRAME.rdwy = y+(hyp.fr.rdwy-hyp.fr.lupy);
			#ifdef DEBUG
			printf("m:move to (%d,%d)\n",x,y);
			#endif
			MMI_SendMessage( DialogID, MM_MOVE, 1, &RSM_FRAME ) ;
		}
	}

	/*	Tmenuにパレットを合わせる	*/
	EIN_initGuiColor();

	/*	背景を表示する							*/
	MMI_SendMessage(MMI_GetBaseObj(), MM_SHOW, 0) ;

	MMI_SendMessage(MMI_GetBaseObj(), MM_SETEXEC, 1, userFunc);
	MMI_CallMessage(MMI_GetApliId(), GM_TITLE, (int)RSMTITLE, 0);

	extern void	userIdleTask();		/*	登録するアイドルタスク	*/
	MMI_SetIdleTaskFunc( userIdleTask );
	userIdleTask();

	return NOERR ;
}

int APL_end()
{
	/*===================================================================*/
	/* リジューム情報更新 (※がついた処理は必ず行って下さい)             */
	/*===================================================================*/
	char	work[RSMWORKSIZE];
	char	buf[RSM_BUFSIZE];
	char	aplpath[128];
	HYPER	hyp;
	register int ret;
	
	// 枠座標を得る
	MMI_SendMessage( DialogID, MM_GETHYPER, 1, &hyp );
	
	// 移動していたらセーブ
	if ( (RSM_FRAME.lupx  != hyp.fr.lupx) ||
	     (RSM_FRAME.lupy  != hyp.fr.lupy) ){
		// マネージャ初期化
		EIN_rsmInit( work, RSMID );
		// バッファ設定(アプリ識別ID,更新時刻は自動設定されます)
		EIN_rsmBufSet( work, buf, RSM_BUFSIZE );
		// アプリディレクトリ保存
		MMI_CallMessage( MMI_GetApliId(), GM_QUERYID, QM_PATH, (int)aplpath );
		EIN_rsmBufPrintf( work, "APLPATH: %s", aplpath );
		// アプリ名保存
		EIN_rsmBufPrintf( work, "TITLE:   %s", RSMTITLE );
		// コメント保存
		EIN_rsmBufCat( work, "COMMENT: フリーメモリを刻々と表示します。標準出力にもprintfします。");
		// アイコン位置保存
		EIN_rsmBufPrintf( work, "PANEL: %d %d", hyp.fr.lupx, hyp.fr.lupy );
		// バッファ終端(^A)設定
		EIN_rsmBufTail( work );
		// バッファをセーブ
		ret = EIN_rsmSave( work );
		#ifdef DEBUG
		printf("save RSM_SIZE:%d\n",ret);
		#endif
	}

	return NOERR ;
}

void userIdleTask(void)
{
	unsigned int mem,i;
	int p,l;
	p = TL_checkMemory(0);
	l = TL_checkMemory(2);
	mem = ((p<l) ? p : l)*4096 ;
	if(mem==beforeMem) return;

	sprintf(mes1,"%03u,%03u,%03u -> %03u,%03u,%03u\0",
				beforeMem/1000000,(beforeMem/1000)%1000,beforeMem%1000,
				mem/1000000,(mem/1000)%1000,mem%1000);
	if(mem>beforeMem)	i = mem - beforeMem ;
			else	i = beforeMem - mem ;
	sprintf(mes2,"%c%03u,%03u,%03u\0",( (mem>beforeMem) ? '+' : '-'),
				i/1000000,(i/1000)%1000,i%1000);

	MMI_SendMessage(MessageID[0],MM_SETMSG,1,mes1);
	MMI_SendMessage(MessageID[1],MM_SETMSG,1,mes2);
	MMI_SendMessage(MessageID[0],MM_SHOW,0);
	MMI_SendMessage(MessageID[1],MM_SHOW,0);
	printf("m:%s (%s)\n",mes2,mes1);

	beforeMem = mem ;

	return ;
}

/*	initDataMGUI:IconID:MJ_ICONL40の呼び出し関数	*/
int	quitFunc(kobj, messId, argc, pev, trigger)
int		kobj ;
int		messId ;
int		argc ;
EVENT	*pev ;
int		trigger ;
{
	MMI_SetHaltFlag( TRUE );
	exitFuncRet = NOERR;
	return NOERR ;
}

