From 5a71e8857560729d165e3154c97788201e063ece Mon Sep 17 00:00:00 2001 From: Vaibhav Devmurari Date: Mon, 30 Sep 2024 15:54:41 +0000 Subject: 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 --- libs/input/KeyCharacterMap.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'libs/input/KeyCharacterMap.cpp') 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 KeyCharacterMap::findKeyCodesMappedToKeyCode(int32_t toKeyCode) const { + std::vector fromKeyCodes; + + for (const auto& [from, to] : mKeyRemapping) { + if (toKeyCode == to) { + fromKeyCodes.push_back(from); + } + } + return fromKeyCodes; +} + std::pair KeyCharacterMap::applyKeyBehavior(int32_t fromKeyCode, int32_t fromMetaState) const { int32_t toKeyCode = fromKeyCode; -- cgit v1.2.3-59-g8ed1b From d78dd9b184cc700b9a887820056e5ed6a4c0ea56 Mon Sep 17 00:00:00 2001 From: Linnan Li Date: Tue, 15 Oct 2024 15:50:01 +0000 Subject: Copy KeyCharacterMap object when we fill InputDeviceInfo(1/n) Currently, in InputDeviceInfo, we store the KeyCharacterMap object, which is actually the original KeyCharacterMap from the EventHub. This could potentially lead to issues where two threads operate on the same KeyCharacterMap object simultaneously, resulting in thread safety problems. To avoid potential risks in the future, we make a copy of the original KeyCharacterMap when generating InputDeviceInfo. This change should not introduce any behavioral changes. Bug: 373011069 Flag: EXEMPT refactor Test: presubmit Signed-off-by: Linnan Li (cherry picked from https://partner-android-review.googlesource.com/q/commit:1be9c1ce7400e38cf3f45921d7181e947929f91c) Merged-In: I0d0155133f95b0f1dc925422eda0da04c6f196ea Change-Id: I0d0155133f95b0f1dc925422eda0da04c6f196ea --- include/input/InputDevice.h | 11 ++++---- include/input/KeyCharacterMap.h | 2 +- libs/input/InputDevice.cpp | 32 +++++++++++++++++++++- libs/input/KeyCharacterMap.cpp | 6 ++-- .../reader/mapper/KeyboardInputMapper.cpp | 4 ++- 5 files changed, 43 insertions(+), 12 deletions(-) (limited to 'libs/input/KeyCharacterMap.cpp') diff --git a/include/input/InputDevice.h b/include/input/InputDevice.h index 1a482396ee..6a248ef188 100644 --- a/include/input/InputDevice.h +++ b/include/input/InputDevice.h @@ -266,6 +266,7 @@ class InputDeviceInfo { public: InputDeviceInfo(); InputDeviceInfo(const InputDeviceInfo& other); + InputDeviceInfo& operator=(const InputDeviceInfo& other); ~InputDeviceInfo(); struct MotionRange { @@ -315,13 +316,11 @@ public: inline const InputDeviceViewBehavior& getViewBehavior() const { return mViewBehavior; } - inline void setKeyCharacterMap(const std::shared_ptr value) { - mKeyCharacterMap = value; + inline void setKeyCharacterMap(std::unique_ptr value) { + mKeyCharacterMap = std::move(value); } - inline const std::shared_ptr getKeyCharacterMap() const { - return mKeyCharacterMap; - } + inline const KeyCharacterMap* getKeyCharacterMap() const { return mKeyCharacterMap.get(); } inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; } inline bool hasVibrator() const { return mHasVibrator; } @@ -364,7 +363,7 @@ private: std::optional mKeyboardLayoutInfo; uint32_t mSources; int32_t mKeyboardType; - std::shared_ptr mKeyCharacterMap; + std::unique_ptr mKeyCharacterMap; std::optional mUsiVersion; ui::LogicalDisplayId mAssociatedDisplayId{ui::LogicalDisplayId::INVALID}; bool mEnabled; diff --git a/include/input/KeyCharacterMap.h b/include/input/KeyCharacterMap.h index 7ea34c2566..0a9e74f73b 100644 --- a/include/input/KeyCharacterMap.h +++ b/include/input/KeyCharacterMap.h @@ -72,7 +72,7 @@ public: }; /* Loads a key character map from a file. */ - static base::Result> load(const std::string& filename, + static base::Result> load(const std::string& filename, Format format); /* Loads a key character map from its string contents. */ diff --git a/libs/input/InputDevice.cpp b/libs/input/InputDevice.cpp index c9030312f9..4a6f66e058 100644 --- a/libs/input/InputDevice.cpp +++ b/libs/input/InputDevice.cpp @@ -191,7 +191,9 @@ InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) mKeyboardLayoutInfo(other.mKeyboardLayoutInfo), mSources(other.mSources), mKeyboardType(other.mKeyboardType), - mKeyCharacterMap(other.mKeyCharacterMap), + mKeyCharacterMap(other.mKeyCharacterMap + ? std::make_unique(*other.mKeyCharacterMap) + : nullptr), mUsiVersion(other.mUsiVersion), mAssociatedDisplayId(other.mAssociatedDisplayId), mEnabled(other.mEnabled), @@ -204,6 +206,34 @@ InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) mLights(other.mLights), mViewBehavior(other.mViewBehavior) {} +InputDeviceInfo& InputDeviceInfo::operator=(const InputDeviceInfo& other) { + mId = other.mId; + mGeneration = other.mGeneration; + mControllerNumber = other.mControllerNumber; + mIdentifier = other.mIdentifier; + mAlias = other.mAlias; + mIsExternal = other.mIsExternal; + mHasMic = other.mHasMic; + mKeyboardLayoutInfo = other.mKeyboardLayoutInfo; + mSources = other.mSources; + mKeyboardType = other.mKeyboardType; + mKeyCharacterMap = other.mKeyCharacterMap + ? std::make_unique(*other.mKeyCharacterMap) + : nullptr; + mUsiVersion = other.mUsiVersion; + mAssociatedDisplayId = other.mAssociatedDisplayId; + mEnabled = other.mEnabled; + mHasVibrator = other.mHasVibrator; + mHasBattery = other.mHasBattery; + mHasButtonUnderPad = other.mHasButtonUnderPad; + mHasSensor = other.mHasSensor; + mMotionRanges = other.mMotionRanges; + mSensors = other.mSensors; + mLights = other.mLights; + mViewBehavior = other.mViewBehavior; + return *this; +} + InputDeviceInfo::~InputDeviceInfo() { } diff --git a/libs/input/KeyCharacterMap.cpp b/libs/input/KeyCharacterMap.cpp index d775327a80..90d29dd190 100644 --- a/libs/input/KeyCharacterMap.cpp +++ b/libs/input/KeyCharacterMap.cpp @@ -84,15 +84,15 @@ static String8 toString(const char16_t* chars, size_t numChars) { KeyCharacterMap::KeyCharacterMap(const std::string& filename) : mLoadFileName(filename) {} -base::Result> KeyCharacterMap::load(const std::string& filename, +base::Result> KeyCharacterMap::load(const std::string& filename, Format format) { Tokenizer* tokenizer; status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer); if (status) { return Errorf("Error {} opening key character map file {}.", status, filename.c_str()); } - std::shared_ptr map = - std::shared_ptr(new KeyCharacterMap(filename)); + std::unique_ptr map = + std::unique_ptr(new KeyCharacterMap(filename)); if (!map.get()) { ALOGE("Error allocating key character map."); return Errorf("Error allocating key character map."); diff --git a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp index 567a3e2ce4..585f61a3eb 100644 --- a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp +++ b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp @@ -132,7 +132,9 @@ std::optional KeyboardInputMapper::getKeyboardLayoutInfo() c void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo& info) { InputMapper::populateDeviceInfo(info); - info.setKeyCharacterMap(getDeviceContext().getKeyCharacterMap()); + if (const auto kcm = getDeviceContext().getKeyCharacterMap(); kcm != nullptr) { + info.setKeyCharacterMap(std::make_unique(*kcm)); + } std::optional keyboardLayoutInfo = getKeyboardLayoutInfo(); if (keyboardLayoutInfo) { -- cgit v1.2.3-59-g8ed1b