diff options
author | 2024-09-30 15:54:41 +0000 | |
---|---|---|
committer | 2024-10-07 09:41:05 +0000 | |
commit | 5a71e8857560729d165e3154c97788201e063ece (patch) | |
tree | b17a0611d5ec856cf0369f6674fc7404c44609b9 | |
parent | 9623dd5092ccddbe0a5bf817c5a6727eeda9244e (diff) |
hasKeycode API should take into account key remapping
Keyboards can generate certain keycodes even if the keys are not
present in the device HID descriptor, by using key remapping APIs
Test: atest ModifierKeyRemappingTest
Bug: 368397939
Flag: EXEMPT bugfix
Change-Id: I30afda89f289eddc2b05fb124555aebfb182852e
-rw-r--r-- | include/input/KeyCharacterMap.h | 3 | ||||
-rw-r--r-- | libs/input/KeyCharacterMap.cpp | 11 | ||||
-rw-r--r-- | services/inputflinger/reader/EventHub.cpp | 14 | ||||
-rw-r--r-- | services/inputflinger/reader/include/EventHub.h | 1 |
4 files changed, 28 insertions, 1 deletions
diff --git a/include/input/KeyCharacterMap.h b/include/input/KeyCharacterMap.h index 67b37b1213..7ea34c2566 100644 --- a/include/input/KeyCharacterMap.h +++ b/include/input/KeyCharacterMap.h @@ -137,6 +137,9 @@ public: /* Returns keycode after applying Android key code remapping defined in mKeyRemapping */ int32_t applyKeyRemapping(int32_t fromKeyCode) const; + /** Returns list of keycodes that remap to provided keycode (@see setKeyRemapping()) */ + std::vector<int32_t> findKeyCodesMappedToKeyCode(int32_t toKeyCode) const; + /* Returns the <keyCode, metaState> pair after applying key behavior defined in the kcm file, * that tries to find a replacement key code based on current meta state */ std::pair<int32_t /*keyCode*/, int32_t /*metaState*/> applyKeyBehavior(int32_t keyCode, diff --git a/libs/input/KeyCharacterMap.cpp b/libs/input/KeyCharacterMap.cpp index b0563abaf7..d775327a80 100644 --- a/libs/input/KeyCharacterMap.cpp +++ b/libs/input/KeyCharacterMap.cpp @@ -365,6 +365,17 @@ int32_t KeyCharacterMap::applyKeyRemapping(int32_t fromKeyCode) const { return toKeyCode; } +std::vector<int32_t> KeyCharacterMap::findKeyCodesMappedToKeyCode(int32_t toKeyCode) const { + std::vector<int32_t> fromKeyCodes; + + for (const auto& [from, to] : mKeyRemapping) { + if (toKeyCode == to) { + fromKeyCodes.push_back(from); + } + } + return fromKeyCodes; +} + std::pair<int32_t, int32_t> KeyCharacterMap::applyKeyBehavior(int32_t fromKeyCode, int32_t fromMetaState) const { int32_t toKeyCode = fromKeyCode; diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp index 0865eed4a2..10cc1eed7b 100644 --- a/services/inputflinger/reader/EventHub.cpp +++ b/services/inputflinger/reader/EventHub.cpp @@ -659,6 +659,19 @@ void EventHub::Device::populateAbsoluteAxisStates() { } bool EventHub::Device::hasKeycodeLocked(int keycode) const { + if (hasKeycodeInternalLocked(keycode)) { + return true; + } + + for (auto& fromKey : getKeyCharacterMap()->findKeyCodesMappedToKeyCode(keycode)) { + if (hasKeycodeInternalLocked(fromKey)) { + return true; + } + } + return false; +} + +bool EventHub::Device::hasKeycodeInternalLocked(int keycode) const { if (!keyMap.haveKeyLayout()) { return false; } @@ -676,7 +689,6 @@ bool EventHub::Device::hasKeycodeLocked(int keycode) const { if (usageCodes.size() > 0 && mscBitmask.test(MSC_SCAN)) { return true; } - return false; } diff --git a/services/inputflinger/reader/include/EventHub.h b/services/inputflinger/reader/include/EventHub.h index edc30379b2..dffd8e3c7a 100644 --- a/services/inputflinger/reader/include/EventHub.h +++ b/services/inputflinger/reader/include/EventHub.h @@ -680,6 +680,7 @@ private: void configureFd(); void populateAbsoluteAxisStates(); bool hasKeycodeLocked(int keycode) const; + bool hasKeycodeInternalLocked(int keycode) const; void loadConfigurationLocked(); bool loadVirtualKeyMapLocked(); status_t loadKeyMapLocked(); |