blob: 25f4893baff852963821306c70bac060d6fc0230 [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
Harry Cutts91e7fa22024-02-09 12:50:07 +000023#include <ftl/enum.h>
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000024#include <input/KeyboardClassifier.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000025#include <ui/Rotation.h>
26
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070027namespace android {
28
29// --- Static Definitions ---
30
Michael Wrighta9cf4192022-12-01 23:46:39 +000031static int32_t rotateKeyCode(int32_t keyCode, ui::Rotation orientation) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000032 static constexpr int32_t KEYCODE_ROTATION_MAP[][4] = {
33 // key codes enumerated counter-clockwise with the original (unrotated) key first
34 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
35 {AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT},
36 {AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN},
37 {AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT},
38 {AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP},
39 {AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT,
40 AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT},
41 {AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP,
42 AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN},
43 {AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT,
44 AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT},
45 {AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN,
46 AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP},
47 };
48
Michael Wrighta9cf4192022-12-01 23:46:39 +000049 if (orientation != ui::ROTATION_0) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000050 for (const auto& rotation : KEYCODE_ROTATION_MAP) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000051 if (rotation[static_cast<size_t>(ui::ROTATION_0)] == keyCode) {
52 return rotation[static_cast<size_t>(orientation)];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 }
54 }
55 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +000056 return keyCode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057}
58
Prabir Pradhane1a41a82022-10-14 18:06:50 +000059static bool isSupportedScanCode(int32_t scanCode) {
60 // KeyboardInputMapper handles keys from keyboards, gamepads, and styluses.
61 return scanCode < BTN_MOUSE || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI) ||
62 scanCode == BTN_STYLUS || scanCode == BTN_STYLUS2 || scanCode == BTN_STYLUS3 ||
63 scanCode >= BTN_WHEEL;
Prabir Pradhan2197cb42022-10-11 02:56:14 +000064}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065
Vaibhav Devmurari2681a812024-01-11 00:15:35 +000066static bool isMediaKey(int32_t keyCode) {
67 switch (keyCode) {
68 case AKEYCODE_MEDIA_PLAY:
69 case AKEYCODE_MEDIA_PAUSE:
70 case AKEYCODE_MEDIA_PLAY_PAUSE:
71 case AKEYCODE_MUTE:
72 case AKEYCODE_HEADSETHOOK:
73 case AKEYCODE_MEDIA_STOP:
74 case AKEYCODE_MEDIA_NEXT:
75 case AKEYCODE_MEDIA_PREVIOUS:
76 case AKEYCODE_MEDIA_REWIND:
77 case AKEYCODE_MEDIA_RECORD:
78 case AKEYCODE_MEDIA_FAST_FORWARD:
79 case AKEYCODE_MEDIA_SKIP_FORWARD:
80 case AKEYCODE_MEDIA_SKIP_BACKWARD:
81 case AKEYCODE_MEDIA_STEP_FORWARD:
82 case AKEYCODE_MEDIA_STEP_BACKWARD:
83 case AKEYCODE_MEDIA_AUDIO_TRACK:
84 case AKEYCODE_VOLUME_UP:
85 case AKEYCODE_VOLUME_DOWN:
86 case AKEYCODE_VOLUME_MUTE:
87 case AKEYCODE_TV_AUDIO_DESCRIPTION:
88 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP:
89 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN:
90 return true;
91 default:
92 return false;
93 }
94}
95
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096// --- KeyboardInputMapper ---
97
Arpit Singh8e6fb252023-04-06 11:49:17 +000098KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext,
99 const InputReaderConfiguration& readerConfig,
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000100 uint32_t source)
101 : InputMapper(deviceContext, readerConfig), mSource(source) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102
Philip Junker4af3b3d2021-12-14 10:36:55 +0100103uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104 return mSource;
105}
106
Michael Wrighta9cf4192022-12-01 23:46:39 +0000107ui::Rotation KeyboardInputMapper::getOrientation() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108 if (mViewport) {
109 return mViewport->orientation;
110 }
Michael Wrighta9cf4192022-12-01 23:46:39 +0000111 return ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112}
113
Linnan Li13bf76a2024-05-05 19:18:02 +0800114ui::LogicalDisplayId KeyboardInputMapper::getDisplayId() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700115 if (mViewport) {
116 return mViewport->displayId;
117 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700118 return ui::LogicalDisplayId::INVALID;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119}
120
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000121std::optional<KeyboardLayoutInfo> KeyboardInputMapper::getKeyboardLayoutInfo() const {
122 if (mKeyboardLayoutInfo) {
123 return mKeyboardLayoutInfo;
124 }
125 std::optional<RawLayoutInfo> layoutInfo = getDeviceContext().getRawLayoutInfo();
126 if (!layoutInfo) {
127 return std::nullopt;
128 }
129 return KeyboardLayoutInfo(layoutInfo->languageTag, layoutInfo->layoutType);
130}
131
Harry Cuttsd02ea102023-03-17 18:21:30 +0000132void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133 InputMapper::populateDeviceInfo(info);
134
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 info.setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Zixuan Qufecb6062022-11-12 04:44:31 +0000136
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000137 std::optional keyboardLayoutInfo = getKeyboardLayoutInfo();
138 if (keyboardLayoutInfo) {
139 info.setKeyboardLayoutInfo(*keyboardLayoutInfo);
Zixuan Qufecb6062022-11-12 04:44:31 +0000140 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141}
142
143void KeyboardInputMapper::dump(std::string& dump) {
144 dump += INDENT2 "Keyboard Input Mapper:\n";
145 dumpParameters(dump);
Harry Cutts91e7fa22024-02-09 12:50:07 +0000146 dump += StringPrintf(INDENT3 "Orientation: %s\n", ftl::enum_string(getOrientation()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147 dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
148 dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState);
Zixuan Qufecb6062022-11-12 04:44:31 +0000149 dump += INDENT3 "KeyboardLayoutInfo: ";
150 if (mKeyboardLayoutInfo) {
151 dump += mKeyboardLayoutInfo->languageTag + ", " + mKeyboardLayoutInfo->layoutType + "\n";
152 } else {
153 dump += "<not set>\n";
154 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700155}
156
157std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
Arpit Singhed6c3de2023-04-05 19:24:37 +0000158 const InputReaderConfiguration& readerConfig) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700159 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800160 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700161 }
162
163 // No associated display defined, try to find default display if orientationAware.
164 if (mParameters.orientationAware) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000165 return readerConfig.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700166 }
167
168 return std::nullopt;
169}
170
Arpit Singh4be4eef2023-03-28 14:26:01 +0000171std::list<NotifyArgs> KeyboardInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000172 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000173 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +0000174 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700175
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000176 if (!changes.any()) { // first time only
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700177 // Configure basic parameters.
178 configureParameters();
179 }
180
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000181 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000182 mViewport = findViewport(config);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700183 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000184
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000185 if (!changes.any() ||
186 changes.test(InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION)) {
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +0000187 std::optional<KeyboardLayoutInfo> newKeyboardLayoutInfo =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000188 getValueByKey(config.keyboardLayoutAssociations, getDeviceContext().getLocation());
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +0000189 if (mKeyboardLayoutInfo != newKeyboardLayoutInfo) {
190 mKeyboardLayoutInfo = newKeyboardLayoutInfo;
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000191 // Also update keyboard layout overlay as soon as we find the new layout info
192 updateKeyboardLayoutOverlay();
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +0000193 bumpGeneration();
194 }
Zixuan Qufecb6062022-11-12 04:44:31 +0000195 }
196
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000197 if (!changes.any() || changes.test(InputReaderConfiguration::Change::KEYBOARD_LAYOUTS)) {
198 if (!getDeviceContext().getDeviceClasses().test(InputDeviceClass::VIRTUAL) &&
199 updateKeyboardLayoutOverlay()) {
200 bumpGeneration();
201 }
202 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700203 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700204}
205
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000206bool KeyboardInputMapper::updateKeyboardLayoutOverlay() {
207 std::shared_ptr<KeyCharacterMap> keyboardLayout =
208 getDeviceContext()
209 .getContext()
210 ->getPolicy()
211 ->getKeyboardLayoutOverlay(getDeviceContext().getDeviceIdentifier(),
212 getKeyboardLayoutInfo());
213 return getDeviceContext().setKeyboardLayoutOverlay(keyboardLayout);
214}
215
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700216void KeyboardInputMapper::configureParameters() {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800217 const PropertyMap& config = getDeviceContext().getConfiguration();
Harry Cuttsf13161a2023-03-08 14:15:49 +0000218 mParameters.orientationAware = config.getBool("keyboard.orientationAware").value_or(false);
219 mParameters.handlesKeyRepeat = config.getBool("keyboard.handlesKeyRepeat").value_or(false);
220 mParameters.doNotWakeByDefault = config.getBool("keyboard.doNotWakeByDefault").value_or(false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221}
222
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000223void KeyboardInputMapper::dumpParameters(std::string& dump) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 dump += INDENT3 "Parameters:\n";
225 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
226 dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
227}
228
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700229std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
230 std::list<NotifyArgs> out = cancelAllDownKeys(when);
Prabir Pradhan84395e62022-10-26 19:52:00 +0000231 mHidUsageAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700232
233 resetLedState();
234
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700235 out += InputMapper::reset(when);
236 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700237}
238
Harry Cuttsa32a1192024-06-04 15:10:31 +0000239std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent& rawEvent) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700240 std::list<NotifyArgs> out;
Harry Cuttsa32a1192024-06-04 15:10:31 +0000241 mHidUsageAccumulator.process(rawEvent);
242 switch (rawEvent.type) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700243 case EV_KEY: {
Harry Cuttsa32a1192024-06-04 15:10:31 +0000244 int32_t scanCode = rawEvent.code;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700245
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000246 if (isSupportedScanCode(scanCode)) {
Harry Cuttsa32a1192024-06-04 15:10:31 +0000247 out += processKey(rawEvent.when, rawEvent.readTime, rawEvent.value != 0,
Prabir Pradhan84395e62022-10-26 19:52:00 +0000248 scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 }
250 break;
251 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700253 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700254}
255
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700256std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
257 int32_t scanCode, int32_t usageCode) {
258 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700259 int32_t keyCode;
260 int32_t keyMetaState;
261 uint32_t policyFlags;
Justin Chung71ddb432023-03-27 04:29:07 +0000262 int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800264 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
265 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266 keyCode = AKEYCODE_UNKNOWN;
267 keyMetaState = mMetaState;
268 policyFlags = 0;
269 }
270
Arthur Hung2141d542022-08-23 07:45:21 +0000271 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000272 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700273 if (down) {
274 // Rotate key codes according to orientation if needed.
275 if (mParameters.orientationAware) {
276 keyCode = rotateKeyCode(keyCode, getOrientation());
277 }
278
279 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000280 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700281 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000282 keyCode = mKeyDowns[*keyDownIndex].keyCode;
283 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000284 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285 } else {
286 // key down
287 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800288 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700290 }
291 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700292 out += getDeviceContext().cancelTouch(when, readTime);
Justin Chung71ddb432023-03-27 04:29:07 +0000293 flags |= AKEY_EVENT_FLAG_KEEP_TOUCH_MODE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700294 }
295
296 KeyDown keyDown;
297 keyDown.keyCode = keyCode;
298 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000299 keyDown.downTime = when;
Justin Chung71ddb432023-03-27 04:29:07 +0000300 keyDown.flags = flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700301 mKeyDowns.push_back(keyDown);
302 }
Arpit Singh82e413e2023-10-10 19:30:58 +0000303 onKeyDownProcessed(downTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700304 } else {
305 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000306 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700307 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000308 keyCode = mKeyDowns[*keyDownIndex].keyCode;
309 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000310 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000311 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700312 } else {
313 // key was not actually down
314 ALOGI("Dropping key up from device %s because the key was not down. "
315 "keyCode=%d, scanCode=%d",
316 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700317 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318 }
319 }
320
321 if (updateMetaStateIfNeeded(keyCode, down)) {
322 // If global meta state changed send it along with the key.
323 // If it has not changed then we'll use what keymap gave us,
324 // since key replacement logic might temporarily reset a few
325 // meta bits for given key.
326 keyMetaState = mMetaState;
327 }
328
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000329 DeviceId deviceId = getDeviceId();
330
331 // On first down: Process key for keyboard classification (will send reconfiguration if the
332 // keyboard type change)
333 if (down && !keyDownIndex) {
334 KeyboardClassifier& classifier = getDeviceContext().getContext()->getKeyboardClassifier();
335 classifier.processKey(deviceId, scanCode, keyMetaState);
336 getDeviceContext().setKeyboardType(classifier.getKeyboardType(deviceId));
337 }
338
339 KeyboardType keyboardType = getDeviceContext().getKeyboardType();
Vaibhav Devmurari16257862023-03-06 10:06:32 +0000340 // Any key down on an external keyboard should wake the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700342 // For internal keyboards and devices for which the default wake behavior is explicitly
343 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
344 // wake key individually.
Vaibhav Devmurari2681a812024-01-11 00:15:35 +0000345 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000346 !(keyboardType != KeyboardType::ALPHABETIC && isMediaKey(keyCode))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347 policyFlags |= POLICY_FLAG_WAKE;
348 }
349
350 if (mParameters.handlesKeyRepeat) {
351 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
352 }
353
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000354 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, deviceId, mSource,
355 getDisplayId(), policyFlags,
Justin Chung71ddb432023-03-27 04:29:07 +0000356 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, flags,
357 keyCode, scanCode, keyMetaState, downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700358 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700359}
360
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000361std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362 size_t n = mKeyDowns.size();
363 for (size_t i = 0; i < n; i++) {
364 if (mKeyDowns[i].scanCode == scanCode) {
365 return i;
366 }
367 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000368 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700369}
370
371int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800372 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373}
374
375int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800376 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700377}
378
Philip Junker4af3b3d2021-12-14 10:36:55 +0100379int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
380 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
381}
382
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700383bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
384 const std::vector<int32_t>& keyCodes,
385 uint8_t* outFlags) {
386 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700387}
388
389int32_t KeyboardInputMapper::getMetaState() {
390 return mMetaState;
391}
392
Arthur Hungcb40a002021-08-03 14:31:01 +0000393bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
394 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
395 return false;
396 }
397
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000399 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700400}
401
402bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
403 int32_t oldMetaState = mMetaState;
404 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800405 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700406 if (metaStateChanged) {
407 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800408 constexpr int32_t allLedMetaState =
409 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
410 if ((metaStateChanged & allLedMetaState) != 0) {
411 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
412 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700413 getContext()->updateGlobalMetaState();
414 }
415
416 return metaStateChanged;
417}
418
419void KeyboardInputMapper::resetLedState() {
420 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
421 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
422 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
423
424 updateLedState(true);
425}
426
427void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800428 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 ledState.on = false;
430}
431
432void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000433 // Clear the local led state then union the global led state.
434 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800435 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700436
437 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700438 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
439 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700440 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};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700443 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700444 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
445 // meta state.
446 if (hasKeyLayout) {
447 for (int i = 0; i < META_NUM; i++) {
448 if (!flags[i]) {
449 mMetaState &= ~metaCodes[i];
450 }
451 }
452 }
453
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700454 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
455 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
456 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
457}
458
459void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
460 int32_t modifier, bool reset) {
461 if (ledState.avail) {
462 bool desiredState = (mMetaState & modifier) != 0;
463 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800464 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700465 ledState.on = desiredState;
466 }
467 }
468}
469
Linnan Li13bf76a2024-05-05 19:18:02 +0800470std::optional<ui::LogicalDisplayId> KeyboardInputMapper::getAssociatedDisplayId() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700471 if (mViewport) {
472 return std::make_optional(mViewport->displayId);
473 }
474 return std::nullopt;
475}
476
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700477std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
478 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000479 size_t n = mKeyDowns.size();
480 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000481 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
482 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
Harry Cutts33476232023-01-30 19:57:29 +0000483 getDisplayId(), /*policyFlags=*/0, AKEY_EVENT_ACTION_UP,
Justin Chung71ddb432023-03-27 04:29:07 +0000484 mKeyDowns[i].flags | AKEY_EVENT_FLAG_CANCELED,
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000485 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
486 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000487 }
488 mKeyDowns.clear();
489 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700490 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000491}
492
Arpit Singh82e413e2023-10-10 19:30:58 +0000493void KeyboardInputMapper::onKeyDownProcessed(nsecs_t downTime) {
Arpit Singha5ea7c12023-07-05 15:39:25 +0000494 InputReaderContext& context = *getContext();
Arpit Singh82e413e2023-10-10 19:30:58 +0000495 context.setLastKeyDownTimestamp(downTime);
Arpit Singha5ea7c12023-07-05 15:39:25 +0000496 // Ignore meta keys or multiple simultaneous down keys as they are likely to be keyboard
497 // shortcuts
498 bool shouldHideCursor = mKeyDowns.size() == 1 && !isMetaKey(mKeyDowns[0].keyCode);
499 if (shouldHideCursor && context.getPolicy()->isInputMethodConnectionActive()) {
Arpit Singha5ea7c12023-07-05 15:39:25 +0000500 context.setPreventingTouchpadTaps(true);
Arpit Singhb3b3f732023-07-04 14:30:05 +0000501 }
502}
503
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700504} // namespace android