⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uiprg_mgr.c

📁 嵌入工linux开发的源码
💻 C
字号:
/*************************************************************
Usage: program manager basic functions

  Other Information: None
**************************************************************/

/**************************************************************
INLCUDE FILE
**************************************************************/
#include <pr2k.h>
#include <kernel/thread.h>
#include <kernel/linklist.h>
#include <stdio.h>

#include <uiwnd.h>
#include <uistringRC.h>
//#include <uisys_info.h>
#include <uirecycle.h>
#include <uiCallback.h>

//#include <sys/loadap.h>
#include <uiprg_mgr.h>
#include <filesys.h>
//#include <FatSys.h>

#include <uikey_hdr.h>

//extern unsigned char const * const	icon_Gui_System;
/**************************************************************
GLOBAL VARIABLE
**************************************************************/
extern voidfun gaKeyHandler[] ;

extern struct thread * CurrentThread ;

// program manager's window handle (defined in win_thd.c )
unsigned long PgrMgrWinHandle;

// program manager's thread id
int PgrMgrThreadID = 0 ;

// 指到current application之data structure的指标
TPM_APP * CurrentApp = NULL ;

// 纪录所有application的串列
struct dLinkList ApplicationList ;

static DWORD AppShortcut[MAX_SHORTCUT_NUMBER] = 
{
	0, 0, 0, 0, 0, 0, 0, 0
};

static int DefaultAppProc(TGuiMessage *msg);

// Emulator 必须加上底下的宣告
#ifdef __WIN32__
void (*pdaCreateDefaultAppsEmu)(void) ;
#endif

DLL_EXP(int) guiInitPrgMgr(void)
{
    CurrentApp = NULL ;
	
    // 初始化用来纪录apps资讯的串列
    initDLinkList(&ApplicationList) ;
	
#ifdef __WIN32__
    pdaCreateDefaultAppsEmu();
#else
	pdaCreateDefaultApps() ;
#endif
	
    return STATUS_OK;
}

DLL_EXP(int) guiLaunchApp(DWORD handle)
{
	TPM_APP *hitApp;
	int i ,found=0;
	
	if (handle == 0)
		return 0;
	
	hitApp = (TPM_APP *)handle;
	
	// 如果是动态载入的程式, 就load到memory吧	by jwlin, 2001/04/17
	if(hitApp->type == PRG_MGR_EXTERNAL_APP)
	{
#ifndef __WIN32__
		if(STATUS_OK!=DLoad_App(&hitApp->runtimeInfo))
		{
			//	sc_free(hitApp);
			return 0;
		}
#else
		return 0;
#endif
		
	}
	
    if(hitApp->mainThreadID == -1)
    {
		if(hitApp->runtimeInfo.entry == NULL)
		{
			return 0;
		}
		for(i=0;i<GUI_MAX_KEY_NUMBER;i++)
		{
			// app一开始使用program manager的key callback
			hitApp->keyFun[i] = gaKeyHandler[i] ;
		}
		
		hitApp->mainThreadID = sc_createThread((void(*)()) hitApp->runtimeInfo.entry,100,hitApp->stackSize,NULL,0) ;
		
	}
	else
	{
		if(hitApp->currentWinHandle == 0)
		{
			CurrentApp = hitApp ;
			return 1;
		}
		// 恢复App的key callback
		
		for(i=0;i<GUI_MAX_KEY_NUMBER;i++)
		{
			gaKeyHandler[i] = hitApp->keyFun[i] ;
		}
		guiWindow_Show(hitApp->currentWinHandle) ;
		
	}
	// uguite current app
	CurrentApp = hitApp ;
	
	return 1;
}


DLL_EXP(int) guiKillApp(DWORD handle)
{
	TPM_APP * hitApp;
	
	if (handle == 0)
		return 0;
	
	hitApp = (TPM_APP *)handle;
	
	if (hitApp->mainThreadID<=1)
	{
		while(1) printString("HELP!! I'm Win-Thread!!");
	}
	else if (hitApp->mainThreadID==2)
	{
		while(1) printString("NO!! I'm PrgMgr!!");
	}
	
	deleteThreadTree(hitApp->mainThreadID);
	
	return 1;
}


//***************************************************************
//        FUNCTION : guiCreatePrevExternalApps()
//
//        将载入的程式记录到 \LOADEDAP.SYS
//
//        return: 0         successful
//                -1        on error(file open error,malloc error)
//
//***************************************************************

