Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #define LOG_TAG "KeyCharacterMap" |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdlib.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <string.h> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 21 | #include <android/keycodes.h> |
| 22 | #include <ui/Keyboard.h> |
| 23 | #include <ui/KeyCharacterMap.h> |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/Errors.h> |
| 26 | #include <utils/Tokenizer.h> |
| 27 | #include <utils/Timers.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 29 | // Enables debug output for the parser. |
| 30 | #define DEBUG_PARSER 0 |
| 31 | |
| 32 | // Enables debug output for parser performance. |
| 33 | #define DEBUG_PARSER_PERFORMANCE 0 |
| 34 | |
| 35 | // Enables debug output for mapping. |
| 36 | #define DEBUG_MAPPING 0 |
| 37 | |
| 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | static const char* WHITESPACE = " \t\r"; |
| 42 | static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r,:"; |
| 43 | |
| 44 | struct Modifier { |
| 45 | const char* label; |
| 46 | int32_t metaState; |
| 47 | }; |
| 48 | static const Modifier modifiers[] = { |
| 49 | { "shift", AMETA_SHIFT_ON }, |
| 50 | { "lshift", AMETA_SHIFT_LEFT_ON }, |
| 51 | { "rshift", AMETA_SHIFT_RIGHT_ON }, |
| 52 | { "alt", AMETA_ALT_ON }, |
| 53 | { "lalt", AMETA_ALT_LEFT_ON }, |
| 54 | { "ralt", AMETA_ALT_RIGHT_ON }, |
| 55 | { "ctrl", AMETA_CTRL_ON }, |
| 56 | { "lctrl", AMETA_CTRL_LEFT_ON }, |
| 57 | { "rctrl", AMETA_CTRL_RIGHT_ON }, |
| 58 | { "meta", AMETA_META_ON }, |
| 59 | { "lmeta", AMETA_META_LEFT_ON }, |
| 60 | { "rmeta", AMETA_META_RIGHT_ON }, |
| 61 | { "sym", AMETA_SYM_ON }, |
| 62 | { "fn", AMETA_FUNCTION_ON }, |
| 63 | { "capslock", AMETA_CAPS_LOCK_ON }, |
| 64 | { "numlock", AMETA_NUM_LOCK_ON }, |
| 65 | { "scrolllock", AMETA_SCROLL_LOCK_ON }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 68 | #if DEBUG_MAPPING |
| 69 | static String8 toString(const char16_t* chars, size_t numChars) { |
| 70 | String8 result; |
| 71 | for (size_t i = 0; i < numChars; i++) { |
| 72 | result.appendFormat(i == 0 ? "%d" : ", %d", chars[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 74 | return result; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | // --- KeyCharacterMap --- |
| 80 | |
| 81 | KeyCharacterMap::KeyCharacterMap() : |
| 82 | mType(KEYBOARD_TYPE_UNKNOWN) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 85 | KeyCharacterMap::~KeyCharacterMap() { |
| 86 | for (size_t i = 0; i < mKeys.size(); i++) { |
| 87 | Key* key = mKeys.editValueAt(i); |
| 88 | delete key; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 92 | status_t KeyCharacterMap::load(const String8& filename, KeyCharacterMap** outMap) { |
| 93 | *outMap = NULL; |
| 94 | |
| 95 | Tokenizer* tokenizer; |
| 96 | status_t status = Tokenizer::open(filename, &tokenizer); |
| 97 | if (status) { |
| 98 | LOGE("Error %d opening key character map file %s.", status, filename.string()); |
| 99 | } else { |
| 100 | KeyCharacterMap* map = new KeyCharacterMap(); |
| 101 | if (!map) { |
| 102 | LOGE("Error allocating key character map."); |
| 103 | status = NO_MEMORY; |
| 104 | } else { |
| 105 | #if DEBUG_PARSER_PERFORMANCE |
| 106 | nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 107 | #endif |
| 108 | Parser parser(map, tokenizer); |
| 109 | status = parser.parse(); |
| 110 | #if DEBUG_PARSER_PERFORMANCE |
| 111 | nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
| 112 | LOGD("Parsed key character map file '%s' %d lines in %0.3fms.", |
| 113 | tokenizer->getFilename().string(), tokenizer->getLineNumber(), |
| 114 | elapsedTime / 1000000.0); |
| 115 | #endif |
| 116 | if (status) { |
| 117 | delete map; |
| 118 | } else { |
| 119 | *outMap = map; |
| 120 | } |
| 121 | } |
| 122 | delete tokenizer; |
| 123 | } |
| 124 | return status; |
| 125 | } |
| 126 | |
| 127 | status_t KeyCharacterMap::loadByDeviceId(int32_t deviceId, KeyCharacterMap** outMap) { |
| 128 | *outMap = NULL; |
| 129 | |
| 130 | String8 filename; |
| 131 | status_t result = getKeyCharacterMapFile(deviceId, filename); |
| 132 | if (!result) { |
| 133 | result = load(filename, outMap); |
| 134 | } |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | int32_t KeyCharacterMap::getKeyboardType() const { |
| 139 | return mType; |
| 140 | } |
| 141 | |
| 142 | char16_t KeyCharacterMap::getDisplayLabel(int32_t keyCode) const { |
| 143 | char16_t result = 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 144 | const Key* key; |
| 145 | if (getKey(keyCode, &key)) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 146 | result = key->label; |
| 147 | } |
| 148 | #if DEBUG_MAPPING |
| 149 | LOGD("getDisplayLabel: keyCode=%d ~ Result %d.", keyCode, result); |
| 150 | #endif |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | char16_t KeyCharacterMap::getNumber(int32_t keyCode) const { |
| 155 | char16_t result = 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 156 | const Key* key; |
| 157 | if (getKey(keyCode, &key)) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 158 | result = key->number; |
| 159 | } |
| 160 | #if DEBUG_MAPPING |
| 161 | LOGD("getNumber: keyCode=%d ~ Result %d.", keyCode, result); |
| 162 | #endif |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | char16_t KeyCharacterMap::getCharacter(int32_t keyCode, int32_t metaState) const { |
| 167 | char16_t result = 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 168 | const Key* key; |
| 169 | const Behavior* behavior; |
| 170 | if (getKeyBehavior(keyCode, metaState, &key, &behavior)) { |
| 171 | result = behavior->character; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 172 | } |
| 173 | #if DEBUG_MAPPING |
| 174 | LOGD("getCharacter: keyCode=%d, metaState=0x%08x ~ Result %d.", keyCode, metaState, result); |
| 175 | #endif |
| 176 | return result; |
| 177 | } |
| 178 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 179 | bool KeyCharacterMap::getFallbackAction(int32_t keyCode, int32_t metaState, |
| 180 | FallbackAction* outFallbackAction) const { |
| 181 | outFallbackAction->keyCode = 0; |
| 182 | outFallbackAction->metaState = 0; |
| 183 | |
| 184 | bool result = false; |
| 185 | const Key* key; |
| 186 | const Behavior* behavior; |
| 187 | if (getKeyBehavior(keyCode, metaState, &key, &behavior)) { |
| 188 | outFallbackAction->keyCode = behavior->fallbackKeyCode; |
| 189 | outFallbackAction->metaState = metaState & ~behavior->metaState; |
| 190 | result = true; |
| 191 | } |
| 192 | #if DEBUG_MAPPING |
| 193 | LOGD("getFallbackKeyCode: keyCode=%d, metaState=0x%08x ~ Result %s, " |
| 194 | "fallback keyCode=%d, fallback metaState=0x%08x.", |
| 195 | keyCode, metaState, result ? "true" : "false", |
| 196 | outFallbackAction->keyCode, outFallbackAction->metaState); |
| 197 | #endif |
| 198 | return result; |
| 199 | } |
| 200 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 201 | char16_t KeyCharacterMap::getMatch(int32_t keyCode, const char16_t* chars, size_t numChars, |
| 202 | int32_t metaState) const { |
| 203 | char16_t result = 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 204 | const Key* key; |
| 205 | if (getKey(keyCode, &key)) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 206 | // Try to find the most general behavior that maps to this character. |
| 207 | // For example, the base key behavior will usually be last in the list. |
| 208 | // However, if we find a perfect meta state match for one behavior then use that one. |
| 209 | for (const Behavior* behavior = key->firstBehavior; behavior; behavior = behavior->next) { |
| 210 | if (behavior->character) { |
| 211 | for (size_t i = 0; i < numChars; i++) { |
| 212 | if (behavior->character == chars[i]) { |
| 213 | result = behavior->character; |
| 214 | if ((behavior->metaState & metaState) == behavior->metaState) { |
| 215 | goto ExactMatch; |
| 216 | } |
| 217 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 222 | ExactMatch: ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 224 | #if DEBUG_MAPPING |
| 225 | LOGD("getMatch: keyCode=%d, chars=[%s], metaState=0x%08x ~ Result %d.", |
| 226 | keyCode, toString(chars, numChars).string(), metaState, result); |
| 227 | #endif |
| 228 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 231 | bool KeyCharacterMap::getEvents(int32_t deviceId, const char16_t* chars, size_t numChars, |
| 232 | Vector<KeyEvent>& outEvents) const { |
| 233 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 234 | |
| 235 | for (size_t i = 0; i < numChars; i++) { |
| 236 | int32_t keyCode, metaState; |
| 237 | char16_t ch = chars[i]; |
| 238 | if (!findKey(ch, &keyCode, &metaState)) { |
| 239 | #if DEBUG_MAPPING |
| 240 | LOGD("getEvents: deviceId=%d, chars=[%s] ~ Failed to find mapping for character %d.", |
| 241 | deviceId, toString(chars, numChars).string(), ch); |
| 242 | #endif |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | int32_t currentMetaState = 0; |
| 247 | addMetaKeys(outEvents, deviceId, metaState, true, now, ¤tMetaState); |
| 248 | addKey(outEvents, deviceId, keyCode, currentMetaState, true, now); |
| 249 | addKey(outEvents, deviceId, keyCode, currentMetaState, false, now); |
| 250 | addMetaKeys(outEvents, deviceId, metaState, false, now, ¤tMetaState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 252 | #if DEBUG_MAPPING |
| 253 | LOGD("getEvents: deviceId=%d, chars=[%s] ~ Generated %d events.", |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 254 | deviceId, toString(chars, numChars).string(), int32_t(outEvents.size())); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 255 | for (size_t i = 0; i < outEvents.size(); i++) { |
| 256 | LOGD(" Key: keyCode=%d, metaState=0x%08x, %s.", |
| 257 | outEvents[i].getKeyCode(), outEvents[i].getMetaState(), |
| 258 | outEvents[i].getAction() == AKEY_EVENT_ACTION_DOWN ? "down" : "up"); |
| 259 | } |
| 260 | #endif |
| 261 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 264 | bool KeyCharacterMap::getKey(int32_t keyCode, const Key** outKey) const { |
| 265 | ssize_t index = mKeys.indexOfKey(keyCode); |
| 266 | if (index >= 0) { |
| 267 | *outKey = mKeys.valueAt(index); |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | bool KeyCharacterMap::getKeyBehavior(int32_t keyCode, int32_t metaState, |
| 274 | const Key** outKey, const Behavior** outBehavior) const { |
| 275 | const Key* key; |
| 276 | if (getKey(keyCode, &key)) { |
| 277 | const Behavior* behavior = key->firstBehavior; |
| 278 | while (behavior) { |
| 279 | if ((behavior->metaState & metaState) == behavior->metaState) { |
| 280 | *outKey = key; |
| 281 | *outBehavior = behavior; |
| 282 | return true; |
| 283 | } |
| 284 | behavior = behavior->next; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 290 | bool KeyCharacterMap::findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const { |
| 291 | if (!ch) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | return false; |
| 293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 295 | for (size_t i = 0; i < mKeys.size(); i++) { |
| 296 | const Key* key = mKeys.valueAt(i); |
| 297 | |
| 298 | // Try to find the most general behavior that maps to this character. |
| 299 | // For example, the base key behavior will usually be last in the list. |
| 300 | const Behavior* found = NULL; |
| 301 | for (const Behavior* behavior = key->firstBehavior; behavior; behavior = behavior->next) { |
| 302 | if (behavior->character == ch) { |
| 303 | found = behavior; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 305 | } |
| 306 | if (found) { |
| 307 | *outKeyCode = mKeys.keyAt(i); |
| 308 | *outMetaState = found->metaState; |
| 309 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 315 | void KeyCharacterMap::addKey(Vector<KeyEvent>& outEvents, |
| 316 | int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time) { |
| 317 | outEvents.push(); |
| 318 | KeyEvent& event = outEvents.editTop(); |
| 319 | event.initialize(deviceId, AINPUT_SOURCE_KEYBOARD, |
| 320 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 321 | 0, keyCode, 0, metaState, 0, time, time); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 324 | void KeyCharacterMap::addMetaKeys(Vector<KeyEvent>& outEvents, |
| 325 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 326 | int32_t* currentMetaState) { |
| 327 | // Add and remove meta keys symmetrically. |
| 328 | if (down) { |
| 329 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 330 | AKEYCODE_CAPS_LOCK, AMETA_CAPS_LOCK_ON, currentMetaState); |
| 331 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 332 | AKEYCODE_NUM_LOCK, AMETA_NUM_LOCK_ON, currentMetaState); |
| 333 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 334 | AKEYCODE_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, currentMetaState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 336 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 337 | AKEYCODE_SHIFT_LEFT, AMETA_SHIFT_LEFT_ON, |
| 338 | AKEYCODE_SHIFT_RIGHT, AMETA_SHIFT_RIGHT_ON, |
| 339 | AMETA_SHIFT_ON, currentMetaState); |
| 340 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 341 | AKEYCODE_ALT_LEFT, AMETA_ALT_LEFT_ON, |
| 342 | AKEYCODE_ALT_RIGHT, AMETA_ALT_RIGHT_ON, |
| 343 | AMETA_ALT_ON, currentMetaState); |
| 344 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 345 | AKEYCODE_CTRL_LEFT, AMETA_CTRL_LEFT_ON, |
| 346 | AKEYCODE_CTRL_RIGHT, AMETA_CTRL_RIGHT_ON, |
| 347 | AMETA_CTRL_ON, currentMetaState); |
| 348 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 349 | AKEYCODE_META_LEFT, AMETA_META_LEFT_ON, |
| 350 | AKEYCODE_META_RIGHT, AMETA_META_RIGHT_ON, |
| 351 | AMETA_META_ON, currentMetaState); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 352 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 353 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 354 | AKEYCODE_SYM, AMETA_SYM_ON, currentMetaState); |
| 355 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, true, time, |
| 356 | AKEYCODE_FUNCTION, AMETA_FUNCTION_ON, currentMetaState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } else { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 358 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 359 | AKEYCODE_FUNCTION, AMETA_FUNCTION_ON, currentMetaState); |
| 360 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 361 | AKEYCODE_SYM, AMETA_SYM_ON, currentMetaState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 363 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 364 | AKEYCODE_META_LEFT, AMETA_META_LEFT_ON, |
| 365 | AKEYCODE_META_RIGHT, AMETA_META_RIGHT_ON, |
| 366 | AMETA_META_ON, currentMetaState); |
| 367 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 368 | AKEYCODE_CTRL_LEFT, AMETA_CTRL_LEFT_ON, |
| 369 | AKEYCODE_CTRL_RIGHT, AMETA_CTRL_RIGHT_ON, |
| 370 | AMETA_CTRL_ON, currentMetaState); |
| 371 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 372 | AKEYCODE_ALT_LEFT, AMETA_ALT_LEFT_ON, |
| 373 | AKEYCODE_ALT_RIGHT, AMETA_ALT_RIGHT_ON, |
| 374 | AMETA_ALT_ON, currentMetaState); |
| 375 | addDoubleEphemeralMetaKey(outEvents, deviceId, metaState, false, time, |
| 376 | AKEYCODE_SHIFT_LEFT, AMETA_SHIFT_LEFT_ON, |
| 377 | AKEYCODE_SHIFT_RIGHT, AMETA_SHIFT_RIGHT_ON, |
| 378 | AMETA_SHIFT_ON, currentMetaState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 380 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 381 | AKEYCODE_SCROLL_LOCK, AMETA_SCROLL_LOCK_ON, currentMetaState); |
| 382 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 383 | AKEYCODE_NUM_LOCK, AMETA_NUM_LOCK_ON, currentMetaState); |
| 384 | addLockedMetaKey(outEvents, deviceId, metaState, time, |
| 385 | AKEYCODE_CAPS_LOCK, AMETA_CAPS_LOCK_ON, currentMetaState); |
| 386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 389 | bool KeyCharacterMap::addSingleEphemeralMetaKey(Vector<KeyEvent>& outEvents, |
| 390 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 391 | int32_t keyCode, int32_t keyMetaState, |
| 392 | int32_t* currentMetaState) { |
| 393 | if ((metaState & keyMetaState) == keyMetaState) { |
| 394 | *currentMetaState = updateMetaState(keyCode, down, *currentMetaState); |
| 395 | addKey(outEvents, deviceId, keyCode, *currentMetaState, down, time); |
| 396 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 398 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 400 | |
| 401 | void KeyCharacterMap::addDoubleEphemeralMetaKey(Vector<KeyEvent>& outEvents, |
| 402 | int32_t deviceId, int32_t metaState, bool down, nsecs_t time, |
| 403 | int32_t leftKeyCode, int32_t leftKeyMetaState, |
| 404 | int32_t rightKeyCode, int32_t rightKeyMetaState, |
| 405 | int32_t eitherKeyMetaState, |
| 406 | int32_t* currentMetaState) { |
| 407 | bool specific = false; |
| 408 | specific |= addSingleEphemeralMetaKey(outEvents, deviceId, metaState, down, time, |
| 409 | leftKeyCode, leftKeyMetaState, currentMetaState); |
| 410 | specific |= addSingleEphemeralMetaKey(outEvents, deviceId, metaState, down, time, |
| 411 | rightKeyCode, rightKeyMetaState, currentMetaState); |
| 412 | |
| 413 | if (!specific) { |
| 414 | addSingleEphemeralMetaKey(outEvents, deviceId, metaState, down, time, |
| 415 | leftKeyCode, eitherKeyMetaState, currentMetaState); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void KeyCharacterMap::addLockedMetaKey(Vector<KeyEvent>& outEvents, |
| 420 | int32_t deviceId, int32_t metaState, nsecs_t time, |
| 421 | int32_t keyCode, int32_t keyMetaState, |
| 422 | int32_t* currentMetaState) { |
| 423 | if ((metaState & keyMetaState) == keyMetaState) { |
| 424 | *currentMetaState = updateMetaState(keyCode, true, *currentMetaState); |
| 425 | addKey(outEvents, deviceId, keyCode, *currentMetaState, true, time); |
| 426 | *currentMetaState = updateMetaState(keyCode, false, *currentMetaState); |
| 427 | addKey(outEvents, deviceId, keyCode, *currentMetaState, false, time); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | // --- KeyCharacterMap::Key --- |
| 433 | |
| 434 | KeyCharacterMap::Key::Key() : |
| 435 | label(0), number(0), firstBehavior(NULL) { |
| 436 | } |
| 437 | |
| 438 | KeyCharacterMap::Key::~Key() { |
| 439 | Behavior* behavior = firstBehavior; |
| 440 | while (behavior) { |
| 441 | Behavior* next = behavior->next; |
| 442 | delete behavior; |
| 443 | behavior = next; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | |
| 448 | // --- KeyCharacterMap::Behavior --- |
| 449 | |
| 450 | KeyCharacterMap::Behavior::Behavior() : |
| 451 | next(NULL), metaState(0), character(0), fallbackKeyCode(0) { |
| 452 | } |
| 453 | |
| 454 | |
| 455 | // --- KeyCharacterMap::Parser --- |
| 456 | |
| 457 | KeyCharacterMap::Parser::Parser(KeyCharacterMap* map, Tokenizer* tokenizer) : |
| 458 | mMap(map), mTokenizer(tokenizer), mState(STATE_TOP) { |
| 459 | } |
| 460 | |
| 461 | KeyCharacterMap::Parser::~Parser() { |
| 462 | } |
| 463 | |
| 464 | status_t KeyCharacterMap::Parser::parse() { |
| 465 | while (!mTokenizer->isEof()) { |
| 466 | #if DEBUG_PARSER |
| 467 | LOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), |
| 468 | mTokenizer->peekRemainderOfLine().string()); |
| 469 | #endif |
| 470 | |
| 471 | mTokenizer->skipDelimiters(WHITESPACE); |
| 472 | |
| 473 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
| 474 | switch (mState) { |
| 475 | case STATE_TOP: { |
| 476 | String8 keywordToken = mTokenizer->nextToken(WHITESPACE); |
| 477 | if (keywordToken == "type") { |
| 478 | mTokenizer->skipDelimiters(WHITESPACE); |
| 479 | status_t status = parseType(); |
| 480 | if (status) return status; |
| 481 | } else if (keywordToken == "key") { |
| 482 | mTokenizer->skipDelimiters(WHITESPACE); |
| 483 | status_t status = parseKey(); |
| 484 | if (status) return status; |
| 485 | } else { |
| 486 | LOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(), |
| 487 | keywordToken.string()); |
| 488 | return BAD_VALUE; |
| 489 | } |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | case STATE_KEY: { |
| 494 | status_t status = parseKeyProperty(); |
| 495 | if (status) return status; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | mTokenizer->skipDelimiters(WHITESPACE); |
| 501 | if (!mTokenizer->isEol()) { |
| 502 | LOGE("%s: Expected end of line, got '%s'.", |
| 503 | mTokenizer->getLocation().string(), |
| 504 | mTokenizer->peekRemainderOfLine().string()); |
| 505 | return BAD_VALUE; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | mTokenizer->nextLine(); |
| 510 | } |
| 511 | |
| 512 | if (mState != STATE_TOP) { |
| 513 | LOGE("%s: Unterminated key description at end of file.", |
| 514 | mTokenizer->getLocation().string()); |
| 515 | return BAD_VALUE; |
| 516 | } |
| 517 | |
| 518 | if (mMap->mType == KEYBOARD_TYPE_UNKNOWN) { |
| 519 | LOGE("%s: Missing required keyboard 'type' declaration.", |
| 520 | mTokenizer->getLocation().string()); |
| 521 | return BAD_VALUE; |
| 522 | } |
| 523 | |
| 524 | return NO_ERROR; |
| 525 | } |
| 526 | |
| 527 | status_t KeyCharacterMap::Parser::parseType() { |
| 528 | if (mMap->mType != KEYBOARD_TYPE_UNKNOWN) { |
| 529 | LOGE("%s: Duplicate keyboard 'type' declaration.", |
| 530 | mTokenizer->getLocation().string()); |
| 531 | return BAD_VALUE; |
| 532 | } |
| 533 | |
| 534 | KeyboardType type; |
| 535 | String8 typeToken = mTokenizer->nextToken(WHITESPACE); |
| 536 | if (typeToken == "NUMERIC") { |
| 537 | type = KEYBOARD_TYPE_NUMERIC; |
| 538 | } else if (typeToken == "PREDICTIVE") { |
| 539 | type = KEYBOARD_TYPE_PREDICTIVE; |
| 540 | } else if (typeToken == "ALPHA") { |
| 541 | type = KEYBOARD_TYPE_ALPHA; |
| 542 | } else if (typeToken == "FULL") { |
| 543 | type = KEYBOARD_TYPE_FULL; |
| 544 | } else if (typeToken == "SPECIAL_FUNCTION") { |
| 545 | type = KEYBOARD_TYPE_SPECIAL_FUNCTION; |
| 546 | } else { |
| 547 | LOGE("%s: Expected keyboard type label, got '%s'.", mTokenizer->getLocation().string(), |
| 548 | typeToken.string()); |
| 549 | return BAD_VALUE; |
| 550 | } |
| 551 | |
| 552 | #if DEBUG_PARSER |
| 553 | LOGD("Parsed type: type=%d.", type); |
| 554 | #endif |
| 555 | mMap->mType = type; |
| 556 | return NO_ERROR; |
| 557 | } |
| 558 | |
| 559 | status_t KeyCharacterMap::Parser::parseKey() { |
| 560 | String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE); |
| 561 | int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string()); |
| 562 | if (!keyCode) { |
| 563 | LOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(), |
| 564 | keyCodeToken.string()); |
| 565 | return BAD_VALUE; |
| 566 | } |
| 567 | if (mMap->mKeys.indexOfKey(keyCode) >= 0) { |
| 568 | LOGE("%s: Duplicate entry for key code '%s'.", mTokenizer->getLocation().string(), |
| 569 | keyCodeToken.string()); |
| 570 | return BAD_VALUE; |
| 571 | } |
| 572 | |
| 573 | mTokenizer->skipDelimiters(WHITESPACE); |
| 574 | String8 openBraceToken = mTokenizer->nextToken(WHITESPACE); |
| 575 | if (openBraceToken != "{") { |
| 576 | LOGE("%s: Expected '{' after key code label, got '%s'.", |
| 577 | mTokenizer->getLocation().string(), openBraceToken.string()); |
| 578 | return BAD_VALUE; |
| 579 | } |
| 580 | |
| 581 | #if DEBUG_PARSER |
| 582 | LOGD("Parsed beginning of key: keyCode=%d.", keyCode); |
| 583 | #endif |
| 584 | mKeyCode = keyCode; |
| 585 | mMap->mKeys.add(keyCode, new Key()); |
| 586 | mState = STATE_KEY; |
| 587 | return NO_ERROR; |
| 588 | } |
| 589 | |
| 590 | status_t KeyCharacterMap::Parser::parseKeyProperty() { |
| 591 | String8 token = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER); |
| 592 | if (token == "}") { |
| 593 | mState = STATE_TOP; |
| 594 | return NO_ERROR; |
| 595 | } |
| 596 | |
| 597 | Vector<Property> properties; |
| 598 | |
| 599 | // Parse all comma-delimited property names up to the first colon. |
| 600 | for (;;) { |
| 601 | if (token == "label") { |
| 602 | properties.add(Property(PROPERTY_LABEL)); |
| 603 | } else if (token == "number") { |
| 604 | properties.add(Property(PROPERTY_NUMBER)); |
| 605 | } else { |
| 606 | int32_t metaState; |
| 607 | status_t status = parseModifier(token, &metaState); |
| 608 | if (status) { |
| 609 | LOGE("%s: Expected a property name or modifier, got '%s'.", |
| 610 | mTokenizer->getLocation().string(), token.string()); |
| 611 | return status; |
| 612 | } |
| 613 | properties.add(Property(PROPERTY_META, metaState)); |
| 614 | } |
| 615 | |
| 616 | mTokenizer->skipDelimiters(WHITESPACE); |
| 617 | if (!mTokenizer->isEol()) { |
| 618 | char ch = mTokenizer->nextChar(); |
| 619 | if (ch == ':') { |
| 620 | break; |
| 621 | } else if (ch == ',') { |
| 622 | mTokenizer->skipDelimiters(WHITESPACE); |
| 623 | token = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER); |
| 624 | continue; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | LOGE("%s: Expected ',' or ':' after property name.", |
| 629 | mTokenizer->getLocation().string()); |
| 630 | return BAD_VALUE; |
| 631 | } |
| 632 | |
| 633 | // Parse behavior after the colon. |
| 634 | mTokenizer->skipDelimiters(WHITESPACE); |
| 635 | |
| 636 | Behavior behavior; |
| 637 | bool haveCharacter = false; |
| 638 | bool haveFallback = false; |
| 639 | |
| 640 | do { |
| 641 | char ch = mTokenizer->peekChar(); |
| 642 | if (ch == '\'') { |
| 643 | char16_t character; |
| 644 | status_t status = parseCharacterLiteral(&character); |
| 645 | if (status || !character) { |
| 646 | LOGE("%s: Invalid character literal for key.", |
| 647 | mTokenizer->getLocation().string()); |
| 648 | return BAD_VALUE; |
| 649 | } |
| 650 | if (haveCharacter) { |
| 651 | LOGE("%s: Cannot combine multiple character literals or 'none'.", |
| 652 | mTokenizer->getLocation().string()); |
| 653 | return BAD_VALUE; |
| 654 | } |
| 655 | behavior.character = character; |
| 656 | haveCharacter = true; |
| 657 | } else { |
| 658 | token = mTokenizer->nextToken(WHITESPACE); |
| 659 | if (token == "none") { |
| 660 | if (haveCharacter) { |
| 661 | LOGE("%s: Cannot combine multiple character literals or 'none'.", |
| 662 | mTokenizer->getLocation().string()); |
| 663 | return BAD_VALUE; |
| 664 | } |
| 665 | haveCharacter = true; |
| 666 | } else if (token == "fallback") { |
| 667 | mTokenizer->skipDelimiters(WHITESPACE); |
| 668 | token = mTokenizer->nextToken(WHITESPACE); |
| 669 | int32_t keyCode = getKeyCodeByLabel(token.string()); |
| 670 | if (!keyCode) { |
| 671 | LOGE("%s: Invalid key code label for fallback behavior, got '%s'.", |
| 672 | mTokenizer->getLocation().string(), |
| 673 | token.string()); |
| 674 | return BAD_VALUE; |
| 675 | } |
| 676 | if (haveFallback) { |
| 677 | LOGE("%s: Cannot combine multiple fallback key codes.", |
| 678 | mTokenizer->getLocation().string()); |
| 679 | return BAD_VALUE; |
| 680 | } |
| 681 | behavior.fallbackKeyCode = keyCode; |
| 682 | haveFallback = true; |
| 683 | } else { |
| 684 | LOGE("%s: Expected a key behavior after ':'.", |
| 685 | mTokenizer->getLocation().string()); |
| 686 | return BAD_VALUE; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | mTokenizer->skipDelimiters(WHITESPACE); |
| 691 | } while (!mTokenizer->isEol()); |
| 692 | |
| 693 | // Add the behavior. |
| 694 | Key* key = mMap->mKeys.valueFor(mKeyCode); |
| 695 | for (size_t i = 0; i < properties.size(); i++) { |
| 696 | const Property& property = properties.itemAt(i); |
| 697 | switch (property.property) { |
| 698 | case PROPERTY_LABEL: |
| 699 | if (key->label) { |
| 700 | LOGE("%s: Duplicate label for key.", |
| 701 | mTokenizer->getLocation().string()); |
| 702 | return BAD_VALUE; |
| 703 | } |
| 704 | key->label = behavior.character; |
| 705 | #if DEBUG_PARSER |
| 706 | LOGD("Parsed key label: keyCode=%d, label=%d.", mKeyCode, key->label); |
| 707 | #endif |
| 708 | break; |
| 709 | case PROPERTY_NUMBER: |
| 710 | if (key->number) { |
| 711 | LOGE("%s: Duplicate number for key.", |
| 712 | mTokenizer->getLocation().string()); |
| 713 | return BAD_VALUE; |
| 714 | } |
| 715 | key->number = behavior.character; |
| 716 | #if DEBUG_PARSER |
| 717 | LOGD("Parsed key number: keyCode=%d, number=%d.", mKeyCode, key->number); |
| 718 | #endif |
| 719 | break; |
| 720 | case PROPERTY_META: { |
| 721 | for (Behavior* b = key->firstBehavior; b; b = b->next) { |
| 722 | if (b->metaState == property.metaState) { |
| 723 | LOGE("%s: Duplicate key behavior for modifier.", |
| 724 | mTokenizer->getLocation().string()); |
| 725 | return BAD_VALUE; |
| 726 | } |
| 727 | } |
| 728 | Behavior* newBehavior = new Behavior(behavior); |
| 729 | newBehavior->metaState = property.metaState; |
| 730 | newBehavior->next = key->firstBehavior; |
| 731 | key->firstBehavior = newBehavior; |
| 732 | #if DEBUG_PARSER |
| 733 | LOGD("Parsed key meta: keyCode=%d, meta=0x%x, char=%d, fallback=%d.", mKeyCode, |
| 734 | newBehavior->metaState, newBehavior->character, newBehavior->fallbackKeyCode); |
| 735 | #endif |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | return NO_ERROR; |
| 741 | } |
| 742 | |
| 743 | status_t KeyCharacterMap::Parser::parseModifier(const String8& token, int32_t* outMetaState) { |
| 744 | if (token == "base") { |
| 745 | *outMetaState = 0; |
| 746 | return NO_ERROR; |
| 747 | } |
| 748 | |
| 749 | int32_t combinedMeta = 0; |
| 750 | |
| 751 | const char* str = token.string(); |
| 752 | const char* start = str; |
| 753 | for (const char* cur = str; ; cur++) { |
| 754 | char ch = *cur; |
| 755 | if (ch == '+' || ch == '\0') { |
| 756 | size_t len = cur - start; |
| 757 | int32_t metaState = 0; |
| 758 | for (size_t i = 0; i < sizeof(modifiers) / sizeof(Modifier); i++) { |
| 759 | if (strlen(modifiers[i].label) == len |
| 760 | && strncmp(modifiers[i].label, start, len) == 0) { |
| 761 | metaState = modifiers[i].metaState; |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | if (!metaState) { |
| 766 | return BAD_VALUE; |
| 767 | } |
| 768 | if (combinedMeta & metaState) { |
| 769 | LOGE("%s: Duplicate modifier combination '%s'.", |
| 770 | mTokenizer->getLocation().string(), token.string()); |
| 771 | return BAD_VALUE; |
| 772 | } |
| 773 | |
| 774 | combinedMeta |= metaState; |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 775 | start = cur + 1; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 776 | |
| 777 | if (ch == '\0') { |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | *outMetaState = combinedMeta; |
| 783 | return NO_ERROR; |
| 784 | } |
| 785 | |
| 786 | status_t KeyCharacterMap::Parser::parseCharacterLiteral(char16_t* outCharacter) { |
| 787 | char ch = mTokenizer->nextChar(); |
| 788 | if (ch != '\'') { |
| 789 | goto Error; |
| 790 | } |
| 791 | |
| 792 | ch = mTokenizer->nextChar(); |
| 793 | if (ch == '\\') { |
| 794 | // Escape sequence. |
| 795 | ch = mTokenizer->nextChar(); |
| 796 | if (ch == 'n') { |
| 797 | *outCharacter = '\n'; |
| 798 | } else if (ch == 't') { |
| 799 | *outCharacter = '\t'; |
| 800 | } else if (ch == '\\') { |
| 801 | *outCharacter = '\\'; |
| 802 | } else if (ch == '\'') { |
| 803 | *outCharacter = '\''; |
| 804 | } else if (ch == '"') { |
| 805 | *outCharacter = '"'; |
| 806 | } else if (ch == 'u') { |
| 807 | *outCharacter = 0; |
| 808 | for (int i = 0; i < 4; i++) { |
| 809 | ch = mTokenizer->nextChar(); |
| 810 | int digit; |
| 811 | if (ch >= '0' && ch <= '9') { |
| 812 | digit = ch - '0'; |
| 813 | } else if (ch >= 'A' && ch <= 'F') { |
| 814 | digit = ch - 'A' + 10; |
| 815 | } else if (ch >= 'a' && ch <= 'f') { |
| 816 | digit = ch - 'a' + 10; |
| 817 | } else { |
| 818 | goto Error; |
| 819 | } |
| 820 | *outCharacter = (*outCharacter << 4) | digit; |
| 821 | } |
| 822 | } else { |
| 823 | goto Error; |
| 824 | } |
| 825 | } else if (ch >= 32 && ch <= 126 && ch != '\'') { |
| 826 | // ASCII literal character. |
| 827 | *outCharacter = ch; |
| 828 | } else { |
| 829 | goto Error; |
| 830 | } |
| 831 | |
| 832 | ch = mTokenizer->nextChar(); |
| 833 | if (ch != '\'') { |
| 834 | goto Error; |
| 835 | } |
| 836 | |
| 837 | // Ensure that we consumed the entire token. |
| 838 | if (mTokenizer->nextToken(WHITESPACE).isEmpty()) { |
| 839 | return NO_ERROR; |
| 840 | } |
| 841 | |
| 842 | Error: |
| 843 | LOGE("%s: Malformed character literal.", mTokenizer->getLocation().string()); |
| 844 | return BAD_VALUE; |
| 845 | } |
| 846 | |
| 847 | } // namespace android |