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 | |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame^] | 23 | #include <ui/Rotation.h> |
| 24 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | // --- Static Definitions --- |
| 28 | |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame^] | 29 | static int32_t rotateKeyCode(int32_t keyCode, ui::Rotation orientation) { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 30 | 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 Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame^] | 47 | if (orientation != ui::ROTATION_0) { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 48 | for (const auto& rotation : KEYCODE_ROTATION_MAP) { |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame^] | 49 | if (rotation[static_cast<size_t>(ui::ROTATION_0)] == keyCode) { |
| 50 | return rotation[static_cast<size_t>(orientation)]; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 54 | return keyCode; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 57 | static 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 Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 62 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 63 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 64 | static 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 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // --- KeyboardInputMapper --- |
| 95 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 96 | KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source, |
| 97 | int32_t keyboardType) |
| 98 | : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 99 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 100 | uint32_t KeyboardInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 101 | return mSource; |
| 102 | } |
| 103 | |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame^] | 104 | ui::Rotation KeyboardInputMapper::getOrientation() { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 105 | if (mViewport) { |
| 106 | return mViewport->orientation; |
| 107 | } |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame^] | 108 | return ui::ROTATION_0; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int32_t KeyboardInputMapper::getDisplayId() { |
| 112 | if (mViewport) { |
| 113 | return mViewport->displayId; |
| 114 | } |
| 115 | return ADISPLAY_ID_NONE; |
| 116 | } |
| 117 | |
| 118 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 119 | InputMapper::populateDeviceInfo(info); |
| 120 | |
| 121 | info->setKeyboardType(mKeyboardType); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 122 | info->setKeyCharacterMap(getDeviceContext().getKeyCharacterMap()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void KeyboardInputMapper::dump(std::string& dump) { |
| 126 | dump += INDENT2 "Keyboard Input Mapper:\n"; |
| 127 | dumpParameters(dump); |
| 128 | dump += StringPrintf(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
| 129 | dump += StringPrintf(INDENT3 "Orientation: %d\n", getOrientation()); |
| 130 | dump += StringPrintf(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size()); |
| 131 | dump += StringPrintf(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | std::optional<DisplayViewport> KeyboardInputMapper::findViewport( |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 135 | const InputReaderConfiguration* config) { |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 136 | if (getDeviceContext().getAssociatedViewport()) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 137 | return getDeviceContext().getAssociatedViewport(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // No associated display defined, try to find default display if orientationAware. |
| 141 | if (mParameters.orientationAware) { |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 142 | return config->getDisplayViewportByType(ViewportType::INTERNAL); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return std::nullopt; |
| 146 | } |
| 147 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 148 | std::list<NotifyArgs> KeyboardInputMapper::configure(nsecs_t when, |
| 149 | const InputReaderConfiguration* config, |
| 150 | uint32_t changes) { |
| 151 | std::list<NotifyArgs> out = InputMapper::configure(when, config, changes); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 152 | |
| 153 | if (!changes) { // first time only |
| 154 | // Configure basic parameters. |
| 155 | configureParameters(); |
| 156 | } |
| 157 | |
| 158 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 159 | mViewport = findViewport(config); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 160 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 161 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 164 | void KeyboardInputMapper::configureParameters() { |
| 165 | mParameters.orientationAware = false; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 166 | const PropertyMap& config = getDeviceContext().getConfiguration(); |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 167 | config.tryGetProperty("keyboard.orientationAware", mParameters.orientationAware); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 168 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 169 | mParameters.handlesKeyRepeat = false; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 170 | config.tryGetProperty("keyboard.handlesKeyRepeat", mParameters.handlesKeyRepeat); |
Powei Feng | d041c5d | 2019-05-03 17:11:33 -0700 | [diff] [blame] | 171 | |
| 172 | mParameters.doNotWakeByDefault = false; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 173 | config.tryGetProperty("keyboard.doNotWakeByDefault", mParameters.doNotWakeByDefault); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 176 | void KeyboardInputMapper::dumpParameters(std::string& dump) const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 177 | dump += INDENT3 "Parameters:\n"; |
| 178 | dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware)); |
| 179 | dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat)); |
| 180 | } |
| 181 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 182 | std::list<NotifyArgs> KeyboardInputMapper::reset(nsecs_t when) { |
| 183 | std::list<NotifyArgs> out = cancelAllDownKeys(when); |
Prabir Pradhan | 84395e6 | 2022-10-26 19:52:00 +0000 | [diff] [blame] | 184 | mHidUsageAccumulator.reset(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 185 | |
| 186 | resetLedState(); |
| 187 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 188 | out += InputMapper::reset(when); |
| 189 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 192 | std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 193 | std::list<NotifyArgs> out; |
Prabir Pradhan | 84395e6 | 2022-10-26 19:52:00 +0000 | [diff] [blame] | 194 | mHidUsageAccumulator.process(*rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 195 | switch (rawEvent->type) { |
| 196 | case EV_KEY: { |
| 197 | int32_t scanCode = rawEvent->code; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 198 | |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 199 | if (isSupportedScanCode(scanCode)) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 200 | out += processKey(rawEvent->when, rawEvent->readTime, rawEvent->value != 0, |
Prabir Pradhan | 84395e6 | 2022-10-26 19:52:00 +0000 | [diff] [blame] | 201 | scanCode, mHidUsageAccumulator.consumeCurrentHidUsage()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 202 | } |
| 203 | break; |
| 204 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 205 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 206 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 209 | std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down, |
| 210 | int32_t scanCode, int32_t usageCode) { |
| 211 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 212 | int32_t keyCode; |
| 213 | int32_t keyMetaState; |
| 214 | uint32_t policyFlags; |
| 215 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 216 | if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState, |
| 217 | &policyFlags)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 218 | keyCode = AKEYCODE_UNKNOWN; |
| 219 | keyMetaState = mMetaState; |
| 220 | policyFlags = 0; |
| 221 | } |
| 222 | |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 223 | nsecs_t downTime = when; |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 224 | std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 225 | if (down) { |
| 226 | // Rotate key codes according to orientation if needed. |
| 227 | if (mParameters.orientationAware) { |
| 228 | keyCode = rotateKeyCode(keyCode, getOrientation()); |
| 229 | } |
| 230 | |
| 231 | // Add key down. |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 232 | if (keyDownIndex) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 233 | // key repeat, be sure to use same keycode as before in case of rotation |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 234 | keyCode = mKeyDowns[*keyDownIndex].keyCode; |
| 235 | downTime = mKeyDowns[*keyDownIndex].downTime; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 236 | } else { |
| 237 | // key down |
| 238 | if ((policyFlags & POLICY_FLAG_VIRTUAL) && |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 239 | getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 240 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 241 | } |
| 242 | if (policyFlags & POLICY_FLAG_GESTURE) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 243 | out += getDeviceContext().cancelTouch(when, readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | KeyDown keyDown; |
| 247 | keyDown.keyCode = keyCode; |
| 248 | keyDown.scanCode = scanCode; |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 249 | keyDown.downTime = when; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 250 | mKeyDowns.push_back(keyDown); |
| 251 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 252 | } else { |
| 253 | // Remove key down. |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 254 | if (keyDownIndex) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 255 | // key up, be sure to use same keycode as before in case of rotation |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 256 | keyCode = mKeyDowns[*keyDownIndex].keyCode; |
| 257 | downTime = mKeyDowns[*keyDownIndex].downTime; |
| 258 | mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 259 | } else { |
| 260 | // key was not actually down |
| 261 | ALOGI("Dropping key up from device %s because the key was not down. " |
| 262 | "keyCode=%d, scanCode=%d", |
| 263 | getDeviceName().c_str(), keyCode, scanCode); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 264 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | if (updateMetaStateIfNeeded(keyCode, down)) { |
| 269 | // If global meta state changed send it along with the key. |
| 270 | // If it has not changed then we'll use what keymap gave us, |
| 271 | // since key replacement logic might temporarily reset a few |
| 272 | // meta bits for given key. |
| 273 | keyMetaState = mMetaState; |
| 274 | } |
| 275 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 276 | // Key down on external an keyboard should wake the device. |
| 277 | // 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] | 278 | // For internal keyboards and devices for which the default wake behavior is explicitly |
| 279 | // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each |
| 280 | // wake key individually. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 281 | // 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] | 282 | if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault && |
Powei Feng | d041c5d | 2019-05-03 17:11:33 -0700 | [diff] [blame] | 283 | !isMediaKey(keyCode)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 284 | policyFlags |= POLICY_FLAG_WAKE; |
| 285 | } |
| 286 | |
| 287 | if (mParameters.handlesKeyRepeat) { |
| 288 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 289 | } |
| 290 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 291 | out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 292 | mSource, getDisplayId(), policyFlags, |
| 293 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 294 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState, |
| 295 | downTime)); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 296 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 299 | std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 300 | size_t n = mKeyDowns.size(); |
| 301 | for (size_t i = 0; i < n; i++) { |
| 302 | if (mKeyDowns[i].scanCode == scanCode) { |
| 303 | return i; |
| 304 | } |
| 305 | } |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 306 | return {}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 310 | return getDeviceContext().getKeyCodeState(keyCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 314 | return getDeviceContext().getScanCodeState(scanCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 317 | int32_t KeyboardInputMapper::getKeyCodeForKeyLocation(int32_t locationKeyCode) const { |
| 318 | return getDeviceContext().getKeyCodeForKeyLocation(locationKeyCode); |
| 319 | } |
| 320 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 321 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, |
| 322 | const std::vector<int32_t>& keyCodes, |
| 323 | uint8_t* outFlags) { |
| 324 | return getDeviceContext().markSupportedKeyCodes(keyCodes, outFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int32_t KeyboardInputMapper::getMetaState() { |
| 328 | return mMetaState; |
| 329 | } |
| 330 | |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 331 | bool KeyboardInputMapper::updateMetaState(int32_t keyCode) { |
| 332 | if (!android::isMetaKey(keyCode) || !getDeviceContext().hasKeyCode(keyCode)) { |
| 333 | return false; |
| 334 | } |
| 335 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 336 | updateMetaStateIfNeeded(keyCode, false); |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 337 | return true; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | bool KeyboardInputMapper::updateMetaStateIfNeeded(int32_t keyCode, bool down) { |
| 341 | int32_t oldMetaState = mMetaState; |
| 342 | int32_t newMetaState = android::updateMetaState(keyCode, down, oldMetaState); |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 343 | int32_t metaStateChanged = oldMetaState ^ newMetaState; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 344 | if (metaStateChanged) { |
| 345 | mMetaState = newMetaState; |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 346 | constexpr int32_t allLedMetaState = |
| 347 | AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON; |
| 348 | if ((metaStateChanged & allLedMetaState) != 0) { |
| 349 | getContext()->updateLedMetaState(newMetaState & allLedMetaState); |
| 350 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 351 | getContext()->updateGlobalMetaState(); |
| 352 | } |
| 353 | |
| 354 | return metaStateChanged; |
| 355 | } |
| 356 | |
| 357 | void KeyboardInputMapper::resetLedState() { |
| 358 | initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK); |
| 359 | initializeLedState(mNumLockLedState, ALED_NUM_LOCK); |
| 360 | initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK); |
| 361 | |
| 362 | updateLedState(true); |
| 363 | } |
| 364 | |
| 365 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 366 | ledState.avail = getDeviceContext().hasLed(led); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 367 | ledState.on = false; |
| 368 | } |
| 369 | |
| 370 | void KeyboardInputMapper::updateLedState(bool reset) { |
Arthur Hung | fb3cc11 | 2022-04-13 07:39:50 +0000 | [diff] [blame] | 371 | // Clear the local led state then union the global led state. |
| 372 | mMetaState &= ~(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON); |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 373 | mMetaState |= getContext()->getLedMetaState(); |
Chris Ye | a52ade1 | 2020-08-27 16:49:20 -0700 | [diff] [blame] | 374 | |
| 375 | constexpr int32_t META_NUM = 3; |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 376 | const std::vector<int32_t> keyCodes{AKEYCODE_CAPS_LOCK, AKEYCODE_NUM_LOCK, |
| 377 | AKEYCODE_SCROLL_LOCK}; |
Chris Ye | a52ade1 | 2020-08-27 16:49:20 -0700 | [diff] [blame] | 378 | const std::array<int32_t, META_NUM> metaCodes = {AMETA_CAPS_LOCK_ON, AMETA_NUM_LOCK_ON, |
| 379 | AMETA_SCROLL_LOCK_ON}; |
| 380 | std::array<uint8_t, META_NUM> flags = {0, 0, 0}; |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 381 | bool hasKeyLayout = getDeviceContext().markSupportedKeyCodes(keyCodes, flags.data()); |
Chris Ye | a52ade1 | 2020-08-27 16:49:20 -0700 | [diff] [blame] | 382 | // If the device doesn't have the physical meta key it shouldn't generate the corresponding |
| 383 | // meta state. |
| 384 | if (hasKeyLayout) { |
| 385 | for (int i = 0; i < META_NUM; i++) { |
| 386 | if (!flags[i]) { |
| 387 | mMetaState &= ~metaCodes[i]; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 392 | updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK, AMETA_CAPS_LOCK_ON, reset); |
| 393 | updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK, AMETA_NUM_LOCK_ON, reset); |
| 394 | updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, reset); |
| 395 | } |
| 396 | |
| 397 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, int32_t led, |
| 398 | int32_t modifier, bool reset) { |
| 399 | if (ledState.avail) { |
| 400 | bool desiredState = (mMetaState & modifier) != 0; |
| 401 | if (reset || ledState.on != desiredState) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 402 | getDeviceContext().setLedState(led, desiredState); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 403 | ledState.on = desiredState; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | std::optional<int32_t> KeyboardInputMapper::getAssociatedDisplayId() { |
| 409 | if (mViewport) { |
| 410 | return std::make_optional(mViewport->displayId); |
| 411 | } |
| 412 | return std::nullopt; |
| 413 | } |
| 414 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 415 | std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) { |
| 416 | std::list<NotifyArgs> out; |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 417 | size_t n = mKeyDowns.size(); |
| 418 | for (size_t i = 0; i < n; i++) { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 419 | out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, |
| 420 | systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource, |
| 421 | getDisplayId(), 0 /*policyFlags*/, AKEY_EVENT_ACTION_UP, |
| 422 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, |
| 423 | mKeyDowns[i].keyCode, mKeyDowns[i].scanCode, AMETA_NONE, |
| 424 | mKeyDowns[i].downTime)); |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 425 | } |
| 426 | mKeyDowns.clear(); |
| 427 | mMetaState = AMETA_NONE; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 428 | return out; |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 431 | } // namespace android |