Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 "KeyCharacterMap" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 22 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | #include <binder/Parcel.h> |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 24 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | #include <android/keycodes.h> |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 26 | #include <attestation/HmacKeyManager.h> |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 27 | #include <input/InputEventLabels.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | #include <input/KeyCharacterMap.h> |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 29 | #include <input/Keyboard.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 31 | #include <gui/constants.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | #include <utils/Errors.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 33 | #include <utils/Log.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 34 | #include <utils/Timers.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 35 | #include <utils/Tokenizer.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 36 | |
| 37 | // Enables debug output for the parser. |
| 38 | #define DEBUG_PARSER 0 |
| 39 | |
| 40 | // Enables debug output for parser performance. |
| 41 | #define DEBUG_PARSER_PERFORMANCE 0 |
| 42 | |
| 43 | // Enables debug output for mapping. |
| 44 | #define DEBUG_MAPPING 0 |
| 45 | |
| 46 | |
| 47 | namespace android { |
| 48 | |
| 49 | static const char* WHITESPACE = " \t\r"; |
| 50 | static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r,:"; |
| 51 | |
| 52 | struct Modifier { |
| 53 | const char* label; |
| 54 | int32_t metaState; |
| 55 | }; |
| 56 | static const Modifier modifiers[] = { |
| 57 | { "shift", AMETA_SHIFT_ON }, |
| 58 | { "lshift", AMETA_SHIFT_LEFT_ON }, |
| 59 | { "rshift", AMETA_SHIFT_RIGHT_ON }, |
| 60 | { "alt", AMETA_ALT_ON }, |
| 61 | { "lalt", AMETA_ALT_LEFT_ON }, |
| 62 | { "ralt", AMETA_ALT_RIGHT_ON }, |
| 63 | { "ctrl", AMETA_CTRL_ON }, |
| 64 | { "lctrl", AMETA_CTRL_LEFT_ON }, |
| 65 | { "rctrl", AMETA_CTRL_RIGHT_ON }, |
| 66 | { "meta", AMETA_META_ON }, |
| 67 | { "lmeta", AMETA_META_LEFT_ON }, |
| 68 | { "rmeta", AMETA_META_RIGHT_ON }, |
| 69 | { "sym", AMETA_SYM_ON }, |
| 70 | { "fn", AMETA_FUNCTION_ON }, |
| 71 | { "capslock", AMETA_CAPS_LOCK_ON }, |
| 72 | { "numlock", AMETA_NUM_LOCK_ON }, |
| 73 | { "scrolllock", AMETA_SCROLL_LOCK_ON }, |
| 74 | }; |
| 75 | |
| 76 | #if DEBUG_MAPPING |
| 77 | static String8 toString(const char16_t* chars, size_t numChars) { |
| 78 | String8 result; |
| 79 | for (size_t i = 0; i < numChars; i++) { |
| 80 | result.appendFormat(i == 0 ? "%d" : ", %d", chars[i]); |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | |
| 87 | // --- KeyCharacterMap --- |
| 88 | |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 89 | KeyCharacterMap::KeyCharacterMap() : mType(KeyboardType::UNKNOWN) {} |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 90 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 91 | KeyCharacterMap::KeyCharacterMap(const KeyCharacterMap& other) |
| 92 | : mType(other.mType), |
| 93 | mKeysByScanCode(other.mKeysByScanCode), |
| 94 | mKeysByUsageCode(other.mKeysByUsageCode) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 95 | for (size_t i = 0; i < other.mKeys.size(); i++) { |
| 96 | mKeys.add(other.mKeys.keyAt(i), new Key(*other.mKeys.valueAt(i))); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | KeyCharacterMap::~KeyCharacterMap() { |
| 101 | for (size_t i = 0; i < mKeys.size(); i++) { |
| 102 | Key* key = mKeys.editValueAt(i); |
| 103 | delete key; |
| 104 | } |
| 105 | } |
| 106 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 107 | bool KeyCharacterMap::operator==(const KeyCharacterMap& other) const { |
| 108 | if (mType != other.mType) { |
| 109 | return false; |
| 110 | } |
| 111 | if (mKeys.size() != other.mKeys.size() || |
| 112 | mKeysByScanCode.size() != other.mKeysByScanCode.size() || |
| 113 | mKeysByUsageCode.size() != other.mKeysByUsageCode.size()) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | for (size_t i = 0; i < mKeys.size(); i++) { |
| 118 | if (mKeys.keyAt(i) != other.mKeys.keyAt(i)) { |
| 119 | return false; |
| 120 | } |
| 121 | const Key* key = mKeys.valueAt(i); |
| 122 | const Key* otherKey = other.mKeys.valueAt(i); |
| 123 | if (key->label != otherKey->label || key->number != otherKey->number) { |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | for (size_t i = 0; i < mKeysByScanCode.size(); i++) { |
| 129 | if (mKeysByScanCode.keyAt(i) != other.mKeysByScanCode.keyAt(i)) { |
| 130 | return false; |
| 131 | } |
| 132 | if (mKeysByScanCode.valueAt(i) != other.mKeysByScanCode.valueAt(i)) { |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | for (size_t i = 0; i < mKeysByUsageCode.size(); i++) { |
| 138 | if (mKeysByUsageCode.keyAt(i) != other.mKeysByUsageCode.keyAt(i)) { |
| 139 | return false; |
| 140 | } |
| 141 | if (mKeysByUsageCode.valueAt(i) != other.mKeysByUsageCode.valueAt(i)) { |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 149 | base::Result<std::shared_ptr<KeyCharacterMap>> KeyCharacterMap::load(const std::string& filename, |
| 150 | Format format) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 151 | Tokenizer* tokenizer; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 152 | status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 153 | if (status) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 154 | return Errorf("Error {} opening key character map file {}.", status, filename.c_str()); |
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 | std::unique_ptr<Tokenizer> t(tokenizer); |
| 157 | auto ret = load(t.get(), format); |
Bernie Innocenti | 9c27af5 | 2020-12-21 12:50:14 +0900 | [diff] [blame] | 158 | if (ret.ok()) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 159 | (*ret)->mLoadFileName = filename; |
| 160 | } |
| 161 | return ret; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 164 | base::Result<std::shared_ptr<KeyCharacterMap>> KeyCharacterMap::loadContents( |
| 165 | const std::string& filename, const char* contents, Format format) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 166 | Tokenizer* tokenizer; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 167 | status_t status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 168 | if (status) { |
| 169 | ALOGE("Error %d opening key character map.", status); |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 170 | return Errorf("Error {} opening key character map.", status); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 171 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 172 | std::unique_ptr<Tokenizer> t(tokenizer); |
| 173 | auto ret = load(t.get(), format); |
Bernie Innocenti | 9c27af5 | 2020-12-21 12:50:14 +0900 | [diff] [blame] | 174 | if (ret.ok()) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 175 | (*ret)->mLoadFileName = filename; |
| 176 | } |
| 177 | return ret; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 180 | base::Result<std::shared_ptr<KeyCharacterMap>> KeyCharacterMap::load(Tokenizer* tokenizer, |
| 181 | Format format) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 182 | status_t status = OK; |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 183 | std::shared_ptr<KeyCharacterMap> map = std::shared_ptr<KeyCharacterMap>(new KeyCharacterMap()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 184 | if (!map.get()) { |
| 185 | ALOGE("Error allocating key character map."); |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 186 | return Errorf("Error allocating key character map."); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 187 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 188 | #if DEBUG_PARSER_PERFORMANCE |
| 189 | nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 190 | #endif |
| 191 | Parser parser(map.get(), tokenizer, format); |
| 192 | status = parser.parse(); |
| 193 | #if DEBUG_PARSER_PERFORMANCE |
| 194 | nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
| 195 | ALOGD("Parsed key character map file '%s' %d lines in %0.3fms.", |
| 196 | tokenizer->getFilename().string(), tokenizer->getLineNumber(), elapsedTime / 1000000.0); |
| 197 | #endif |
| 198 | if (status == OK) { |
| 199 | return map; |
| 200 | } |
| 201 | |
| 202 | return Errorf("Load KeyCharacterMap failed {}.", status); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 205 | void KeyCharacterMap::combine(const KeyCharacterMap& overlay) { |
| 206 | for (size_t i = 0; i < overlay.mKeys.size(); i++) { |
| 207 | int32_t keyCode = overlay.mKeys.keyAt(i); |
| 208 | Key* key = overlay.mKeys.valueAt(i); |
| 209 | ssize_t oldIndex = mKeys.indexOfKey(keyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 210 | if (oldIndex >= 0) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 211 | delete mKeys.valueAt(oldIndex); |
| 212 | mKeys.editValueAt(oldIndex) = new Key(*key); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 213 | } else { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 214 | mKeys.add(keyCode, new Key(*key)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 218 | for (size_t i = 0; i < overlay.mKeysByScanCode.size(); i++) { |
| 219 | mKeysByScanCode.replaceValueFor(overlay.mKeysByScanCode.keyAt(i), |
| 220 | overlay.mKeysByScanCode.valueAt(i)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 223 | for (size_t i = 0; i < overlay.mKeysByUsageCode.size(); i++) { |
| 224 | mKeysByUsageCode.replaceValueFor(overlay.mKeysByUsageCode.keyAt(i), |
| 225 | overlay.mKeysByUsageCode.valueAt(i)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 226 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 227 | mLoadFileName = overlay.mLoadFileName; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 230 | KeyCharacterMap::KeyboardType KeyCharacterMap::getKeyboardType() const { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 231 | return mType; |
| 232 | } |
| 233 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 234 | const std::string KeyCharacterMap::getLoadFileName() const { |
| 235 | return mLoadFileName; |
| 236 | } |
| 237 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 238 | char16_t KeyCharacterMap::getDisplayLabel(int32_t keyCode) const { |
| 239 | char16_t result = 0; |
| 240 | const Key* key; |
| 241 | if (getKey(keyCode, &key)) { |
| 242 | result = key->label; |
| 243 | } |
| 244 | #if DEBUG_MAPPING |
| 245 | ALOGD("getDisplayLabel: keyCode=%d ~ Result %d.", keyCode, result); |
| 246 | #endif |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | char16_t KeyCharacterMap::getNumber(int32_t keyCode) const { |
| 251 | char16_t result = 0; |
| 252 | const Key* key; |
| 253 | if (getKey(keyCode, &key)) { |
| 254 | result = key->number; |
| 255 | } |
| 256 | #if DEBUG_MAPPING |
| 257 | ALOGD("getNumber: keyCode=%d ~ Result %d.", keyCode, result); |
| 258 | #endif |
| 259 | return result; |
| 260 | } |
| 261 | |
| 262 | char16_t KeyCharacterMap::getCharacter(int32_t keyCode, int32_t metaState) const { |
| 263 | char16_t result = 0; |
| 264 | const Key* key; |
| 265 | const Behavior* behavior; |
| 266 | if (getKeyBehavior(keyCode, metaState, &key, &behavior)) { |
| 267 | result = behavior->character; |
| 268 | } |
| 269 | #if DEBUG_MAPPING |
| 270 | ALOGD("getCharacter: keyCode=%d, metaState=0x%08x ~ Result %d.", keyCode, metaState, result); |
| 271 | #endif |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | bool KeyCharacterMap::getFallbackAction(int32_t keyCode, int32_t metaState, |
| 276 | FallbackAction* outFallbackAction) const { |
| 277 | outFallbackAction->keyCode = 0; |
| 278 | outFallbackAction->metaState = 0; |
| 279 | |
| 280 | bool result = false; |
| 281 | const Key* key; |
| 282 | const Behavior* behavior; |
| 283 | if (getKeyBehavior(keyCode, metaState, &key, &behavior)) { |
| 284 | if (behavior->fallbackKeyCode) { |
| 285 | outFallbackAction->keyCode = behavior->fallbackKeyCode; |
| 286 | outFallbackAction->metaState = metaState & ~behavior->metaState; |
| 287 | result = true; |
| 288 | } |
| 289 | } |
| 290 | #if DEBUG_MAPPING |
| 291 | ALOGD("getFallbackKeyCode: keyCode=%d, metaState=0x%08x ~ Result %s, " |
| 292 | "fallback keyCode=%d, fallback metaState=0x%08x.", |
| 293 | keyCode, metaState, result ? "true" : "false", |
| 294 | outFallbackAction->keyCode, outFallbackAction->metaState); |
| 295 | #endif |
| 296 | return result; |
| 297 | } |
| 298 | |
| 299 | char16_t KeyCharacterMap::getMatch(int32_t keyCode, const char16_t* chars, size_t numChars, |
| 300 | int32_t metaState) const { |
| 301 | char16_t result = 0; |
| 302 | const Key* key; |
| 303 | if (getKey(keyCode, &key)) { |
| 304 | // Try to find the most general behavior that maps to this character. |
| 305 | // For example, the base key behavior will usually be last in the list. |
| 306 | // However, if we find a perfect meta state match for one behavior then use that one. |
| 307 | for (const Behavior* behavior = key->firstBehavior; behavior; behavior = behavior->next) { |
| 308 | if (behavior->character) { |
| 309 | for (size_t i = 0; i < numChars; i++) { |
| 310 | if (behavior->character == chars[i]) { |
| 311 | result = behavior->character; |
| 312 | if ((behavior->metaState & metaState) == behavior->metaState) { |
| 313 | goto ExactMatch; |
| 314 | } |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | ExactMatch: ; |
| 321 | } |
| 322 | #if DEBUG_MAPPING |
| 323 | ALOGD("getMatch: keyCode=%d, chars=[%s], metaState=0x%08x ~ Result %d.", |
| 324 | keyCode, toString(chars, numChars).string(), metaState, result); |
| 325 | #endif |
| 326 | return result; |
| 327 | } |
| 328 | |
| 329 | bool KeyCharacterMap::getEvents(int32_t deviceId, const char16_t* chars, size_t numChars, |
| 330 | Vector<KeyEvent>& outEvents) const { |
| 331 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 332 | |
| 333 | for (size_t i = 0; i < numChars; i++) { |
| 334 | int32_t keyCode, metaState; |
| 335 | char16_t ch = chars[i]; |
| 336 | if (!findKey(ch, &keyCode, &metaState)) { |
| 337 | #if DEBUG_MAPPING |
| 338 | ALOGD("getEvents: deviceId=%d, chars=[%s] ~ Failed to find mapping for character %d.", |
| 339 | deviceId, toString(chars, numChars).string(), ch); |
| 340 | #endif |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | int32_t currentMetaState = 0; |
| 345 | addMetaKeys(outEvents, deviceId, metaState, true, now, ¤tMetaState); |
| 346 | addKey(outEvents, deviceId, keyCode, currentMetaState, true, now); |
| 347 | addKey(outEvents, deviceId, keyCode, currentMetaState, false, now); |
| 348 | addMetaKeys(outEvents, deviceId, metaState, false, now, ¤tMetaState); |
| 349 | } |
| 350 | #if DEBUG_MAPPING |
| 351 | ALOGD("getEvents: deviceId=%d, chars=[%s] ~ Generated %d events.", |
| 352 | deviceId, toString(chars, numChars).string(), int32_t(outEvents.size())); |
| 353 | for (size_t i = 0; i < outEvents.size(); i++) { |
| 354 | ALOGD(" Key: keyCode=%d, metaState=0x%08x, %s.", |
| 355 | outEvents[i].getKeyCode(), outEvents[i].getMetaState(), |
| 356 | outEvents[i].getAction() == AKEY_EVENT_ACTION_DOWN ? "down" : "up"); |
| 357 | } |
| 358 | #endif |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | status_t KeyCharacterMap::mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode) const { |
| 363 | if (usageCode) { |
| 364 | ssize_t index = mKeysByUsageCode.indexOfKey(usageCode); |
| 365 | if (index >= 0) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 366 | *outKeyCode = mKeysByUsageCode.valueAt(index); |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 367 | #if DEBUG_MAPPING |
| 368 | ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d.", |
| 369 | scanCode, usageCode, *outKeyCode); |
| 370 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 371 | return OK; |
| 372 | } |
| 373 | } |
| 374 | if (scanCode) { |
| 375 | ssize_t index = mKeysByScanCode.indexOfKey(scanCode); |
| 376 | if (index >= 0) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 377 | *outKeyCode = mKeysByScanCode.valueAt(index); |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 378 | #if DEBUG_MAPPING |
| 379 | ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d.", |
| 380 | scanCode, usageCode, *outKeyCode); |
| 381 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 382 | return OK; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | #if DEBUG_MAPPING |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 387 | ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 388 | #endif |
| 389 | *outKeyCode = AKEYCODE_UNKNOWN; |
| 390 | return NAME_NOT_FOUND; |
| 391 | } |
| 392 | |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 393 | void KeyCharacterMap::tryRemapKey(int32_t keyCode, int32_t metaState, |
| 394 | int32_t *outKeyCode, int32_t *outMetaState) const { |
| 395 | *outKeyCode = keyCode; |
| 396 | *outMetaState = metaState; |
| 397 | |
| 398 | const Key* key; |
| 399 | const Behavior* behavior; |
| 400 | if (getKeyBehavior(keyCode, metaState, &key, &behavior)) { |
| 401 | if (behavior->replacementKeyCode) { |
| 402 | *outKeyCode = behavior->replacementKeyCode; |
| 403 | int32_t newMetaState = metaState & ~behavior->metaState; |
| 404 | // Reset dependent meta states. |
| 405 | if (behavior->metaState & AMETA_ALT_ON) { |
| 406 | newMetaState &= ~(AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON); |
| 407 | } |
| 408 | if (behavior->metaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) { |
| 409 | newMetaState &= ~AMETA_ALT_ON; |
| 410 | } |
| 411 | if (behavior->metaState & AMETA_CTRL_ON) { |
| 412 | newMetaState &= ~(AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON); |
| 413 | } |
| 414 | if (behavior->metaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) { |
| 415 | newMetaState &= ~AMETA_CTRL_ON; |
| 416 | } |
| 417 | if (behavior->metaState & AMETA_SHIFT_ON) { |
| 418 | newMetaState &= ~(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON); |
| 419 | } |
| 420 | if (behavior->metaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) { |
| 421 | newMetaState &= ~AMETA_SHIFT_ON; |
| 422 | } |
| 423 | // ... and put universal bits back if needed |
| 424 | *outMetaState = normalizeMetaState(newMetaState); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #if DEBUG_MAPPING |
| 429 | ALOGD("tryRemapKey: keyCode=%d, metaState=0x%08x ~ " |
| 430 | "replacement keyCode=%d, replacement metaState=0x%08x.", |
| 431 | keyCode, metaState, *outKeyCode, *outMetaState); |
| 432 | #endif |
| 433 | } |
| 434 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 435 | bool KeyCharacterMap::getKey(int32_t keyCode, const Key** outKey) const { |
| 436 | ssize_t index = mKeys.indexOfKey(keyCode); |
| 437 | if (index >= 0) { |
| 438 | *outKey = mKeys.valueAt(index); |
| 439 | return true; |
| 440 | } |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | bool KeyCharacterMap::getKeyBehavior(int32_t keyCode, int32_t metaState, |
| 445 | const Key** outKey, const Behavior** outBehavior) const { |
| 446 | const Key* key; |
| 447 | if (getKey(keyCode, &key)) { |
| 448 | const Behavior* behavior = key->firstBehavior; |
| 449 | while (behavior) { |
| 450 | if (matchesMetaState(metaState, behavior->metaState)) { |
| 451 | *outKey = key; |
| 452 | *outBehavior = behavior; |
| 453 | return true; |
| 454 | } |
| 455 | behavior = behavior->next; |
| 456 | } |
| 457 | } |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | bool KeyCharacterMap::matchesMetaState(int32_t eventMetaState, int32_t behaviorMetaState) { |
| 462 | // Behavior must have at least the set of meta states specified. |
| 463 | // And if the key event has CTRL, ALT or META then the behavior must exactly |
| 464 | // match those, taking into account that a behavior can specify that it handles |
| 465 | // one, both or either of a left/right modifier pair. |
| 466 | if ((eventMetaState & behaviorMetaState) == behaviorMetaState) { |
| 467 | const int32_t EXACT_META_STATES = |
| 468 | AMETA_CTRL_ON | AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON |
| 469 | | AMETA_ALT_ON | AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON |
| 470 | | AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON; |
| 471 | int32_t unmatchedMetaState = eventMetaState & ~behaviorMetaState & EXACT_META_STATES; |
| 472 | if (behaviorMetaState & AMETA_CTRL_ON) { |
| 473 | unmatchedMetaState &= ~(AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON); |
| 474 | } else if (behaviorMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) { |
| 475 | unmatchedMetaState &= ~AMETA_CTRL_ON; |
| 476 | } |
| 477 | if (behaviorMetaState & AMETA_ALT_ON) { |
| 478 | unmatchedMetaState &= ~(AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON); |
| 479 | } else if (behaviorMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) { |
| 480 | unmatchedMetaState &= ~AMETA_ALT_ON; |
| 481 | } |
| 482 | if (behaviorMetaState & AMETA_META_ON) { |
| 483 | unmatchedMetaState &= ~(AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 484 | } else if (behaviorMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) { |
| 485 | unmatchedMetaState &= ~AMETA_META_ON; |
| 486 | } |
| 487 | return !unmatchedMetaState; |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | bool KeyCharacterMap::findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const { |
| 493 | if (!ch) { |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | for (size_t i = 0; i < mKeys.size(); i++) { |
| 498 | const Key* key = mKeys.valueAt(i); |
| 499 | |
| 500 | // Try to find the most general behavior that maps to this character. |
| 501 | // For example, the base key behavior will usually be last in the list. |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 502 | const Behavior* found = nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 503 | for (const Behavior* behavior = key->firstBehavior; behavior; behavior = behavior->next) { |
| 504 | if (behavior->character == ch) { |
| 505 | found = behavior; |
| 506 | } |
| 507 | } |
| 508 | if (found) { |
| 509 | *outKeyCode = mKeys.keyAt(i); |
| 510 | *outMetaState = found->metaState; |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | void KeyCharacterMap::addKey(Vector<KeyEvent>& outEvents, |
| 518 | int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time) { |
| 519 | outEvents.push(); |
| 520 | KeyEvent& event = outEvents.editTop(); |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 521 | event.initialize(InputEvent::nextId(), deviceId, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 522 | INVALID_HMAC, down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, 0, keyCode, |
| 523 | 0, metaState, 0, time, time); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void KeyCharacterMap::addMetaKeys(Vector<KeyEvent>& outEvents, |
| 527 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 528 | int32_t* currentMetaState) { |
| 529 | // Add and remove meta keys symmetrically. |
| 530 | if (down) { |
| 531 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 532 | AKEYCODE_CAPS_LOCK, AMETA_CAPS_LOCK_ON, currentMetaState); |
| 533 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 534 | AKEYCODE_NUM_LOCK, AMETA_NUM_LOCK_ON, currentMetaState); |
| 535 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 536 | AKEYCODE_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, currentMetaState); |
| 537 | |
| 538 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 539 | AKEYCODE_SHIFT_LEFT, AMETA_SHIFT_LEFT_ON, |
| 540 | AKEYCODE_SHIFT_RIGHT, AMETA_SHIFT_RIGHT_ON, |
| 541 | AMETA_SHIFT_ON, currentMetaState); |
| 542 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 543 | AKEYCODE_ALT_LEFT, AMETA_ALT_LEFT_ON, |
| 544 | AKEYCODE_ALT_RIGHT, AMETA_ALT_RIGHT_ON, |
| 545 | AMETA_ALT_ON, currentMetaState); |
| 546 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 547 | AKEYCODE_CTRL_LEFT, AMETA_CTRL_LEFT_ON, |
| 548 | AKEYCODE_CTRL_RIGHT, AMETA_CTRL_RIGHT_ON, |
| 549 | AMETA_CTRL_ON, currentMetaState); |
| 550 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 551 | AKEYCODE_META_LEFT, AMETA_META_LEFT_ON, |
| 552 | AKEYCODE_META_RIGHT, AMETA_META_RIGHT_ON, |
| 553 | AMETA_META_ON, currentMetaState); |
| 554 | |
| 555 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 556 | AKEYCODE_SYM, AMETA_SYM_ON, currentMetaState); |
| 557 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 558 | AKEYCODE_FUNCTION, AMETA_FUNCTION_ON, currentMetaState); |
| 559 | } else { |
| 560 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 561 | AKEYCODE_FUNCTION, AMETA_FUNCTION_ON, currentMetaState); |
| 562 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 563 | AKEYCODE_SYM, AMETA_SYM_ON, currentMetaState); |
| 564 | |
| 565 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 566 | AKEYCODE_META_LEFT, AMETA_META_LEFT_ON, |
| 567 | AKEYCODE_META_RIGHT, AMETA_META_RIGHT_ON, |
| 568 | AMETA_META_ON, currentMetaState); |
| 569 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 570 | AKEYCODE_CTRL_LEFT, AMETA_CTRL_LEFT_ON, |
| 571 | AKEYCODE_CTRL_RIGHT, AMETA_CTRL_RIGHT_ON, |
| 572 | AMETA_CTRL_ON, currentMetaState); |
| 573 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 574 | AKEYCODE_ALT_LEFT, AMETA_ALT_LEFT_ON, |
| 575 | AKEYCODE_ALT_RIGHT, AMETA_ALT_RIGHT_ON, |
| 576 | AMETA_ALT_ON, currentMetaState); |
| 577 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 578 | AKEYCODE_SHIFT_LEFT, AMETA_SHIFT_LEFT_ON, |
| 579 | AKEYCODE_SHIFT_RIGHT, AMETA_SHIFT_RIGHT_ON, |
| 580 | AMETA_SHIFT_ON, currentMetaState); |
| 581 | |
| 582 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 583 | AKEYCODE_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, currentMetaState); |
| 584 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 585 | AKEYCODE_NUM_LOCK, AMETA_NUM_LOCK_ON, currentMetaState); |
| 586 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 587 | AKEYCODE_CAPS_LOCK, AMETA_CAPS_LOCK_ON, currentMetaState); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | bool KeyCharacterMap::addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, |
| 592 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 593 | int32_t keyCode, int32_t keyMetaState, |
| 594 | int32_t* currentMetaState) { |
| 595 | if ((metaState & keyMetaState) == keyMetaState) { |
| 596 | *currentMetaState = updateMetaState(keyCode, down, *currentMetaState); |
| 597 | addKey(outEvents, deviceId, keyCode, *currentMetaState, down, time); |
| 598 | return true; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | void KeyCharacterMap::addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, |
| 604 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 605 | int32_t leftKeyCode, int32_t leftKeyMetaState, |
| 606 | int32_t rightKeyCode, int32_t rightKeyMetaState, |
| 607 | int32_t eitherKeyMetaState, |
| 608 | int32_t* currentMetaState) { |
| 609 | bool specific = false; |
| 610 | specific |= addSingleEphemeralMetaKey(outEvents, deviceId, metaState, down, time, |
| 611 | leftKeyCode, leftKeyMetaState, currentMetaState); |
| 612 | specific |= addSingleEphemeralMetaKey(outEvents, deviceId, metaState, down, time, |
| 613 | rightKeyCode, rightKeyMetaState, currentMetaState); |
| 614 | |
| 615 | if (!specific) { |
| 616 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, down, time, |
| 617 | leftKeyCode, eitherKeyMetaState, currentMetaState); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void KeyCharacterMap::addLockedMetaKey(Vector<KeyEvent>& outEvents, |
| 622 | int32_t deviceId, int32_t metaState, nsecs_t time, |
| 623 | int32_t keyCode, int32_t keyMetaState, |
| 624 | int32_t* currentMetaState) { |
| 625 | if ((metaState & keyMetaState) == keyMetaState) { |
| 626 | *currentMetaState = updateMetaState(keyCode, true, *currentMetaState); |
| 627 | addKey(outEvents, deviceId, keyCode, *currentMetaState, true, time); |
| 628 | *currentMetaState = updateMetaState(keyCode, false, *currentMetaState); |
| 629 | addKey(outEvents, deviceId, keyCode, *currentMetaState, false, time); |
| 630 | } |
| 631 | } |
| 632 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 633 | #ifdef __linux__ |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 634 | std::shared_ptr<KeyCharacterMap> KeyCharacterMap::readFromParcel(Parcel* parcel) { |
| 635 | if (parcel == nullptr) { |
| 636 | ALOGE("%s: Null parcel", __func__); |
| 637 | return nullptr; |
| 638 | } |
| 639 | std::shared_ptr<KeyCharacterMap> map = std::shared_ptr<KeyCharacterMap>(new KeyCharacterMap()); |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 640 | map->mType = static_cast<KeyCharacterMap::KeyboardType>(parcel->readInt32()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 641 | size_t numKeys = parcel->readInt32(); |
| 642 | if (parcel->errorCheck()) { |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 643 | return nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 644 | } |
Michael Wright | 4c971c0 | 2015-10-21 14:38:03 +0100 | [diff] [blame] | 645 | if (numKeys > MAX_KEYS) { |
Ian Pedowitz | d57d9b9 | 2016-02-19 08:34:43 +0000 | [diff] [blame] | 646 | ALOGE("Too many keys in KeyCharacterMap (%zu > %d)", numKeys, MAX_KEYS); |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 647 | return nullptr; |
Michael Wright | 4c971c0 | 2015-10-21 14:38:03 +0100 | [diff] [blame] | 648 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 649 | |
| 650 | for (size_t i = 0; i < numKeys; i++) { |
| 651 | int32_t keyCode = parcel->readInt32(); |
| 652 | char16_t label = parcel->readInt32(); |
| 653 | char16_t number = parcel->readInt32(); |
| 654 | if (parcel->errorCheck()) { |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 655 | return nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | Key* key = new Key(); |
| 659 | key->label = label; |
| 660 | key->number = number; |
| 661 | map->mKeys.add(keyCode, key); |
| 662 | |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 663 | Behavior* lastBehavior = nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 664 | while (parcel->readInt32()) { |
| 665 | int32_t metaState = parcel->readInt32(); |
| 666 | char16_t character = parcel->readInt32(); |
| 667 | int32_t fallbackKeyCode = parcel->readInt32(); |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 668 | int32_t replacementKeyCode = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 669 | if (parcel->errorCheck()) { |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 670 | return nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | Behavior* behavior = new Behavior(); |
| 674 | behavior->metaState = metaState; |
| 675 | behavior->character = character; |
| 676 | behavior->fallbackKeyCode = fallbackKeyCode; |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 677 | behavior->replacementKeyCode = replacementKeyCode; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 678 | if (lastBehavior) { |
| 679 | lastBehavior->next = behavior; |
| 680 | } else { |
| 681 | key->firstBehavior = behavior; |
| 682 | } |
| 683 | lastBehavior = behavior; |
| 684 | } |
| 685 | |
| 686 | if (parcel->errorCheck()) { |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 687 | return nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | return map; |
| 691 | } |
| 692 | |
| 693 | void KeyCharacterMap::writeToParcel(Parcel* parcel) const { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 694 | if (parcel == nullptr) { |
| 695 | ALOGE("%s: Null parcel", __func__); |
| 696 | return; |
| 697 | } |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 698 | parcel->writeInt32(static_cast<int32_t>(mType)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 699 | |
| 700 | size_t numKeys = mKeys.size(); |
| 701 | parcel->writeInt32(numKeys); |
| 702 | for (size_t i = 0; i < numKeys; i++) { |
| 703 | int32_t keyCode = mKeys.keyAt(i); |
| 704 | const Key* key = mKeys.valueAt(i); |
| 705 | parcel->writeInt32(keyCode); |
| 706 | parcel->writeInt32(key->label); |
| 707 | parcel->writeInt32(key->number); |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 708 | for (const Behavior* behavior = key->firstBehavior; behavior != nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 709 | behavior = behavior->next) { |
| 710 | parcel->writeInt32(1); |
| 711 | parcel->writeInt32(behavior->metaState); |
| 712 | parcel->writeInt32(behavior->character); |
| 713 | parcel->writeInt32(behavior->fallbackKeyCode); |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 714 | parcel->writeInt32(behavior->replacementKeyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 715 | } |
| 716 | parcel->writeInt32(0); |
| 717 | } |
| 718 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 719 | #endif // __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 720 | |
| 721 | // --- KeyCharacterMap::Key --- |
| 722 | |
| 723 | KeyCharacterMap::Key::Key() : |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 724 | label(0), number(0), firstBehavior(nullptr) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | KeyCharacterMap::Key::Key(const Key& other) : |
| 728 | label(other.label), number(other.number), |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 729 | firstBehavior(other.firstBehavior ? new Behavior(*other.firstBehavior) : nullptr) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | KeyCharacterMap::Key::~Key() { |
| 733 | Behavior* behavior = firstBehavior; |
| 734 | while (behavior) { |
| 735 | Behavior* next = behavior->next; |
| 736 | delete behavior; |
| 737 | behavior = next; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | |
| 742 | // --- KeyCharacterMap::Behavior --- |
| 743 | |
| 744 | KeyCharacterMap::Behavior::Behavior() : |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 745 | next(nullptr), metaState(0), character(0), fallbackKeyCode(0), replacementKeyCode(0) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | KeyCharacterMap::Behavior::Behavior(const Behavior& other) : |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 749 | next(other.next ? new Behavior(*other.next) : nullptr), |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 750 | metaState(other.metaState), character(other.character), |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 751 | fallbackKeyCode(other.fallbackKeyCode), |
| 752 | replacementKeyCode(other.replacementKeyCode) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | |
| 756 | // --- KeyCharacterMap::Parser --- |
| 757 | |
| 758 | KeyCharacterMap::Parser::Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format) : |
| 759 | mMap(map), mTokenizer(tokenizer), mFormat(format), mState(STATE_TOP) { |
| 760 | } |
| 761 | |
| 762 | KeyCharacterMap::Parser::~Parser() { |
| 763 | } |
| 764 | |
| 765 | status_t KeyCharacterMap::Parser::parse() { |
| 766 | while (!mTokenizer->isEof()) { |
| 767 | #if DEBUG_PARSER |
| 768 | ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), |
| 769 | mTokenizer->peekRemainderOfLine().string()); |
| 770 | #endif |
| 771 | |
| 772 | mTokenizer->skipDelimiters(WHITESPACE); |
| 773 | |
| 774 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
| 775 | switch (mState) { |
| 776 | case STATE_TOP: { |
| 777 | String8 keywordToken = mTokenizer->nextToken(WHITESPACE); |
| 778 | if (keywordToken == "type") { |
| 779 | mTokenizer->skipDelimiters(WHITESPACE); |
| 780 | status_t status = parseType(); |
| 781 | if (status) return status; |
| 782 | } else if (keywordToken == "map") { |
| 783 | mTokenizer->skipDelimiters(WHITESPACE); |
| 784 | status_t status = parseMap(); |
| 785 | if (status) return status; |
| 786 | } else if (keywordToken == "key") { |
| 787 | mTokenizer->skipDelimiters(WHITESPACE); |
| 788 | status_t status = parseKey(); |
| 789 | if (status) return status; |
| 790 | } else { |
| 791 | ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(), |
| 792 | keywordToken.string()); |
| 793 | return BAD_VALUE; |
| 794 | } |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | case STATE_KEY: { |
| 799 | status_t status = parseKeyProperty(); |
| 800 | if (status) return status; |
| 801 | break; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | mTokenizer->skipDelimiters(WHITESPACE); |
| 806 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
| 807 | ALOGE("%s: Expected end of line or trailing comment, got '%s'.", |
| 808 | mTokenizer->getLocation().string(), |
| 809 | mTokenizer->peekRemainderOfLine().string()); |
| 810 | return BAD_VALUE; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | mTokenizer->nextLine(); |
| 815 | } |
| 816 | |
| 817 | if (mState != STATE_TOP) { |
| 818 | ALOGE("%s: Unterminated key description at end of file.", |
| 819 | mTokenizer->getLocation().string()); |
| 820 | return BAD_VALUE; |
| 821 | } |
| 822 | |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 823 | if (mMap->mType == KeyboardType::UNKNOWN) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 824 | ALOGE("%s: Keyboard layout missing required keyboard 'type' declaration.", |
| 825 | mTokenizer->getLocation().string()); |
| 826 | return BAD_VALUE; |
| 827 | } |
| 828 | |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 829 | if (mFormat == Format::BASE) { |
| 830 | if (mMap->mType == KeyboardType::OVERLAY) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 831 | ALOGE("%s: Base keyboard layout must specify a keyboard 'type' other than 'OVERLAY'.", |
| 832 | mTokenizer->getLocation().string()); |
| 833 | return BAD_VALUE; |
| 834 | } |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 835 | } else if (mFormat == Format::OVERLAY) { |
| 836 | if (mMap->mType != KeyboardType::OVERLAY) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 837 | ALOGE("%s: Overlay keyboard layout missing required keyboard " |
| 838 | "'type OVERLAY' declaration.", |
| 839 | mTokenizer->getLocation().string()); |
| 840 | return BAD_VALUE; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | return NO_ERROR; |
| 845 | } |
| 846 | |
| 847 | status_t KeyCharacterMap::Parser::parseType() { |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 848 | if (mMap->mType != KeyboardType::UNKNOWN) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 849 | ALOGE("%s: Duplicate keyboard 'type' declaration.", |
| 850 | mTokenizer->getLocation().string()); |
| 851 | return BAD_VALUE; |
| 852 | } |
| 853 | |
| 854 | KeyboardType type; |
| 855 | String8 typeToken = mTokenizer->nextToken(WHITESPACE); |
| 856 | if (typeToken == "NUMERIC") { |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 857 | type = KeyboardType::NUMERIC; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 858 | } else if (typeToken == "PREDICTIVE") { |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 859 | type = KeyboardType::PREDICTIVE; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 860 | } else if (typeToken == "ALPHA") { |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 861 | type = KeyboardType::ALPHA; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 862 | } else if (typeToken == "FULL") { |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 863 | type = KeyboardType::FULL; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 864 | } else if (typeToken == "SPECIAL_FUNCTION") { |
Siarhei Vishniakou | 61da25a | 2018-02-15 21:04:49 -0600 | [diff] [blame] | 865 | ALOGW("The SPECIAL_FUNCTION type is now declared in the device's IDC file, please set " |
| 866 | "the property 'keyboard.specialFunction' to '1' there instead."); |
| 867 | // TODO: return BAD_VALUE here in Q |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 868 | type = KeyboardType::SPECIAL_FUNCTION; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 869 | } else if (typeToken == "OVERLAY") { |
Michael Wright | 102936e | 2020-11-04 03:44:27 +0000 | [diff] [blame] | 870 | type = KeyboardType::OVERLAY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 871 | } else { |
| 872 | ALOGE("%s: Expected keyboard type label, got '%s'.", mTokenizer->getLocation().string(), |
| 873 | typeToken.string()); |
| 874 | return BAD_VALUE; |
| 875 | } |
| 876 | |
| 877 | #if DEBUG_PARSER |
| 878 | ALOGD("Parsed type: type=%d.", type); |
| 879 | #endif |
| 880 | mMap->mType = type; |
| 881 | return NO_ERROR; |
| 882 | } |
| 883 | |
| 884 | status_t KeyCharacterMap::Parser::parseMap() { |
| 885 | String8 keywordToken = mTokenizer->nextToken(WHITESPACE); |
| 886 | if (keywordToken == "key") { |
| 887 | mTokenizer->skipDelimiters(WHITESPACE); |
| 888 | return parseMapKey(); |
| 889 | } |
| 890 | ALOGE("%s: Expected keyword after 'map', got '%s'.", mTokenizer->getLocation().string(), |
| 891 | keywordToken.string()); |
| 892 | return BAD_VALUE; |
| 893 | } |
| 894 | |
| 895 | status_t KeyCharacterMap::Parser::parseMapKey() { |
| 896 | String8 codeToken = mTokenizer->nextToken(WHITESPACE); |
| 897 | bool mapUsage = false; |
| 898 | if (codeToken == "usage") { |
| 899 | mapUsage = true; |
| 900 | mTokenizer->skipDelimiters(WHITESPACE); |
| 901 | codeToken = mTokenizer->nextToken(WHITESPACE); |
| 902 | } |
| 903 | |
| 904 | char* end; |
| 905 | int32_t code = int32_t(strtol(codeToken.string(), &end, 0)); |
| 906 | if (*end) { |
| 907 | ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(), |
| 908 | mapUsage ? "usage" : "scan code", codeToken.string()); |
| 909 | return BAD_VALUE; |
| 910 | } |
| 911 | KeyedVector<int32_t, int32_t>& map = |
| 912 | mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode; |
| 913 | if (map.indexOfKey(code) >= 0) { |
| 914 | ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(), |
| 915 | mapUsage ? "usage" : "scan code", codeToken.string()); |
| 916 | return BAD_VALUE; |
| 917 | } |
| 918 | |
| 919 | mTokenizer->skipDelimiters(WHITESPACE); |
| 920 | String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE); |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 921 | int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 922 | if (!keyCode) { |
| 923 | ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(), |
| 924 | keyCodeToken.string()); |
| 925 | return BAD_VALUE; |
| 926 | } |
| 927 | |
| 928 | #if DEBUG_PARSER |
| 929 | ALOGD("Parsed map key %s: code=%d, keyCode=%d.", |
| 930 | mapUsage ? "usage" : "scan code", code, keyCode); |
| 931 | #endif |
| 932 | map.add(code, keyCode); |
| 933 | return NO_ERROR; |
| 934 | } |
| 935 | |
| 936 | status_t KeyCharacterMap::Parser::parseKey() { |
| 937 | String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE); |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 938 | int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 939 | if (!keyCode) { |
| 940 | ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(), |
| 941 | keyCodeToken.string()); |
| 942 | return BAD_VALUE; |
| 943 | } |
| 944 | if (mMap->mKeys.indexOfKey(keyCode) >= 0) { |
| 945 | ALOGE("%s: Duplicate entry for key code '%s'.", mTokenizer->getLocation().string(), |
| 946 | keyCodeToken.string()); |
| 947 | return BAD_VALUE; |
| 948 | } |
| 949 | |
| 950 | mTokenizer->skipDelimiters(WHITESPACE); |
| 951 | String8 openBraceToken = mTokenizer->nextToken(WHITESPACE); |
| 952 | if (openBraceToken != "{") { |
| 953 | ALOGE("%s: Expected '{' after key code label, got '%s'.", |
| 954 | mTokenizer->getLocation().string(), openBraceToken.string()); |
| 955 | return BAD_VALUE; |
| 956 | } |
| 957 | |
| 958 | #if DEBUG_PARSER |
| 959 | ALOGD("Parsed beginning of key: keyCode=%d.", keyCode); |
| 960 | #endif |
| 961 | mKeyCode = keyCode; |
| 962 | mMap->mKeys.add(keyCode, new Key()); |
| 963 | mState = STATE_KEY; |
| 964 | return NO_ERROR; |
| 965 | } |
| 966 | |
| 967 | status_t KeyCharacterMap::Parser::parseKeyProperty() { |
| 968 | Key* key = mMap->mKeys.valueFor(mKeyCode); |
| 969 | String8 token = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER); |
| 970 | if (token == "}") { |
| 971 | mState = STATE_TOP; |
| 972 | return finishKey(key); |
| 973 | } |
| 974 | |
| 975 | Vector<Property> properties; |
| 976 | |
| 977 | // Parse all comma-delimited property names up to the first colon. |
| 978 | for (;;) { |
| 979 | if (token == "label") { |
| 980 | properties.add(Property(PROPERTY_LABEL)); |
| 981 | } else if (token == "number") { |
| 982 | properties.add(Property(PROPERTY_NUMBER)); |
| 983 | } else { |
| 984 | int32_t metaState; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 985 | status_t status = parseModifier(token.string(), &metaState); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 986 | if (status) { |
| 987 | ALOGE("%s: Expected a property name or modifier, got '%s'.", |
| 988 | mTokenizer->getLocation().string(), token.string()); |
| 989 | return status; |
| 990 | } |
| 991 | properties.add(Property(PROPERTY_META, metaState)); |
| 992 | } |
| 993 | |
| 994 | mTokenizer->skipDelimiters(WHITESPACE); |
| 995 | if (!mTokenizer->isEol()) { |
| 996 | char ch = mTokenizer->nextChar(); |
| 997 | if (ch == ':') { |
| 998 | break; |
| 999 | } else if (ch == ',') { |
| 1000 | mTokenizer->skipDelimiters(WHITESPACE); |
| 1001 | token = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER); |
| 1002 | continue; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | ALOGE("%s: Expected ',' or ':' after property name.", |
| 1007 | mTokenizer->getLocation().string()); |
| 1008 | return BAD_VALUE; |
| 1009 | } |
| 1010 | |
| 1011 | // Parse behavior after the colon. |
| 1012 | mTokenizer->skipDelimiters(WHITESPACE); |
| 1013 | |
| 1014 | Behavior behavior; |
| 1015 | bool haveCharacter = false; |
| 1016 | bool haveFallback = false; |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1017 | bool haveReplacement = false; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1018 | |
| 1019 | do { |
| 1020 | char ch = mTokenizer->peekChar(); |
| 1021 | if (ch == '\'') { |
| 1022 | char16_t character; |
| 1023 | status_t status = parseCharacterLiteral(&character); |
| 1024 | if (status || !character) { |
| 1025 | ALOGE("%s: Invalid character literal for key.", |
| 1026 | mTokenizer->getLocation().string()); |
| 1027 | return BAD_VALUE; |
| 1028 | } |
| 1029 | if (haveCharacter) { |
| 1030 | ALOGE("%s: Cannot combine multiple character literals or 'none'.", |
| 1031 | mTokenizer->getLocation().string()); |
| 1032 | return BAD_VALUE; |
| 1033 | } |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1034 | if (haveReplacement) { |
| 1035 | ALOGE("%s: Cannot combine character literal with replace action.", |
| 1036 | mTokenizer->getLocation().string()); |
| 1037 | return BAD_VALUE; |
| 1038 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1039 | behavior.character = character; |
| 1040 | haveCharacter = true; |
| 1041 | } else { |
| 1042 | token = mTokenizer->nextToken(WHITESPACE); |
| 1043 | if (token == "none") { |
| 1044 | if (haveCharacter) { |
| 1045 | ALOGE("%s: Cannot combine multiple character literals or 'none'.", |
| 1046 | mTokenizer->getLocation().string()); |
| 1047 | return BAD_VALUE; |
| 1048 | } |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1049 | if (haveReplacement) { |
| 1050 | ALOGE("%s: Cannot combine 'none' with replace action.", |
| 1051 | mTokenizer->getLocation().string()); |
| 1052 | return BAD_VALUE; |
| 1053 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1054 | haveCharacter = true; |
| 1055 | } else if (token == "fallback") { |
| 1056 | mTokenizer->skipDelimiters(WHITESPACE); |
| 1057 | token = mTokenizer->nextToken(WHITESPACE); |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 1058 | int32_t keyCode = InputEventLookup::getKeyCodeByLabel(token.string()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1059 | if (!keyCode) { |
| 1060 | ALOGE("%s: Invalid key code label for fallback behavior, got '%s'.", |
| 1061 | mTokenizer->getLocation().string(), |
| 1062 | token.string()); |
| 1063 | return BAD_VALUE; |
| 1064 | } |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1065 | if (haveFallback || haveReplacement) { |
| 1066 | ALOGE("%s: Cannot combine multiple fallback/replacement key codes.", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1067 | mTokenizer->getLocation().string()); |
| 1068 | return BAD_VALUE; |
| 1069 | } |
| 1070 | behavior.fallbackKeyCode = keyCode; |
| 1071 | haveFallback = true; |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1072 | } else if (token == "replace") { |
| 1073 | mTokenizer->skipDelimiters(WHITESPACE); |
| 1074 | token = mTokenizer->nextToken(WHITESPACE); |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 1075 | int32_t keyCode = InputEventLookup::getKeyCodeByLabel(token.string()); |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1076 | if (!keyCode) { |
| 1077 | ALOGE("%s: Invalid key code label for replace, got '%s'.", |
| 1078 | mTokenizer->getLocation().string(), |
| 1079 | token.string()); |
| 1080 | return BAD_VALUE; |
| 1081 | } |
| 1082 | if (haveCharacter) { |
| 1083 | ALOGE("%s: Cannot combine character literal with replace action.", |
| 1084 | mTokenizer->getLocation().string()); |
| 1085 | return BAD_VALUE; |
| 1086 | } |
| 1087 | if (haveFallback || haveReplacement) { |
| 1088 | ALOGE("%s: Cannot combine multiple fallback/replacement key codes.", |
| 1089 | mTokenizer->getLocation().string()); |
| 1090 | return BAD_VALUE; |
| 1091 | } |
| 1092 | behavior.replacementKeyCode = keyCode; |
| 1093 | haveReplacement = true; |
| 1094 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1095 | } else { |
| 1096 | ALOGE("%s: Expected a key behavior after ':'.", |
| 1097 | mTokenizer->getLocation().string()); |
| 1098 | return BAD_VALUE; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | mTokenizer->skipDelimiters(WHITESPACE); |
| 1103 | } while (!mTokenizer->isEol() && mTokenizer->peekChar() != '#'); |
| 1104 | |
| 1105 | // Add the behavior. |
| 1106 | for (size_t i = 0; i < properties.size(); i++) { |
| 1107 | const Property& property = properties.itemAt(i); |
| 1108 | switch (property.property) { |
| 1109 | case PROPERTY_LABEL: |
| 1110 | if (key->label) { |
| 1111 | ALOGE("%s: Duplicate label for key.", |
| 1112 | mTokenizer->getLocation().string()); |
| 1113 | return BAD_VALUE; |
| 1114 | } |
| 1115 | key->label = behavior.character; |
| 1116 | #if DEBUG_PARSER |
| 1117 | ALOGD("Parsed key label: keyCode=%d, label=%d.", mKeyCode, key->label); |
| 1118 | #endif |
| 1119 | break; |
| 1120 | case PROPERTY_NUMBER: |
| 1121 | if (key->number) { |
| 1122 | ALOGE("%s: Duplicate number for key.", |
| 1123 | mTokenizer->getLocation().string()); |
| 1124 | return BAD_VALUE; |
| 1125 | } |
| 1126 | key->number = behavior.character; |
| 1127 | #if DEBUG_PARSER |
| 1128 | ALOGD("Parsed key number: keyCode=%d, number=%d.", mKeyCode, key->number); |
| 1129 | #endif |
| 1130 | break; |
| 1131 | case PROPERTY_META: { |
| 1132 | for (Behavior* b = key->firstBehavior; b; b = b->next) { |
| 1133 | if (b->metaState == property.metaState) { |
| 1134 | ALOGE("%s: Duplicate key behavior for modifier.", |
| 1135 | mTokenizer->getLocation().string()); |
| 1136 | return BAD_VALUE; |
| 1137 | } |
| 1138 | } |
| 1139 | Behavior* newBehavior = new Behavior(behavior); |
| 1140 | newBehavior->metaState = property.metaState; |
| 1141 | newBehavior->next = key->firstBehavior; |
| 1142 | key->firstBehavior = newBehavior; |
| 1143 | #if DEBUG_PARSER |
Dmitry Torokhov | 115f93e | 2015-09-17 18:04:50 -0700 | [diff] [blame] | 1144 | ALOGD("Parsed key meta: keyCode=%d, meta=0x%x, char=%d, fallback=%d replace=%d.", |
| 1145 | mKeyCode, |
| 1146 | newBehavior->metaState, newBehavior->character, |
| 1147 | newBehavior->fallbackKeyCode, newBehavior->replacementKeyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1148 | #endif |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | return NO_ERROR; |
| 1154 | } |
| 1155 | |
| 1156 | status_t KeyCharacterMap::Parser::finishKey(Key* key) { |
| 1157 | // Fill in default number property. |
| 1158 | if (!key->number) { |
| 1159 | char16_t digit = 0; |
| 1160 | char16_t symbol = 0; |
| 1161 | for (Behavior* b = key->firstBehavior; b; b = b->next) { |
| 1162 | char16_t ch = b->character; |
| 1163 | if (ch) { |
| 1164 | if (ch >= '0' && ch <= '9') { |
| 1165 | digit = ch; |
| 1166 | } else if (ch == '(' || ch == ')' || ch == '#' || ch == '*' |
| 1167 | || ch == '-' || ch == '+' || ch == ',' || ch == '.' |
| 1168 | || ch == '\'' || ch == ':' || ch == ';' || ch == '/') { |
| 1169 | symbol = ch; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | key->number = digit ? digit : symbol; |
| 1174 | } |
| 1175 | return NO_ERROR; |
| 1176 | } |
| 1177 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1178 | status_t KeyCharacterMap::Parser::parseModifier(const std::string& token, int32_t* outMetaState) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1179 | if (token == "base") { |
| 1180 | *outMetaState = 0; |
| 1181 | return NO_ERROR; |
| 1182 | } |
| 1183 | |
| 1184 | int32_t combinedMeta = 0; |
| 1185 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1186 | const char* str = token.c_str(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1187 | const char* start = str; |
| 1188 | for (const char* cur = str; ; cur++) { |
| 1189 | char ch = *cur; |
| 1190 | if (ch == '+' || ch == '\0') { |
| 1191 | size_t len = cur - start; |
| 1192 | int32_t metaState = 0; |
| 1193 | for (size_t i = 0; i < sizeof(modifiers) / sizeof(Modifier); i++) { |
| 1194 | if (strlen(modifiers[i].label) == len |
| 1195 | && strncmp(modifiers[i].label, start, len) == 0) { |
| 1196 | metaState = modifiers[i].metaState; |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
| 1200 | if (!metaState) { |
| 1201 | return BAD_VALUE; |
| 1202 | } |
| 1203 | if (combinedMeta & metaState) { |
| 1204 | ALOGE("%s: Duplicate modifier combination '%s'.", |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1205 | mTokenizer->getLocation().string(), token.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1206 | return BAD_VALUE; |
| 1207 | } |
| 1208 | |
| 1209 | combinedMeta |= metaState; |
| 1210 | start = cur + 1; |
| 1211 | |
| 1212 | if (ch == '\0') { |
| 1213 | break; |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | *outMetaState = combinedMeta; |
| 1218 | return NO_ERROR; |
| 1219 | } |
| 1220 | |
| 1221 | status_t KeyCharacterMap::Parser::parseCharacterLiteral(char16_t* outCharacter) { |
| 1222 | char ch = mTokenizer->nextChar(); |
| 1223 | if (ch != '\'') { |
| 1224 | goto Error; |
| 1225 | } |
| 1226 | |
| 1227 | ch = mTokenizer->nextChar(); |
| 1228 | if (ch == '\\') { |
| 1229 | // Escape sequence. |
| 1230 | ch = mTokenizer->nextChar(); |
| 1231 | if (ch == 'n') { |
| 1232 | *outCharacter = '\n'; |
| 1233 | } else if (ch == 't') { |
| 1234 | *outCharacter = '\t'; |
| 1235 | } else if (ch == '\\') { |
| 1236 | *outCharacter = '\\'; |
| 1237 | } else if (ch == '\'') { |
| 1238 | *outCharacter = '\''; |
| 1239 | } else if (ch == '"') { |
| 1240 | *outCharacter = '"'; |
| 1241 | } else if (ch == 'u') { |
| 1242 | *outCharacter = 0; |
| 1243 | for (int i = 0; i < 4; i++) { |
| 1244 | ch = mTokenizer->nextChar(); |
| 1245 | int digit; |
| 1246 | if (ch >= '0' && ch <= '9') { |
| 1247 | digit = ch - '0'; |
| 1248 | } else if (ch >= 'A' && ch <= 'F') { |
| 1249 | digit = ch - 'A' + 10; |
| 1250 | } else if (ch >= 'a' && ch <= 'f') { |
| 1251 | digit = ch - 'a' + 10; |
| 1252 | } else { |
| 1253 | goto Error; |
| 1254 | } |
| 1255 | *outCharacter = (*outCharacter << 4) | digit; |
| 1256 | } |
| 1257 | } else { |
| 1258 | goto Error; |
| 1259 | } |
| 1260 | } else if (ch >= 32 && ch <= 126 && ch != '\'') { |
| 1261 | // ASCII literal character. |
| 1262 | *outCharacter = ch; |
| 1263 | } else { |
| 1264 | goto Error; |
| 1265 | } |
| 1266 | |
| 1267 | ch = mTokenizer->nextChar(); |
| 1268 | if (ch != '\'') { |
| 1269 | goto Error; |
| 1270 | } |
| 1271 | |
| 1272 | // Ensure that we consumed the entire token. |
| 1273 | if (mTokenizer->nextToken(WHITESPACE).isEmpty()) { |
| 1274 | return NO_ERROR; |
| 1275 | } |
| 1276 | |
| 1277 | Error: |
| 1278 | ALOGE("%s: Malformed character literal.", mTokenizer->getLocation().string()); |
| 1279 | return BAD_VALUE; |
| 1280 | } |
| 1281 | |
| 1282 | } // namespace android |