Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2008 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #define LOG_TAG "KeyLayoutMap" |
michael@0 | 18 | #include "cutils_log.h" |
michael@0 | 19 | |
michael@0 | 20 | #include <stdlib.h> |
michael@0 | 21 | #include "android_keycodes.h" |
michael@0 | 22 | #include "Keyboard.h" |
michael@0 | 23 | #include "KeyLayoutMap.h" |
michael@0 | 24 | #include <utils/Errors.h> |
michael@0 | 25 | #include "Tokenizer.h" |
michael@0 | 26 | #include <utils/Timers.h> |
michael@0 | 27 | |
michael@0 | 28 | // Enables debug output for the parser. |
michael@0 | 29 | #define DEBUG_PARSER 0 |
michael@0 | 30 | |
michael@0 | 31 | // Enables debug output for parser performance. |
michael@0 | 32 | #define DEBUG_PARSER_PERFORMANCE 0 |
michael@0 | 33 | |
michael@0 | 34 | // Enables debug output for mapping. |
michael@0 | 35 | #define DEBUG_MAPPING 0 |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | namespace android { |
michael@0 | 39 | |
michael@0 | 40 | static const char* WHITESPACE = " \t\r"; |
michael@0 | 41 | |
michael@0 | 42 | // --- KeyLayoutMap --- |
michael@0 | 43 | |
michael@0 | 44 | KeyLayoutMap::KeyLayoutMap() { |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | KeyLayoutMap::~KeyLayoutMap() { |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | status_t KeyLayoutMap::load(const String8& filename, sp<KeyLayoutMap>* outMap) { |
michael@0 | 51 | outMap->clear(); |
michael@0 | 52 | |
michael@0 | 53 | Tokenizer* tokenizer; |
michael@0 | 54 | status_t status = Tokenizer::open(filename, &tokenizer); |
michael@0 | 55 | if (status) { |
michael@0 | 56 | ALOGE("Error %d opening key layout map file %s.", status, filename.string()); |
michael@0 | 57 | } else { |
michael@0 | 58 | sp<KeyLayoutMap> map = new KeyLayoutMap(); |
michael@0 | 59 | if (!map.get()) { |
michael@0 | 60 | ALOGE("Error allocating key layout map."); |
michael@0 | 61 | status = NO_MEMORY; |
michael@0 | 62 | } else { |
michael@0 | 63 | #if DEBUG_PARSER_PERFORMANCE |
michael@0 | 64 | nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
michael@0 | 65 | #endif |
michael@0 | 66 | Parser parser(map.get(), tokenizer); |
michael@0 | 67 | status = parser.parse(); |
michael@0 | 68 | #if DEBUG_PARSER_PERFORMANCE |
michael@0 | 69 | nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
michael@0 | 70 | ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.", |
michael@0 | 71 | tokenizer->getFilename().string(), tokenizer->getLineNumber(), |
michael@0 | 72 | elapsedTime / 1000000.0); |
michael@0 | 73 | #endif |
michael@0 | 74 | if (!status) { |
michael@0 | 75 | *outMap = map; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | delete tokenizer; |
michael@0 | 79 | } |
michael@0 | 80 | return status; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode, |
michael@0 | 84 | int32_t* outKeyCode, uint32_t* outFlags) const { |
michael@0 | 85 | const Key* key = getKey(scanCode, usageCode); |
michael@0 | 86 | if (!key) { |
michael@0 | 87 | #if DEBUG_MAPPING |
michael@0 | 88 | ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode); |
michael@0 | 89 | #endif |
michael@0 | 90 | *outKeyCode = AKEYCODE_UNKNOWN; |
michael@0 | 91 | *outFlags = 0; |
michael@0 | 92 | return NAME_NOT_FOUND; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | *outKeyCode = key->keyCode; |
michael@0 | 96 | *outFlags = key->flags; |
michael@0 | 97 | |
michael@0 | 98 | #if DEBUG_MAPPING |
michael@0 | 99 | ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.", |
michael@0 | 100 | scanCode, usageCode, *outKeyCode, *outFlags); |
michael@0 | 101 | #endif |
michael@0 | 102 | return NO_ERROR; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const { |
michael@0 | 106 | if (usageCode) { |
michael@0 | 107 | ssize_t index = mKeysByUsageCode.indexOfKey(usageCode); |
michael@0 | 108 | if (index >= 0) { |
michael@0 | 109 | return &mKeysByUsageCode.valueAt(index); |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | if (scanCode) { |
michael@0 | 113 | ssize_t index = mKeysByScanCode.indexOfKey(scanCode); |
michael@0 | 114 | if (index >= 0) { |
michael@0 | 115 | return &mKeysByScanCode.valueAt(index); |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | return NULL; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | status_t KeyLayoutMap::findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const { |
michael@0 | 122 | const size_t N = mKeysByScanCode.size(); |
michael@0 | 123 | for (size_t i=0; i<N; i++) { |
michael@0 | 124 | if (mKeysByScanCode.valueAt(i).keyCode == keyCode) { |
michael@0 | 125 | outScanCodes->add(mKeysByScanCode.keyAt(i)); |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | return NO_ERROR; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { |
michael@0 | 132 | ssize_t index = mAxes.indexOfKey(scanCode); |
michael@0 | 133 | if (index < 0) { |
michael@0 | 134 | #if DEBUG_MAPPING |
michael@0 | 135 | ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode); |
michael@0 | 136 | #endif |
michael@0 | 137 | return NAME_NOT_FOUND; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | *outAxisInfo = mAxes.valueAt(index); |
michael@0 | 141 | |
michael@0 | 142 | #if DEBUG_MAPPING |
michael@0 | 143 | ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, " |
michael@0 | 144 | "splitValue=%d, flatOverride=%d.", |
michael@0 | 145 | scanCode, |
michael@0 | 146 | outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis, |
michael@0 | 147 | outAxisInfo->splitValue, outAxisInfo->flatOverride); |
michael@0 | 148 | #endif |
michael@0 | 149 | return NO_ERROR; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | |
michael@0 | 153 | // --- KeyLayoutMap::Parser --- |
michael@0 | 154 | |
michael@0 | 155 | KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) : |
michael@0 | 156 | mMap(map), mTokenizer(tokenizer) { |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | KeyLayoutMap::Parser::~Parser() { |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | status_t KeyLayoutMap::Parser::parse() { |
michael@0 | 163 | while (!mTokenizer->isEof()) { |
michael@0 | 164 | #if DEBUG_PARSER |
michael@0 | 165 | ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 166 | mTokenizer->peekRemainderOfLine().string()); |
michael@0 | 167 | #endif |
michael@0 | 168 | |
michael@0 | 169 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 170 | |
michael@0 | 171 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
michael@0 | 172 | String8 keywordToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 173 | if (keywordToken == "key") { |
michael@0 | 174 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 175 | status_t status = parseKey(); |
michael@0 | 176 | if (status) return status; |
michael@0 | 177 | } else if (keywordToken == "axis") { |
michael@0 | 178 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 179 | status_t status = parseAxis(); |
michael@0 | 180 | if (status) return status; |
michael@0 | 181 | } else { |
michael@0 | 182 | ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 183 | keywordToken.string()); |
michael@0 | 184 | return BAD_VALUE; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 188 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
michael@0 | 189 | ALOGE("%s: Expected end of line or trailing comment, got '%s'.", |
michael@0 | 190 | mTokenizer->getLocation().string(), |
michael@0 | 191 | mTokenizer->peekRemainderOfLine().string()); |
michael@0 | 192 | return BAD_VALUE; |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | mTokenizer->nextLine(); |
michael@0 | 197 | } |
michael@0 | 198 | return NO_ERROR; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | status_t KeyLayoutMap::Parser::parseKey() { |
michael@0 | 202 | String8 codeToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 203 | bool mapUsage = false; |
michael@0 | 204 | if (codeToken == "usage") { |
michael@0 | 205 | mapUsage = true; |
michael@0 | 206 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 207 | codeToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | char* end; |
michael@0 | 211 | int32_t code = int32_t(strtol(codeToken.string(), &end, 0)); |
michael@0 | 212 | if (*end) { |
michael@0 | 213 | ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 214 | mapUsage ? "usage" : "scan code", codeToken.string()); |
michael@0 | 215 | return BAD_VALUE; |
michael@0 | 216 | } |
michael@0 | 217 | KeyedVector<int32_t, Key>& map = |
michael@0 | 218 | mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode; |
michael@0 | 219 | if (map.indexOfKey(code) >= 0) { |
michael@0 | 220 | ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 221 | mapUsage ? "usage" : "scan code", codeToken.string()); |
michael@0 | 222 | return BAD_VALUE; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 226 | String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 227 | int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string()); |
michael@0 | 228 | if (!keyCode) { |
michael@0 | 229 | ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 230 | keyCodeToken.string()); |
michael@0 | 231 | return BAD_VALUE; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | uint32_t flags = 0; |
michael@0 | 235 | for (;;) { |
michael@0 | 236 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 237 | if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break; |
michael@0 | 238 | |
michael@0 | 239 | String8 flagToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 240 | uint32_t flag = getKeyFlagByLabel(flagToken.string()); |
michael@0 | 241 | if (!flag) { |
michael@0 | 242 | ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 243 | flagToken.string()); |
michael@0 | 244 | return BAD_VALUE; |
michael@0 | 245 | } |
michael@0 | 246 | if (flags & flag) { |
michael@0 | 247 | ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 248 | flagToken.string()); |
michael@0 | 249 | return BAD_VALUE; |
michael@0 | 250 | } |
michael@0 | 251 | flags |= flag; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | #if DEBUG_PARSER |
michael@0 | 255 | ALOGD("Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.", |
michael@0 | 256 | mapUsage ? "usage" : "scan code", code, keyCode, flags); |
michael@0 | 257 | #endif |
michael@0 | 258 | Key key; |
michael@0 | 259 | key.keyCode = keyCode; |
michael@0 | 260 | key.flags = flags; |
michael@0 | 261 | map.add(code, key); |
michael@0 | 262 | return NO_ERROR; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | status_t KeyLayoutMap::Parser::parseAxis() { |
michael@0 | 266 | String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 267 | char* end; |
michael@0 | 268 | int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0)); |
michael@0 | 269 | if (*end) { |
michael@0 | 270 | ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 271 | scanCodeToken.string()); |
michael@0 | 272 | return BAD_VALUE; |
michael@0 | 273 | } |
michael@0 | 274 | if (mMap->mAxes.indexOfKey(scanCode) >= 0) { |
michael@0 | 275 | ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(), |
michael@0 | 276 | scanCodeToken.string()); |
michael@0 | 277 | return BAD_VALUE; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | AxisInfo axisInfo; |
michael@0 | 281 | |
michael@0 | 282 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 283 | String8 token = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 284 | if (token == "invert") { |
michael@0 | 285 | axisInfo.mode = AxisInfo::MODE_INVERT; |
michael@0 | 286 | |
michael@0 | 287 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 288 | String8 axisToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 289 | axisInfo.axis = getAxisByLabel(axisToken.string()); |
michael@0 | 290 | if (axisInfo.axis < 0) { |
michael@0 | 291 | ALOGE("%s: Expected inverted axis label, got '%s'.", |
michael@0 | 292 | mTokenizer->getLocation().string(), axisToken.string()); |
michael@0 | 293 | return BAD_VALUE; |
michael@0 | 294 | } |
michael@0 | 295 | } else if (token == "split") { |
michael@0 | 296 | axisInfo.mode = AxisInfo::MODE_SPLIT; |
michael@0 | 297 | |
michael@0 | 298 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 299 | String8 splitToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 300 | axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0)); |
michael@0 | 301 | if (*end) { |
michael@0 | 302 | ALOGE("%s: Expected split value, got '%s'.", |
michael@0 | 303 | mTokenizer->getLocation().string(), splitToken.string()); |
michael@0 | 304 | return BAD_VALUE; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 308 | String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 309 | axisInfo.axis = getAxisByLabel(lowAxisToken.string()); |
michael@0 | 310 | if (axisInfo.axis < 0) { |
michael@0 | 311 | ALOGE("%s: Expected low axis label, got '%s'.", |
michael@0 | 312 | mTokenizer->getLocation().string(), lowAxisToken.string()); |
michael@0 | 313 | return BAD_VALUE; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 317 | String8 highAxisToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 318 | axisInfo.highAxis = getAxisByLabel(highAxisToken.string()); |
michael@0 | 319 | if (axisInfo.highAxis < 0) { |
michael@0 | 320 | ALOGE("%s: Expected high axis label, got '%s'.", |
michael@0 | 321 | mTokenizer->getLocation().string(), highAxisToken.string()); |
michael@0 | 322 | return BAD_VALUE; |
michael@0 | 323 | } |
michael@0 | 324 | } else { |
michael@0 | 325 | axisInfo.axis = getAxisByLabel(token.string()); |
michael@0 | 326 | if (axisInfo.axis < 0) { |
michael@0 | 327 | ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.", |
michael@0 | 328 | mTokenizer->getLocation().string(), token.string()); |
michael@0 | 329 | return BAD_VALUE; |
michael@0 | 330 | } |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | for (;;) { |
michael@0 | 334 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 335 | if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') { |
michael@0 | 336 | break; |
michael@0 | 337 | } |
michael@0 | 338 | String8 keywordToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 339 | if (keywordToken == "flat") { |
michael@0 | 340 | mTokenizer->skipDelimiters(WHITESPACE); |
michael@0 | 341 | String8 flatToken = mTokenizer->nextToken(WHITESPACE); |
michael@0 | 342 | axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0)); |
michael@0 | 343 | if (*end) { |
michael@0 | 344 | ALOGE("%s: Expected flat value, got '%s'.", |
michael@0 | 345 | mTokenizer->getLocation().string(), flatToken.string()); |
michael@0 | 346 | return BAD_VALUE; |
michael@0 | 347 | } |
michael@0 | 348 | } else { |
michael@0 | 349 | ALOGE("%s: Expected keyword 'flat', got '%s'.", |
michael@0 | 350 | mTokenizer->getLocation().string(), keywordToken.string()); |
michael@0 | 351 | return BAD_VALUE; |
michael@0 | 352 | } |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | #if DEBUG_PARSER |
michael@0 | 356 | ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, " |
michael@0 | 357 | "splitValue=%d, flatOverride=%d.", |
michael@0 | 358 | scanCode, |
michael@0 | 359 | axisInfo.mode, axisInfo.axis, axisInfo.highAxis, |
michael@0 | 360 | axisInfo.splitValue, axisInfo.flatOverride); |
michael@0 | 361 | #endif |
michael@0 | 362 | mMap->mAxes.add(scanCode, axisInfo); |
michael@0 | 363 | return NO_ERROR; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | }; |