Skip to content

Commit

Permalink
Log detected keyboard layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Nov 19, 2024
1 parent e72a852 commit bfaf48c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions far/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,66 @@ static auto default_keyboard_layout()
return GetKeyboardLayout(0);
}

static auto keyboard_layout_list_hr(std::span<HKL const> const List)
{
string Result;

const auto is_ime = [](HKL const i){ return (std::bit_cast<intptr_t>(i) & 0xF000'0000) == 0xE000'0000; };
const auto is_special = [](HKL const i){ return (std::bit_cast<intptr_t>(i) & 0xF000'0000) == 0xF000'0000; };

const auto LayoutsPath = L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts"sv;

std::unordered_map<short, string> SpecialLayouts;

if (std::ranges::any_of(List, is_special))
{
if (const auto LayoutsKey = reg::key::local_machine.open(LayoutsPath, KEY_ENUMERATE_SUB_KEYS))
{
for (const auto& Key: LayoutsKey->enum_keys())
{
const auto LayoutIdStr = reg::key::local_machine.get_string(concat(LayoutsPath, L"\\", Key), L"Layout Id"sv);
if (!LayoutIdStr)
continue;

short LayoutId;
if (!from_string(*LayoutIdStr, LayoutId, {}, 16))
continue;

SpecialLayouts.emplace(LayoutId, Key);
}
}
}

for (const auto& Layout: List)
{
const auto LayoutValue = static_cast<uint32_t>(std::bit_cast<intptr_t>(Layout));

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeyboardlayout
// The low word contains a Language Identifier for the input language
// and the high word contains a device handle to the physical layout of the keyboard.
const auto InputLangugeId = extract_integer<uint16_t, 0>(LayoutValue);
const auto LayoutHandle = extract_integer<uint16_t, 1>(LayoutValue);

const auto LayoutKey =
is_special(Layout)? SpecialLayouts[LayoutHandle & 0xFFF] :
far::format(L"{:08X}"sv, is_ime(Layout)? LayoutValue : LayoutHandle);

const auto Locale = MAKELCID(InputLangugeId, SORT_DEFAULT);

string LanguageName;

if (!get_locale_value(Locale, LOCALE_SENGLISHDISPLAYNAME, LanguageName)) // >= Windows 7
if (!get_locale_value(Locale, LOCALE_SLOCALIZEDDISPLAYNAME, LanguageName)) // < Windows 7
LanguageName = far::format(L"{:08X}"sv, Locale);

const auto LayoutName = reg::key::local_machine.get_string(concat(LayoutsPath, L"\\", LayoutKey), L"Layout Text"sv);

far::format_to(Result, L"\n{:08X} {} / {}"sv, LayoutValue, LanguageName, LayoutName? *LayoutName : LayoutKey);
}

return Result;
}

static std::vector<HKL> try_get_keyboard_list(function_ref<std::vector<HKL>()> Callable)
{
try
Expand Down Expand Up @@ -816,6 +876,8 @@ std::vector<HKL> get_keyboard_layout_list()
else
Result.insert(Result.begin(), DefaultLayout);

LOGINFO(L"Detected keyboard layouts:{}\n"sv, keyboard_layout_list_hr(Result));

return Result;
}

Expand Down

0 comments on commit bfaf48c

Please sign in to comment.