www.pudn.com > emGUI.rar > application.c


/* 
 *  Application layer 
 * 
 * 
 *  COPYRIGHT (c) 2001 - 2010. 
 *  emTech System Corporation. 
 * 
 *  The license and distribution terms for this file may be 
 *  found in found in the file LICENSE. 
 */ 
 
/*	Huangf emcore@263.net 
 */ 
  
#include "emGUI.h" 
 
#include  
#include  
 
static Chain_Control _ApplicationList; 
 
void AppInitialization() 
{ 
	_Chain_Initialize_empty( 
		&_ApplicationList 
	); 
} 
 
Application *FirstApplication() 
{ 
	Application *first; 
	/*  ApplicationEnter(); */ 
	if (_Chain_Is_empty(&_ApplicationList)){ 
		/*  ApplicationLeave(); */ 
		return NULL; 
	} 
 
	first = (Application *)_ApplicationList.first; 
 
	/*  ApplicationLeave(); */ 
	return first; 
} 
 
Application *CreateApplication( 
	void		*appMain, 
	void	 	*appProc, 
	unsigned32	*phCount 
) 
{ 
	unsigned long 	msgQ = 0;		 
	Application 	*app = 0;	 
	unsigned long 	task = 0;	 
 
	if (appMain == NULL || appProc == NULL || phCount == NULL){ 
		return NULL; 
	} 
	 
	app = (struct Application *)malloc(sizeof(Application)); 
	if (app == NULL){ 
		return NULL; 
	} 
	memset(app, 0, sizeof(*app)); 
	 
	app->phCount		= phCount; 
	app->appProc		= appProc; 
	_Chain_Initialize_empty( 
		&app->WinList 
	); 
		 
	msgQ = CreateMsgQ(); 
	if (msgQ == 0){ 
		goto failed; 
	} 
 
	app->msgQ 			= msgQ;					 
 
	task = CreateAppTask( 
		app,  
		appMain 
	); 
 
	if (task == 0){ 
		goto failed; 
	} 
		 
	/*  ApplicationEnter(); */ 
	_Chain_Prepend_unprotected( 
		&_ApplicationList, 
		&app->node 
	); 
	/*  ApplicationLeave(); */ 
	 
	PostAppMessage( 
		app, 
		APPCREATE, 
		0, 
		0 
	); 
	 
	return app; 
	 
failed: 
	if (task){ 
		SysDeleteTask(task); 
	} 
	if (app){ 
		free(app); 
	} 
	if (msgQ){ 
		SysDeleteMsgQ(msgQ); 
	} 
	return 	NULL; 
} 
 
void DestroyApplication( 
	Application *app 
) 
{ 
	/*  ApplicationEnter(); */ 
	_Chain_Extract_unprotected( 
		&app->node 
	); 
	/*  ApplicationLeave(); */ 
 
/*  task is deleted automatically,  
 *  we only delete messageQ for current application  
 */ 
 
	/*  destroy message */ 
	SysDeleteMsgQ(app->msgQ); 
} 
 
boolean SwitchApplication( 
	Application *newapp 
) 
{ 
	/*  ApplicationEnter(); */		 
 
	if (_Chain_Is_empty(&_ApplicationList)){ 
		/*  ApplicationLeave(); */ 
		return FALSE; 
	} 
	 
	if ((Application *)_ApplicationList.first == newapp){ 
		/*  ApplicationLeave(); */ 
		return TRUE; 
	} 
	 
	_Chain_Extract_unprotected( 
		&newapp->node 
	); 
 
	_Chain_Prepend_unprotected( 
		&_ApplicationList, 
		&newapp->node 
	); 
 
	/*  ApplicationLeave(); */ 
	return TRUE; 
}