[INPUT] Refactor LayoutList_Create (#4626)

LayoutList_Create function was too complicated. CORE-11700
This commit is contained in:
Katayama Hirofumi MZ 2022-08-25 08:38:49 +09:00 committed by GitHub
parent 0ed302ef65
commit 4d724b6fbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,158 +80,115 @@ LayoutList_Destroy(VOID)
_LayoutList = NULL; _LayoutList = NULL;
} }
static BOOL
VOID LayoutList_ReadLayout(HKEY hLayoutKey, LPCWSTR szLayoutId, LPCWSTR szSystemDirectory)
LayoutList_Create(VOID)
{ {
WCHAR szSystemDirectory[MAX_PATH]; WCHAR szBuffer[MAX_PATH], szFilePath[MAX_PATH], szDllPath[MAX_PATH];
WCHAR szLayoutId[MAX_PATH]; INT iIndex, iLength = 0;
DWORD dwIndex = 0; DWORD dwSize, dwSpecialId, dwLayoutId = DWORDfromString(szLayoutId);
DWORD dwSize; HINSTANCE hDllInst;
HKEY hKey;
if (!GetSystemDirectoryW(szSystemDirectory, ARRAYSIZE(szSystemDirectory)))
{
return;
}
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts",
0,
KEY_ENUMERATE_SUB_KEYS,
&hKey) != ERROR_SUCCESS)
{
return;
}
dwSize = ARRAYSIZE(szLayoutId);
while (RegEnumKeyExW(hKey, dwIndex, szLayoutId, &dwSize,
NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
HKEY hLayoutKey;
if (RegOpenKeyExW(hKey,
szLayoutId,
0,
KEY_QUERY_VALUE,
&hLayoutKey) == ERROR_SUCCESS)
{
WCHAR szBuffer[MAX_PATH];
dwSize = sizeof(szBuffer); dwSize = sizeof(szBuffer);
if (RegQueryValueExW(hLayoutKey, L"Layout File", NULL, NULL,
if (RegQueryValueExW(hLayoutKey, (LPBYTE)szBuffer, &dwSize) != ERROR_SUCCESS)
L"Layout File",
NULL, NULL,
(LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS)
{ {
WCHAR szFilePath[MAX_PATH]; return FALSE; /* No "Layout File" value */
}
StringCchPrintfW(szFilePath, ARRAYSIZE(szFilePath), /* Build the "Layout File" full path and check existence */
L"%s\\%s", szSystemDirectory, szBuffer); StringCchPrintfW(szFilePath, ARRAYSIZE(szFilePath), L"%s\\%s", szSystemDirectory, szBuffer);
if (GetFileAttributesW(szFilePath) == INVALID_FILE_ATTRIBUTES)
if (GetFileAttributesW(szFilePath) != INVALID_FILE_ATTRIBUTES) return FALSE; /* No layout file found */
{
DWORD dwSpecialId = 0;
/* Get the special ID */
dwSpecialId = 0;
dwSize = sizeof(szBuffer); dwSize = sizeof(szBuffer);
if (RegQueryValueExW(hLayoutKey, L"Layout Id", NULL, NULL,
if (RegQueryValueExW(hLayoutKey,
L"Layout Id",
NULL, NULL,
(LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS)
{ {
dwSpecialId = DWORDfromString(szBuffer); dwSpecialId = DWORDfromString(szBuffer);
} }
/* If there is a valid "Layout Display Name", then use it as the entry name */
dwSize = sizeof(szBuffer); dwSize = sizeof(szBuffer);
if (RegQueryValueExW(hLayoutKey, L"Layout Display Name", NULL, NULL,
if (RegQueryValueExW(hLayoutKey, (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS && szBuffer[0] == L'@')
L"Layout Display Name",
NULL, NULL,
(LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS &&
szBuffer[0] == L'@')
{ {
WCHAR *pBuffer; /* FIXME: Use shlwapi!SHLoadRegUIStringW instead if it had fully implemented */
WCHAR *pIndex;
/* Move to the position after the character "@" */ /* Move to the position after the character "@" */
pBuffer = szBuffer + 1; WCHAR *pBuffer = szBuffer + 1;
/* Get a pointer to the beginning ",-" */ /* Get a pointer to the beginning ",-" */
pIndex = wcsstr(pBuffer, L",-"); WCHAR *pIndex = wcsstr(pBuffer, L",-");
if (pIndex != NULL) if (pIndex)
{ {
WCHAR szPath[MAX_PATH];
INT iIndex;
/* Convert the number in the string after the ",-" */ /* Convert the number in the string after the ",-" */
iIndex = _wtoi(pIndex + 2); iIndex = _wtoi(pIndex + 2);
pIndex[0] = 0; *pIndex = 0; /* Cut the string */
if (ExpandEnvironmentStringsW(pBuffer, szPath, ARRAYSIZE(szPath)) != 0) if (ExpandEnvironmentStringsW(pBuffer, szDllPath, ARRAYSIZE(szDllPath)) != 0)
{ {
HANDLE hHandle; hDllInst = LoadLibraryW(szDllPath);
if (hDllInst)
hHandle = LoadLibraryW(szPath);
if (hHandle != NULL)
{ {
INT iLength = LoadStringW(hHandle, iIndex, szBuffer, ARRAYSIZE(szBuffer)); iLength = LoadStringW(hDllInst, iIndex, szBuffer, ARRAYSIZE(szBuffer));
FreeLibrary(hDllInst);
FreeLibrary(hHandle); if (iLength > 0)
if (iLength != 0)
{ {
DWORD dwLayoutId = DWORDfromString(szLayoutId);
LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer);
} return TRUE;
else
{
goto NotTranslated;
} }
} }
else
{
goto NotTranslated;
} }
} }
else
{
goto NotTranslated;
} }
}
else
{
goto NotTranslated;
}
}
else
{
NotTranslated:
dwSize = sizeof(szBuffer);
if (RegQueryValueExW(hLayoutKey, /* Otherwise, use "Layout Text" value as the entry name */
L"Layout Text", dwSize = sizeof(szBuffer);
NULL, NULL, if (RegQueryValueExW(hLayoutKey, L"Layout Text", NULL, NULL,
(LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS)
{ {
DWORD dwLayoutId = DWORDfromString(szLayoutId);
LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer);
} return TRUE;
}
}
} }
return FALSE;
}
VOID
LayoutList_Create(VOID)
{
WCHAR szSystemDirectory[MAX_PATH], szLayoutId[MAX_PATH];
DWORD dwSize, dwIndex;
HKEY hKey, hLayoutKey;
if (!GetSystemDirectoryW(szSystemDirectory, ARRAYSIZE(szSystemDirectory)))
return;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts",
0, KEY_ENUMERATE_SUB_KEYS, &hKey) != ERROR_SUCCESS)
{
return;
}
for (dwIndex = 0; ; ++dwIndex)
{
dwSize = ARRAYSIZE(szLayoutId);
if (RegEnumKeyExW(hKey, dwIndex, szLayoutId, &dwSize, NULL, NULL,
NULL, NULL) != ERROR_SUCCESS)
{
break;
}
if (RegOpenKeyExW(hKey, szLayoutId, 0, KEY_QUERY_VALUE, &hLayoutKey) == ERROR_SUCCESS)
{
LayoutList_ReadLayout(hLayoutKey, szLayoutId, szSystemDirectory);
RegCloseKey(hLayoutKey); RegCloseKey(hLayoutKey);
} }
dwSize = ARRAYSIZE(szLayoutId);
++dwIndex;
} }
RegCloseKey(hKey); RegCloseKey(hKey);