blob: 9e7e9568669c726b2d5e70c4750435a2d7f74aee [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
Vaibhav Devmurari2681a812024-01-11 00:15:35 +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 }
92}
93
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094// --- KeyboardInputMapper ---
95
Arpit Singh8e6fb252023-04-06 11:49:17 +000096KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext,
97 const InputReaderConfiguration& readerConfig,
98 uint32_t source, int32_t keyboardType)
99 : InputMapper(deviceContext, readerConfig), mSource(source), mKeyboardType(keyboardType) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100
Philip Junker4af3b3d2021-12-14 10:36:55 +0100101uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 return mSource;
103}
104
Michael Wrighta9cf4192022-12-01 23:46:39 +0000105ui::Rotation KeyboardInputMapper::getOrientation() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 if (mViewport) {
107 return mViewport->orientation;
108 }
Michael Wrighta9cf4192022-12-01 23:46:39 +0000109 return ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110}
111
112int32_t KeyboardInputMapper::getDisplayId() {
113 if (mViewport) {
114 return mViewport->displayId;
115 }
116 return ADISPLAY_ID_NONE;
117}
118
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000119std::optional<KeyboardLayoutInfo> KeyboardInputMapper::getKeyboardLayoutInfo() const {
120 if (mKeyboardLayoutInfo) {
121 return mKeyboardLayoutInfo;
122 }
123 std::optional<RawLayoutInfo> layoutInfo = getDeviceContext().getRawLayoutInfo();
124 if (!layoutInfo) {
125 return std::nullopt;
126 }
127 return KeyboardLayoutInfo(layoutInfo->languageTag, layoutInfo->layoutType);
128}
129
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131 InputMapper::populateDeviceInfo(info);
132
Harry Cuttsd02ea102023-03-17 18:21:30 +0000133 info.setKeyboardType(mKeyboardType);
134 info.setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Zixuan Qufecb6062022-11-12 04:44:31 +0000135
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000136 std::optional keyboardLayoutInfo = getKeyboardLayoutInfo();
137 if (keyboardLayoutInfo) {
138 info.setKeyboardLayoutInfo(*keyboardLayoutInfo);
Zixuan Qufecb6062022-11-12 04:44:31 +0000139 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140}
141
142void KeyboardInputMapper::dump(std::string& dump) {
143 dump += INDENT2 "Keyboard Input Mapper:\n";
144 dumpParameters(dump);
145 dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType);
146 dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation());
147 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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700239std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
240 std::list<NotifyArgs> out;
Prabir Pradhan84395e62022-10-26 19:52:00 +0000241 mHidUsageAccumulator.process(*rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700242 switch (rawEvent->type) {
243 case EV_KEY: {
244 int32_t scanCode = rawEvent->code;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700245
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000246 if (isSupportedScanCode(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700247 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 Devmurari16257862023-03-06 10:06:32 +0000329 // Any key down on an external keyboard should wake the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700331 // For internal keyboards and devices for which the default wake behavior is explicitly
332 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
333 // wake key individually.
Vaibhav Devmurari2681a812024-01-11 00:15:35 +0000334 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
335 !(mKeyboardType != AINPUT_KEYBOARD_TYPE_ALPHABETIC && isMediaKey(keyCode))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 policyFlags |= POLICY_FLAG_WAKE;
337 }
338
339 if (mParameters.handlesKeyRepeat) {
340 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
341 }
342
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000343 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
344 mSource, getDisplayId(), policyFlags,
Justin Chung71ddb432023-03-27 04:29:07 +0000345 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, flags,
346 keyCode, scanCode, keyMetaState, downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700347 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700348}
349
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000350std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700351 size_t n = mKeyDowns.size();
352 for (size_t i = 0; i < n; i++) {
353 if (mKeyDowns[i].scanCode == scanCode) {
354 return i;
355 }
356 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000357 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700358}
359
360int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800361 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362}
363
364int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800365 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700366}
367
Philip Junker4af3b3d2021-12-14 10:36:55 +0100368int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
369 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
370}
371
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700372bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
373 const std::vector<int32_t>& keyCodes,
374 uint8_t* outFlags) {
375 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700376}
377
378int32_t KeyboardInputMapper::getMetaState() {
379 return mMetaState;
380}
381
Arthur Hungcb40a002021-08-03 14:31:01 +0000382bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
383 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
384 return false;
385 }
386
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700387 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000388 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389}
390
391bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
392 int32_t oldMetaState = mMetaState;
393 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800394 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395 if (metaStateChanged) {
396 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800397 constexpr int32_t allLedMetaState =
398 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
399 if ((metaStateChanged & allLedMetaState) != 0) {
400 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
401 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402 getContext()->updateGlobalMetaState();
403 }
404
405 return metaStateChanged;
406}
407
408void KeyboardInputMapper::resetLedState() {
409 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
410 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
411 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
412
413 updateLedState(true);
414}
415
416void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800417 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700418 ledState.on = false;
419}
420
421void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000422 // Clear the local led state then union the global led state.
423 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800424 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700425
426 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700427 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
428 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700429 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
430 AMETA_SCROLL_LOCK_ON};
431 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700432 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700433 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
434 // meta state.
435 if (hasKeyLayout) {
436 for (int i = 0; i < META_NUM; i++) {
437 if (!flags[i]) {
438 mMetaState &= ~metaCodes[i];
439 }
440 }
441 }
442
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
444 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
445 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
446}
447
448void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
449 int32_t modifier, bool reset) {
450 if (ledState.avail) {
451 bool desiredState = (mMetaState & modifier) != 0;
452 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800453 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700454 ledState.on = desiredState;
455 }
456 }
457}
458
459std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
460 if (mViewport) {
461 return std::make_optional(mViewport->displayId);
462 }
463 return std::nullopt;
464}
465
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700466std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
467 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000468 size_t n = mKeyDowns.size();
469 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000470 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
471 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
Harry Cutts33476232023-01-30 19:57:29 +0000472 getDisplayId(), /*policyFlags=*/0, AKEY_EVENT_ACTION_UP,
Justin Chung71ddb432023-03-27 04:29:07 +0000473 mKeyDowns[i].flags | AKEY_EVENT_FLAG_CANCELED,
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000474 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
475 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000476 }
477 mKeyDowns.clear();
478 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700479 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000480}
481
Arpit Singh82e413e2023-10-10 19:30:58 +0000482void KeyboardInputMapper::onKeyDownProcessed(nsecs_t downTime) {
Arpit Singha5ea7c12023-07-05 15:39:25 +0000483 InputReaderContext& context = *getContext();
Arpit Singh82e413e2023-10-10 19:30:58 +0000484 context.setLastKeyDownTimestamp(downTime);
Arpit Singha5ea7c12023-07-05 15:39:25 +0000485 if (context.isPreventingTouchpadTaps()) {
Arpit Singhc00f5e92023-09-19 13:11:05 +0000486 // avoid pinging java service unnecessarily, just fade pointer again if it became visible
487 context.fadePointer();
Arpit Singha5ea7c12023-07-05 15:39:25 +0000488 return;
489 }
490 // Ignore meta keys or multiple simultaneous down keys as they are likely to be keyboard
491 // shortcuts
492 bool shouldHideCursor = mKeyDowns.size() == 1 && !isMetaKey(mKeyDowns[0].keyCode);
493 if (shouldHideCursor && context.getPolicy()->isInputMethodConnectionActive()) {
494 context.fadePointer();
495 context.setPreventingTouchpadTaps(true);
Arpit Singhb3b3f732023-07-04 14:30:05 +0000496 }
497}
498
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700499} // namespace android