blob: 852ed08699c8749d17facfefc6a8bf412a1615f0 [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)
Prabir Pradhan38636652024-07-23 21:59:36 +0000101 : InputMapper(deviceContext, readerConfig), mMapperSource(source) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102
Philip Junker4af3b3d2021-12-14 10:36:55 +0100103uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhan38636652024-07-23 21:59:36 +0000104 return mMapperSource;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105}
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: {
David Padlipsky48fd8842024-10-18 03:36:58 +0000244 // Skip processing repeated keys (value == 2) since auto repeat is handled by Android
245 // internally.
246 if (rawEvent.value == 2) {
247 break;
248 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249
David Padlipsky48fd8842024-10-18 03:36:58 +0000250 const int32_t scanCode = rawEvent.code;
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000251 if (isSupportedScanCode(scanCode)) {
David Padlipsky48fd8842024-10-18 03:36:58 +0000252 out += processKey(rawEvent.when, rawEvent.readTime, rawEvent.value != 0, scanCode,
253 mHidUsageAccumulator.consumeCurrentHidUsage());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700254 }
255 break;
256 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700258 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700259}
260
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700261std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
262 int32_t scanCode, int32_t usageCode) {
263 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 int32_t keyCode;
265 int32_t keyMetaState;
266 uint32_t policyFlags;
Justin Chung71ddb432023-03-27 04:29:07 +0000267 int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800269 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
270 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 keyCode = AKEYCODE_UNKNOWN;
272 keyMetaState = mMetaState;
273 policyFlags = 0;
274 }
275
Arthur Hung2141d542022-08-23 07:45:21 +0000276 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000277 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700278 if (down) {
279 // Rotate key codes according to orientation if needed.
280 if (mParameters.orientationAware) {
281 keyCode = rotateKeyCode(keyCode, getOrientation());
282 }
283
284 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000285 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700286 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000287 keyCode = mKeyDowns[*keyDownIndex].keyCode;
288 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000289 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700290 } else {
291 // key down
292 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800293 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295 }
296 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700297 out += getDeviceContext().cancelTouch(when, readTime);
Justin Chung71ddb432023-03-27 04:29:07 +0000298 flags |= AKEY_EVENT_FLAG_KEEP_TOUCH_MODE;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299 }
300
301 KeyDown keyDown;
302 keyDown.keyCode = keyCode;
303 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000304 keyDown.downTime = when;
Justin Chung71ddb432023-03-27 04:29:07 +0000305 keyDown.flags = flags;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 mKeyDowns.push_back(keyDown);
307 }
Arpit Singh82e413e2023-10-10 19:30:58 +0000308 onKeyDownProcessed(downTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700309 } else {
310 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000311 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700312 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000313 keyCode = mKeyDowns[*keyDownIndex].keyCode;
314 downTime = mKeyDowns[*keyDownIndex].downTime;
Justin Chung71ddb432023-03-27 04:29:07 +0000315 flags = mKeyDowns[*keyDownIndex].flags;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000316 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700317 } else {
318 // key was not actually down
319 ALOGI("Dropping key up from device %s because the key was not down. "
320 "keyCode=%d, scanCode=%d",
321 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700322 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323 }
324 }
325
326 if (updateMetaStateIfNeeded(keyCode, down)) {
327 // If global meta state changed send it along with the key.
328 // If it has not changed then we'll use what keymap gave us,
329 // since key replacement logic might temporarily reset a few
330 // meta bits for given key.
331 keyMetaState = mMetaState;
332 }
333
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000334 DeviceId deviceId = getDeviceId();
335
336 // On first down: Process key for keyboard classification (will send reconfiguration if the
337 // keyboard type change)
338 if (down && !keyDownIndex) {
339 KeyboardClassifier& classifier = getDeviceContext().getContext()->getKeyboardClassifier();
340 classifier.processKey(deviceId, scanCode, keyMetaState);
341 getDeviceContext().setKeyboardType(classifier.getKeyboardType(deviceId));
342 }
343
344 KeyboardType keyboardType = getDeviceContext().getKeyboardType();
Vaibhav Devmurari16257862023-03-06 10:06:32 +0000345 // Any key down on an external keyboard should wake the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700347 // For internal keyboards and devices for which the default wake behavior is explicitly
348 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
349 // wake key individually.
Vaibhav Devmurari2681a812024-01-11 00:15:35 +0000350 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000351 !(keyboardType != KeyboardType::ALPHABETIC && isMediaKey(keyCode))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700352 policyFlags |= POLICY_FLAG_WAKE;
353 }
354
355 if (mParameters.handlesKeyRepeat) {
356 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
357 }
358
Prabir Pradhan38636652024-07-23 21:59:36 +0000359 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, deviceId,
360 getEventSource(), getDisplayId(), policyFlags,
Justin Chung71ddb432023-03-27 04:29:07 +0000361 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, flags,
362 keyCode, scanCode, keyMetaState, downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700363 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700364}
365
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000366std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700367 size_t n = mKeyDowns.size();
368 for (size_t i = 0; i < n; i++) {
369 if (mKeyDowns[i].scanCode == scanCode) {
370 return i;
371 }
372 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000373 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374}
375
376int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800377 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700378}
379
380int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800381 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700382}
383
Philip Junker4af3b3d2021-12-14 10:36:55 +0100384int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
385 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
386}
387
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700388bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
389 const std::vector<int32_t>& keyCodes,
390 uint8_t* outFlags) {
391 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392}
393
394int32_t KeyboardInputMapper::getMetaState() {
395 return mMetaState;
396}
397
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
399 int32_t oldMetaState = mMetaState;
400 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800401 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402 if (metaStateChanged) {
403 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800404 constexpr int32_t allLedMetaState =
405 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
406 if ((metaStateChanged & allLedMetaState) != 0) {
407 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
408 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 getContext()->updateGlobalMetaState();
410 }
411
412 return metaStateChanged;
413}
414
415void KeyboardInputMapper::resetLedState() {
416 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
417 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
418 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
419
420 updateLedState(true);
421}
422
423void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800424 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 ledState.on = false;
426}
427
428void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000429 // Clear the local led state then union the global led state.
430 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800431 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700432
Vaibhav Devmurari5c4f7982024-10-07 09:41:23 +0000433 std::vector<int32_t> keyCodesToCheck{AKEYCODE_NUM_LOCK, AKEYCODE_SCROLL_LOCK};
434 std::vector<int32_t> metaCodes{AMETA_NUM_LOCK_ON, AMETA_SCROLL_LOCK_ON};
435 // Check for physical CapsLock key only for non-alphabetic keyboards. For Alphabetic
436 // keyboards, we will allow Caps Lock even if there is no physical CapsLock key.
437 if (getDeviceContext().getKeyboardType() != KeyboardType::ALPHABETIC) {
438 keyCodesToCheck.push_back(AKEYCODE_CAPS_LOCK);
439 metaCodes.push_back(AMETA_CAPS_LOCK_ON);
440 }
441 size_t size = keyCodesToCheck.size();
442 std::vector<uint8_t> flags(size, 0);
443 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodesToCheck, 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) {
Vaibhav Devmurari5c4f7982024-10-07 09:41:23 +0000447 for (size_t i = 0; i < size; i++) {
Chris Yea52ade12020-08-27 16:49:20 -0700448 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 Pradhan38636652024-07-23 21:59:36 +0000481 out.emplace_back(
482 NotifyKeyArgs(getContext()->getNextId(), when, systemTime(SYSTEM_TIME_MONOTONIC),
483 getDeviceId(), getEventSource(), getDisplayId(), /*policyFlags=*/0,
484 AKEY_EVENT_ACTION_UP, mKeyDowns[i].flags | AKEY_EVENT_FLAG_CANCELED,
485 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 Singhb3b3f732023-07-04 14:30:05 +0000496}
497
Prabir Pradhan38636652024-07-23 21:59:36 +0000498uint32_t KeyboardInputMapper::getEventSource() const {
499 // For all input events generated by this mapper, use the source that's shared across all
500 // KeyboardInputMappers for this device in case there are more than one.
501 static constexpr auto ALL_KEYBOARD_SOURCES =
502 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD;
503 const auto deviceSources = getDeviceContext().getDeviceSources();
504 LOG_ALWAYS_FATAL_IF((deviceSources & mMapperSource) != mMapperSource);
505 return deviceSources & ALL_KEYBOARD_SOURCES;
506}
507
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700508} // namespace android