Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | #define LOG_TAG "Keyboard" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <limits.h> |
| 22 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | #include <input/InputDevice.h> |
Siarhei Vishniakou | a9fd82c | 2022-05-18 09:42:52 -0700 | [diff] [blame^] | 24 | #include <input/InputEventLabels.h> |
| 25 | #include <input/KeyCharacterMap.h> |
| 26 | #include <input/KeyLayoutMap.h> |
| 27 | #include <input/Keyboard.h> |
| 28 | #include <log/log.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 29 | #include <utils/Errors.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Siarhei Vishniakou | a9fd82c | 2022-05-18 09:42:52 -0700 | [diff] [blame^] | 33 | static std::string getPath(const InputDeviceIdentifier& deviceIdentifier, const std::string& name, |
| 34 | InputDeviceConfigurationFileType type) { |
| 35 | return name.empty() |
| 36 | ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type) |
| 37 | : getInputDeviceConfigurationFilePathByName(name, type); |
| 38 | } |
| 39 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 40 | // --- KeyMap --- |
| 41 | |
| 42 | KeyMap::KeyMap() { |
| 43 | } |
| 44 | |
| 45 | KeyMap::~KeyMap() { |
| 46 | } |
| 47 | |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 48 | status_t KeyMap::load(const InputDeviceIdentifier& deviceIdentifier, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 49 | const PropertyMap* deviceConfiguration) { |
| 50 | // Use the configured key layout if available. |
| 51 | if (deviceConfiguration) { |
| 52 | String8 keyLayoutName; |
| 53 | if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"), |
| 54 | keyLayoutName)) { |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 55 | status_t status = loadKeyLayout(deviceIdentifier, keyLayoutName.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 56 | if (status == NAME_NOT_FOUND) { |
| 57 | ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but " |
| 58 | "it was not found.", |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 59 | deviceIdentifier.name.c_str(), keyLayoutName.string()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | String8 keyCharacterMapName; |
| 64 | if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"), |
| 65 | keyCharacterMapName)) { |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 66 | status_t status = loadKeyCharacterMap(deviceIdentifier, keyCharacterMapName.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 67 | if (status == NAME_NOT_FOUND) { |
| 68 | ALOGE("Configuration for keyboard device '%s' requested keyboard character " |
| 69 | "map '%s' but it was not found.", |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 70 | deviceIdentifier.name.c_str(), keyCharacterMapName.string()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | if (isComplete()) { |
| 75 | return OK; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Try searching by device identifier. |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 80 | if (probeKeyMap(deviceIdentifier, "")) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 81 | return OK; |
| 82 | } |
| 83 | |
| 84 | // Fall back on the Generic key map. |
| 85 | // TODO Apply some additional heuristics here to figure out what kind of |
| 86 | // generic key map to use (US English, etc.) for typical external keyboards. |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 87 | if (probeKeyMap(deviceIdentifier, "Generic")) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 88 | return OK; |
| 89 | } |
| 90 | |
| 91 | // Try the Virtual key map as a last resort. |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 92 | if (probeKeyMap(deviceIdentifier, "Virtual")) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 93 | return OK; |
| 94 | } |
| 95 | |
| 96 | // Give up! |
| 97 | ALOGE("Could not determine key map for device '%s' and no default key maps were found!", |
Yuichiro Hanada | f92661b | 2019-06-06 18:50:18 +0900 | [diff] [blame] | 98 | deviceIdentifier.name.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 99 | return NAME_NOT_FOUND; |
| 100 | } |
| 101 | |
| 102 | bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier, |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 103 | const std::string& keyMapName) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 104 | if (!haveKeyLayout()) { |
| 105 | loadKeyLayout(deviceIdentifier, keyMapName); |
| 106 | } |
| 107 | if (!haveKeyCharacterMap()) { |
| 108 | loadKeyCharacterMap(deviceIdentifier, keyMapName); |
| 109 | } |
| 110 | return isComplete(); |
| 111 | } |
| 112 | |
| 113 | status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier, |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 114 | const std::string& name) { |
Chris Ye | 1d927aa | 2020-07-04 18:22:41 -0700 | [diff] [blame] | 115 | std::string path(getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_LAYOUT)); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 116 | if (path.empty()) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 117 | return NAME_NOT_FOUND; |
| 118 | } |
| 119 | |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 120 | base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path); |
Siarhei Vishniakou | a9fd82c | 2022-05-18 09:42:52 -0700 | [diff] [blame^] | 121 | if (ret.ok()) { |
| 122 | keyLayoutMap = *ret; |
| 123 | keyLayoutFile = path; |
| 124 | return OK; |
| 125 | } |
| 126 | |
| 127 | // Try to load fallback layout if the regular layout could not be loaded due to missing |
| 128 | // kernel modules |
| 129 | std::string fallbackPath( |
| 130 | getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, |
| 131 | InputDeviceConfigurationFileType:: |
| 132 | KEY_LAYOUT, |
| 133 | "_fallback")); |
| 134 | ret = KeyLayoutMap::load(fallbackPath); |
Bernie Innocenti | 55396a8 | 2020-12-19 18:47:35 +0900 | [diff] [blame] | 135 | if (!ret.ok()) { |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 136 | return ret.error().code(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 137 | } |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 138 | keyLayoutMap = *ret; |
Siarhei Vishniakou | a9fd82c | 2022-05-18 09:42:52 -0700 | [diff] [blame^] | 139 | keyLayoutFile = fallbackPath; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 140 | return OK; |
| 141 | } |
| 142 | |
| 143 | status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier, |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 144 | const std::string& name) { |
Chris Ye | 1d927aa | 2020-07-04 18:22:41 -0700 | [diff] [blame] | 145 | std::string path = |
| 146 | getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_CHARACTER_MAP); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 147 | if (path.empty()) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 148 | return NAME_NOT_FOUND; |
| 149 | } |
| 150 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 151 | base::Result<std::shared_ptr<KeyCharacterMap>> ret = |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 152 | KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE); |
Bernie Innocenti | 55396a8 | 2020-12-19 18:47:35 +0900 | [diff] [blame] | 153 | if (!ret.ok()) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 154 | return ret.error().code(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 155 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 156 | keyCharacterMap = *ret; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 157 | keyCharacterMapFile = path; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 158 | return OK; |
| 159 | } |
| 160 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 161 | // --- Global functions --- |
| 162 | |
Siarhei Vishniakou | 61da25a | 2018-02-15 21:04:49 -0600 | [diff] [blame] | 163 | bool isKeyboardSpecialFunction(const PropertyMap* config) { |
| 164 | if (config == nullptr) { |
| 165 | return false; |
| 166 | } |
| 167 | bool isSpecialFunction = false; |
| 168 | config->tryGetProperty(String8("keyboard.specialFunction"), isSpecialFunction); |
| 169 | return isSpecialFunction; |
| 170 | } |
| 171 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 172 | bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier, |
| 173 | const PropertyMap* deviceConfiguration, const KeyMap* keyMap) { |
Siarhei Vishniakou | 61da25a | 2018-02-15 21:04:49 -0600 | [diff] [blame] | 174 | // TODO: remove the third OR statement (SPECIAL_FUNCTION) in Q |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 175 | if (!keyMap->haveKeyCharacterMap() || isKeyboardSpecialFunction(deviceConfiguration) || |
| 176 | keyMap->keyCharacterMap->getKeyboardType() == |
| 177 | KeyCharacterMap::KeyboardType::SPECIAL_FUNCTION) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
| 181 | if (deviceConfiguration) { |
| 182 | bool builtIn = false; |
| 183 | if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn) |
| 184 | && builtIn) { |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 189 | return strstr(deviceIdentifier.name.c_str(), "-keypad"); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 192 | static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) { |
| 193 | int32_t newMetaState; |
| 194 | if (down) { |
| 195 | newMetaState = oldMetaState | mask; |
| 196 | } else { |
| 197 | newMetaState = oldMetaState & |
| 198 | ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON); |
| 199 | } |
| 200 | |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 201 | return normalizeMetaState(newMetaState); |
| 202 | } |
| 203 | |
| 204 | int32_t normalizeMetaState(int32_t oldMetaState) { |
| 205 | int32_t newMetaState = oldMetaState; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 206 | if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) { |
| 207 | newMetaState |= AMETA_ALT_ON; |
| 208 | } |
| 209 | |
| 210 | if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) { |
| 211 | newMetaState |= AMETA_SHIFT_ON; |
| 212 | } |
| 213 | |
| 214 | if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) { |
| 215 | newMetaState |= AMETA_CTRL_ON; |
| 216 | } |
| 217 | |
| 218 | if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) { |
| 219 | newMetaState |= AMETA_META_ON; |
| 220 | } |
| 221 | return newMetaState; |
| 222 | } |
| 223 | |
| 224 | static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) { |
| 225 | if (down) { |
| 226 | return oldMetaState; |
| 227 | } else { |
| 228 | return oldMetaState ^ mask; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 233 | switch (keyCode) { |
| 234 | case AKEYCODE_ALT_LEFT: |
| 235 | return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState); |
| 236 | case AKEYCODE_ALT_RIGHT: |
| 237 | return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState); |
| 238 | case AKEYCODE_SHIFT_LEFT: |
| 239 | return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState); |
| 240 | case AKEYCODE_SHIFT_RIGHT: |
| 241 | return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState); |
| 242 | case AKEYCODE_SYM: |
| 243 | return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState); |
| 244 | case AKEYCODE_FUNCTION: |
| 245 | return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState); |
| 246 | case AKEYCODE_CTRL_LEFT: |
| 247 | return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState); |
| 248 | case AKEYCODE_CTRL_RIGHT: |
| 249 | return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState); |
| 250 | case AKEYCODE_META_LEFT: |
| 251 | return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState); |
| 252 | case AKEYCODE_META_RIGHT: |
| 253 | return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState); |
| 254 | case AKEYCODE_CAPS_LOCK: |
| 255 | return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState); |
| 256 | case AKEYCODE_NUM_LOCK: |
| 257 | return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState); |
| 258 | case AKEYCODE_SCROLL_LOCK: |
| 259 | return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState); |
| 260 | default: |
| 261 | return oldMetaState; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | bool isMetaKey(int32_t keyCode) { |
| 266 | switch (keyCode) { |
| 267 | case AKEYCODE_ALT_LEFT: |
| 268 | case AKEYCODE_ALT_RIGHT: |
| 269 | case AKEYCODE_SHIFT_LEFT: |
| 270 | case AKEYCODE_SHIFT_RIGHT: |
| 271 | case AKEYCODE_SYM: |
| 272 | case AKEYCODE_FUNCTION: |
| 273 | case AKEYCODE_CTRL_LEFT: |
| 274 | case AKEYCODE_CTRL_RIGHT: |
| 275 | case AKEYCODE_META_LEFT: |
| 276 | case AKEYCODE_META_RIGHT: |
| 277 | case AKEYCODE_CAPS_LOCK: |
| 278 | case AKEYCODE_NUM_LOCK: |
| 279 | case AKEYCODE_SCROLL_LOCK: |
| 280 | return true; |
| 281 | default: |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | |
| 287 | } // namespace android |