Skip to content

Commit 5c4f798

Browse files
author
Vaibhav Devmurari
committed
Allow toggle caps lock even if device doesn't a have caps lock key
For Alphabetic keyboards without CapsLock key, we should allow system to toggle CapsLock modifier state. Test: atest CtsInputTestCases Bug: 368397939 Flag: EXEMPT bugfix Change-Id: I57cfe94bf5ab7804a9a1579dae769c18684776cb
1 parent 5a71e88 commit 5c4f798

10 files changed

Lines changed: 14 additions & 87 deletions

File tree

services/inputflinger/reader/InputDevice.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,6 @@ int32_t InputDevice::getMetaState() {
691691
return result;
692692
}
693693

694-
void InputDevice::updateMetaState(int32_t keyCode) {
695-
first_in_mappers<bool>([keyCode](InputMapper& mapper) {
696-
if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
697-
mapper.updateMetaState(keyCode)) {
698-
return std::make_optional(true);
699-
}
700-
return std::optional<bool>();
701-
});
702-
}
703-
704694
void InputDevice::bumpGeneration() {
705695
mGeneration = mContext->bumpGeneration();
706696
}

services/inputflinger/reader/InputReader.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -584,18 +584,9 @@ int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32
584584

585585
void InputReader::toggleCapsLockState(int32_t deviceId) {
586586
std::scoped_lock _l(mLock);
587-
InputDevice* device = findInputDeviceLocked(deviceId);
588-
if (!device) {
589-
ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
590-
return;
591-
}
592-
593-
if (device->isIgnored()) {
594-
ALOGW("Ignoring toggleCapsLock for ignored deviceId %" PRId32 ".", deviceId);
595-
return;
587+
if (mKeyboardClassifier->getKeyboardType(deviceId) == KeyboardType::ALPHABETIC) {
588+
updateLedMetaStateLocked(mLedMetaState ^ AMETA_CAPS_LOCK_ON);
596589
}
597-
598-
device->updateMetaState(AKEYCODE_CAPS_LOCK);
599590
}
600591

601592
bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,

services/inputflinger/reader/include/InputDevice.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ class InputDevice {
122122
std::optional<int32_t> getLightPlayerId(int32_t lightId);
123123

124124
int32_t getMetaState();
125-
void updateMetaState(int32_t keyCode);
126-
127125
void setKeyboardType(KeyboardType keyboardType);
128126

129127
void bumpGeneration();

services/inputflinger/reader/mapper/InputMapper.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ int32_t InputMapper::getMetaState() {
109109
return 0;
110110
}
111111

112-
bool InputMapper::updateMetaState(int32_t keyCode) {
113-
return false;
114-
}
115-
116112
std::list<NotifyArgs> InputMapper::updateExternalStylusState(const StylusState& state) {
117113
return {};
118114
}

services/inputflinger/reader/mapper/InputMapper.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ class InputMapper {
111111
virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; }
112112

113113
virtual int32_t getMetaState();
114-
/**
115-
* Process the meta key and update the global meta state when changed.
116-
* Return true if the meta key could be handled by the InputMapper.
117-
*/
118-
virtual bool updateMetaState(int32_t keyCode);
119114

120115
[[nodiscard]] virtual std::list<NotifyArgs> updateExternalStylusState(const StylusState& state);
121116

services/inputflinger/reader/mapper/KeyboardInputMapper.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,6 @@ int32_t KeyboardInputMapper::getMetaState() {
390390
return mMetaState;
391391
}
392392

393-
bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
394-
if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
395-
return false;
396-
}
397-
398-
updateMetaStateIfNeeded(keyCode, false);
399-
return true;
400-
}
401-
402393
bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
403394
int32_t oldMetaState = mMetaState;
404395
int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
@@ -434,17 +425,21 @@ void KeyboardInputMapper::updateLedState(bool reset) {
434425
mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
435426
mMetaState |= getContext()->getLedMetaState();
436427

437-
constexpr int32_t META_NUM = 3;
438-
const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
439-
AKEYCODE_SCROLL_LOCK};
440-
const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
441-
AMETA_SCROLL_LOCK_ON};
442-
std::array<uint8_t, META_NUM> flags = {0, 0, 0};
443-
bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
428+
std::vector<int32_t> keyCodesToCheck{AKEYCODE_NUM_LOCK, AKEYCODE_SCROLL_LOCK};
429+
std::vector<int32_t> metaCodes{AMETA_NUM_LOCK_ON, AMETA_SCROLL_LOCK_ON};
430+
// Check for physical CapsLock key only for non-alphabetic keyboards. For Alphabetic
431+
// keyboards, we will allow Caps Lock even if there is no physical CapsLock key.
432+
if (getDeviceContext().getKeyboardType() != KeyboardType::ALPHABETIC) {
433+
keyCodesToCheck.push_back(AKEYCODE_CAPS_LOCK);
434+
metaCodes.push_back(AMETA_CAPS_LOCK_ON);
435+
}
436+
size_t size = keyCodesToCheck.size();
437+
std::vector<uint8_t> flags(size, 0);
438+
bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodesToCheck, flags.data());
444439
// If the device doesn't have the physical meta key it shouldn't generate the corresponding
445440
// meta state.
446441
if (hasKeyLayout) {
447-
for (int i = 0; i < META_NUM; i++) {
442+
for (size_t i = 0; i < size; i++) {
448443
if (!flags[i]) {
449444
mMetaState &= ~metaCodes[i];
450445
}

services/inputflinger/reader/mapper/KeyboardInputMapper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class KeyboardInputMapper : public InputMapper {
4545
int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
4646

4747
int32_t getMetaState() override;
48-
bool updateMetaState(int32_t keyCode) override;
4948
std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override;
5049
void updateLedState(bool reset) override;
5150

services/inputflinger/tests/InputReader_test.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,40 +3671,6 @@ TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds)
36713671
ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
36723672
}
36733673

3674-
TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3675-
mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3676-
mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3677-
mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3678-
mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3679-
3680-
KeyboardInputMapper& mapper =
3681-
constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
3682-
3683-
// Meta state should be AMETA_NONE after reset
3684-
std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
3685-
ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3686-
// Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3687-
mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3688-
ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3689-
3690-
NotifyKeyArgs args;
3691-
// Press button "A"
3692-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
3693-
ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3694-
ASSERT_EQ(AMETA_NONE, args.metaState);
3695-
ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3696-
ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3697-
ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3698-
3699-
// Button up.
3700-
process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
3701-
ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3702-
ASSERT_EQ(AMETA_NONE, args.metaState);
3703-
ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3704-
ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3705-
ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3706-
}
3707-
37083674
TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
37093675
// keyboard 1.
37103676
mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);

services/inputflinger/tests/InterfaceMocks.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ class MockInputDevice : public InputDevice {
246246
MOCK_METHOD(std::optional<int32_t>, getLightPlayerId, (int32_t lightId), ());
247247

248248
MOCK_METHOD(int32_t, getMetaState, (), ());
249-
MOCK_METHOD(void, updateMetaState, (int32_t keyCode), ());
250-
251249
MOCK_METHOD(void, setKeyboardType, (KeyboardType keyboardType), ());
252250

253251
MOCK_METHOD(void, bumpGeneration, (), ());

services/inputflinger/tests/fuzzers/KeyboardInputFuzzer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
9999
nullptr);
100100
},
101101
[&]() -> void { mapper.getMetaState(); },
102-
[&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); },
103102
[&]() -> void { mapper.getAssociatedDisplayId(); },
104103
})();
105104
}

0 commit comments

Comments
 (0)