STATUS guiCreateExternalApps(void)
{
	int hFile_Reg,hFile;
	int i,num,ret=STATUS_OK;
	char szFileName[DL_FNAME_MAX_LEN];
	
	hFile_Reg=fs_open(FNAME_LOADEDAP,O_CREATE);
	if(hFile_Reg<0)
	{
		return STATUS_OK;
	}
	
	num=fs_filelength(hFile_Reg)/DL_FNAME_MAX_LEN;
	while(num--)
	{
		if(fs_read(hFile_Reg,(unsigned char *)szFileName,DL_FNAME_MAX_LEN)!=DL_FNAME_MAX_LEN)
		{
			fs_close(hFile_Reg);
			return STATUS_ERR;
		}
		
		if(guiAddExternalApp(szFileName)!=STATUS_OK)
		{
			ret=STATUS_ERR;
		}
		
	}
	
	fs_close(hFile_Reg);
	return ret;
}



/**************************************************************
FUNCTION :registerAppShortcut
注册一个App的捷径 以方便呼叫App
**************************************************************/
//DLL_EXP(int) registerAppShortcut(int n, PF_VOID entryPoint)    // by zhang xue ping
DLL_EXP(int) registerAppShortcut(int n, int AppID)    // by zhang xue ping
{
	//struct pdaApp * hitApp = NULL ;
	struct tagPM_APP * hitApp = NULL ;
	struct dLinkList * temp = &ApplicationList ;
	
    if ( n<0 || n>=MAX_SHORTCUT_NUMBER )
    {
        // illegal index
        return 0 ;
    }
	
	/*
	if(entryPoint==NULL)
	{        
	AppShortcut[n] = 0 ;
	return 1 ;
    }
	*/   
	
	while(temp->back != NULL)
	{
		temp = temp->back ;
		//hitApp = (struct pdaApp *)(temp->elementPointer) ;
		hitApp = (struct tagPM_APP *)(temp->elementPointer) ;
		
        if (hitApp!=NULL)
        {
			//if((PF_VOID)(hitApp->runtimeInfo.entry) == entryPoint)
			//if((PF_VOID)(hitApp->runtimeInfo.entry) == entryPoint)
			if(hitApp->classs == AppID)
			{
				// found it, and register it in shortcut array
				//AppShortcut[n] = hitApp->handle ;
				AppShortcut[n] = AppID;
				return 1 ;
			}	
		}
	}
	return 0 ;
}

DLL_EXP(int) guiRegistergaKeyAppID(int keyType, int appID) 
{
	PF_VOID handler;
	struct tagPM_APP *hitApp = NULL ;
	struct dLinkList * temp = &ApplicationList ;
	
	while(temp->back != NULL)
	{
		temp = temp->back ;
		//hitApp = (struct pdaApp *)(temp->elementPointer) ;
		hitApp = (struct tagPM_APP *)(temp->elementPointer) ;
		
        if (hitApp!=NULL)
        {
			//if((PF_VOID)(hitApp->runtimeInfo.entry) == entryPoint)
			//if((PF_VOID)(hitApp->runtimeInfo.entry) == entryPoint)
			if(hitApp->classs == appID)
			{
				// found it, and register it in shortcut array
				//AppShortcut[n] = hitApp->handle ;
				handler = (PF_VOID)hitApp->runtimeInfo.entry;
				//guiRegistergaKeyHandler(keyType, handler);  // by zhangxp
				return 1 ;
			}
		}
	}
	
	return 0;
	
}


/**************************************************************
FUNCTION :pdaSwitch2App
- 根据entry point切换application
- 为了让program manager有机会介入切换app的动作
所以在此送讯息给program manager thread
**************************************************************/

DLL_EXP(int) guiSwitch2App(DWORD handle)          // by zhang xue ping
{
	//struct pdaMessage msg ;
	TGuiMessage  msg;
	struct tagGuiWindow *win ;
	int result;
	
    if (handle == 0)
        return 0;
	
	msg.handle = handle ;
	msg.messageType = PRG_MGR_SWITCH_APP ;
	
	//win = (struct pdaWindow *)PgrMgrWinHandle ;
	win = (struct tagGuiWindow *)PgrMgrWinHandle ;
	result = _guiEnqueue(win->messageQueue,&msg) ;
	
	return result;
}



/**************************************************************
FUNCTION :pdaSwitch2AppByShortcut
- 利用Shortcut切换app
**************************************************************/

