blob: ef46fb144fb8bf5aae84715e99e1baf643ad6295 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Wrightfe3de7d2020-07-02 19:05:30 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wrightfe3de7d2020-07-02 19:05:30 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "KeyboardInputMapper.h"
22
Michael Wrighta9cf4192022-12-01 23:46:39 +000023#include <ui/Rotation.h>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025namespace android {
26
27// --- Static Definitions ---
28
Michael Wrighta9cf4192022-12-01 23:46:39 +000029static int32_t rotateKeyCode(int32_t keyCode, ui::Rotation orientation) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000030 static constexpr int32_t KEYCODE_ROTATION_MAP[][4] = {
31 // key codes enumerated counter-clockwise with the original (unrotated) key first
32 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
33 {AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT},
34 {AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN},
35 {AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT},
36 {AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP},
37 {AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT,
38 AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT},
39 {AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP,
40 AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN},
41 {AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT,
42 AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT},
43 {AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN,
44 AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP},
45 };
46
Michael Wrighta9cf4192022-12-01 23:46:39 +000047 if (orientation != ui::ROTATION_0) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000048 for (const auto& rotation : KEYCODE_ROTATION_MAP) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000049 if (rotation[static_cast<size_t>(ui::ROTATION_0)] == keyCode) {
50 return rotation[static_cast<size_t>(orientation)];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051 }
52 }
53 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +000054 return keyCode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055}
56
Prabir Pradhane1a41a82022-10-14 18:06:50 +000057static bool isSupportedScanCode(int32_t scanCode) {
58 // KeyboardInputMapper handles keys from keyboards, gamepads, and styluses.
59 return scanCode < BTN_MOUSE || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI) ||
60 scanCode == BTN_STYLUS || scanCode == BTN_STYLUS2 || scanCode == BTN_STYLUS3 ||
61 scanCode >= BTN_WHEEL;
Prabir Pradhan2197cb42022-10-11 02:56:14 +000062}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070064// --- KeyboardInputMapper ---
65
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080066KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source,
67 int32_t keyboardType)
68 : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069
Philip Junker4af3b3d2021-12-14 10:36:55 +010070uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070071 return mSource;
72}
73
Michael Wrighta9cf4192022-12-01 23:46:39 +000074ui::Rotation KeyboardInputMapper::getOrientation() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 if (mViewport) {
76 return mViewport->orientation;
77 }
Michael Wrighta9cf4192022-12-01 23:46:39 +000078 return ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079}
80
81int32_t KeyboardInputMapper::getDisplayId() {
82 if (mViewport) {
83 return mViewport->displayId;
84 }
85 return ADISPLAY_ID_NONE;
86}
87
Harry Cuttsd02ea102023-03-17 18:21:30 +000088void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 InputMapper::populateDeviceInfo(info);
90
Harry Cuttsd02ea102023-03-17 18:21:30 +000091 info.setKeyboardType(mKeyboardType);
92 info.setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Zixuan Qufecb6062022-11-12 04:44:31 +000093
94 if (mKeyboardLayoutInfo) {
Harry Cuttsd02ea102023-03-17 18:21:30 +000095 info.setKeyboardLayoutInfo(*mKeyboardLayoutInfo);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +000096 } else {
97 std::optional<RawLayoutInfo> layoutInfo = getDeviceContext().getRawLayoutInfo();
98 if (layoutInfo) {
Harry Cuttsd02ea102023-03-17 18:21:30 +000099 info.setKeyboardLayoutInfo(
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000100 KeyboardLayoutInfo(layoutInfo->languageTag, layoutInfo->layoutType));
101 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000102 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103}
104
105void KeyboardInputMapper::dump(std::string& dump) {
106 dump += INDENT2 "Keyboard Input Mapper:\n";
107 dumpParameters(dump);
108 dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType);
109 dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation());
110 dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
111 dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState);
Zixuan Qufecb6062022-11-12 04:44:31 +0000112 dump += INDENT3 "KeyboardLayoutInfo: ";
113 if (mKeyboardLayoutInfo) {
114 dump += mKeyboardLayoutInfo->languageTag + ", " + mKeyboardLayoutInfo->layoutType + "\n";
115 } else {
116 dump += "<not set>\n";
117 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118}
119
120std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000121 const InputReaderConfiguration* config) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700122 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800123 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700124 }
125
126 // No associated display defined, try to find default display if orientationAware.
127 if (mParameters.orientationAware) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100128 return config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700129 }
130
131 return std::nullopt;
132}
133
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700134std::list<NotifyArgs> KeyboardInputMapper::configure(nsecs_t when,
135 const InputReaderConfiguration* config,
136 uint32_t changes) {
137 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138
139 if (!changes) { // first time only
140 // Configure basic parameters.
141 configureParameters();
142 }
143
144 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000145 mViewport = findViewport(config);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700146 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000147
148 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION)) {
149 mKeyboardLayoutInfo =
150 getValueByKey(config->keyboardLayoutAssociations, getDeviceContext().getLocation());
151 }
152
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700153 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700154}
155
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700156void KeyboardInputMapper::configureParameters() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800157 const PropertyMap& config = getDeviceContext().getConfiguration();
Harry Cuttsf13161a2023-03-08 14:15:49 +0000158 mParameters.orientationAware = config.getBool("keyboard.orientationAware").value_or(false);
159 mParameters.handlesKeyRepeat = config.getBool("keyboard.handlesKeyRepeat").value_or(false);
160 mParameters.doNotWakeByDefault = config.getBool("keyboard.doNotWakeByDefault").value_or(false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700161}
162
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000163void KeyboardInputMapper::dumpParameters(std::string& dump) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700164 dump += INDENT3 "Parameters:\n";
165 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
166 dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
167}
168
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700169std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
170 std::list<NotifyArgs> out = cancelAllDownKeys(when);
Prabir Pradhan84395e62022-10-26 19:52:00 +0000171 mHidUsageAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700172
173 resetLedState();
174
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700175 out += InputMapper::reset(when);
176 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700177}
178
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700179std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
180 std::list<NotifyArgs> out;
Prabir Pradhan84395e62022-10-26 19:52:00 +0000181 mHidUsageAccumulator.process(*rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182 switch (rawEvent->type) {
183 case EV_KEY: {
184 int32_t scanCode = rawEvent->code;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700185
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000186 if (isSupportedScanCode(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700187 out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
Prabir Pradhan84395e62022-10-26 19:52:00 +0000188 scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700189 }
190 break;
191 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700192 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700193 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700194}
195
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700196std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
197 int32_t scanCode, int32_t usageCode) {
198 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700199 int32_t keyCode;
200 int32_t keyMetaState;
201 uint32_t policyFlags;
Justin Chung71ddb432023-03-27 04:29:07 +0000202 int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700203
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800204 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
205 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700206 keyCode = AKEYCODE_UNKNOWN;
207 keyMetaState = mMetaState;
208 policyFlags = 0;
209 }
210
Arthur Hung2141d542022-08-23 07:45:21 +0000211 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000212 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700213 if (down) {
214 // Rotate key codes according to orientation if needed.
215 if (mParameters.orientationAware) {
216 keyCode = rotateKeyCode(keyCode, getOrientation());
217 }
218
219 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000220 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000222 keyCode = mKeyDowns[*keyDownIndex].keyCode;
223 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000224 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700225 } else {
226 // key down
227 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800228 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700229 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700230 }
231 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700232 out += getDeviceContext().cancelTouch(when, readTime);
Justin Chung71ddb432023-03-27 04:29:07 +0000233 flags |= AKEY_EVENT_FLAG_KEEP_TOUCH_MODE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700234 }
235
236 KeyDown keyDown;
237 keyDown.keyCode = keyCode;
238 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000239 keyDown.downTime = when;
Justin Chung71ddb432023-03-27 04:29:07 +0000240 keyDown.flags = flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700241 mKeyDowns.push_back(keyDown);
242 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700243 } else {
244 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000245 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700246 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000247 keyCode = mKeyDowns[*keyDownIndex].keyCode;
248 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000249 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000250 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700251 } else {
252 // key was not actually down
253 ALOGI("Dropping key up from device %s because the key was not down. "
254 "keyCode=%d, scanCode=%d",
255 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700256 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 }
258 }
259
260 if (updateMetaStateIfNeeded(keyCode, down)) {
261 // If global meta state changed send it along with the key.
262 // If it has not changed then we'll use what keymap gave us,
263 // since key replacement logic might temporarily reset a few
264 // meta bits for given key.
265 keyMetaState = mMetaState;
266 }
267
Vaibhav Devmurari16257862023-03-06 10:06:32 +0000268 // Any key down on an external keyboard should wake the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700269 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700270 // For internal keyboards and devices for which the default wake behavior is explicitly
271 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
272 // wake key individually.
Vaibhav Devmurari16257862023-03-06 10:06:32 +0000273 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700274 policyFlags |= POLICY_FLAG_WAKE;
275 }
276
277 if (mParameters.handlesKeyRepeat) {
278 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
279 }
280
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000281 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
282 mSource, getDisplayId(), policyFlags,
Justin Chung71ddb432023-03-27 04:29:07 +0000283 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, flags,
284 keyCode, scanCode, keyMetaState, downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700285 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700286}
287
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000288std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700289 size_t n = mKeyDowns.size();
290 for (size_t i = 0; i < n; i++) {
291 if (mKeyDowns[i].scanCode == scanCode) {
292 return i;
293 }
294 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000295 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700296}
297
298int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800299 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300}
301
302int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800303 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700304}
305
Philip Junker4af3b3d2021-12-14 10:36:55 +0100306int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
307 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
308}
309
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700310bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
311 const std::vector<int32_t>& keyCodes,
312 uint8_t* outFlags) {
313 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314}
315
316int32_t KeyboardInputMapper::getMetaState() {
317 return mMetaState;
318}
319
Arthur Hungcb40a002021-08-03 14:31:01 +0000320bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
321 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
322 return false;
323 }
324
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700325 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000326 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700327}
328
329bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
330 int32_t oldMetaState = mMetaState;
331 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800332 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 if (metaStateChanged) {
334 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800335 constexpr int32_t allLedMetaState =
336 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
337 if ((metaStateChanged & allLedMetaState) != 0) {
338 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
339 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 getContext()->updateGlobalMetaState();
341 }
342
343 return metaStateChanged;
344}
345
346void KeyboardInputMapper::resetLedState() {
347 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
348 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
349 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
350
351 updateLedState(true);
352}
353
354void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800355 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700356 ledState.on = false;
357}
358
359void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000360 // Clear the local led state then union the global led state.
361 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800362 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700363
364 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700365 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
366 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700367 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
368 AMETA_SCROLL_LOCK_ON};
369 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700370 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700371 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
372 // meta state.
373 if (hasKeyLayout) {
374 for (int i = 0; i < META_NUM; i++) {
375 if (!flags[i]) {
376 mMetaState &= ~metaCodes[i];
377 }
378 }
379 }
380
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700381 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
382 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
383 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
384}
385
386void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
387 int32_t modifier, bool reset) {
388 if (ledState.avail) {
389 bool desiredState = (mMetaState & modifier) != 0;
390 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800391 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392 ledState.on = desiredState;
393 }
394 }
395}
396
397std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
398 if (mViewport) {
399 return std::make_optional(mViewport->displayId);
400 }
401 return std::nullopt;
402}
403
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700404std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
405 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000406 size_t n = mKeyDowns.size();
407 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000408 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
409 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
Harry Cutts33476232023-01-30 19:57:29 +0000410 getDisplayId(), /*policyFlags=*/0, AKEY_EVENT_ACTION_UP,
Justin Chung71ddb432023-03-27 04:29:07 +0000411 mKeyDowns[i].flags | AKEY_EVENT_FLAG_CANCELED,
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000412 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
413 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000414 }
415 mKeyDowns.clear();
416 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700417 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000418}
419
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700420} // namespace android