blob: 8704d1b211d0b3d8725bd10302454a0c10a8ed2d [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
27static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
28 const int32_t map[][4], size_t mapSize) {
29 if (orientation != DISPLAY_ORIENTATION_0) {
30 for (size_t i = 0; i < mapSize; i++) {
31 if (value == map[i][0]) {
32 return map[i][orientation];
33 }
34 }
35 }
36 return value;
37}
38
39static const int32_t keyCodeRotationMap[][4] = {
40 // key codes enumerated counter-clockwise with the original (unrotated) key first
41 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
42 {AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT},
43 {AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN},
44 {AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT},
45 {AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP},
46 {AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT,
47 AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT},
48 {AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP,
49 AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN},
50 {AKEYCODE_SYSTEM_NAVIGATION_UP, AKEYCODE_SYSTEM_NAVIGATION_LEFT,
51 AKEYCODE_SYSTEM_NAVIGATION_DOWN, AKEYCODE_SYSTEM_NAVIGATION_RIGHT},
52 {AKEYCODE_SYSTEM_NAVIGATION_LEFT, AKEYCODE_SYSTEM_NAVIGATION_DOWN,
53 AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP},
54};
55
56static const size_t keyCodeRotationMapSize =
57 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
58
59static int32_t rotateStemKey(int32_t value, int32_t orientation, const int32_t map[][2],
60 size_t mapSize) {
61 if (orientation == DISPLAY_ORIENTATION_180) {
62 for (size_t i = 0; i < mapSize; i++) {
63 if (value == map[i][0]) {
64 return map[i][1];
65 }
66 }
67 }
68 return value;
69}
70
71// The mapping can be defined using input device configuration properties keyboard.rotated.stem_X
72static int32_t stemKeyRotationMap[][2] = {
73 // key codes enumerated with the original (unrotated) key first
74 // no rotation, 180 degree rotation
75 {AKEYCODE_STEM_PRIMARY, AKEYCODE_STEM_PRIMARY},
76 {AKEYCODE_STEM_1, AKEYCODE_STEM_1},
77 {AKEYCODE_STEM_2, AKEYCODE_STEM_2},
78 {AKEYCODE_STEM_3, AKEYCODE_STEM_3},
79};
80
81static const size_t stemKeyRotationMapSize =
82 sizeof(stemKeyRotationMap) / sizeof(stemKeyRotationMap[0]);
83
84static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
85 keyCode = rotateStemKey(keyCode, orientation, stemKeyRotationMap, stemKeyRotationMapSize);
86 return rotateValueUsingRotationMap(keyCode, orientation, keyCodeRotationMap,
87 keyCodeRotationMapSize);
88}
89
90// --- KeyboardInputMapper ---
91
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080092KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source,
93 int32_t keyboardType)
94 : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095
96KeyboardInputMapper::~KeyboardInputMapper() {}
97
Philip Junker4af3b3d2021-12-14 10:36:55 +010098uint32_t KeyboardInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099 return mSource;
100}
101
102int32_t KeyboardInputMapper::getOrientation() {
103 if (mViewport) {
104 return mViewport->orientation;
105 }
106 return DISPLAY_ORIENTATION_0;
107}
108
109int32_t KeyboardInputMapper::getDisplayId() {
110 if (mViewport) {
111 return mViewport->displayId;
112 }
113 return ADISPLAY_ID_NONE;
114}
115
116void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
117 InputMapper::populateDeviceInfo(info);
118
119 info->setKeyboardType(mKeyboardType);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800120 info->setKeyCharacterMap(getDeviceContext().getKeyCharacterMap());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121}
122
123void KeyboardInputMapper::dump(std::string& dump) {
124 dump += INDENT2 "Keyboard Input Mapper:\n";
125 dumpParameters(dump);
126 dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType);
127 dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation());
128 dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
129 dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130}
131
132std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
133 nsecs_t when, const InputReaderConfiguration* config) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700134 if (getDeviceContext().getAssociatedViewport()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800135 return getDeviceContext().getAssociatedViewport();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136 }
137
138 // No associated display defined, try to find default display if orientationAware.
139 if (mParameters.orientationAware) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100140 return config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141 }
142
143 return std::nullopt;
144}
145
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700146std::list<NotifyArgs> KeyboardInputMapper::configure(nsecs_t when,
147 const InputReaderConfiguration* config,
148 uint32_t changes) {
149 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700150
151 if (!changes) { // first time only
152 // Configure basic parameters.
153 configureParameters();
154 }
155
156 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
157 mViewport = findViewport(when, config);
158 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700159 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700160}
161
162static void mapStemKey(int32_t keyCode, const PropertyMap& config, char const* property) {
163 int32_t mapped = 0;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700164 if (config.tryGetProperty(property, mapped) && mapped > 0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700165 for (size_t i = 0; i < stemKeyRotationMapSize; i++) {
166 if (stemKeyRotationMap[i][0] == keyCode) {
167 stemKeyRotationMap[i][1] = mapped;
168 return;
169 }
170 }
171 }
172}
173
174void KeyboardInputMapper::configureParameters() {
175 mParameters.orientationAware = false;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800176 const PropertyMap& config = getDeviceContext().getConfiguration();
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700177 config.tryGetProperty("keyboard.orientationAware", mParameters.orientationAware);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700178
179 if (mParameters.orientationAware) {
180 mapStemKey(AKEYCODE_STEM_PRIMARY, config, "keyboard.rotated.stem_primary");
181 mapStemKey(AKEYCODE_STEM_1, config, "keyboard.rotated.stem_1");
182 mapStemKey(AKEYCODE_STEM_2, config, "keyboard.rotated.stem_2");
183 mapStemKey(AKEYCODE_STEM_3, config, "keyboard.rotated.stem_3");
184 }
185
186 mParameters.handlesKeyRepeat = false;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700187 config.tryGetProperty("keyboard.handlesKeyRepeat", mParameters.handlesKeyRepeat);
Powei Fengd041c5d2019-05-03 17:11:33 -0700188
189 mParameters.doNotWakeByDefault = false;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700190 config.tryGetProperty("keyboard.doNotWakeByDefault", mParameters.doNotWakeByDefault);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191}
192
193void KeyboardInputMapper::dumpParameters(std::string& dump) {
194 dump += INDENT3 "Parameters:\n";
195 dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
196 dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
197}
198
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700199std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) {
200 std::list<NotifyArgs> out = cancelAllDownKeys(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700201 mCurrentHidUsage = 0;
202
203 resetLedState();
204
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700205 out += InputMapper::reset(when);
206 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700207}
208
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700209std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
210 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700211 switch (rawEvent->type) {
212 case EV_KEY: {
213 int32_t scanCode = rawEvent->code;
214 int32_t usageCode = mCurrentHidUsage;
215 mCurrentHidUsage = 0;
216
217 if (isKeyboardOrGamepadKey(scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700218 out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0,
219 scanCode, usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700220 }
221 break;
222 }
223 case EV_MSC: {
224 if (rawEvent->code == MSC_SCAN) {
225 mCurrentHidUsage = rawEvent->value;
226 }
227 break;
228 }
229 case EV_SYN: {
230 if (rawEvent->code == SYN_REPORT) {
231 mCurrentHidUsage = 0;
232 }
233 }
234 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700235 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700236}
237
238bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -0700239 return scanCode < BTN_MOUSE || scanCode >= BTN_WHEEL ||
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700240 (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) ||
241 (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
242}
243
244bool KeyboardInputMapper::isMediaKey(int32_t keyCode) {
245 switch (keyCode) {
246 case AKEYCODE_MEDIA_PLAY:
247 case AKEYCODE_MEDIA_PAUSE:
248 case AKEYCODE_MEDIA_PLAY_PAUSE:
249 case AKEYCODE_MUTE:
250 case AKEYCODE_HEADSETHOOK:
251 case AKEYCODE_MEDIA_STOP:
252 case AKEYCODE_MEDIA_NEXT:
253 case AKEYCODE_MEDIA_PREVIOUS:
254 case AKEYCODE_MEDIA_REWIND:
255 case AKEYCODE_MEDIA_RECORD:
256 case AKEYCODE_MEDIA_FAST_FORWARD:
257 case AKEYCODE_MEDIA_SKIP_FORWARD:
258 case AKEYCODE_MEDIA_SKIP_BACKWARD:
259 case AKEYCODE_MEDIA_STEP_FORWARD:
260 case AKEYCODE_MEDIA_STEP_BACKWARD:
261 case AKEYCODE_MEDIA_AUDIO_TRACK:
262 case AKEYCODE_VOLUME_UP:
263 case AKEYCODE_VOLUME_DOWN:
264 case AKEYCODE_VOLUME_MUTE:
265 case AKEYCODE_TV_AUDIO_DESCRIPTION:
266 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP:
267 case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN:
268 return true;
269 }
270 return false;
271}
272
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700273std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
274 int32_t scanCode, int32_t usageCode) {
275 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 int32_t keyCode;
277 int32_t keyMetaState;
278 uint32_t policyFlags;
279
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800280 if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState,
281 &policyFlags)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700282 keyCode = AKEYCODE_UNKNOWN;
283 keyMetaState = mMetaState;
284 policyFlags = 0;
285 }
286
Arthur Hung2141d542022-08-23 07:45:21 +0000287 nsecs_t downTime = when;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700288 if (down) {
289 // Rotate key codes according to orientation if needed.
290 if (mParameters.orientationAware) {
291 keyCode = rotateKeyCode(keyCode, getOrientation());
292 }
293
294 // Add key down.
295 ssize_t keyDownIndex = findKeyDown(scanCode);
296 if (keyDownIndex >= 0) {
297 // key repeat, be sure to use same keycode as before in case of rotation
298 keyCode = mKeyDowns[keyDownIndex].keyCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000299 downTime = mKeyDowns[keyDownIndex].downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300 } else {
301 // key down
302 if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800303 getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700304 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700305 }
306 if (policyFlags & POLICY_FLAG_GESTURE) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700307 out += getDeviceContext().cancelTouch(when, readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308 }
309
310 KeyDown keyDown;
311 keyDown.keyCode = keyCode;
312 keyDown.scanCode = scanCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000313 keyDown.downTime = when;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314 mKeyDowns.push_back(keyDown);
315 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 } else {
317 // Remove key down.
318 ssize_t keyDownIndex = findKeyDown(scanCode);
319 if (keyDownIndex >= 0) {
320 // key up, be sure to use same keycode as before in case of rotation
321 keyCode = mKeyDowns[keyDownIndex].keyCode;
Arthur Hung2141d542022-08-23 07:45:21 +0000322 downTime = mKeyDowns[keyDownIndex].downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323 mKeyDowns.erase(mKeyDowns.begin() + (size_t)keyDownIndex);
324 } else {
325 // key was not actually down
326 ALOGI("Dropping key up from device %s because the key was not down. "
327 "keyCode=%d, scanCode=%d",
328 getDeviceName().c_str(), keyCode, scanCode);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700329 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330 }
331 }
332
333 if (updateMetaStateIfNeeded(keyCode, down)) {
334 // If global meta state changed send it along with the key.
335 // If it has not changed then we'll use what keymap gave us,
336 // since key replacement logic might temporarily reset a few
337 // meta bits for given key.
338 keyMetaState = mMetaState;
339 }
340
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341 // Key down on external an keyboard should wake the device.
342 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
Powei Fengd041c5d2019-05-03 17:11:33 -0700343 // For internal keyboards and devices for which the default wake behavior is explicitly
344 // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each
345 // wake key individually.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 // TODO: Use the input device configuration to control this behavior more finely.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800347 if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault &&
Powei Fengd041c5d2019-05-03 17:11:33 -0700348 !isMediaKey(keyCode)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349 policyFlags |= POLICY_FLAG_WAKE;
350 }
351
352 if (mParameters.handlesKeyRepeat) {
353 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
354 }
355
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700356 out.push_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
357 getDisplayId(), policyFlags,
358 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
359 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState,
360 downTime));
361 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362}
363
364ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
365 size_t n = mKeyDowns.size();
366 for (size_t i = 0; i < n; i++) {
367 if (mKeyDowns[i].scanCode == scanCode) {
368 return i;
369 }
370 }
371 return -1;
372}
373
374int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800375 return getDeviceContext().getKeyCodeState(keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700376}
377
378int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800379 return getDeviceContext().getScanCodeState(scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380}
381
Philip Junker4af3b3d2021-12-14 10:36:55 +0100382int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
383 return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode);
384}
385
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700386bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask,
387 const std::vector<int32_t>& keyCodes,
388 uint8_t* outFlags) {
389 return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390}
391
392int32_t KeyboardInputMapper::getMetaState() {
393 return mMetaState;
394}
395
Arthur Hungcb40a002021-08-03 14:31:01 +0000396bool KeyboardInputMapper::updateMetaState(int32_t keyCode) {
397 if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) {
398 return false;
399 }
400
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700401 updateMetaStateIfNeeded(keyCode, false);
Arthur Hungcb40a002021-08-03 14:31:01 +0000402 return true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403}
404
405bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) {
406 int32_t oldMetaState = mMetaState;
407 int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState);
arthurhungc903df12020-08-11 15:08:42 +0800408 int32_t metaStateChanged = oldMetaState ^ newMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 if (metaStateChanged) {
410 mMetaState = newMetaState;
arthurhungc903df12020-08-11 15:08:42 +0800411 constexpr int32_t allLedMetaState =
412 AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON;
413 if ((metaStateChanged & allLedMetaState) != 0) {
414 getContext()->updateLedMetaState(newMetaState & allLedMetaState);
415 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700416 getContext()->updateGlobalMetaState();
417 }
418
419 return metaStateChanged;
420}
421
422void KeyboardInputMapper::resetLedState() {
423 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
424 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
425 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
426
427 updateLedState(true);
428}
429
430void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800431 ledState.avail = getDeviceContext().hasLed(led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700432 ledState.on = false;
433}
434
435void KeyboardInputMapper::updateLedState(bool reset) {
Arthur Hungfb3cc112022-04-13 07:39:50 +0000436 // Clear the local led state then union the global led state.
437 mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON);
arthurhungc903df12020-08-11 15:08:42 +0800438 mMetaState |= getContext()->getLedMetaState();
Chris Yea52ade12020-08-27 16:49:20 -0700439
440 constexpr int32_t META_NUM = 3;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700441 const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK,
442 AKEYCODE_SCROLL_LOCK};
Chris Yea52ade12020-08-27 16:49:20 -0700443 const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON,
444 AMETA_SCROLL_LOCK_ON};
445 std::array<uint8_t, META_NUM> flags = {0, 0, 0};
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700446 bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data());
Chris Yea52ade12020-08-27 16:49:20 -0700447 // If the device doesn't have the physical meta key it shouldn't generate the corresponding
448 // meta state.
449 if (hasKeyLayout) {
450 for (int i = 0; i < META_NUM; i++) {
451 if (!flags[i]) {
452 mMetaState &= ~metaCodes[i];
453 }
454 }
455 }
456
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset);
458 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset);
459 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset);
460}
461
462void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led,
463 int32_t modifier, bool reset) {
464 if (ledState.avail) {
465 bool desiredState = (mMetaState & modifier) != 0;
466 if (reset || ledState.on != desiredState) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800467 getDeviceContext().setLedState(led, desiredState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700468 ledState.on = desiredState;
469 }
470 }
471}
472
473std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() {
474 if (mViewport) {
475 return std::make_optional(mViewport->displayId);
476 }
477 return std::nullopt;
478}
479
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700480std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
481 std::list<NotifyArgs> out;
Arthur Hung2141d542022-08-23 07:45:21 +0000482 size_t n = mKeyDowns.size();
483 for (size_t i = 0; i < n; i++) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700484 out.push_back(NotifyKeyArgs(getContext()->getNextId(), when,
485 systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
486 getDisplayId(), 0 /*policyFlags*/, AKEY_EVENT_ACTION_UP,
487 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED,
488 mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE,
489 mKeyDowns[i].downTime));
Arthur Hung2141d542022-08-23 07:45:21 +0000490 }
491 mKeyDowns.clear();
492 mMetaState = AMETA_NONE;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700493 return out;
Arthur Hung2141d542022-08-23 07:45:21 +0000494}
495
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700496} // namespace android