blob: 33b8a1bab9987129d5a41863807b893e258a8b83 [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 Pradhanbaa5c822019-08-30 15:27:05 -0700183 mCurrentHidUsage = 0;
184
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 Pradhanbaa5c822019-08-30 15:27:05 -0700193 switch (rawEvent->type) {
194 case EV_KEY: {
195 int32_t scanCode = rawEvent->code;
196 int32_t usageCode = mCurrentHidUsage;
197 mCurrentHidUsage = 0;
198
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000199 if (isSupportedScanCode(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700200 out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
201 scanCode, usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700202 }
203 break;
204 }
205 case EV_MSC: {
206 if (rawEvent->code == MSC_SCAN) {
207 mCurrentHidUsage = rawEvent->value;
208 }
209 break;
210 }
211 case EV_SYN: {
212 if (rawEvent->code == SYN_REPORT) {
213 mCurrentHidUsage = 0;
214 }
215 }
216 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700217 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218}
219
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700220std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
221 int32_t scanCode, int32_t usageCode) {
222 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700223 int32_t keyCode;
224 int32_t keyMetaState;
225 uint32_t policyFlags;
226
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800227 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
228 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 keyCode = AKEYCODE_UNKNOWN;
230 keyMetaState = mMetaState;
231 policyFlags = 0;
232 }
233
Arthur Hung2141d542022-08-23 07:45:21 +0000234 nsecs_t downTime = when;
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000235 std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700236 if (down) {
237 // Rotate key codes according to orientation if needed.
238 if (mParameters.orientationAware) {
239 keyCode = rotateKeyCode(keyCode, getOrientation());
240 }
241
242 // Add key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000243 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700244 // key repeat, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000245 keyCode = mKeyDowns[*keyDownIndex].keyCode;
246 downTime = mKeyDowns[*keyDownIndex].downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700247 } else {
248 // key down
249 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800250 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700251 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252 }
253 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700254 out += getDeviceContext().cancelTouch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 }
256
257 KeyDown keyDown;
258 keyDown.keyCode = keyCode;
259 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000260 keyDown.downTime = when;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700261 mKeyDowns.push_back(keyDown);
262 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 } else {
264 // Remove key down.
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000265 if (keyDownIndex) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266 // key up, be sure to use same keycode as before in case of rotation
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000267 keyCode = mKeyDowns[*keyDownIndex].keyCode;
268 downTime = mKeyDowns[*keyDownIndex].downTime;
269 mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270 } else {
271 // key was not actually down
272 ALOGI("Dropping key up from device %s because the key was not down. "
273 "keyCode=%d, scanCode=%d",
274 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700275 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 }
277 }
278
279 if (updateMetaStateIfNeeded(keyCode, down)) {
280 // If global meta state changed send it along with the key.
281 // If it has not changed then we'll use what keymap gave us,
282 // since key replacement logic might temporarily reset a few
283 // meta bits for given key.
284 keyMetaState = mMetaState;
285 }
286
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700287 // Key down on external an keyboard should wake the device.
288 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700289 // For internal keyboards and devices for which the default wake behavior is explicitly
290 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
291 // wake key individually.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 // TODO: Use the input device configuration to control this behavior more finely.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800293 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
Powei Fengd041c5d2019-05-03 17:11:33 -0700294 !isMediaKey(keyCode)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295 policyFlags |= POLICY_FLAG_WAKE;
296 }
297
298 if (mParameters.handlesKeyRepeat) {
299 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
300 }
301
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000302 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
303 mSource, getDisplayId(), policyFlags,
304 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
305 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState,
306 downTime));
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700307 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308}
309
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000310std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311 size_t n = mKeyDowns.size();
312 for (size_t i = 0; i < n; i++) {
313 if (mKeyDowns[i].scanCode == scanCode) {
314 return i;
315 }
316 }
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000317 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318}
319
320int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800321 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700322}
323
324int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800325 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326}
327
Philip Junker4af3b3d2021-12-14 10:36:55 +0100328int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
329 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
330}
331
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700332bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
333 const std::vector<int32_t>& keyCodes,
334 uint8_t* outFlags) {
335 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336}
337
338int32_t KeyboardInputMapper::getMetaState() {
339 return mMetaState;
340}
341
Arthur Hungcb40a002021-08-03 14:31:01 +0000342bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
343 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
344 return false;
345 }
346
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000348 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349}
350
351bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
352 int32_t oldMetaState = mMetaState;
353 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800354 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700355 if (metaStateChanged) {
356 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800357 constexpr int32_t allLedMetaState =
358 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
359 if ((metaStateChanged & allLedMetaState) != 0) {
360 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
361 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362 getContext()->updateGlobalMetaState();
363 }
364
365 return metaStateChanged;
366}
367
368void KeyboardInputMapper::resetLedState() {
369 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
370 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
371 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
372
373 updateLedState(true);
374}
375
376void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800377 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700378 ledState.on = false;
379}
380
381void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000382 // Clear the local led state then union the global led state.
383 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800384 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700385
386 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700387 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
388 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700389 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
390 AMETA_SCROLL_LOCK_ON};
391 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700392 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700393 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
394 // meta state.
395 if (hasKeyLayout) {
396 for (int i = 0; i < META_NUM; i++) {
397 if (!flags[i]) {
398 mMetaState &= ~metaCodes[i];
399 }
400 }
401 }
402
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
404 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
405 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
406}
407
408void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
409 int32_t modifier, bool reset) {
410 if (ledState.avail) {
411 bool desiredState = (mMetaState & modifier) != 0;
412 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800413 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700414 ledState.on = desiredState;
415 }
416 }
417}
418
419std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
420 if (mViewport) {
421 return std::make_optional(mViewport->displayId);
422 }
423 return std::nullopt;
424}
425
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700426std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
427 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000428 size_t n = mKeyDowns.size();
429 for (size_t i = 0; i < n; i++) {
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000430 out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
431 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
432 getDisplayId(), 0 /*policyFlags*/, AKEY_EVENT_ACTION_UP,
433 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED,
434 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
435 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000436 }
437 mKeyDowns.clear();
438 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700439 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000440}
441
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442} // namespace android