From ea74c3c872c9fb80bd50b73661cafb8f2a262637 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 20 Sep 2020 23:31:45 +0100 Subject: [PATCH] Fixed Linux crash caused by wrong mapping of key codes. --- CHANGES.md | 1 + .../ImGui/Private/ImGuiInteroperability.cpp | 29 +++++++++++++------ Source/ImGui/Private/ImGuiInteroperability.h | 5 +--- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c130fff..3b70610 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ Improving stability - Improved hot-reload stability and support for reloading after recompiling outside of the editor. Both methods should be equally supported and work together. - Improved behaviour of delegates when hot-reloading. - Changed context index mapping to fix issues with multi-PIE debugging in 4.25. +- Fixed Linux crash caused by wrong mapping of key codes. Version: 1.20 (2020/06) Transition to IWYU and maintenance: diff --git a/Source/ImGui/Private/ImGuiInteroperability.cpp b/Source/ImGui/Private/ImGuiInteroperability.cpp index aef13ec..e30247c 100644 --- a/Source/ImGui/Private/ImGuiInteroperability.cpp +++ b/Source/ImGui/Private/ImGuiInteroperability.cpp @@ -122,6 +122,17 @@ namespace ImGuiInterops Copy(Mapping.KeyMap, IO.KeyMap); } + // Simple transform mapping key codes to 0-511 range used in ImGui. + // From what I can tell, on most supported platforms key codes should comfortably fit in that range anyway + // but the SDL key-codes used on Linux can go way out of this range (because of the extra flag). However, + // after this transform they should fit in the range without conflicts. + // NOTE: Should any of the platforms have other conflicts or any trouble with inputs, this is the likely + // candidate for change. + static uint32 MapKeyCode(uint32 KeyCode) + { + return (KeyCode < 512) ? KeyCode : 256 + (KeyCode % 256); + } + uint32 GetKeyIndex(const FKey& Key) { const uint32* pKeyCode = nullptr; @@ -129,17 +140,17 @@ namespace ImGuiInterops FInputKeyManager::Get().GetCodesFromKey(Key, pKeyCode, pCharCode); - if (pKeyCode) - { - return *pKeyCode; - } + const uint32 KeyCode = + pKeyCode ? *pKeyCode + : pCharCode ? *pCharCode + : 0; - if (pCharCode) - { - return *pCharCode; - } + return MapKeyCode(KeyCode); + } - return 0; + uint32 GetKeyIndex(const FKeyEvent& KeyEvent) + { + return MapKeyCode(KeyEvent.GetKeyCode()); } uint32 GetMouseIndex(const FKey& MouseButton) diff --git a/Source/ImGui/Private/ImGuiInteroperability.h b/Source/ImGui/Private/ImGuiInteroperability.h index 67918ae..62e4cd1 100644 --- a/Source/ImGui/Private/ImGuiInteroperability.h +++ b/Source/ImGui/Private/ImGuiInteroperability.h @@ -40,10 +40,7 @@ namespace ImGuiInterops uint32 GetKeyIndex(const FKey& Key); // Map key event to index in keys buffer. - FORCEINLINE uint32 GetKeyIndex(const FKeyEvent& KeyEvent) - { - return KeyEvent.GetKeyCode(); - } + uint32 GetKeyIndex(const FKeyEvent& KeyEvent); // Map mouse FKey to index in mouse buttons buffer. uint32 GetMouseIndex(const FKey& MouseButton);