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 "KeyLayoutMap" 18 19 #include <stdlib.h> 20 21 #include <android/keycodes.h> 22 #include <input/InputEventLabels.h> 23 #include <input/Keyboard.h> 24 #include <input/KeyLayoutMap.h> 25 #include <utils/Log.h> 26 #include <utils/Errors.h> 27 #include <utils/Tokenizer.h> 28 #include <utils/Timers.h> 29 30 // Enables debug output for the parser. 31 #define DEBUG_PARSER 0 32 33 // Enables debug output for parser performance. 34 #define DEBUG_PARSER_PERFORMANCE 0 35 36 // Enables debug output for mapping. 37 #define DEBUG_MAPPING 0 38 39 40 namespace android { 41 42 static const char* WHITESPACE = " \t\r"; 43 44 // --- KeyLayoutMap --- 45 46 KeyLayoutMap::KeyLayoutMap() { 47 } 48 49 KeyLayoutMap::~KeyLayoutMap() { 50 } 51 52 status_t KeyLayoutMap::load(const std::string& filename, sp<KeyLayoutMap>* outMap) { 53 outMap->clear(); 54 55 Tokenizer* tokenizer; 56 status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer); 57 if (status) { 58 ALOGE("Error %d opening key layout map file %s.", status, filename.c_str()); 59 } else { 60 sp<KeyLayoutMap> map = new KeyLayoutMap(); 61 if (!map.get()) { 62 ALOGE("Error allocating key layout map."); 63 status = NO_MEMORY; 64 } else { 65 #if DEBUG_PARSER_PERFORMANCE 66 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); 67 #endif 68 Parser parser(map.get(), tokenizer); 69 status = parser.parse(); 70 #if DEBUG_PARSER_PERFORMANCE 71 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; 72 ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.", 73 tokenizer->getFilename().string(), tokenizer->getLineNumber(), 74 elapsedTime / 1000000.0); 75 #endif 76 if (!status) { 77 *outMap = map; 78 } 79 } 80 delete tokenizer; 81 } 82 return status; 83 } 84 85 status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode, 86 int32_t* outKeyCode, uint32_t* outFlags) const { 87 const Key* key = getKey(scanCode, usageCode); 88 if (!key) { 89 #if DEBUG_MAPPING 90 ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode); 91 #endif 92 *outKeyCode = AKEYCODE_UNKNOWN; 93 *outFlags = 0; 94 return NAME_NOT_FOUND; 95 } 96 97 *outKeyCode = key->keyCode; 98 *outFlags = key->flags; 99 100 #if DEBUG_MAPPING 101 ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.", 102 scanCode, usageCode, *outKeyCode, *outFlags); 103 #endif 104 return NO_ERROR; 105 } 106 107 const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const { 108 if (usageCode) { 109 ssize_t index = mKeysByUsageCode.indexOfKey(usageCode); 110 if (index >= 0) { 111 return &mKeysByUsageCode.valueAt(index); 112 } 113 } 114 if (scanCode) { 115 ssize_t index = mKeysByScanCode.indexOfKey(scanCode); 116 if (index >= 0) { 117 return &mKeysByScanCode.valueAt(index); 118 } 119 } 120 return nullptr; 121 } 122 123 status_t KeyLayoutMap::findScanCodesForKey( 124 int32_t keyCode, std::vector<int32_t>* outScanCodes) const { 125 const size_t N = mKeysByScanCode.size(); 126 for (size_t i=0; i<N; i++) { 127 if (mKeysByScanCode.valueAt(i).keyCode == keyCode) { 128 outScanCodes->push_back(mKeysByScanCode.keyAt(i)); 129 } 130 } 131 return NO_ERROR; 132 } 133 134 status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { 135 ssize_t index = mAxes.indexOfKey(scanCode); 136 if (index < 0) { 137 #if DEBUG_MAPPING 138 ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode); 139 #endif 140 return NAME_NOT_FOUND; 141 } 142 143 *outAxisInfo = mAxes.valueAt(index); 144 145 #if DEBUG_MAPPING 146 ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, " 147 "splitValue=%d, flatOverride=%d.", 148 scanCode, 149 outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis, 150 outAxisInfo->splitValue, outAxisInfo->flatOverride); 151 #endif 152 return NO_ERROR; 153 } 154 155 status_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const { 156 const size_t N = mLedsByScanCode.size(); 157 for (size_t i = 0; i < N; i++) { 158 if (mLedsByScanCode.valueAt(i).ledCode == ledCode) { 159 *outScanCode = mLedsByScanCode.keyAt(i); 160 #if DEBUG_MAPPING 161 ALOGD("findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode, *outScanCode); 162 #endif 163 return NO_ERROR; 164 } 165 } 166 #if DEBUG_MAPPING 167 ALOGD("findScanCodeForLed: ledCode=%d ~ Not found.", ledCode); 168 #endif 169 return NAME_NOT_FOUND; 170 } 171 172 status_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const { 173 const size_t N = mLedsByUsageCode.size(); 174 for (size_t i = 0; i < N; i++) { 175 if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) { 176 *outUsageCode = mLedsByUsageCode.keyAt(i); 177 #if DEBUG_MAPPING 178 ALOGD("findUsageForLed: ledCode=%d, usage=%x.", ledCode, *outUsageCode); 179 #endif 180 return NO_ERROR; 181 } 182 } 183 #if DEBUG_MAPPING 184 ALOGD("findUsageForLed: ledCode=%d ~ Not found.", ledCode); 185 #endif 186 return NAME_NOT_FOUND; 187 } 188 189 190 // --- KeyLayoutMap::Parser --- 191 192 KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) : 193 mMap(map), mTokenizer(tokenizer) { 194 } 195 196 KeyLayoutMap::Parser::~Parser() { 197 } 198 199 status_t KeyLayoutMap::Parser::parse() { 200 while (!mTokenizer->isEof()) { 201 #if DEBUG_PARSER 202 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), 203 mTokenizer->peekRemainderOfLine().string()); 204 #endif 205 206 mTokenizer->skipDelimiters(WHITESPACE); 207 208 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { 209 String8 keywordToken = mTokenizer->nextToken(WHITESPACE); 210 if (keywordToken == "key") { 211 mTokenizer->skipDelimiters(WHITESPACE); 212 status_t status = parseKey(); 213 if (status) return status; 214 } else if (keywordToken == "axis") { 215 mTokenizer->skipDelimiters(WHITESPACE); 216 status_t status = parseAxis(); 217 if (status) return status; 218 } else if (keywordToken == "led") { 219 mTokenizer->skipDelimiters(WHITESPACE); 220 status_t status = parseLed(); 221 if (status) return status; 222 } else { 223 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(), 224 keywordToken.string()); 225 return BAD_VALUE; 226 } 227 228 mTokenizer->skipDelimiters(WHITESPACE); 229 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { 230 ALOGE("%s: Expected end of line or trailing comment, got '%s'.", 231 mTokenizer->getLocation().string(), 232 mTokenizer->peekRemainderOfLine().string()); 233 return BAD_VALUE; 234 } 235 } 236 237 mTokenizer->nextLine(); 238 } 239 return NO_ERROR; 240 } 241 242 status_t KeyLayoutMap::Parser::parseKey() { 243 String8 codeToken = mTokenizer->nextToken(WHITESPACE); 244 bool mapUsage = false; 245 if (codeToken == "usage") { 246 mapUsage = true; 247 mTokenizer->skipDelimiters(WHITESPACE); 248 codeToken = mTokenizer->nextToken(WHITESPACE); 249 } 250 251 char* end; 252 int32_t code = int32_t(strtol(codeToken.string(), &end, 0)); 253 if (*end) { 254 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(), 255 mapUsage ? "usage" : "scan code", codeToken.string()); 256 return BAD_VALUE; 257 } 258 KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode; 259 if (map.indexOfKey(code) >= 0) { 260 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(), 261 mapUsage ? "usage" : "scan code", codeToken.string()); 262 return BAD_VALUE; 263 } 264 265 mTokenizer->skipDelimiters(WHITESPACE); 266 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE); 267 int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string()); 268 if (!keyCode) { 269 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(), 270 keyCodeToken.string()); 271 return BAD_VALUE; 272 } 273 274 uint32_t flags = 0; 275 for (;;) { 276 mTokenizer->skipDelimiters(WHITESPACE); 277 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break; 278 279 String8 flagToken = mTokenizer->nextToken(WHITESPACE); 280 uint32_t flag = getKeyFlagByLabel(flagToken.string()); 281 if (!flag) { 282 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(), 283 flagToken.string()); 284 return BAD_VALUE; 285 } 286 if (flags & flag) { 287 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(), 288 flagToken.string()); 289 return BAD_VALUE; 290 } 291 flags |= flag; 292 } 293 294 #if DEBUG_PARSER 295 ALOGD("Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.", 296 mapUsage ? "usage" : "scan code", code, keyCode, flags); 297 #endif 298 Key key; 299 key.keyCode = keyCode; 300 key.flags = flags; 301 map.add(code, key); 302 return NO_ERROR; 303 } 304 305 status_t KeyLayoutMap::Parser::parseAxis() { 306 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE); 307 char* end; 308 int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0)); 309 if (*end) { 310 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(), 311 scanCodeToken.string()); 312 return BAD_VALUE; 313 } 314 if (mMap->mAxes.indexOfKey(scanCode) >= 0) { 315 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(), 316 scanCodeToken.string()); 317 return BAD_VALUE; 318 } 319 320 AxisInfo axisInfo; 321 322 mTokenizer->skipDelimiters(WHITESPACE); 323 String8 token = mTokenizer->nextToken(WHITESPACE); 324 if (token == "invert") { 325 axisInfo.mode = AxisInfo::MODE_INVERT; 326 327 mTokenizer->skipDelimiters(WHITESPACE); 328 String8 axisToken = mTokenizer->nextToken(WHITESPACE); 329 axisInfo.axis = getAxisByLabel(axisToken.string()); 330 if (axisInfo.axis < 0) { 331 ALOGE("%s: Expected inverted axis label, got '%s'.", 332 mTokenizer->getLocation().string(), axisToken.string()); 333 return BAD_VALUE; 334 } 335 } else if (token == "split") { 336 axisInfo.mode = AxisInfo::MODE_SPLIT; 337 338 mTokenizer->skipDelimiters(WHITESPACE); 339 String8 splitToken = mTokenizer->nextToken(WHITESPACE); 340 axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0)); 341 if (*end) { 342 ALOGE("%s: Expected split value, got '%s'.", 343 mTokenizer->getLocation().string(), splitToken.string()); 344 return BAD_VALUE; 345 } 346 347 mTokenizer->skipDelimiters(WHITESPACE); 348 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE); 349 axisInfo.axis = getAxisByLabel(lowAxisToken.string()); 350 if (axisInfo.axis < 0) { 351 ALOGE("%s: Expected low axis label, got '%s'.", 352 mTokenizer->getLocation().string(), lowAxisToken.string()); 353 return BAD_VALUE; 354 } 355 356 mTokenizer->skipDelimiters(WHITESPACE); 357 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE); 358 axisInfo.highAxis = getAxisByLabel(highAxisToken.string()); 359 if (axisInfo.highAxis < 0) { 360 ALOGE("%s: Expected high axis label, got '%s'.", 361 mTokenizer->getLocation().string(), highAxisToken.string()); 362 return BAD_VALUE; 363 } 364 } else { 365 axisInfo.axis = getAxisByLabel(token.string()); 366 if (axisInfo.axis < 0) { 367 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.", 368 mTokenizer->getLocation().string(), token.string()); 369 return BAD_VALUE; 370 } 371 } 372 373 for (;;) { 374 mTokenizer->skipDelimiters(WHITESPACE); 375 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') { 376 break; 377 } 378 String8 keywordToken = mTokenizer->nextToken(WHITESPACE); 379 if (keywordToken == "flat") { 380 mTokenizer->skipDelimiters(WHITESPACE); 381 String8 flatToken = mTokenizer->nextToken(WHITESPACE); 382 axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0)); 383 if (*end) { 384 ALOGE("%s: Expected flat value, got '%s'.", 385 mTokenizer->getLocation().string(), flatToken.string()); 386 return BAD_VALUE; 387 } 388 } else { 389 ALOGE("%s: Expected keyword 'flat', got '%s'.", 390 mTokenizer->getLocation().string(), keywordToken.string()); 391 return BAD_VALUE; 392 } 393 } 394 395 #if DEBUG_PARSER 396 ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, " 397 "splitValue=%d, flatOverride=%d.", 398 scanCode, 399 axisInfo.mode, axisInfo.axis, axisInfo.highAxis, 400 axisInfo.splitValue, axisInfo.flatOverride); 401 #endif 402 mMap->mAxes.add(scanCode, axisInfo); 403 return NO_ERROR; 404 } 405 406 status_t KeyLayoutMap::Parser::parseLed() { 407 String8 codeToken = mTokenizer->nextToken(WHITESPACE); 408 bool mapUsage = false; 409 if (codeToken == "usage") { 410 mapUsage = true; 411 mTokenizer->skipDelimiters(WHITESPACE); 412 codeToken = mTokenizer->nextToken(WHITESPACE); 413 } 414 char* end; 415 int32_t code = int32_t(strtol(codeToken.string(), &end, 0)); 416 if (*end) { 417 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(), 418 mapUsage ? "usage" : "scan code", codeToken.string()); 419 return BAD_VALUE; 420 } 421 422 KeyedVector<int32_t, Led>& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode; 423 if (map.indexOfKey(code) >= 0) { 424 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(), 425 mapUsage ? "usage" : "scan code", codeToken.string()); 426 return BAD_VALUE; 427 } 428 429 mTokenizer->skipDelimiters(WHITESPACE); 430 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE); 431 int32_t ledCode = getLedByLabel(ledCodeToken.string()); 432 if (ledCode < 0) { 433 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(), 434 ledCodeToken.string()); 435 return BAD_VALUE; 436 } 437 438 #if DEBUG_PARSER 439 ALOGD("Parsed led %s: code=%d, ledCode=%d.", 440 mapUsage ? "usage" : "scan code", code, ledCode); 441 #endif 442 443 Led led; 444 led.ledCode = ledCode; 445 map.add(code, led); 446 return NO_ERROR; 447 } 448 }; 449