-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Fix various KKP encoding issues #20052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lhecker
wants to merge
9
commits into
main
Choose a base branch
from
dev/lhecker/19977-kkp-altgr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
446f0df
Fix various KKP encoding issues
lhecker e27f76d
Spel
lhecker f0e3432
Rethought how AltGr works
lhecker fcda6e9
Fixed TerminalInputModifierKeyTests
lhecker 0e4d6b8
Added test hooks, Use French AZERTY for tests
lhecker 552a79f
SPELL CHECK
lhecker 5141679
Merge remote-tracking branch 'origin/main' into dev/lhecker/19977-kkp…
lhecker 6e3bb0e
Fix compilation failures?
lhecker 63b6bba
Fixed SetTerminalInputKeyboardLayout
lhecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| #include "precomp.h" | ||
| #include "TestHook.h" | ||
|
|
||
| using namespace TestHook; | ||
|
|
||
| thread_local HKL g_keyboardLayout; | ||
|
|
||
| extern "C" HKL TestHook_TerminalInput_KeyboardLayout() | ||
| { | ||
| return g_keyboardLayout; | ||
| } | ||
|
|
||
| static bool isPreloadedLayout(const wchar_t* klid) noexcept | ||
| { | ||
| wil::unique_hkey preloadKey; | ||
| if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Keyboard Layout\\Preload", 0, KEY_READ, preloadKey.addressof()) != ERROR_SUCCESS) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| wil::unique_hkey substitutesKey; | ||
| RegOpenKeyExW(HKEY_CURRENT_USER, L"Keyboard Layout\\Substitutes", 0, KEY_READ, substitutesKey.addressof()); | ||
|
|
||
| wchar_t idx[16]; | ||
| wchar_t layoutId[KL_NAMELENGTH]; | ||
|
|
||
| for (DWORD i = 0;; i++) | ||
| { | ||
| DWORD idxLen = ARRAYSIZE(idx); | ||
| DWORD layoutIdSize = sizeof(layoutId); | ||
| if (RegEnumValueW(preloadKey.get(), i, idx, &idxLen, nullptr, nullptr, reinterpret_cast<BYTE*>(layoutId), &layoutIdSize) != ERROR_SUCCESS) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| // Preload contains base language IDs (e.g. "0000040c"). | ||
| // The actual layout ID (e.g. "0001040c") may only appear in the Substitutes key. | ||
| if (substitutesKey) | ||
| { | ||
| wchar_t substitute[KL_NAMELENGTH]; | ||
| DWORD substituteSize = sizeof(substitute); | ||
| if (RegGetValueW(substitutesKey.get(), nullptr, layoutId, RRF_RT_REG_SZ, nullptr, substitute, &substituteSize) == ERROR_SUCCESS) | ||
| { | ||
| memcpy(layoutId, substitute, sizeof(layoutId)); | ||
| } | ||
| } | ||
|
|
||
| if (wcscmp(layoutId, klid) == 0) | ||
|
|
||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| void LayoutGuard::_destroy() const noexcept | ||
| { | ||
| if (g_keyboardLayout == _layout) | ||
| { | ||
| g_keyboardLayout = nullptr; | ||
| } | ||
| if (_needsUnload) | ||
| { | ||
| UnloadKeyboardLayout(_layout); | ||
| } | ||
| } | ||
|
|
||
| LayoutGuard::LayoutGuard(HKL layout, bool needsUnload) noexcept : | ||
| _layout{ layout }, | ||
| _needsUnload{ needsUnload } | ||
| { | ||
| } | ||
|
|
||
| LayoutGuard::~LayoutGuard() | ||
| { | ||
| _destroy(); | ||
| } | ||
|
|
||
| LayoutGuard::LayoutGuard(LayoutGuard&& other) noexcept : | ||
| _layout{ std::exchange(other._layout, nullptr) }, | ||
| _needsUnload{ std::exchange(other._needsUnload, false) } | ||
| { | ||
| } | ||
|
|
||
| LayoutGuard& LayoutGuard::operator=(LayoutGuard&& other) noexcept | ||
| { | ||
| if (this != &other) | ||
| { | ||
| _destroy(); | ||
| _layout = std::exchange(other._layout, nullptr); | ||
| _needsUnload = std::exchange(other._needsUnload, false); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| LayoutGuard::operator bool() const noexcept | ||
| { | ||
| return _layout != nullptr; | ||
| } | ||
|
|
||
| LayoutGuard::operator HKL() const noexcept | ||
| { | ||
| return _layout; | ||
| } | ||
|
|
||
| LayoutGuard TestHook::SetTerminalInputKeyboardLayout(const wchar_t* klid) | ||
|
|
||
| { | ||
| THROW_HR_IF_MSG(E_UNEXPECTED, g_keyboardLayout != nullptr, "Nested layout test overrides are not supported"); | ||
|
|
||
| // Check if the layout is installed. LoadKeyboardLayoutW silently returns the | ||
| // current active layout if the requested one is missing. | ||
| const auto keyPath = fmt::format(FMT_COMPILE(L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\{}"), klid); | ||
| wil::unique_hkey key; | ||
| if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath.c_str(), 0, KEY_READ, key.addressof()) != ERROR_SUCCESS) | ||
| { | ||
| return {}; | ||
| } | ||
|
|
||
| const auto layout = LoadKeyboardLayoutW(klid, KLF_NOTELLSHELL); | ||
|
|
||
| THROW_LAST_ERROR_IF_NULL(layout); | ||
|
|
||
| g_keyboardLayout = layout; | ||
|
|
||
| // Unload the layout if it's not one of the user's layouts. | ||
| // GetKeyboardLayoutList is unreliable for this purpose, as the keyboard layout API mutates global OS state. | ||
| // If a process crashes or exits early without calling UnloadKeyboardLayout all future processes will get it | ||
| // returned in their GetKeyboardLayoutList calls. Shell could fix it but alas. So we peek into the registry. | ||
| const auto needsUnload = !isPreloadedLayout(klid); | ||
|
|
||
|
|
||
| return { layout, needsUnload }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #pragma once | ||
|
|
||
| namespace TestHook | ||
| { | ||
| struct LayoutGuard | ||
| { | ||
| LayoutGuard() = default; | ||
| LayoutGuard(HKL layout, bool needsUnload) noexcept; | ||
| ~LayoutGuard(); | ||
|
|
||
| LayoutGuard(const LayoutGuard&) = delete; | ||
| LayoutGuard& operator=(const LayoutGuard&) = delete; | ||
| LayoutGuard(LayoutGuard&& other) noexcept; | ||
| LayoutGuard& operator=(LayoutGuard&& other) noexcept; | ||
|
|
||
| explicit operator bool() const noexcept; | ||
| operator HKL() const noexcept; | ||
|
|
||
| private: | ||
| void _destroy() const noexcept; | ||
|
|
||
| HKL _layout = nullptr; | ||
| bool _needsUnload = false; | ||
| }; | ||
|
|
||
| LayoutGuard SetTerminalInputKeyboardLayout(const wchar_t* klid); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.