blob: da9413e4ca6839ebc35604f3e62f08f0ec5e150f [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
23namespace android {
24
25// --- Static Definitions ---
26
Prabir Pradhan2197cb42022-10-11 02:56:14 +000027static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
28 static constexpr int32_t KEYCODE_ROTATION_MAP[][4] = {
29 // key codes enumerated counter-clockwise with the original (unrotated) key first
30 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
31 {AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT},
32 {AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN},
33 {AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT},
34 {AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP},
35 {AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT,
36 AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT},
37 {AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP,
38 AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN},
39 {AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT,
40 AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT},
41 {AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN,
42 AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP},
43 };
44
45 LOG_ALWAYS_FATAL_IF(orientation < 0 || orientation > 3, "Invalid orientation: %d", orientation);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 if (orientation != DISPLAY_ORIENTATION_0) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000047 for (const auto& rotation : KEYCODE_ROTATION_MAP) {
48 if (rotation[DISPLAY_ORIENTATION_0] == keyCode) {
49 return rotation[orientation];
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 }
51 }
52 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +000053 return keyCode;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054}
55
Prabir Pradhane1a41a82022-10-14 18:06:50 +000056static bool isSupportedScanCode(int32_t scanCode) {
57 // KeyboardInputMapper handles keys from keyboards, gamepads, and styluses.
58 return scanCode < BTN_MOUSE || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI) ||
59 scanCode == BTN_STYLUS || scanCode == BTN_STYLUS2 || scanCode == BTN_STYLUS3 ||
60 scanCode >= BTN_WHEEL;
Prabir Pradhan2197cb42022-10-11 02:56:14 +000061}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062
Prabir Pradhan2197cb42022-10-11 02:56:14 +000063static bool isMediaKey(int32_t keyCode) {
64 switch (keyCode) {
65 case AKEYCODE_MEDIA_PLAY:
66 case AKEYCODE_MEDIA_PAUSE:
67 case AKEYCODE_MEDIA_PLAY_PAUSE:
68 case AKEYCODE_MUTE:
69 case AKEYCODE_HEADSETHOOK:
70 case AKEYCODE_MEDIA_STOP:
71 case AKEYCODE_MEDIA_NEXT:
72 case AKEYCODE_MEDIA_PREVIOUS:
73 case AKEYCODE_MEDIA_REWIND:
74 case AKEYCODE_MEDIA_RECORD:
75 case AKEYCODE_MEDIA_FAST_FORWARD:
76 case AKEYCODE_MEDIA_SKIP_FORWARD:
77 case AKEYCODE_MEDIA_SKIP_BACKWARD:
78 case AKEYCODE_MEDIA_STEP_FORWARD:
79 case AKEYCODE_MEDIA_STEP_BACKWARD:
80 case AKEYCODE_MEDIA_AUDIO_TRACK:
81 case AKEYCODE_VOLUME_UP:
82 case AKEYCODE_VOLUME_DOWN:
83 case AKEYCODE_VOLUME_MUTE:
84 case AKEYCODE_TV_AUDIO_DESCRIPTION:
85 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP:
86 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN:
87 return true;
88 default:
89 return false;
90 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091}
92
93// --- KeyboardInputMapper ---
94
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080095KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source,
96 int32_t keyboardType)
97 : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
Philip Junker4af3b3d2021-12-14 10:36:55 +010099uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100 return mSource;
101}
102
103int32_t KeyboardInputMapper::getOrientation() {
104 if (mViewport) {
105 return mViewport->orientation;
106 }
107 return DISPLAY_ORIENTATION_0;
108}
109
110int32_t KeyboardInputMapper::getDisplayId() {
111 if (mViewport) {
112 return mViewport->displayId;
113 }
114 return ADISPLAY_ID_NONE;
115}
116
117void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
118 InputMapper::populateDeviceInfo(info);
119
120 info->setKeyboardType(mKeyboardType);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800121 info->setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122}
123
124void KeyboardInputMapper::dump(std::string& dump) {
125 dump += INDENT2 "Keyboard Input Mapper:\n";
126 dumpParameters(dump);
127 dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType);
128 dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation());
129 dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
130 dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131}
132
133std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000134 const InputReaderConfiguration* config) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700135 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800136 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137 }
138
139 // No associated display defined, try to find default display if orientationAware.
140 if (mParameters.orientationAware) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100141 return config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142 }
143
144 return std::nullopt;
145}
146
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700147std::list<NotifyArgs> KeyboardInputMapper::configure(nsecs_t when,
148 const InputReaderConfiguration* config,
149 uint32_t changes) {
150 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700151
152 if (!changes) { // first time only
153 // Configure basic parameters.
154 configureParameters();
155 }
156
157 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000158 mViewport = findViewport(config);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700160 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700161}
162
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700163void KeyboardInputMapper::configureParameters() {
164 mParameters.orientationAware = false;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800165 const PropertyMap& config = getDeviceContext().getConfiguration();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700166 config.tryGetProperty("keyboard.orientationAware", mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700167
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700168 mParameters.handlesKeyRepeat = false;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700169 config.tryGetProperty("keyboard.handlesKeyRepeat", mParameters.handlesKeyRepeat);
Powei Fengd041c5d2019-05-03 17:11:33 -0700170
171 mParameters.doNotWakeByDefault = false;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700172 config.tryGetProperty("keyboard.doNotWakeByDefault", mParameters.doNotWakeByDefault);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173}
174
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000175void KeyboardInputMapper::dumpParameters(std::string& dump) const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700176 dump += INDENT3 "Parameters:\n";
177 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
178 dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
179}
180
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700181std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
182 std::list<NotifyArgs> out = cancelAllDownKeys(when);
Prabir Pradhan84395e62022-10-26 19:52:00 +0000183 mHidUsageAccumulator.reset();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700184
185 resetLedState();
186
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700187 out += InputMapper::reset(when);
188 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700189}
190
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700191std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
192 std::list<NotifyArgs> out;
Prabir Pradhan84395e62022-10-26 19:52:00 +0000193 mHidUsageAccumulator.process(*rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700194 switch (rawEvent->type) {
195 case EV_KEY: {
196 int32_t scanCode = rawEvent->code;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700197
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000198 if (isSupportedScanCode(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700199 out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
Prabir Pradhan84395e62022-10-26 19:52:00 +0000200 scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700201 }
202 break;
203 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700204 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700205 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700206}
207
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700208std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
209 int32_t scanCode, int32_t usageCode) {
210 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700211 int32_t keyCode;
212 int32_t keyMetaState;
213 uint32_t policyFlags;
214
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800215 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
216 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700217 keyCode = AKEYCODE_UNKNOWN;
218 keyMetaState = mMetaState;
219 policyFlags = 0;
220 }
221
Arthur Hung2141d542022-08-23 07:45:21 +0000222 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000223 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 if (down) {
225 // Rotate key codes according to orientation if needed.
226 if (mParameters.orientationAware) {
227 keyCode = rotateKeyCode(keyCode, getOrientation());
228 }
229
230 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000231 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700232 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000233 keyCode = mKeyDowns[*keyDownIndex].keyCode;
234 downTime = mKeyDowns[*keyDownIndex].downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700235 } else {
236 // key down
237 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800238 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700239 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700240 }
241 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700242 out += getDeviceContext().cancelTouch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700243 }
244
245 KeyDown keyDown;
246 keyDown.keyCode = keyCode;
247 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000248 keyDown.downTime = when;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 mKeyDowns.push_back(keyDown);
250 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700251 } else {
252 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000253 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700254 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000255 keyCode = mKeyDowns[*keyDownIndex].keyCode;
256 downTime = mKeyDowns[*keyDownIndex].downTime;
257 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700258 } else {
259 // key was not actually down
260 ALOGI("Dropping key up from device %s because the key was not down. "
261 "keyCode=%d, scanCode=%d",
262 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700263 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 }
265 }
266
267 if (updateMetaStateIfNeeded(keyCode, down)) {
268 // If global meta state changed send it along with the key.
269 // If it has not changed then we'll use what keymap gave us,
270 // since key replacement logic might temporarily reset a few
271 // meta bits for given key.
272 keyMetaState = mMetaState;
273 }
274
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700275 // Key down on external an keyboard should wake the device.
276 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700277 // For internal keyboards and devices for which the default wake behavior is explicitly
278 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
279 // wake key individually.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 // TODO: Use the input device configuration to control this behavior more finely.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800281 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
Powei Fengd041c5d2019-05-03 17:11:33 -0700282 !isMediaKey(keyCode)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700283 policyFlags |= POLICY_FLAG_WAKE;
284 }
285
286 if (mParameters.handlesKeyRepeat) {
287 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
288 }
289
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000290 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
291 mSource, getDisplayId(), policyFlags,
292 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
293 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState,
294 downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700295 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700296}
297
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000298std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299 size_t n = mKeyDowns.size();
300 for (size_t i = 0; i < n; i++) {
301 if (mKeyDowns[i].scanCode == scanCode) {
302 return i;
303 }
304 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000305 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306}
307
308int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800309 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700310}
311
312int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800313 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314}
315
Philip Junker4af3b3d2021-12-14 10:36:55 +0100316int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
317 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
318}
319
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
321 const std::vector<int32_t>& keyCodes,
322 uint8_t* outFlags) {
323 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700324}
325
326int32_t KeyboardInputMapper::getMetaState() {
327 return mMetaState;
328}
329
Arthur Hungcb40a002021-08-03 14:31:01 +0000330bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
331 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
332 return false;
333 }
334
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700335 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000336 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337}
338
339bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
340 int32_t oldMetaState = mMetaState;
341 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800342 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343 if (metaStateChanged) {
344 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800345 constexpr int32_t allLedMetaState =
346 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
347 if ((metaStateChanged & allLedMetaState) != 0) {
348 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
349 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700350 getContext()->updateGlobalMetaState();
351 }
352
353 return metaStateChanged;
354}
355
356void KeyboardInputMapper::resetLedState() {
357 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
358 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
359 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
360
361 updateLedState(true);
362}
363
364void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800365 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700366 ledState.on = false;
367}
368
369void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000370 // Clear the local led state then union the global led state.
371 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800372 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700373
374 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700375 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
376 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700377 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
378 AMETA_SCROLL_LOCK_ON};
379 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700380 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700381 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
382 // meta state.
383 if (hasKeyLayout) {
384 for (int i = 0; i < META_NUM; i++) {
385 if (!flags[i]) {
386 mMetaState &= ~metaCodes[i];
387 }
388 }
389 }
390
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700391 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
392 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
393 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
394}
395
396void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
397 int32_t modifier, bool reset) {
398 if (ledState.avail) {
399 bool desiredState = (mMetaState & modifier) != 0;
400 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800401 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402 ledState.on = desiredState;
403 }
404 }
405}
406
407std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
408 if (mViewport) {
409 return std::make_optional(mViewport->displayId);
410 }
411 return std::nullopt;
412}
413
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700414std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
415 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000416 size_t n = mKeyDowns.size();
417 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000418 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
419 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
420 getDisplayId(), 0 /*policyFlags*/, AKEY_EVENT_ACTION_UP,
421 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED,
422 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
423 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000424 }
425 mKeyDowns.clear();
426 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700427 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000428}
429
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430} // namespace android