int guiSwitch2AppByShortcut(int index)       // by zhang xue ping
{
	struct tagPM_APP * hitApp = NULL ;
	struct dLinkList * temp = &ApplicationList ;
	int    appID, ret;
	DWORD  handle; 
	
	appID = AppShortcut[index];
	if(appID==0)
		return 0;
	
	while(temp->back != NULL)
	{
		temp = temp->back ;
		//hitApp = (struct pdaApp *)(temp->elementPointer) ;
		hitApp = (struct tagPM_APP *)(temp->elementPointer) ;
		
        if (hitApp!=NULL)
        {
			if(hitApp->classs == appID)
			{
				handle = hitApp->handle;
				ret = guiSwitch2App(handle);
				return ret;
			}
		}
	}
	return 0;
	
	/*
    if (index>=0 && index<MAX_SHORTCUT_NUMBER)
    {
	return guiSwitch2App(AppShortcut[index]);
    }
    return 0;
	*/
	
}

/**************************************************************
FUNCTION :guiBack2PrgMgr
通知程式管理员切回program manager
系统应该会suspend current window的thread
**************************************************************/

DLL_EXP(void) guiBack2PrgMgr(void)
{
	int tid;
	TGuiMessage msg ;
	TWindow *win ;
	
	msg.messageType = WIN_BACK_TO_PRG_MGR ;
	win = (TWindow *)PgrMgrWinHandle ;
	_guiEnqueue( win->messageQueue, &msg) ;
    if((gpTopWindow != NULL)&&(gpTopWindow->ownerThreadID > 2))
        sc_suspend(gpTopWindow->ownerThreadID);
	
	//tong begin
	tid=sc_getThreadID();
	if(tid>2)
	{
		if(gpTopWindow!=NULL&&tid==gpTopWindow->ownerThreadID)
		{
			return;
		}
		sc_suspend(tid);
	}
	return;
	//tong end
}

/**************************************************************
FUNCTION :guiAddExternalApp
//
// 加入一个新的external application到program managet的menu中
// (动态载入的应用程式称作external application)
//
//  return -1 if something error
**************************************************************/
                       
DLL_EXP(STATUS) guiAddExternalApp(const char *szFileName)
{
	                     
#ifndef __WIN32__    
    char fileName[128];
	
    int i,hFile,len;
    unsigned char * iconBuffer ;
    TPM_APP * newApp ;
	TAppHeader tAppHeader;
	
	newApp = (TPM_APP *)sc_malloc(sizeof(TPM_APP)) ;
    if(newApp == NULL)
		return STATUS_ERR ;
	
#if 0
	strcpy(fileName, "A:\\");
	strcpy(&fileName[3], szFileName);
	hFile=fs_open((char *)fileName,O_RDONLY);
#else	
	hFile=fs_open((char *)szFileName,O_RDONLY);
#endif
	if(hFile<0)
	{
		return STATUS_ERR;
	}
	
	if(fs_read(hFile,(unsigned char *)&tAppHeader,sizeof(TAppHeader))!=sizeof(TAppHeader))
	{
		fs_close(hFile);
		return STATUS_ERR;
	}
	
    //////////////////////////////////////////////////////////////////////
    //
    // 配置一个新的 guiApp structure
    //
    newApp->handle = (unsigned long)newApp ;
	
	strcpy((newApp->runtimeInfo).name, szFileName) ;
	(newApp->runtimeInfo).size=fs_filelength(hFile)-tAppHeader.Entry_Offset;
	
	//
    // 读入 icon
    //
    if ((char)tAppHeader.iconBuffer[0] == -1)
    {
		// 预设 ICON
		iconBuffer = (unsigned char *)icon_Gui_System ;
    }
    else
    {
		// 配置 icon buffer
		iconBuffer = (unsigned char*)sc_malloc(DL_ICON_MAX_LEN) ;
		if(iconBuffer == NULL)
		{
			// 预设 ICON
			iconBuffer = (unsigned char *)icon_Gui_System ;
		}
		else
			memcpy(iconBuffer, tAppHeader.iconBuffer, DL_ICON_MAX_LEN) ;
    }
	
    newApp->iconBitmap = iconBuffer ;
	
    //
    // 设定新app的属性
    //
    //
	//strncpy(newApp->appName,tAppHeader.szAppName,DL_APPNAME_MAX_LEN-1);
	//newApp->appName[DL_APPNAME_MAX_LEN-1]=0;
	for(i=0; i<LANGUAGE_NUM; i++)
	{
		strncpy(newApp->appName[i],tAppHeader.szAppName[i],MAX_APP_NAME_LENGTH-1);
		//strncpy(newApp->appName[i],tAppHeader.szAppName[0],MAX_APP_NAME_LENGTH-1);
		
		newApp->appName[i][MAX_APP_NAME_LENGTH-1]=0;		
	}
	
	
    newApp->mainThreadID = -1 ;                   // -1 means the app has never executed
    newApp->currentWinHandle = 0 ;                //  0 means the app has never executed
    newApp->type = PRG_MGR_EXTERNAL_APP ;
    newApp->classs = tAppHeader.ClassID ;
    if(tAppHeader.StackSize<=0||tAppHeader.StackSize>512)
    {
		newApp->stackSize = GUI_DEFAULT_STACK_SIZE ;
    }
    else
    {
		newApp->stackSize = tAppHeader.StackSize*1024;
	}
	
    for(i=0;i<GUI_MAX_KEY_NUMBER;i++)
		newApp->keyFun[i] = NULL ;
	
	newApp->action = DefaultAppProc;
	//newApp->apCmd = 0;
	//	memset(&newApp->apCmd,0,sizeof(newApp->apCmd));
	
	sc_disableInt() ;
    // 加入串列ApplicationList中
    insertTo(&ApplicationList,newApp) ;
    sc_enableInt() ;
	
    fs_close(hFile);
    return STATUS_OK;
#endif
	return STATUS_OK;
}


