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