https://git.reactos.org/?p=reactos.git;a=commitdiff;h=4d724b6fbbae3043f0bfd8de56b6115eb2b65e90
commit 4d724b6fbbae3043f0bfd8de56b6115eb2b65e90 Author: Katayama Hirofumi MZ <katayama.hirofumi...@gmail.com> AuthorDate: Thu Aug 25 08:38:49 2022 +0900 Commit: GitHub <nore...@github.com> CommitDate: Thu Aug 25 08:38:49 2022 +0900 [INPUT] Refactor LayoutList_Create (#4626) LayoutList_Create function was too complicated. CORE-11700 --- dll/cpl/input/layout_list.c | 217 ++++++++++++++++++-------------------------- 1 file changed, 87 insertions(+), 130 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; } - -VOID -LayoutList_Create(VOID) +static BOOL +LayoutList_ReadLayout(HKEY hLayoutKey, LPCWSTR szLayoutId, LPCWSTR szSystemDirectory) { - WCHAR szSystemDirectory[MAX_PATH]; - WCHAR szLayoutId[MAX_PATH]; - DWORD dwIndex = 0; - DWORD dwSize; - HKEY hKey; - - if (!GetSystemDirectoryW(szSystemDirectory, ARRAYSIZE(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; + return FALSE; /* No "Layout File" value */ } - if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, - L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts", - 0, - KEY_ENUMERATE_SUB_KEYS, - &hKey) != ERROR_SUCCESS) + /* 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) { - return; + dwSpecialId = DWORDfromString(szBuffer); } - dwSize = ARRAYSIZE(szLayoutId); - - while (RegEnumKeyExW(hKey, dwIndex, szLayoutId, &dwSize, - NULL, NULL, NULL, NULL) == ERROR_SUCCESS) + /* 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'@') { - HKEY hLayoutKey; + /* FIXME: Use shlwapi!SHLoadRegUIStringW instead if it had fully implemented */ - if (RegOpenKeyExW(hKey, - szLayoutId, - 0, - KEY_QUERY_VALUE, - &hLayoutKey) == ERROR_SUCCESS) - { - WCHAR szBuffer[MAX_PATH]; + /* Move to the position after the character "@" */ + WCHAR *pBuffer = szBuffer + 1; - dwSize = sizeof(szBuffer); + /* Get a pointer to the beginning ",-" */ + WCHAR *pIndex = wcsstr(pBuffer, L",-"); - if (RegQueryValueExW(hLayoutKey, - L"Layout File", - NULL, NULL, - (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS) - { - WCHAR szFilePath[MAX_PATH]; + if (pIndex) + { + /* Convert the number in the string after the ",-" */ + iIndex = _wtoi(pIndex + 2); - StringCchPrintfW(szFilePath, ARRAYSIZE(szFilePath), - L"%s\\%s", szSystemDirectory, szBuffer); + *pIndex = 0; /* Cut the string */ - if (GetFileAttributesW(szFilePath) != INVALID_FILE_ATTRIBUTES) + if (ExpandEnvironmentStringsW(pBuffer, szDllPath, ARRAYSIZE(szDllPath)) != 0) + { + hDllInst = LoadLibraryW(szDllPath); + if (hDllInst) { - 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); + iLength = LoadStringW(hDllInst, iIndex, szBuffer, ARRAYSIZE(szBuffer)); + FreeLibrary(hDllInst); - if (RegQueryValueExW(hLayoutKey, - L"Layout Display Name", - NULL, NULL, - (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS && - szBuffer[0] == L'@') + if (iLength > 0) { - 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); - } + LayoutList_AppendNode(dwLayoutId, dwSpecialId, szBuffer); + return TRUE; } } } - - RegCloseKey(hLayoutKey); } + } + /* 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], 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); - ++dwIndex; + 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(hKey);