Skip to content

Commit 6a3c7c9

Browse files
Vaibhav DevmurariAndroid (Google) Code Review
authored andcommitted
Merge changes from topics "alt+meta=caps", "hasKeycode_bugfix" into main
* changes: Allow toggle caps lock even if device doesn't a have caps lock key hasKeycode API should take into account key remapping
2 parents 9a43d3f + 5c4f798 commit 6a3c7c9

14 files changed

Lines changed: 42 additions & 88 deletions

File tree

include/input/KeyCharacterMap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ class KeyCharacterMap {
137137
/* Returns keycode after applying Android key code remapping defined in mKeyRemapping */
138138
int32_t applyKeyRemapping(int32_t fromKeyCode) const;
139139

140+
/** Returns list of keycodes that remap to provided keycode (@see setKeyRemapping()) */
141+
std::vector<int32_t> findKeyCodesMappedToKeyCode(int32_t toKeyCode) const;
142+
140143
/* Returns the <keyCode, metaState> pair after applying key behavior defined in the kcm file,
141144
* that tries to find a replacement key code based on current meta state */
142145
std::pair<int32_t /*keyCode*/, int32_t /*metaState*/> applyKeyBehavior(int32_t keyCode,

libs/input/KeyCharacterMap.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ int32_t KeyCharacterMap::applyKeyRemapping(int32_t fromKeyCode) const {
365365
return toKeyCode;
366366
}
367367

368+
std::vector<int32_t> KeyCharacterMap::findKeyCodesMappedToKeyCode(int32_t toKeyCode) const {
369+
std::vector<int32_t> fromKeyCodes;
370+
371+
for (const auto& [from, to] : mKeyRemapping) {
372+
if (toKeyCode == to) {
373+
fromKeyCodes.push_back(from);
374+
}
375+
}
376+
return fromKeyCodes;
377+
}
378+
368379
std::pair<int32_t, int32_t> KeyCharacterMap::applyKeyBehavior(int32_t fromKeyCode,
369380
int32_t fromMetaState) const {
370381
int32_t toKeyCode = fromKeyCode;

services/inputflinger/reader/EventHub.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,19 @@ void EventHub::Device::populateAbsoluteAxisStates() {
659659
}
660660

661661
bool EventHub::Device::hasKeycodeLocked(int keycode) const {
662+
if (hasKeycodeInternalLocked(keycode)) {
663+
return true;
664+
}
665+
666+
for (auto& fromKey : getKeyCharacterMap()->findKeyCodesMappedToKeyCode(keycode)) {
667+
if (hasKeycodeInternalLocked(fromKey)) {
668+
return true;
669+
}
670+
}
671+
return false;
672+
}
673+
674+
bool EventHub::Device::hasKeycodeInternalLocked(int keycode) const {
662675
if (!keyMap.haveKeyLayout()) {
663676
return false;
664677
}
@@ -676,7 +689,6 @@ bool EventHub::Device::hasKeycodeLocked(int keycode) const {
676689
if (usageCodes.size() > 0 && mscBitmask.test(MSC_SCAN)) {
677690
return true;
678691
}
679-
680692
return false;
681693
}
682694

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/EventHub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ class EventHub : public EventHubInterface {
680680
void configureFd();
681681
void populateAbsoluteAxisStates();
682682
bool hasKeycodeLocked(int keycode) const;
683+
bool hasKeycodeInternalLocked(int keycode) const;
683684
void loadConfigurationLocked();
684685
bool loadVirtualKeyMapLocked();
685686
status_t loadKeyMapLocked();

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
}

0 commit comments

Comments
 (0)