Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 17 | // clang-format off |
Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 18 | #include "../Macros.h" |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 19 | // clang-format on |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 20 | |
| 21 | #include "KeyboardInputMapper.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // --- Static Definitions --- |
| 26 | |
| 27 | static 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 | |
| 39 | static 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 | |
| 56 | static const size_t keyCodeRotationMapSize = |
| 57 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 58 | |
| 59 | static 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 |
| 72 | static 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 | |
| 81 | static const size_t stemKeyRotationMapSize = |
| 82 | sizeof(stemKeyRotationMap) / sizeof(stemKeyRotationMap[0]); |
| 83 | |
| 84 | static 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. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 92 | KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source, |
| 93 | int32_t keyboardType) |
| 94 | : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 95 | |
| 96 | KeyboardInputMapper::~KeyboardInputMapper() {} |
| 97 | |
| 98 | uint32_t KeyboardInputMapper::getSources() { |
| 99 | return mSource; |
| 100 | } |
| 101 | |
| 102 | int32_t KeyboardInputMapper::getOrientation() { |
| 103 | if (mViewport) { |
| 104 | return mViewport->orientation; |
| 105 | } |
| 106 | return DISPLAY_ORIENTATION_0; |
| 107 | } |
| 108 | |
| 109 | int32_t KeyboardInputMapper::getDisplayId() { |
| 110 | if (mViewport) { |
| 111 | return mViewport->displayId; |
| 112 | } |
| 113 | return ADISPLAY_ID_NONE; |
| 114 | } |
| 115 | |
| 116 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 117 | InputMapper::populateDeviceInfo(info); |
| 118 | |
| 119 | info->setKeyboardType(mKeyboardType); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 120 | info->setKeyCharacterMap(getDeviceContext().getKeyCharacterMap()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void 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); |
| 130 | dump += StringPrintf(INDENT3 "DownTime: %" PRId64 "\n", mDownTime); |
| 131 | } |
| 132 | |
| 133 | std::optional<DisplayViewport> KeyboardInputMapper::findViewport( |
| 134 | nsecs_t when, const InputReaderConfiguration* config) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 135 | const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 136 | if (displayPort) { |
| 137 | // Find the viewport that contains the same port |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 138 | return getDeviceContext().getAssociatedViewport(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // No associated display defined, try to find default display if orientationAware. |
| 142 | if (mParameters.orientationAware) { |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 143 | return config->getDisplayViewportByType(ViewportType::INTERNAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return std::nullopt; |
| 147 | } |
| 148 | |
| 149 | void KeyboardInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config, |
| 150 | uint32_t changes) { |
| 151 | InputMapper::configure(when, config, changes); |
| 152 | |
| 153 | if (!changes) { // first time only |
| 154 | // Configure basic parameters. |
| 155 | configureParameters(); |
| 156 | } |
| 157 | |
| 158 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 159 | mViewport = findViewport(when, config); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void mapStemKey(int32_t keyCode, const PropertyMap& config, char const* property) { |
| 164 | int32_t mapped = 0; |
| 165 | if (config.tryGetProperty(String8(property), mapped) && mapped > 0) { |
| 166 | for (size_t i = 0; i < stemKeyRotationMapSize; i++) { |
| 167 | if (stemKeyRotationMap[i][0] == keyCode) { |
| 168 | stemKeyRotationMap[i][1] = mapped; |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void KeyboardInputMapper::configureParameters() { |
| 176 | mParameters.orientationAware = false; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 177 | const PropertyMap& config = getDeviceContext().getConfiguration(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 178 | config.tryGetProperty(String8("keyboard.orientationAware"), mParameters.orientationAware); |
| 179 | |
| 180 | if (mParameters.orientationAware) { |
| 181 | mapStemKey(AKEYCODE_STEM_PRIMARY, config, "keyboard.rotated.stem_primary"); |
| 182 | mapStemKey(AKEYCODE_STEM_1, config, "keyboard.rotated.stem_1"); |
| 183 | mapStemKey(AKEYCODE_STEM_2, config, "keyboard.rotated.stem_2"); |
| 184 | mapStemKey(AKEYCODE_STEM_3, config, "keyboard.rotated.stem_3"); |
| 185 | } |
| 186 | |
| 187 | mParameters.handlesKeyRepeat = false; |
| 188 | config.tryGetProperty(String8("keyboard.handlesKeyRepeat"), mParameters.handlesKeyRepeat); |
Powei Feng | d041c5d | 2019-05-03 17:11:33 -0700 | [diff] [blame] | 189 | |
| 190 | mParameters.doNotWakeByDefault = false; |
| 191 | config.tryGetProperty(String8("keyboard.doNotWakeByDefault"), mParameters.doNotWakeByDefault); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void KeyboardInputMapper::dumpParameters(std::string& dump) { |
| 195 | dump += INDENT3 "Parameters:\n"; |
| 196 | dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware)); |
| 197 | dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat)); |
| 198 | } |
| 199 | |
| 200 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 201 | mMetaState = AMETA_NONE; |
| 202 | mDownTime = 0; |
| 203 | mKeyDowns.clear(); |
| 204 | mCurrentHidUsage = 0; |
| 205 | |
| 206 | resetLedState(); |
| 207 | |
| 208 | InputMapper::reset(when); |
| 209 | } |
| 210 | |
| 211 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 212 | switch (rawEvent->type) { |
| 213 | case EV_KEY: { |
| 214 | int32_t scanCode = rawEvent->code; |
| 215 | int32_t usageCode = mCurrentHidUsage; |
| 216 | mCurrentHidUsage = 0; |
| 217 | |
| 218 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 219 | processKey(rawEvent->when, rawEvent->value != 0, scanCode, usageCode); |
| 220 | } |
| 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 | } |
| 235 | } |
| 236 | |
| 237 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
Siarhei Vishniakou | a0d2b80 | 2020-05-13 14:00:31 -0700 | [diff] [blame] | 238 | return scanCode < BTN_MOUSE || scanCode >= BTN_WHEEL || |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 239 | (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) || |
| 240 | (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
| 241 | } |
| 242 | |
| 243 | bool KeyboardInputMapper::isMediaKey(int32_t keyCode) { |
| 244 | switch (keyCode) { |
| 245 | case AKEYCODE_MEDIA_PLAY: |
| 246 | case AKEYCODE_MEDIA_PAUSE: |
| 247 | case AKEYCODE_MEDIA_PLAY_PAUSE: |
| 248 | case AKEYCODE_MUTE: |
| 249 | case AKEYCODE_HEADSETHOOK: |
| 250 | case AKEYCODE_MEDIA_STOP: |
| 251 | case AKEYCODE_MEDIA_NEXT: |
| 252 | case AKEYCODE_MEDIA_PREVIOUS: |
| 253 | case AKEYCODE_MEDIA_REWIND: |
| 254 | case AKEYCODE_MEDIA_RECORD: |
| 255 | case AKEYCODE_MEDIA_FAST_FORWARD: |
| 256 | case AKEYCODE_MEDIA_SKIP_FORWARD: |
| 257 | case AKEYCODE_MEDIA_SKIP_BACKWARD: |
| 258 | case AKEYCODE_MEDIA_STEP_FORWARD: |
| 259 | case AKEYCODE_MEDIA_STEP_BACKWARD: |
| 260 | case AKEYCODE_MEDIA_AUDIO_TRACK: |
| 261 | case AKEYCODE_VOLUME_UP: |
| 262 | case AKEYCODE_VOLUME_DOWN: |
| 263 | case AKEYCODE_VOLUME_MUTE: |
| 264 | case AKEYCODE_TV_AUDIO_DESCRIPTION: |
| 265 | case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP: |
| 266 | case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN: |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode) { |
| 273 | int32_t keyCode; |
| 274 | int32_t keyMetaState; |
| 275 | uint32_t policyFlags; |
| 276 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 277 | if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState, |
| 278 | &policyFlags)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 279 | keyCode = AKEYCODE_UNKNOWN; |
| 280 | keyMetaState = mMetaState; |
| 281 | policyFlags = 0; |
| 282 | } |
| 283 | |
| 284 | if (down) { |
| 285 | // Rotate key codes according to orientation if needed. |
| 286 | if (mParameters.orientationAware) { |
| 287 | keyCode = rotateKeyCode(keyCode, getOrientation()); |
| 288 | } |
| 289 | |
| 290 | // Add key down. |
| 291 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 292 | if (keyDownIndex >= 0) { |
| 293 | // key repeat, be sure to use same keycode as before in case of rotation |
| 294 | keyCode = mKeyDowns[keyDownIndex].keyCode; |
| 295 | } else { |
| 296 | // key down |
| 297 | if ((policyFlags & POLICY_FLAG_VIRTUAL) && |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 298 | getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | if (policyFlags & POLICY_FLAG_GESTURE) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 302 | getDeviceContext().cancelTouch(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | KeyDown keyDown; |
| 306 | keyDown.keyCode = keyCode; |
| 307 | keyDown.scanCode = scanCode; |
| 308 | mKeyDowns.push_back(keyDown); |
| 309 | } |
| 310 | |
| 311 | mDownTime = when; |
| 312 | } else { |
| 313 | // Remove key down. |
| 314 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 315 | if (keyDownIndex >= 0) { |
| 316 | // key up, be sure to use same keycode as before in case of rotation |
| 317 | keyCode = mKeyDowns[keyDownIndex].keyCode; |
| 318 | mKeyDowns.erase(mKeyDowns.begin() + (size_t)keyDownIndex); |
| 319 | } else { |
| 320 | // key was not actually down |
| 321 | ALOGI("Dropping key up from device %s because the key was not down. " |
| 322 | "keyCode=%d, scanCode=%d", |
| 323 | getDeviceName().c_str(), keyCode, scanCode); |
| 324 | return; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (updateMetaStateIfNeeded(keyCode, down)) { |
| 329 | // If global meta state changed send it along with the key. |
| 330 | // If it has not changed then we'll use what keymap gave us, |
| 331 | // since key replacement logic might temporarily reset a few |
| 332 | // meta bits for given key. |
| 333 | keyMetaState = mMetaState; |
| 334 | } |
| 335 | |
| 336 | nsecs_t downTime = mDownTime; |
| 337 | |
| 338 | // Key down on external an keyboard should wake the device. |
| 339 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
Powei Feng | d041c5d | 2019-05-03 17:11:33 -0700 | [diff] [blame] | 340 | // For internal keyboards and devices for which the default wake behavior is explicitly |
| 341 | // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each |
| 342 | // wake key individually. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 343 | // TODO: Use the input device configuration to control this behavior more finely. |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 344 | if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault && |
Powei Feng | d041c5d | 2019-05-03 17:11:33 -0700 | [diff] [blame] | 345 | !isMediaKey(keyCode)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 346 | policyFlags |= POLICY_FLAG_WAKE; |
| 347 | } |
| 348 | |
| 349 | if (mParameters.handlesKeyRepeat) { |
| 350 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 351 | } |
| 352 | |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 353 | NotifyKeyArgs args(getContext()->getNextId(), when, getDeviceId(), mSource, getDisplayId(), |
| 354 | policyFlags, down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 355 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState, downTime); |
| 356 | getListener()->notifyKey(&args); |
| 357 | } |
| 358 | |
| 359 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 360 | size_t n = mKeyDowns.size(); |
| 361 | for (size_t i = 0; i < n; i++) { |
| 362 | if (mKeyDowns[i].scanCode == scanCode) { |
| 363 | return i; |
| 364 | } |
| 365 | } |
| 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 370 | return getDeviceContext().getKeyCodeState(keyCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 374 | return getDeviceContext().getScanCodeState(scanCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 378 | const int32_t* keyCodes, uint8_t* outFlags) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 379 | return getDeviceContext().markSupportedKeyCodes(numCodes, keyCodes, outFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | int32_t KeyboardInputMapper::getMetaState() { |
| 383 | return mMetaState; |
| 384 | } |
| 385 | |
| 386 | void KeyboardInputMapper::updateMetaState(int32_t keyCode) { |
| 387 | updateMetaStateIfNeeded(keyCode, false); |
| 388 | } |
| 389 | |
| 390 | bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) { |
| 391 | int32_t oldMetaState = mMetaState; |
| 392 | int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState); |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 393 | int32_t metaStateChanged = oldMetaState ^ newMetaState; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 394 | if (metaStateChanged) { |
| 395 | mMetaState = newMetaState; |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 396 | constexpr int32_t allLedMetaState = |
| 397 | AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON; |
| 398 | if ((metaStateChanged & allLedMetaState) != 0) { |
| 399 | getContext()->updateLedMetaState(newMetaState & allLedMetaState); |
| 400 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 401 | getContext()->updateGlobalMetaState(); |
| 402 | } |
| 403 | |
| 404 | return metaStateChanged; |
| 405 | } |
| 406 | |
| 407 | void KeyboardInputMapper::resetLedState() { |
| 408 | initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK); |
| 409 | initializeLedState(mNumLockLedState, ALED_NUM_LOCK); |
| 410 | initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK); |
| 411 | |
| 412 | updateLedState(true); |
| 413 | } |
| 414 | |
| 415 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 416 | ledState.avail = getDeviceContext().hasLed(led); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 417 | ledState.on = false; |
| 418 | } |
| 419 | |
| 420 | void KeyboardInputMapper::updateLedState(bool reset) { |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 421 | mMetaState |= getContext()->getLedMetaState(); |
Chris Ye | a52ade1 | 2020-08-27 16:49:20 -0700 | [diff] [blame^] | 422 | |
| 423 | constexpr int32_t META_NUM = 3; |
| 424 | const std::array<int32_t, META_NUM> keyCodes = {AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK, |
| 425 | AKEYCODE_SCROLL_LOCK}; |
| 426 | const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON, |
| 427 | AMETA_SCROLL_LOCK_ON}; |
| 428 | std::array<uint8_t, META_NUM> flags = {0, 0, 0}; |
| 429 | bool hasKeyLayout = |
| 430 | getDeviceContext().markSupportedKeyCodes(META_NUM, keyCodes.data(), flags.data()); |
| 431 | // If the device doesn't have the physical meta key it shouldn't generate the corresponding |
| 432 | // meta state. |
| 433 | if (hasKeyLayout) { |
| 434 | for (int i = 0; i < META_NUM; i++) { |
| 435 | if (!flags[i]) { |
| 436 | mMetaState &= ~metaCodes[i]; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 441 | updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset); |
| 442 | updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset); |
| 443 | updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset); |
| 444 | } |
| 445 | |
| 446 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led, |
| 447 | int32_t modifier, bool reset) { |
| 448 | if (ledState.avail) { |
| 449 | bool desiredState = (mMetaState & modifier) != 0; |
| 450 | if (reset || ledState.on != desiredState) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 451 | getDeviceContext().setLedState(led, desiredState); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 452 | ledState.on = desiredState; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() { |
| 458 | if (mViewport) { |
| 459 | return std::make_optional(mViewport->displayId); |
| 460 | } |
| 461 | return std::nullopt; |
| 462 | } |
| 463 | |
| 464 | } // namespace android |