/**************************************************************
FUNCTION :guiAddInternalApp
//
// 加入一个新的internal application到program manager的menu中
// (会和kernel连结在一起的称作internal application)
//
//  return -1 if something error

  **************************************************************/
  DLL_EXP(int) guiAddInternalApp(BYTE *iconBuffer, PF_VOID entryPoint, TAppName App_Name, int classs,DWORD stackSize)
	  //  DLL_EXP(int) guiAddInternalApp(BYTE *iconBuffer, PF_VOID entryPoint, const char **App_Name, int classs,DWORD stackSize)
  {
	  TPM_APP * newApp ;
	  int i;
	  
	  // 配置一个新的guiApp structure
	  newApp = (TPM_APP *)sc_malloc(sizeof(TPM_APP)) ;
	  if(newApp == NULL)
		  return STATUS_ERR ;
	  
	  newApp->handle = (DWORD)newApp ;
	  newApp->iconBitmap = iconBuffer ;
	  
	  // 设定新app的属性
	  //strncpy(newApp->appName,App_Name.szName_GB,MAX_APP_NAME_LENGTH-1);
	  //newApp->appName[MAX_APP_NAME_LENGTH-1]=0;
	  for(i=0; i<LANGUAGE_NUM; i++)
	  {
		  strncpy(newApp->appName[i],App_Name.szName[i],MAX_APP_NAME_LENGTH-1);
		  newApp->appName[i][MAX_APP_NAME_LENGTH-1]=0;		
	  }
	  
	  newApp->mainThreadID = -1 ;                 // -1 means the app has never executed
	  newApp->currentWinHandle = 0 ;                //  0 means the app has never executed
	  newApp->type = PRG_MGR_INTERNAL_APP ;
	  newApp->classs = classs ;
	  newApp->runtimeInfo.entry = (BYTE *)entryPoint ;
	  
	  if(stackSize == 0)
		  newApp->stackSize = GUI_DEFAULT_STACK_SIZE ;
	  else
		  newApp->stackSize = stackSize ;
	  
	  for(i=0;i<GUI_MAX_KEY_NUMBER;i++)
		  newApp->keyFun[i] = NULL ;
	  
	  newApp->action = DefaultAppProc;
	  //newApp->apCmd = NULL;
	  
	  sc_disableInt() ;
	  // 加入串列ApplicationList中
	  insertTo(&ApplicationList,newApp) ;
	  sc_enableInt() ;
	  return STATUS_OK ;
  }
  
  
  static int DefaultAppProc(TGuiMessage *msg)
  {
	  int handled = 0;
	  TPM_APP *theApp;
	  
	  theApp = (TPM_APP *)(msg->handle);
	  switch (msg->messageType)
	  {
	  case WIN_APP_EXIT:
		  if (theApp->mainThreadID!=-1)
		  {
			  deleteThreadTree(theApp->mainThreadID);
		  }
		  
		  handled = 1;
		  break;
		  
	  default:
		  ;// do nothing
		  break;
		  
	  }
	  
	  return handled	;
  }
  
  
  DLL_EXP(void) guiSetPrgMgrWinHandle(DWORD handle)
  {
	  PgrMgrWinHandle=handle;
  }
  
  
  DLL_EXP(DWORD) guiGetPrgMgrWinHandle(void)
  {
	  return PgrMgrWinHandle;
  }
  
  
  DLL_EXP(void) SetPgrMgrThreadID(int PgrMgrTID)
  {
	  PgrMgrThreadID = PgrMgrTID;
  }
  
  
  DLL_EXP(struct dLinkList*) guiGetAppList(void)
  {
	  return (&ApplicationList);
  }
  
  
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -