diff options
author | 2024-11-15 13:46:29 +0000 | |
---|---|---|
committer | 2024-11-18 11:10:58 +0000 | |
commit | d3795b544170b9edef03b0804147f7fc6fa39513 (patch) | |
tree | 078b3936a15561b7195d7a6d068498a6bcacbd07 | |
parent | f9854d272c816ca2ad2fec085e17c6591721bff9 (diff) |
Allow resetting locked modifier state
Test: atest inputflinger_tests
Bug: 377353219
Flag: EXEMPT bugfix
Change-Id: Ia8d78066781bd432d76a48b992b4b0a9d454af04
5 files changed, 40 insertions, 0 deletions
diff --git a/services/inputflinger/include/InputReaderBase.h b/services/inputflinger/include/InputReaderBase.h index 305feab6c7..580cde39b1 100644 --- a/services/inputflinger/include/InputReaderBase.h +++ b/services/inputflinger/include/InputReaderBase.h @@ -362,6 +362,9 @@ public: /* Toggle Caps Lock */ virtual void toggleCapsLockState(int32_t deviceId) = 0; + /* Resets locked modifier state */ + virtual void resetLockedModifierState() = 0; + /* Determine whether physical keys exist for the given framework-domain key codes. */ virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes, uint8_t* outFlags) = 0; diff --git a/services/inputflinger/reader/InputReader.cpp b/services/inputflinger/reader/InputReader.cpp index ada6653f65..24919b678d 100644 --- a/services/inputflinger/reader/InputReader.cpp +++ b/services/inputflinger/reader/InputReader.cpp @@ -590,6 +590,11 @@ void InputReader::toggleCapsLockState(int32_t deviceId) { } } +void InputReader::resetLockedModifierState() { + std::scoped_lock _l(mLock); + updateLedMetaStateLocked(0); +} + bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes, uint8_t* outFlags) { std::scoped_lock _l(mLock); diff --git a/services/inputflinger/reader/include/InputReader.h b/services/inputflinger/reader/include/InputReader.h index 7614a05470..1403ca2986 100644 --- a/services/inputflinger/reader/include/InputReader.h +++ b/services/inputflinger/reader/include/InputReader.h @@ -69,6 +69,8 @@ public: void toggleCapsLockState(int32_t deviceId) override; + void resetLockedModifierState() override; + bool hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes, uint8_t* outFlags) override; diff --git a/services/inputflinger/tests/KeyboardInputMapper_test.cpp b/services/inputflinger/tests/KeyboardInputMapper_test.cpp index 39b583c6ae..1dd32c447b 100644 --- a/services/inputflinger/tests/KeyboardInputMapper_test.cpp +++ b/services/inputflinger/tests/KeyboardInputMapper_test.cpp @@ -926,6 +926,33 @@ TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) { ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState()); } +TEST_F(KeyboardInputMapperTest, Process_ResetLockedModifierState) { + mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0); + mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0); + mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0); + + KeyboardInputMapper& mapper = + constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD); + // Initial metastate is AMETA_NONE. + ASSERT_EQ(AMETA_NONE, mapper.getMetaState()); + + // Toggle caps lock on. + process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1); + process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0); + + // Toggle num lock on. + process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1); + process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0); + + // Toggle scroll lock on. + process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1); + process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0); + ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState()); + + mReader->resetLockedModifierState(); + ASSERT_EQ(AMETA_NONE, mapper.getMetaState()); +} + TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) { // keyboard 1. mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/); diff --git a/services/inputflinger/tests/fuzzers/InputReaderFuzzer.cpp b/services/inputflinger/tests/fuzzers/InputReaderFuzzer.cpp index 64f3c279a6..6be922dfdb 100644 --- a/services/inputflinger/tests/fuzzers/InputReaderFuzzer.cpp +++ b/services/inputflinger/tests/fuzzers/InputReaderFuzzer.cpp @@ -75,6 +75,8 @@ public: void toggleCapsLockState(int32_t deviceId) { reader->toggleCapsLockState(deviceId); } + void resetLockedModifierState() { reader->resetLockedModifierState(); } + bool hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes, uint8_t* outFlags) { return reader->hasKeys(deviceId, sourceMask, keyCodes, outFlags); @@ -226,6 +228,7 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { fdp->ConsumeIntegral<int32_t>()); }, [&]() -> void { reader->toggleCapsLockState(fdp->ConsumeIntegral<int32_t>()); }, + [&]() -> void { reader->resetLockedModifierState(); }, [&]() -> void { size_t count = fdp->ConsumeIntegralInRange<size_t>(1, 1024); std::vector<uint8_t> outFlags(count); |