blob: e03a7730ae0b561765463d7474c1ff5cb8d4ea05 [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
Arpit Singh8e6fb252023-04-06 11:49:17 +000066KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext,
67 const InputReaderConfiguration& readerConfig,
68 uint32_t source, int32_t keyboardType)
69 : InputMapper(deviceContext, readerConfig), mSource(source), mKeyboardType(keyboardType) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070
Philip Junker4af3b3d2021-12-14 10:36:55 +010071uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 return mSource;
73}
74
Michael Wrighta9cf4192022-12-01 23:46:39 +000075ui::Rotation KeyboardInputMapper::getOrientation() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070076 if (mViewport) {
77 return mViewport->orientation;
78 }
Michael Wrighta9cf4192022-12-01 23:46:39 +000079 return ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080}
81
82int32_t KeyboardInputMapper::getDisplayId() {
83 if (mViewport) {
84 return mViewport->displayId;
85 }
86 return ADISPLAY_ID_NONE;
87}
88
Harry Cuttsd02ea102023-03-17 18:21:30 +000089void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 InputMapper::populateDeviceInfo(info);
91
Harry Cuttsd02ea102023-03-17 18:21:30 +000092 info.setKeyboardType(mKeyboardType);
93 info.setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Zixuan Qufecb6062022-11-12 04:44:31 +000094
95 if (mKeyboardLayoutInfo) {
Harry Cuttsd02ea102023-03-17 18:21:30 +000096 info.setKeyboardLayoutInfo(*mKeyboardLayoutInfo);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +000097 } else {
98 std::optional<RawLayoutInfo> layoutInfo = getDeviceContext().getRawLayoutInfo();
99 if (layoutInfo) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000100 info.setKeyboardLayoutInfo(
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000101 KeyboardLayoutInfo(layoutInfo->languageTag, layoutInfo->layoutType));
102 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000103 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104}
105
106void KeyboardInputMapper::dump(std::string& dump) {
107 dump += INDENT2 "Keyboard Input Mapper:\n";
108 dumpParameters(dump);
109 dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType);
110 dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation());
111 dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
112 dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState);
Zixuan Qufecb6062022-11-12 04:44:31 +0000113 dump += INDENT3 "KeyboardLayoutInfo: ";
114 if (mKeyboardLayoutInfo) {
115 dump += mKeyboardLayoutInfo->languageTag + ", " + mKeyboardLayoutInfo->layoutType + "\n";
116 } else {
117 dump += "<not set>\n";
118 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119}
120
121std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
Arpit Singhed6c3de2023-04-05 19:24:37 +0000122 const InputReaderConfiguration& readerConfig) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700123 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800124 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700125 }
126
127 // No associated display defined, try to find default display if orientationAware.
128 if (mParameters.orientationAware) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000129 return readerConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130 }
131
132 return std::nullopt;
133}
134
Arpit Singh4be4eef2023-03-28 14:26:01 +0000135std::list<NotifyArgs> KeyboardInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000136 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000137 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +0000138 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000140 if (!changes.any()) { // first time only
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141 // Configure basic parameters.
142 configureParameters();
143 }
144
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000145 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000146 mViewport = findViewport(config);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000148
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000149 if (!changes.any() ||
150 changes.test(InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION)) {
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +0000151 std::optional<KeyboardLayoutInfo> newKeyboardLayoutInfo =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000152 getValueByKey(config.keyboardLayoutAssociations, getDeviceContext().getLocation());
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +0000153 if (mKeyboardLayoutInfo != newKeyboardLayoutInfo) {
154 mKeyboardLayoutInfo = newKeyboardLayoutInfo;
155 bumpGeneration();
156 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000157 }
158
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700159 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700160}
161
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700162void KeyboardInputMapper::configureParameters() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800163 const PropertyMap& config = getDeviceContext().getConfiguration();
Harry Cuttsf13161a2023-03-08 14:15:49 +0000164 mParameters.orientationAware = config.getBool("keyboard.orientationAware").value_or(false);
165 mParameters.handlesKeyRepeat = config.getBool("keyboard.handlesKeyRepeat").value_or(false);
166 mParameters.doNotWakeByDefault = config.getBool("keyboard.doNotWakeByDefault").value_or(false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700167}
168
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000169void KeyboardInputMapper::dumpParameters(std::string& dump) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700170 dump += INDENT3 "Parameters:\n";
171 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
172 dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
173}
174
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700175std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
176 std::list<NotifyArgs> out = cancelAllDownKeys(when);
Prabir Pradhan84395e62022-10-26 19:52:00 +0000177 mHidUsageAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700178
179 resetLedState();
180
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700181 out += InputMapper::reset(when);
182 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700183}
184
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700185std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
186 std::list<NotifyArgs> out;
Prabir Pradhan84395e62022-10-26 19:52:00 +0000187 mHidUsageAccumulator.process(*rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700188 switch (rawEvent->type) {
189 case EV_KEY: {
190 int32_t scanCode = rawEvent->code;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000192 if (isSupportedScanCode(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700193 out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
Prabir Pradhan84395e62022-10-26 19:52:00 +0000194 scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700195 }
196 break;
197 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700198 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700199 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700200}
201
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700202std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
203 int32_t scanCode, int32_t usageCode) {
204 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700205 int32_t keyCode;
206 int32_t keyMetaState;
207 uint32_t policyFlags;
Justin Chung71ddb432023-03-27 04:29:07 +0000208 int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800210 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
211 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700212 keyCode = AKEYCODE_UNKNOWN;
213 keyMetaState = mMetaState;
214 policyFlags = 0;
215 }
216
Arthur Hung2141d542022-08-23 07:45:21 +0000217 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000218 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700219 if (down) {
220 // Rotate key codes according to orientation if needed.
221 if (mParameters.orientationAware) {
222 keyCode = rotateKeyCode(keyCode, getOrientation());
223 }
224
225 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000226 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700227 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000228 keyCode = mKeyDowns[*keyDownIndex].keyCode;
229 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000230 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700231 } else {
232 // key down
233 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800234 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700235 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700236 }
237 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700238 out += getDeviceContext().cancelTouch(when, readTime);
Justin Chung71ddb432023-03-27 04:29:07 +0000239 flags |= AKEY_EVENT_FLAG_KEEP_TOUCH_MODE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700240 }
241
242 KeyDown keyDown;
243 keyDown.keyCode = keyCode;
244 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000245 keyDown.downTime = when;
Justin Chung71ddb432023-03-27 04:29:07 +0000246 keyDown.flags = flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700247 mKeyDowns.push_back(keyDown);
248 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 } else {
250 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000251 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000253 keyCode = mKeyDowns[*keyDownIndex].keyCode;
254 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000255 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000256 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 } else {
258 // key was not actually down
259 ALOGI("Dropping key up from device %s because the key was not down. "
260 "keyCode=%d, scanCode=%d",
261 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700262 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 }
264 }
265
266 if (updateMetaStateIfNeeded(keyCode, down)) {
267 // If global meta state changed send it along with the key.
268 // If it has not changed then we'll use what keymap gave us,
269 // since key replacement logic might temporarily reset a few
270 // meta bits for given key.
271 keyMetaState = mMetaState;
272 }
273
Vaibhav Devmurari16257862023-03-06 10:06:32 +0000274 // Any key down on an external keyboard should wake the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700275 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700276 // For internal keyboards and devices for which the default wake behavior is explicitly
277 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
278 // wake key individually.
Vaibhav Devmurari16257862023-03-06 10:06:32 +0000279 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 policyFlags |= POLICY_FLAG_WAKE;
281 }
282
283 if (mParameters.handlesKeyRepeat) {
284 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
285 }
286
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000287 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
288 mSource, getDisplayId(), policyFlags,
Justin Chung71ddb432023-03-27 04:29:07 +0000289 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, flags,
290 keyCode, scanCode, keyMetaState, downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700291 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292}
293
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000294std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295 size_t n = mKeyDowns.size();
296 for (size_t i = 0; i < n; i++) {
297 if (mKeyDowns[i].scanCode == scanCode) {
298 return i;
299 }
300 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000301 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700302}
303
304int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800305 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306}
307
308int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800309 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700310}
311
Philip Junker4af3b3d2021-12-14 10:36:55 +0100312int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
313 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
314}
315
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700316bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
317 const std::vector<int32_t>& keyCodes,
318 uint8_t* outFlags) {
319 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700320}
321
322int32_t KeyboardInputMapper::getMetaState() {
323 return mMetaState;
324}
325
Arthur Hungcb40a002021-08-03 14:31:01 +0000326bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
327 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
328 return false;
329 }
330
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700331 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000332 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333}
334
335bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
336 int32_t oldMetaState = mMetaState;
337 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800338 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700339 if (metaStateChanged) {
340 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800341 constexpr int32_t allLedMetaState =
342 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
343 if ((metaStateChanged & allLedMetaState) != 0) {
344 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
345 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 getContext()->updateGlobalMetaState();
347 }
348
349 return metaStateChanged;
350}
351
352void KeyboardInputMapper::resetLedState() {
353 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
354 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
355 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
356
357 updateLedState(true);
358}
359
360void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800361 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362 ledState.on = false;
363}
364
365void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000366 // Clear the local led state then union the global led state.
367 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800368 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700369
370 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700371 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
372 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700373 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
374 AMETA_SCROLL_LOCK_ON};
375 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700376 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700377 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
378 // meta state.
379 if (hasKeyLayout) {
380 for (int i = 0; i < META_NUM; i++) {
381 if (!flags[i]) {
382 mMetaState &= ~metaCodes[i];
383 }
384 }
385 }
386
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700387 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
388 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
389 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
390}
391
392void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
393 int32_t modifier, bool reset) {
394 if (ledState.avail) {
395 bool desiredState = (mMetaState & modifier) != 0;
396 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800397 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398 ledState.on = desiredState;
399 }
400 }
401}
402
403std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
404 if (mViewport) {
405 return std::make_optional(mViewport->displayId);
406 }
407 return std::nullopt;
408}
409
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700410std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
411 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000412 size_t n = mKeyDowns.size();
413 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000414 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
415 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
Harry Cutts33476232023-01-30 19:57:29 +0000416 getDisplayId(), /*policyFlags=*/0, AKEY_EVENT_ACTION_UP,
Justin Chung71ddb432023-03-27 04:29:07 +0000417 mKeyDowns[i].flags | AKEY_EVENT_FLAG_CANCELED,
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000418 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
419 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000420 }
421 mKeyDowns.clear();
422 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700423 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000424}
425
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700426} // namespace android