Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/actions/spelling/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ allocing
alpc
ALTERNATENAME
ALTF
ALTGR
ALTNUMPAD
ALWAYSTIP
ansicpg
Expand Down Expand Up @@ -79,6 +80,7 @@ autoscrolling
Autowrap
AVerify
awch
AZERTY
azurecr
AZZ
backgrounded
Expand Down Expand Up @@ -867,6 +869,8 @@ kinda
KIYEOK
KKP
KLF
klid
KLLF
KLMNO
KOK
KPRIORITY
Expand Down Expand Up @@ -1133,6 +1137,7 @@ NOSELECTION
NOSENDCHANGING
NOSIZE
NOSNAPSHOT
NOTELLSHELL
NOTHOUSANDS
NOTICKS
NOTIMEOUTIFNOTHUNG
Expand Down Expand Up @@ -1984,8 +1989,8 @@ WRITECONSOLEINPUT
WRITECONSOLEOUTPUT
WRITECONSOLEOUTPUTSTRING
wrkstr
wrl
WRL
wrl
wrp
WRunoff
WSLENV
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/adapter/ut_adapter/Adapter.UnitTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
<ClCompile Include="inputTest.cpp" />
<ClCompile Include="kittyKeyboardProtocol.cpp" />
<ClCompile Include="MouseInputTest.cpp" />
<ClCompile Include="TestHook.cpp" />
<ClCompile Include="..\precomp.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\precomp.h" />
<ClInclude Include="TestHook.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\buffer\out\lib\bufferout.vcxproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
<ClCompile Include="kittyKeyboardProtocol.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="TestHook.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\precomp.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TestHook.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="$(SolutionDir)tools\ConsoleTypes.natvis" />
Expand Down
132 changes: 132 additions & 0 deletions src/terminal/adapter/ut_adapter/TestHook.cpp
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
Comment thread Fixed
{
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)
Comment thread Fixed
{
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)
Comment thread Fixed
{
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);
Comment thread Fixed
Comment thread Fixed
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);
Comment thread Fixed

return { layout, needsUnload };
}
27 changes: 27 additions & 0 deletions src/terminal/adapter/ut_adapter/TestHook.h
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);
}
Loading
Loading