From 4d724b6fbbae3043f0bfd8de56b6115eb2b65e90 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Thu, 25 Aug 2022 08:38:49 +0900 Subject: [PATCH] [INPUT] Refactor LayoutList_Create (#4626) LayoutList_Create function was too complicated. CORE-11700 --- dll/cpl/input/layout_list.c | 233 +++++++++++++++--------------------- 1 file changed, 95 insertions(+), 138 deletions(-) diff --git a/dll/cpl/input/layout_list.c b/dll/cpl/input/layout_list.c index 3541a81f300..7a06ea77f70 100644 --- a/dll/cpl/input/layout_list.c +++ b/dll/cpl/input/layout_list.c @@ -80,158 +80,115 @@ LayoutList_Destroy(VOID) _LayoutList = NULL; } +static BOOL +LayoutList_ReadLayout(HKEY hLayoutKey, LPCWSTR szLayoutId, LPCWSTR szSystemDirectory) +{ + WCHAR szBuffer[MAX_PATH], szFilePath[MAX_PATH], szDllPath[MAX_PATH]; + INT iIndex, iLength = 0; + DWORD dwSize, dwSpecialId, dwLayoutId = DWORDfromString(szLayoutId); + HINSTANCE hDllInst; + + dwSize = sizeof(szBuffer); + if (RegQueryValueExW(hLayoutKey, L"Layout File", NULL, NULL, + (LPBYTE)szBuffer, &dwSize) != ERROR_SUCCESS) + { + return FALSE; /* No "Layout File" value */ + } + + /* Build the "Layout File" full path and check existence */ + StringCchPrintfW(szFilePath, ARRAYSIZE(szFilePath), L"%s\\%s", szSystemDirectory, szBuffer); + if (GetFileAttributesW(szFilePath) == INVALID_FILE_ATTRIBUTES) + return FALSE; /* No layout file found */ + + /* Get the special ID */ + dwSpecialId = 0; + dwSize = sizeof(szBuffer); + if (RegQueryValueExW(hLayoutKey, L"Layout Id", NULL, NULL, + (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) + { + dwSpecialId = DWORDfromString(szBuffer); + } + + /* If there is a valid "Layout Display Name", then use it as the entry name */ + dwSize = sizeof(szBuffer); + if (RegQueryValueExW(hLayoutKey, L"Layout Display Name", NULL, NULL, + (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS && szBuffer[0] == L'@') + { + /* FIXME: Use shlwapi!SHLoadRegUIStringW instead if it had fully implemented */ + + /* Move to the position after the character "@" */ + WCHAR *pBuffer = szBuffer + 1; + + /* Get a pointer to the beginning ",-" */ + WCHAR *pIndex = wcsstr(pBuffer, L",-"); + + if (pIndex) + { + /* Convert the number in the string after the ",-" */ + iIndex = _wtoi(pIndex + 2); + + *pIndex = 0; /* Cut the string */ + + if (ExpandEnvironmentStringsW(pBuffer, szDllPath, ARRAYSIZE(szDllPath)) != 0) + { + hDllInst = LoadLibraryW(szDllPath); + if (hDllInst) + { + iLength = LoadStringW(hDllInst, iIndex, szBuffer, ARRAYSIZE(szBuffer)); + FreeLibrary(hDllInst); + + if (iLength > 0) + { + LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); + return TRUE; + } + } + } + } + } + + /* Otherwise, use "Layout Text" value as the entry name */ + dwSize = sizeof(szBuffer); + if (RegQueryValueExW(hLayoutKey, L"Layout Text", NULL, NULL, + (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) + { + LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); + return TRUE; + } + + return FALSE; +} VOID LayoutList_Create(VOID) { - WCHAR szSystemDirectory[MAX_PATH]; - WCHAR szLayoutId[MAX_PATH]; - DWORD dwIndex = 0; - DWORD dwSize; - HKEY hKey; + 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; } - if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, - L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts", - 0, - KEY_ENUMERATE_SUB_KEYS, - &hKey) != ERROR_SUCCESS) + for (dwIndex = 0; ; ++dwIndex) { - 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) + dwSize = ARRAYSIZE(szLayoutId); + if (RegEnumKeyExW(hKey, dwIndex, szLayoutId, &dwSize, NULL, NULL, + NULL, NULL) != ERROR_SUCCESS) { - WCHAR szBuffer[MAX_PATH]; - - dwSize = sizeof(szBuffer); - - if (RegQueryValueExW(hLayoutKey, - L"Layout File", - NULL, NULL, - (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) - { - WCHAR szFilePath[MAX_PATH]; - - StringCchPrintfW(szFilePath, ARRAYSIZE(szFilePath), - L"%s\\%s", szSystemDirectory, szBuffer); - - if (GetFileAttributesW(szFilePath) != INVALID_FILE_ATTRIBUTES) - { - DWORD dwSpecialId = 0; - - dwSize = sizeof(szBuffer); - - if (RegQueryValueExW(hLayoutKey, - L"Layout Id", - NULL, NULL, - (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) - { - dwSpecialId = DWORDfromString(szBuffer); - } - - dwSize = sizeof(szBuffer); - - if (RegQueryValueExW(hLayoutKey, - L"Layout Display Name", - NULL, NULL, - (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS && - szBuffer[0] == L'@') - { - WCHAR *pBuffer; - WCHAR *pIndex; - - /* Move to the position after the character "@" */ - pBuffer = szBuffer + 1; - - /* Get a pointer to the beginning ",-" */ - pIndex = wcsstr(pBuffer, L",-"); - - if (pIndex != NULL) - { - WCHAR szPath[MAX_PATH]; - INT iIndex; - - /* Convert the number in the string after the ",-" */ - iIndex = _wtoi(pIndex + 2); - - pIndex[0] = 0; - - if (ExpandEnvironmentStringsW(pBuffer, szPath, ARRAYSIZE(szPath)) != 0) - { - HANDLE hHandle; - - hHandle = LoadLibraryW(szPath); - if (hHandle != NULL) - { - INT iLength = LoadStringW(hHandle, iIndex, szBuffer, ARRAYSIZE(szBuffer)); - - FreeLibrary(hHandle); - - if (iLength != 0) - { - DWORD dwLayoutId = DWORDfromString(szLayoutId); - - LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); - } - else - { - goto NotTranslated; - } - } - else - { - goto NotTranslated; - } - } - else - { - goto NotTranslated; - } - } - else - { - goto NotTranslated; - } - } - else - { -NotTranslated: - dwSize = sizeof(szBuffer); - - if (RegQueryValueExW(hLayoutKey, - L"Layout Text", - NULL, NULL, - (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) - { - DWORD dwLayoutId = DWORDfromString(szLayoutId); - - LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); - } - } - } - } - - RegCloseKey(hLayoutKey); + break; } - dwSize = ARRAYSIZE(szLayoutId); - ++dwIndex; + if (RegOpenKeyExW(hKey, szLayoutId, 0, KEY_QUERY_VALUE, &hLayoutKey) == ERROR_SUCCESS) + { + LayoutList_ReadLayout(hLayoutKey, szLayoutId, szSystemDirectory); + RegCloseKey(hLayoutKey); + } } RegCloseKey(hKey);