15912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown/*
25912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * Copyright (C) 2008 The Android Open Source Project
35912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown *
45912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
55912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * you may not use this file except in compliance with the License.
65912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * You may obtain a copy of the License at
75912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown *
85912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
95912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown *
105912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * Unless required by applicable law or agreed to in writing, software
115912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
125912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * See the License for the specific language governing permissions and
145912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown * limitations under the License.
155912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown */
165912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
175912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#define LOG_TAG "KeyLayoutMap"
185912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
195912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <stdlib.h>
205912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
215912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <android/keycodes.h>
22872db4f11e407accccba9d37c335ef7e3597eba4Michael Wright#include <input/InputEventLabels.h>
235912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <input/Keyboard.h>
245912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <input/KeyLayoutMap.h>
255912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <utils/Log.h>
265912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <utils/Errors.h>
275912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <utils/Tokenizer.h>
285912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#include <utils/Timers.h>
295912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
305912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown// Enables debug output for the parser.
315912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#define DEBUG_PARSER 0
325912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
335912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown// Enables debug output for parser performance.
345912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#define DEBUG_PARSER_PERFORMANCE 0
355912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
365912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown// Enables debug output for mapping.
375912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#define DEBUG_MAPPING 0
385912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
395912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
405912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownnamespace android {
415912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
425912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatic const char* WHITESPACE = " \t\r";
435912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
445912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown// --- KeyLayoutMap ---
455912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
465912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff BrownKeyLayoutMap::KeyLayoutMap() {
475912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
485912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
495912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff BrownKeyLayoutMap::~KeyLayoutMap() {
505912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
515912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
525912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::load(const String8& filename, sp<KeyLayoutMap>* outMap) {
535912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    outMap->clear();
545912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
555912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    Tokenizer* tokenizer;
565912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    status_t status = Tokenizer::open(filename, &tokenizer);
575912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (status) {
585912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGE("Error %d opening key layout map file %s.", status, filename.string());
595912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    } else {
605912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        sp<KeyLayoutMap> map = new KeyLayoutMap();
615912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (!map.get()) {
625912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("Error allocating key layout map.");
635912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            status = NO_MEMORY;
645912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        } else {
655912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_PARSER_PERFORMANCE
665912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
675912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
685912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            Parser parser(map.get(), tokenizer);
695912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            status = parser.parse();
705912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_PARSER_PERFORMANCE
715912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
725912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
735912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    tokenizer->getFilename().string(), tokenizer->getLineNumber(),
745912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    elapsedTime / 1000000.0);
755912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
765912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            if (!status) {
775912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                *outMap = map;
785912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            }
795912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
805912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        delete tokenizer;
815912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
825912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return status;
835912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
845912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
855912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
865912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        int32_t* outKeyCode, uint32_t* outFlags) const {
875912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    const Key* key = getKey(scanCode, usageCode);
885912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (!key) {
895912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_MAPPING
905912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode);
915912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
925912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        *outKeyCode = AKEYCODE_UNKNOWN;
935912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        *outFlags = 0;
945912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return NAME_NOT_FOUND;
955912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
965912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
975912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    *outKeyCode = key->keyCode;
985912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    *outFlags = key->flags;
995912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1005912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_MAPPING
1015912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
1025912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            scanCode, usageCode, *outKeyCode, *outFlags);
1035912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
1045912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NO_ERROR;
1055912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
1065912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1075912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownconst KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
1085912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (usageCode) {
1095912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ssize_t index = mKeysByUsageCode.indexOfKey(usageCode);
1105912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (index >= 0) {
1115912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return &mKeysByUsageCode.valueAt(index);
1125912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
1135912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
1145912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (scanCode) {
1155912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ssize_t index = mKeysByScanCode.indexOfKey(scanCode);
1165912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (index >= 0) {
1175912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return &mKeysByScanCode.valueAt(index);
1185912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
1195912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
1205912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NULL;
1215912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
1225912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1235912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const {
1245912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    const size_t N = mKeysByScanCode.size();
1255912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    for (size_t i=0; i<N; i++) {
1265912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
1275912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            outScanCodes->add(mKeysByScanCode.keyAt(i));
1285912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
1295912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
1305912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NO_ERROR;
1315912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
1325912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1335912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
1345912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    ssize_t index = mAxes.indexOfKey(scanCode);
1355912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (index < 0) {
1365912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_MAPPING
1375912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode);
1385912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
1395912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return NAME_NOT_FOUND;
1405912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
1415912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1425912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    *outAxisInfo = mAxes.valueAt(index);
1435912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1445912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_MAPPING
1455912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
1465912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            "splitValue=%d, flatOverride=%d.",
1475912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            scanCode,
1485912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis,
1495912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            outAxisInfo->splitValue, outAxisInfo->flatOverride);
1505912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
1515912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NO_ERROR;
1525912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
1535912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
15474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wrightstatus_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const {
15574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    const size_t N = mLedsByScanCode.size();
15674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    for (size_t i = 0; i < N; i++) {
15774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        if (mLedsByScanCode.valueAt(i).ledCode == ledCode) {
15874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            *outScanCode = mLedsByScanCode.keyAt(i);
15974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#if DEBUG_MAPPING
16074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            ALOGD("findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode, *outScanCode);
16174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#endif
16274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            return NO_ERROR;
16374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        }
16474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    }
16574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#if DEBUG_MAPPING
16674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            ALOGD("findScanCodeForLed: ledCode=%d ~ Not found.", ledCode);
16774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#endif
16874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    return NAME_NOT_FOUND;
16974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright}
17074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright
17174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wrightstatus_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const {
17274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    const size_t N = mLedsByUsageCode.size();
17374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    for (size_t i = 0; i < N; i++) {
17474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) {
17574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            *outUsageCode = mLedsByUsageCode.keyAt(i);
17674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#if DEBUG_MAPPING
17774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            ALOGD("findUsageForLed: ledCode=%d, usage=%x.", ledCode, *outUsageCode);
17874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#endif
17974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            return NO_ERROR;
18074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        }
18174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    }
18274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#if DEBUG_MAPPING
18374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            ALOGD("findUsageForLed: ledCode=%d ~ Not found.", ledCode);
18474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#endif
18574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    return NAME_NOT_FOUND;
18674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright}
18774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright
1885912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1895912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown// --- KeyLayoutMap::Parser ---
1905912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1915912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff BrownKeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
1925912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mMap(map), mTokenizer(tokenizer) {
1935912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
1945912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1955912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff BrownKeyLayoutMap::Parser::~Parser() {
1965912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
1975912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
1985912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::Parser::parse() {
1995912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    while (!mTokenizer->isEof()) {
2005912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_PARSER
2015912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
2025912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                mTokenizer->peekRemainderOfLine().string());
2035912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
2045912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2055912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
2065912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2075912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
2085912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
2095912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            if (keywordToken == "key") {
2105912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                mTokenizer->skipDelimiters(WHITESPACE);
2115912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                status_t status = parseKey();
2125912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                if (status) return status;
2135912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            } else if (keywordToken == "axis") {
2145912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                mTokenizer->skipDelimiters(WHITESPACE);
2155912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                status_t status = parseAxis();
2165912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                if (status) return status;
21774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            } else if (keywordToken == "led") {
21874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright                mTokenizer->skipDelimiters(WHITESPACE);
21974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright                status_t status = parseLed();
22074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright                if (status) return status;
2215912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            } else {
2225912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
2235912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                        keywordToken.string());
2245912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                return BAD_VALUE;
2255912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            }
2265912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2275912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            mTokenizer->skipDelimiters(WHITESPACE);
2285912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
2295912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
2305912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                        mTokenizer->getLocation().string(),
2315912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                        mTokenizer->peekRemainderOfLine().string());
2325912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                return BAD_VALUE;
2335912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            }
2345912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
2355912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2365912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->nextLine();
2375912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
2385912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NO_ERROR;
2395912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
2405912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2415912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::Parser::parseKey() {
2425912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    String8 codeToken = mTokenizer->nextToken(WHITESPACE);
2435912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    bool mapUsage = false;
2445912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (codeToken == "usage") {
2455912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mapUsage = true;
2465912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
2475912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        codeToken = mTokenizer->nextToken(WHITESPACE);
2485912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
2495912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2505912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    char* end;
2515912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
2525912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (*end) {
2535912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
2545912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                mapUsage ? "usage" : "scan code", codeToken.string());
2555912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return BAD_VALUE;
2565912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
25774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
2585912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (map.indexOfKey(code) >= 0) {
2595912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
2605912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                mapUsage ? "usage" : "scan code", codeToken.string());
2615912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return BAD_VALUE;
2625912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
2635912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2645912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    mTokenizer->skipDelimiters(WHITESPACE);
2655912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
2665912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string());
2675912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (!keyCode) {
2685912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
2695912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                keyCodeToken.string());
2705912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return BAD_VALUE;
2715912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
2725912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2735912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    uint32_t flags = 0;
2745912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    for (;;) {
2755912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
2765912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
2775912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2785912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        String8 flagToken = mTokenizer->nextToken(WHITESPACE);
2795912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        uint32_t flag = getKeyFlagByLabel(flagToken.string());
2805912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (!flag) {
2815912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
2825912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    flagToken.string());
2835912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
2845912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
2855912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (flags & flag) {
2865912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
2875912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    flagToken.string());
2885912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
2895912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
2905912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        flags |= flag;
2915912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
2925912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
2935912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_PARSER
2945912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    ALOGD("Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
2955912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            mapUsage ? "usage" : "scan code", code, keyCode, flags);
2965912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
2975912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    Key key;
2985912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    key.keyCode = keyCode;
2995912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    key.flags = flags;
3005912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    map.add(code, key);
3015912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NO_ERROR;
3025912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
3035912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3045912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brownstatus_t KeyLayoutMap::Parser::parseAxis() {
3055912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
3065912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    char* end;
3075912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
3085912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (*end) {
3095912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
3105912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                scanCodeToken.string());
3115912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return BAD_VALUE;
3125912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
3135912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (mMap->mAxes.indexOfKey(scanCode) >= 0) {
3145912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
3155912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                scanCodeToken.string());
3165912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        return BAD_VALUE;
3175912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
3185912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3195912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    AxisInfo axisInfo;
3205912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3215912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    mTokenizer->skipDelimiters(WHITESPACE);
3225912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    String8 token = mTokenizer->nextToken(WHITESPACE);
3235912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    if (token == "invert") {
3245912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.mode = AxisInfo::MODE_INVERT;
3255912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3265912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
3275912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        String8 axisToken = mTokenizer->nextToken(WHITESPACE);
3285912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.axis = getAxisByLabel(axisToken.string());
3295912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (axisInfo.axis < 0) {
3305912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected inverted axis label, got '%s'.",
3315912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    mTokenizer->getLocation().string(), axisToken.string());
3325912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
3335912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3345912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    } else if (token == "split") {
3355912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.mode = AxisInfo::MODE_SPLIT;
3365912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3375912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
3385912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        String8 splitToken = mTokenizer->nextToken(WHITESPACE);
3395912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
3405912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (*end) {
3415912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected split value, got '%s'.",
3425912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    mTokenizer->getLocation().string(), splitToken.string());
3435912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
3445912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3455912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3465912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
3475912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
3485912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.axis = getAxisByLabel(lowAxisToken.string());
3495912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (axisInfo.axis < 0) {
3505912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected low axis label, got '%s'.",
3515912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    mTokenizer->getLocation().string(), lowAxisToken.string());
3525912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
3535912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3545912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3555912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
3565912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
3575912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.highAxis = getAxisByLabel(highAxisToken.string());
3585912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (axisInfo.highAxis < 0) {
3595912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected high axis label, got '%s'.",
3605912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    mTokenizer->getLocation().string(), highAxisToken.string());
3615912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
3625912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3635912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    } else {
3645912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        axisInfo.axis = getAxisByLabel(token.string());
3655912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (axisInfo.axis < 0) {
3665912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
3675912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    mTokenizer->getLocation().string(), token.string());
3685912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
3695912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3705912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
3715912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3725912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    for (;;) {
3735912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        mTokenizer->skipDelimiters(WHITESPACE);
3745912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
3755912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            break;
3765912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3775912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
3785912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        if (keywordToken == "flat") {
3795912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            mTokenizer->skipDelimiters(WHITESPACE);
3805912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            String8 flatToken = mTokenizer->nextToken(WHITESPACE);
3815912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
3825912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            if (*end) {
3835912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                ALOGE("%s: Expected flat value, got '%s'.",
3845912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                        mTokenizer->getLocation().string(), flatToken.string());
3855912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                return BAD_VALUE;
3865912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            }
3875912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        } else {
3885912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            ALOGE("%s: Expected keyword 'flat', got '%s'.",
3895912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown                    mTokenizer->getLocation().string(), keywordToken.string());
3905912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            return BAD_VALUE;
3915912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown        }
3925912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    }
3935912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
3945912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#if DEBUG_PARSER
3955912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
3965912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            "splitValue=%d, flatOverride=%d.",
3975912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            scanCode,
3985912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            axisInfo.mode, axisInfo.axis, axisInfo.highAxis,
3995912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown            axisInfo.splitValue, axisInfo.flatOverride);
4005912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown#endif
4015912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    mMap->mAxes.add(scanCode, axisInfo);
4025912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown    return NO_ERROR;
4035912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown}
4045912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown
40574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wrightstatus_t KeyLayoutMap::Parser::parseLed() {
40674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    String8 codeToken = mTokenizer->nextToken(WHITESPACE);
40774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    bool mapUsage = false;
40874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    if (codeToken == "usage") {
40974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        mapUsage = true;
41074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        mTokenizer->skipDelimiters(WHITESPACE);
41174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        codeToken = mTokenizer->nextToken(WHITESPACE);
41274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    }
41374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    char* end;
41474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
41574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    if (*end) {
41674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
41774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright                mapUsage ? "usage" : "scan code", codeToken.string());
41874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        return BAD_VALUE;
41974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    }
42074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright
42174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    KeyedVector<int32_t, Led>& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
42274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    if (map.indexOfKey(code) >= 0) {
42374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
42474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright                mapUsage ? "usage" : "scan code", codeToken.string());
42574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        return BAD_VALUE;
42674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    }
42774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright
42874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    mTokenizer->skipDelimiters(WHITESPACE);
42974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
43074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    int32_t ledCode = getLedByLabel(ledCodeToken.string());
43174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    if (ledCode < 0) {
43274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
43374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright                ledCodeToken.string());
43474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright        return BAD_VALUE;
43574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    }
43674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright
43774bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#if DEBUG_PARSER
43874bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    ALOGD("Parsed led %s: code=%d, ledCode=%d.",
43974bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright            mapUsage ? "usage" : "scan code", code, ledCode);
44074bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright#endif
44174bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright
44274bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    Led led;
44374bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    led.ledCode = ledCode;
44474bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    map.add(code, led);
44574bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright    return NO_ERROR;
44674bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497Michael Wright}
4475912f95d26f77d2b6df13e1f2672e48e3f9b871cJeff Brown};
448