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

📄 registry.c

📁 Lido PXA270平台开发板的最新BSP,包括源代码
💻 C
字号:
/******************************************************************************
<module>
* Name         : Registry.c
* Title        : PVR D3DM registry functions.
* Author(s)    : Imagination Technologies
* Created      : 2 March 2004
*
* Copyright    : 2004 by Imagination Technologies Limited.
*                All rights reserved.  No part of this software, either
*                material or conceptual may be copied or distributed,
*                transmitted, transcribed, stored in a retrieval system
*                or translated into any human or computer language in any
*                form by any means, electronic, mechanical, manual or
*                other-wise, or disclosed to third parties without the
*                express written permission of Imagination Technologies
*                Limited, Unit 8, HomePark Industrial Estate,
*                King's Langley, Hertfordshire, WD4 8LZ, U.K.
*
* Description  : PVR D3DM registry functions.
*
* Platform     : Windows CE
*
</module>

$Log: registry.c $


********************************************************************************/
#include "context.h"
/*****************************************************************************
 Function Name  : AppendFileSizeToName
 Inputs         : PSTR pszOutBuf
                  DWORD dwOutBufSize
                  PSTR pszFileName
 Outputs        : none
 Returns        : TRUE if successful
 Globals used   : none
 Description    : Appends the size of the file to the filename
*****************************************************************************/
IMG_BOOL AppendFileSizeToName(PSTR pszOutBuf,
							  DWORD dwOutBufSize,
							  PSTR pszFileName)
{
	HANDLE			hProcTable;
	PROCESSENTRY32	sProcEntry;
	TCHAR			*pszExe;
	TCHAR			ptszFileName[256];
	HANDLE			fIn;
	DWORD			dwFileSize;
	CHAR			szNumStr[20];

	/* Get details of the current processes */
	hProcTable = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if(hProcTable == INVALID_HANDLE_VALUE)
	{
		D3DM_DPF((DPF_ERROR, "AppendFileSizeToName: Couldn't obtain process handles"));
		return FALSE;
	}

	/* Get the first process */
	sProcEntry.dwSize = sizeof(PROCESSENTRY32);
	if (!Process32First(hProcTable, &sProcEntry))
	{
		D3DM_DPF((DPF_ERROR, "AppendFileSizeToName: Couldn't read the first process entry"));
		goto ErrorExit;
	}

	/* Convert filename for comparison */
	mbstowcs(ptszFileName, pszFileName, strlen(pszFileName) + 1);

	/*
		Extract the file name
	*/
	pszExe = _tcsrchr(sProcEntry.szExeFile, _T('\\'));
	if (pszExe)
	{
		pszExe++;
	}
	else
	{
		pszExe = sProcEntry.szExeFile;
	}

	while (_tcsicmp(pszExe, ptszFileName))
	{
		if (!Process32Next(hProcTable, &sProcEntry))
		{
			D3DM_DPF((DPF_ERROR, "AppendFileSizeToName: Couldn't read correct process name"));
			goto ErrorExit;
		}

		/* Extract the file name */
		pszExe = _tcsrchr(sProcEntry.szExeFile, _T('\\'));
		if (pszExe)
		{
			pszExe++;
		}
		else
		{
			pszExe = sProcEntry.szExeFile;
		}
	}

	/*
		OK, we've found the right process (and therefore the
		right filename) so get the size of this file
	*/
	fIn = CreateFile(sProcEntry.szExeFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (fIn == INVALID_HANDLE_VALUE)
	{
		D3DM_DPF((DPF_ERROR, "AppendFileSizeToName: Couldn't open the file."));
		goto ErrorExit;
	}

	dwFileSize = GetFileSize(fIn, NULL);
	if (dwFileSize <= 0)
	{
		D3DM_DPF((DPF_ERROR, "AppendFileSizeToName: Couldn't find the size of the file."));
		CloseHandle(fIn);
		goto ErrorExit;
	}

	CloseHandle(fIn);

	sprintf(szNumStr, "%i", dwFileSize);

	if ((_tcslen(pszExe) + strlen(szNumStr)) > dwOutBufSize)
	{
		goto ErrorExit;
	}

	/*
		Copy exe name to the buffer
	*/
	wcstombs(pszOutBuf, pszExe, dwOutBufSize);

	/*
		Append the file size
	*/
	strcat(pszOutBuf, szNumStr);

	CloseToolhelp32Snapshot(hProcTable);
	return IMG_TRUE;

ErrorExit:

	CloseToolhelp32Snapshot(hProcTable);
	return IMG_FALSE;
}
/*****************************************************************************
 Function Name  : GetCurProcessName
 Inputs         : PSTR pszOutBuf
                  DWORD dwOutBufSize
 Outputs        : none
 Returns        : TRUE if successful
 Globals used   : none
 Description    : Gets the current process name
*****************************************************************************/
BOOL GetCurProcessName(PSTR pszOutBuf, DWORD dwOutBufSize)
{
	TCHAR	ptszBuf[256];
	TCHAR	*ptszExe;

	/* Get module name */
	if	(GetModuleFileName(NULL, ptszBuf, sizeof(ptszBuf)/sizeof(TCHAR)))
	{
		/* Determine name of calling application. */
		ptszExe = _tcsrchr(ptszBuf, '\\');
		if	(ptszExe)
		{
			ptszExe++;
		}
		else
		{
			ptszExe = ptszBuf;
		}

		/* ... otherwise exe name to the buffer */
		if	(_tcslen(ptszExe) < dwOutBufSize)
		{
			wcstombs(pszOutBuf, ptszExe, dwOutBufSize);
			return TRUE;
		}
	}
	return FALSE;
}
/*****************************************************************************
 FUNCTION	: ReadDefaultRegSettings
    
 PURPOSE	: 

 PARAMETERS	: psRegData
			  
 RETURNS	: None
*****************************************************************************/
void ReadDefaultRegSettings(PPVR_REG_DATA psRegData,PD3DM_APP_HINT psCurrent)
{
	DWORD 	dwType;
	PDWORD	pdwRegData;

	pdwRegData = (PDWORD) psRegData;
	dwType	= psCurrent->dwType;

	while(dwType != D3DM_AHINTTYPE_END)
	{
		/*
			Ignore entry, or not...
		*/
		if (!(dwType & D3DM_AHINTTYPE_DRVIGNORE))
		{
			switch(dwType & D3DM_AHINTTYPE_BASEMASK)
			{
				case D3DM_AHINTTYPE_SIMPLEFLAG:
				{
					/*
						Set the flag if value is non zero.
					*/
					if (psCurrent->dwValue)
					{
						pdwRegData[psCurrent->dwFIndex] |= psCurrent->dwFlag;
					}

					break;
				}
				case D3DM_AHINTTYPE_VAL32:
				{
					/*
						Just set value directly.
					*/
					pdwRegData[psCurrent->dwFIndex] = psCurrent->dwValue;
					
					break;
				}
				case D3DM_AHINTTYPE_CHILDFLAGS:
				case D3DM_AHINTTYPE_GANGEDFLAGS:
				case D3DM_AHINTTYPE_GROUP:
				{
					/*
						Recurse into branch as necessary.
					*/
					if (psCurrent->psMore)
					{
						if	((psCurrent->ptszVarName && psCurrent->dwValue) || !psCurrent->ptszVarName)
						{
							if	(psCurrent->dwValue)
							{
								pdwRegData[psCurrent->dwFIndex] |= psCurrent->dwFlag;
							}

							ReadDefaultRegSettings(psRegData, psCurrent->psMore);
						}
					}
					else
					{
						D3DM_DPF((DPF_REG, "Invalid entry in app hint table (missing branch pointer) !"));
					}

					break;
				}
				default:
				{
					break;
				}
			}
		}

		psCurrent++;
		dwType = psCurrent->dwType;
	}
}

/*****************************************************************************
 FUNCTION	: ReadRegStrings
    
 PURPOSE	: 

 PARAMETERS	: psRegData
			  psCurrent
			  psPath
			  
 RETURNS	: None
*****************************************************************************/
void ReadRegStrings(DWORD			dwDevCookie, 
					PPVR_REG_DATA	psRegData,
					PD3DM_APP_HINT	psCurrent,
					PSTR			pszPath)
{
	BOOL	bStringPresent;
	DWORD 	dwType;
	DWORD 	dwValue=0;
	PDWORD	pdwRegData;
	char	pszKey[256] = POWERVR_REG_ROOT;

	strcat(pszKey, pszPath);

	pdwRegData = (PDWORD) psRegData;
	dwType = psCurrent->dwType;

	while (dwType != D3DM_AHINTTYPE_END)
	{
		if (!(dwType & D3DM_AHINTTYPE_DRVIGNORE))
		{
			/*
				Try reading the string and do the thang...
			*/
			bStringPresent = FALSE;

			if	(psCurrent->ptszVarName)
			{
				if	(HostReadRegistryInt(dwDevCookie, 
									     pszKey,
									     psCurrent->ptszVarName,
									     &dwValue))
				{
					bStringPresent = TRUE;
				}
			}

			switch(dwType & D3DM_AHINTTYPE_BASEMASK)
			{
				case D3DM_AHINTTYPE_SIMPLEFLAG:
				{
					/*
						Set the flag if value is non zero.
					*/
					if (bStringPresent)
					{
						if (dwValue)
						{
							pdwRegData[psCurrent->dwFIndex] |= psCurrent->dwFlag;
						}
						else
						{
							pdwRegData[psCurrent->dwFIndex] &= ~psCurrent->dwFlag;
						}
					}

					break;
				}
				case D3DM_AHINTTYPE_VAL32:
				{
					/*
						Just set value directly.
					*/
					if (bStringPresent)
					{
						pdwRegData[psCurrent->dwFIndex] = dwValue;
					}
					
					break;
				}
				case D3DM_AHINTTYPE_CHILDFLAGS:
				case D3DM_AHINTTYPE_GANGEDFLAGS:
				case D3DM_AHINTTYPE_GROUP:
				{
					if (psCurrent->psMore)
					{
						if (bStringPresent)
						{
							/*
								For any of these types, if a validating string is present recursion
								only takes place if it is enabled.
							*/
							if (dwValue)
							{
								pdwRegData[psCurrent->dwFIndex] |= psCurrent->dwFlag;

								ReadRegStrings(dwDevCookie, 
											   psRegData,
											   psCurrent->psMore,
											   pszKey);
							}
							else
							{
								pdwRegData[psCurrent->dwFIndex] &= ~psCurrent->dwFlag;
							}
						}
						else
						{
							/*
								No validating string so always recurse.
							*/
							ReadRegStrings(dwDevCookie, 
										   psRegData,
										   psCurrent->psMore,
										   pszKey);
						}
					}
					else
					{
						D3DM_DPF((DPF_REG,"Invalid entry in app hint table (missing branch pointer) !"));
					}

					break;
				}
				default:
				{
					break;
				}
			}
		}

		psCurrent++;
		dwType = psCurrent->dwType;
	}
}

/*****************************************************************************
 FUNCTION	: ReadBaseSettings
    
 PURPOSE	: 

 PARAMETERS	: psRegData
			  ppszStrings
			  psPath
			  
 RETURNS	: None
*****************************************************************************/
void ReadBaseSettings(DWORD			dwDevCookie, 
					  PPVR_REG_DATA	psRegData,
					  PSTR			*ppszStrings,
					  PSTR			pszPath)
{
	PSTR 	pszString;
	PDWORD	pdwRegData;
	DWORD	dwValue;
	DWORD	i;
	char	pszKey[256] = POWERVR_REG_ROOT;

	strcat(pszKey, pszPath);
	i = 0;
	pszString  = ppszStrings[0];
	pdwRegData = (PDWORD) psRegData;

	do
	{
		if (HostReadRegistryInt(dwDevCookie, pszKey, pszString, &dwValue))
		{
			pdwRegData[i] = dwValue;	
		}

		i++;
		pszString = ppszStrings[i];
	}
	while (pszString[0] != 0);
}

/*****************************************************************************
 FUNCTION	: GetD3DMRegSettings
    
 PURPOSE	: Get PVRHAL registry settings..

 PARAMETERS	: PPVR_REG_DATA psRegData
			  
 RETURNS	: None
*****************************************************************************/
IMG_VOID GetD3DMRegSettings(DWORD dwDevCookie, PPVR_REG_DATA psRegData)
{
	char pszCaller[64];
	char pszAppHintKey[256] = D3DM_REGAPPHINT_ROOT;

	/* 
		Zero reg data 
	*/
	memset(psRegData, 0, sizeof(PVR_REG_DATA));

	/*
		Read default values from internal table.
		"Game Settings\\D3DM\\"
	*/
	ReadDefaultRegSettings(psRegData, psD3DMAppHints);

	/*
		Read D3DM "Base" reg settings.
		"Global\\D3DM\\"
	*/
	ReadBaseSettings(dwDevCookie, 
					 psRegData, 
					 ppszDXFlagStrings,
					 D3DM_REGCOMMON_ROOT);

	/*
		Read strings from common section.
		"Game Settings\\D3DM\\Common\\"
	*/
	ReadRegStrings(dwDevCookie, 
				   psRegData,
				   psD3DMAppHints,
				   D3DM_REGAPPHINTCOMMON_ROOT);
	
	/* 
		Read per process apphints - "Game Settings\\D3DM\\"
		Find current process name 
	*/
	if(GetCurProcessName(pszCaller, sizeof(pszCaller)/sizeof(*pszCaller)))
	{
		D3DM_DPF((DPF_REG,"Caller is %hs", pszCaller));

		if	(strlen(pszAppHintKey) + strlen(pszCaller) < sizeof(pszAppHintKey)/sizeof(*pszAppHintKey))
		{
			strcat(pszAppHintKey, pszCaller);
		}
		else
		{
			D3DM_DPF((DPF_REG,"GetD3DMRegSettings : pszAppHintKey is not big enough"));
		}

		ReadRegStrings(dwDevCookie, 
					   psRegData,
					   psD3DMAppHints,
					   pszAppHintKey);

		/*
			Check if this is a duplicate .exe name
			and if so find the correct apphints
		*/
		if	(psRegData->dwFlags2 & D3DMREG2_DUPLICATE_APPNAME)
		{
			if (AppendFileSizeToName(pszCaller, 
									 sizeof(pszCaller)/sizeof(*pszCaller),
									 pszCaller))
			{
				/*
					Reset app hint key string
				*/
				strcpy(pszAppHintKey, D3DM_REGAPPHINT_ROOT);

				if	(strlen(pszAppHintKey) + strlen(pszCaller) < (sizeof(pszAppHintKey)/sizeof(*pszAppHintKey)))
				{
					strcat(pszAppHintKey, pszCaller);
				}
				else
				{
					D3DM_DPF((DPF_REG,"GetD3DMRegSettings : pszAppHintKey is not big enough"));
				}

				ReadRegStrings(dwDevCookie, 
							   psRegData,
							   psD3DMAppHints,
							   pszAppHintKey);
			}
		}
	}

	return;
}

⌨️ 快捷键说明

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