blob: d147d60f4e3055169b082c827cb8938861d5e0ab [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 Pradhan2197cb42022-10-11 02:56:14 +000064static bool isMediaKey(int32_t keyCode) {
65 switch (keyCode) {
66 case AKEYCODE_MEDIA_PLAY:
67 case AKEYCODE_MEDIA_PAUSE:
68 case AKEYCODE_MEDIA_PLAY_PAUSE:
69 case AKEYCODE_MUTE:
70 case AKEYCODE_HEADSETHOOK:
71 case AKEYCODE_MEDIA_STOP:
72 case AKEYCODE_MEDIA_NEXT:
73 case AKEYCODE_MEDIA_PREVIOUS:
74 case AKEYCODE_MEDIA_REWIND:
75 case AKEYCODE_MEDIA_RECORD:
76 case AKEYCODE_MEDIA_FAST_FORWARD:
77 case AKEYCODE_MEDIA_SKIP_FORWARD:
78 case AKEYCODE_MEDIA_SKIP_BACKWARD:
79 case AKEYCODE_MEDIA_STEP_FORWARD:
80 case AKEYCODE_MEDIA_STEP_BACKWARD:
81 case AKEYCODE_MEDIA_AUDIO_TRACK:
82 case AKEYCODE_VOLUME_UP:
83 case AKEYCODE_VOLUME_DOWN:
84 case AKEYCODE_VOLUME_MUTE:
85 case AKEYCODE_TV_AUDIO_DESCRIPTION:
86 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP:
87 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN:
88 return true;
89 default:
90 return false;
91 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092}
93
94// --- KeyboardInputMapper ---
95
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080096KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source,
97 int32_t keyboardType)
98 : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099
Philip Junker4af3b3d2021-12-14 10:36:55 +0100100uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101 return mSource;
102}
103
Michael Wrighta9cf4192022-12-01 23:46:39 +0000104ui::Rotation KeyboardInputMapper::getOrientation() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105 if (mViewport) {
106 return mViewport->orientation;
107 }
Michael Wrighta9cf4192022-12-01 23:46:39 +0000108 return ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700109}
110
111int32_t KeyboardInputMapper::getDisplayId() {
112 if (mViewport) {
113 return mViewport->displayId;
114 }
115 return ADISPLAY_ID_NONE;
116}
117
118void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
119 InputMapper::populateDeviceInfo(info);
120
121 info->setKeyboardType(mKeyboardType);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800122 info->setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Zixuan Qufecb6062022-11-12 04:44:31 +0000123
124 if (mKeyboardLayoutInfo) {
125 info->setKeyboardLayoutInfo(*mKeyboardLayoutInfo);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000126 } else {
127 std::optional<RawLayoutInfo> layoutInfo = getDeviceContext().getRawLayoutInfo();
128 if (layoutInfo) {
129 info->setKeyboardLayoutInfo(
130 KeyboardLayoutInfo(layoutInfo->languageTag, layoutInfo->layoutType));
131 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000132 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133}
134
135void KeyboardInputMapper::dump(std::string& dump) {
136 dump += INDENT2 "Keyboard Input Mapper:\n";
137 dumpParameters(dump);
138 dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType);
139 dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation());
140 dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
141 dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState);
Zixuan Qufecb6062022-11-12 04:44:31 +0000142 dump += INDENT3 "KeyboardLayoutInfo: ";
143 if (mKeyboardLayoutInfo) {
144 dump += mKeyboardLayoutInfo->languageTag + ", " + mKeyboardLayoutInfo->layoutType + "\n";
145 } else {
146 dump += "<not set>\n";
147 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700148}
149
150std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000151 const InputReaderConfiguration* config) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700152 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800153 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700154 }
155
156 // No associated display defined, try to find default display if orientationAware.
157 if (mParameters.orientationAware) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100158 return config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159 }
160
161 return std::nullopt;
162}
163
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700164std::list<NotifyArgs> KeyboardInputMapper::configure(nsecs_t when,
165 const InputReaderConfiguration* config,
166 uint32_t changes) {
167 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700168
169 if (!changes) { // first time only
170 // Configure basic parameters.
171 configureParameters();
172 }
173
174 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000175 mViewport = findViewport(config);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700176 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000177
178 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION)) {
179 mKeyboardLayoutInfo =
180 getValueByKey(config->keyboardLayoutAssociations, getDeviceContext().getLocation());
181 }
182
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700183 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700184}
185
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700186void KeyboardInputMapper::configureParameters() {
187 mParameters.orientationAware = false;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800188 const PropertyMap& config = getDeviceContext().getConfiguration();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700189 config.tryGetProperty("keyboard.orientationAware", mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700190
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191 mParameters.handlesKeyRepeat = false;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700192 config.tryGetProperty("keyboard.handlesKeyRepeat", mParameters.handlesKeyRepeat);
Powei Fengd041c5d2019-05-03 17:11:33 -0700193
194 mParameters.doNotWakeByDefault = false;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700195 config.tryGetProperty("keyboard.doNotWakeByDefault", mParameters.doNotWakeByDefault);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700196}
197
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000198void KeyboardInputMapper::dumpParameters(std::string& dump) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700199 dump += INDENT3 "Parameters:\n";
200 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
201 dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
202}
203
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700204std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
205 std::list<NotifyArgs> out = cancelAllDownKeys(when);
Prabir Pradhan84395e62022-10-26 19:52:00 +0000206 mHidUsageAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700207
208 resetLedState();
209
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700210 out += InputMapper::reset(when);
211 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700212}
213
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700214std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
215 std::list<NotifyArgs> out;
Prabir Pradhan84395e62022-10-26 19:52:00 +0000216 mHidUsageAccumulator.process(*rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700217 switch (rawEvent->type) {
218 case EV_KEY: {
219 int32_t scanCode = rawEvent->code;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700220
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000221 if (isSupportedScanCode(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700222 out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
Prabir Pradhan84395e62022-10-26 19:52:00 +0000223 scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 }
225 break;
226 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700227 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700228 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229}
230
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700231std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
232 int32_t scanCode, int32_t usageCode) {
233 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700234 int32_t keyCode;
235 int32_t keyMetaState;
236 uint32_t policyFlags;
237
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800238 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
239 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700240 keyCode = AKEYCODE_UNKNOWN;
241 keyMetaState = mMetaState;
242 policyFlags = 0;
243 }
244
Arthur Hung2141d542022-08-23 07:45:21 +0000245 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000246 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700247 if (down) {
248 // Rotate key codes according to orientation if needed.
249 if (mParameters.orientationAware) {
250 keyCode = rotateKeyCode(keyCode, getOrientation());
251 }
252
253 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000254 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000256 keyCode = mKeyDowns[*keyDownIndex].keyCode;
257 downTime = mKeyDowns[*keyDownIndex].downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700258 } else {
259 // key down
260 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800261 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700262 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 }
264 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700265 out += getDeviceContext().cancelTouch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266 }
267
268 KeyDown keyDown;
269 keyDown.keyCode = keyCode;
270 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000271 keyDown.downTime = when;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 mKeyDowns.push_back(keyDown);
273 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700274 } else {
275 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000276 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700277 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000278 keyCode = mKeyDowns[*keyDownIndex].keyCode;
279 downTime = mKeyDowns[*keyDownIndex].downTime;
280 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700281 } else {
282 // key was not actually down
283 ALOGI("Dropping key up from device %s because the key was not down. "
284 "keyCode=%d, scanCode=%d",
285 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700287 }
288 }
289
290 if (updateMetaStateIfNeeded(keyCode, down)) {
291 // If global meta state changed send it along with the key.
292 // If it has not changed then we'll use what keymap gave us,
293 // since key replacement logic might temporarily reset a few
294 // meta bits for given key.
295 keyMetaState = mMetaState;
296 }
297
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700298 // Key down on external an keyboard should wake the device.
299 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700300 // For internal keyboards and devices for which the default wake behavior is explicitly
301 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
302 // wake key individually.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700303 // TODO: Use the input device configuration to control this behavior more finely.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800304 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
Powei Fengd041c5d2019-05-03 17:11:33 -0700305 !isMediaKey(keyCode)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 policyFlags |= POLICY_FLAG_WAKE;
307 }
308
309 if (mParameters.handlesKeyRepeat) {
310 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
311 }
312
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000313 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
314 mSource, getDisplayId(), policyFlags,
315 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
316 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState,
317 downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700318 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700319}
320
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000321std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700322 size_t n = mKeyDowns.size();
323 for (size_t i = 0; i < n; i++) {
324 if (mKeyDowns[i].scanCode == scanCode) {
325 return i;
326 }
327 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000328 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329}
330
331int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800332 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333}
334
335int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800336 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337}
338
Philip Junker4af3b3d2021-12-14 10:36:55 +0100339int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
340 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
341}
342
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
344 const std::vector<int32_t>& keyCodes,
345 uint8_t* outFlags) {
346 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347}
348
349int32_t KeyboardInputMapper::getMetaState() {
350 return mMetaState;
351}
352
Arthur Hungcb40a002021-08-03 14:31:01 +0000353bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
354 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
355 return false;
356 }
357
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700358 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000359 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700360}
361
362bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
363 int32_t oldMetaState = mMetaState;
364 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800365 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700366 if (metaStateChanged) {
367 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800368 constexpr int32_t allLedMetaState =
369 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
370 if ((metaStateChanged & allLedMetaState) != 0) {
371 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
372 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373 getContext()->updateGlobalMetaState();
374 }
375
376 return metaStateChanged;
377}
378
379void KeyboardInputMapper::resetLedState() {
380 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
381 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
382 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
383
384 updateLedState(true);
385}
386
387void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800388 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389 ledState.on = false;
390}
391
392void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000393 // Clear the local led state then union the global led state.
394 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800395 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700396
397 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700398 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
399 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700400 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
401 AMETA_SCROLL_LOCK_ON};
402 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700403 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700404 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
405 // meta state.
406 if (hasKeyLayout) {
407 for (int i = 0; i < META_NUM; i++) {
408 if (!flags[i]) {
409 mMetaState &= ~metaCodes[i];
410 }
411 }
412 }
413
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700414 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
415 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
416 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
417}
418
419void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
420 int32_t modifier, bool reset) {
421 if (ledState.avail) {
422 bool desiredState = (mMetaState & modifier) != 0;
423 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800424 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 ledState.on = desiredState;
426 }
427 }
428}
429
430std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
431 if (mViewport) {
432 return std::make_optional(mViewport->displayId);
433 }
434 return std::nullopt;
435}
436
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700437std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
438 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000439 size_t n = mKeyDowns.size();
440 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000441 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
442 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
443 getDisplayId(), 0 /*policyFlags*/, AKEY_EVENT_ACTION_UP,
444 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED,
445 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
446 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000447 }
448 mKeyDowns.clear();
449 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700450 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000451}
452
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453} // namespace android