2010-11-11 00:03:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
#define LOG_TAG "KeyLayoutMap"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2010-11-11 00:03:06 +00:00
|
|
|
#include <android/keycodes.h>
|
|
|
|
#include <ui/Keyboard.h>
|
|
|
|
#include <ui/KeyLayoutMap.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <utils/Log.h>
|
2010-11-11 00:03:06 +00:00
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/Tokenizer.h>
|
|
|
|
#include <utils/Timers.h>
|
|
|
|
|
|
|
|
// Enables debug output for the parser.
|
|
|
|
#define DEBUG_PARSER 0
|
|
|
|
|
|
|
|
// Enables debug output for parser performance.
|
|
|
|
#define DEBUG_PARSER_PERFORMANCE 0
|
|
|
|
|
|
|
|
// Enables debug output for mapping.
|
|
|
|
#define DEBUG_MAPPING 0
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
static const char* WHITESPACE = " \t\r";
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
// --- KeyLayoutMap ---
|
|
|
|
|
|
|
|
KeyLayoutMap::KeyLayoutMap() {
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
KeyLayoutMap::~KeyLayoutMap() {
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
status_t KeyLayoutMap::load(const String8& filename, KeyLayoutMap** outMap) {
|
|
|
|
*outMap = NULL;
|
|
|
|
|
|
|
|
Tokenizer* tokenizer;
|
|
|
|
status_t status = Tokenizer::open(filename, &tokenizer);
|
|
|
|
if (status) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Error %d opening key layout map file %s.", status, filename.string());
|
2010-11-11 00:03:06 +00:00
|
|
|
} else {
|
|
|
|
KeyLayoutMap* map = new KeyLayoutMap();
|
|
|
|
if (!map) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Error allocating key layout map.");
|
2010-11-11 00:03:06 +00:00
|
|
|
status = NO_MEMORY;
|
|
|
|
} else {
|
|
|
|
#if DEBUG_PARSER_PERFORMANCE
|
|
|
|
nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
#endif
|
|
|
|
Parser parser(map, tokenizer);
|
|
|
|
status = parser.parse();
|
|
|
|
#if DEBUG_PARSER_PERFORMANCE
|
|
|
|
nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
|
2010-11-11 00:03:06 +00:00
|
|
|
tokenizer->getFilename().string(), tokenizer->getLineNumber(),
|
|
|
|
elapsedTime / 1000000.0);
|
|
|
|
#endif
|
|
|
|
if (status) {
|
|
|
|
delete map;
|
|
|
|
} else {
|
|
|
|
*outMap = map;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-11-11 00:03:06 +00:00
|
|
|
delete tokenizer;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-11-11 00:03:06 +00:00
|
|
|
return status;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 09:08:02 +00:00
|
|
|
status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t* keyCode, uint32_t* flags) const {
|
2010-11-11 00:03:06 +00:00
|
|
|
ssize_t index = mKeys.indexOfKey(scanCode);
|
|
|
|
if (index < 0) {
|
|
|
|
#if DEBUG_MAPPING
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("mapKey: scanCode=%d ~ Failed.", scanCode);
|
2010-11-11 00:03:06 +00:00
|
|
|
#endif
|
|
|
|
*keyCode = AKEYCODE_UNKNOWN;
|
|
|
|
*flags = 0;
|
|
|
|
return NAME_NOT_FOUND;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
const Key& k = mKeys.valueAt(index);
|
|
|
|
*keyCode = k.keyCode;
|
|
|
|
*flags = k.flags;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
#if DEBUG_MAPPING
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("mapKey: scanCode=%d ~ Result keyCode=%d, flags=0x%08x.", scanCode, *keyCode, *flags);
|
2010-11-11 00:03:06 +00:00
|
|
|
#endif
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2011-02-19 09:08:02 +00:00
|
|
|
status_t KeyLayoutMap::findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const {
|
2010-11-11 00:03:06 +00:00
|
|
|
const size_t N = mKeys.size();
|
|
|
|
for (size_t i=0; i<N; i++) {
|
|
|
|
if (mKeys.valueAt(i).keyCode == keyCode) {
|
|
|
|
outScanCodes->add(mKeys.keyAt(i));
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-11 00:03:06 +00:00
|
|
|
return NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 21:07:49 +00:00
|
|
|
status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
|
2011-02-19 09:08:02 +00:00
|
|
|
ssize_t index = mAxes.indexOfKey(scanCode);
|
|
|
|
if (index < 0) {
|
|
|
|
#if DEBUG_MAPPING
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode);
|
2011-02-19 09:08:02 +00:00
|
|
|
#endif
|
|
|
|
return NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2011-03-04 21:07:49 +00:00
|
|
|
*outAxisInfo = mAxes.valueAt(index);
|
2011-02-19 09:08:02 +00:00
|
|
|
|
|
|
|
#if DEBUG_MAPPING
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
|
2011-03-04 21:07:49 +00:00
|
|
|
"splitValue=%d, flatOverride=%d.",
|
|
|
|
scanCode,
|
|
|
|
outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis,
|
|
|
|
outAxisInfo->splitValue, outAxisInfo->flatOverride);
|
2011-02-19 09:08:02 +00:00
|
|
|
#endif
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
// --- KeyLayoutMap::Parser ---
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
|
|
|
|
mMap(map), mTokenizer(tokenizer) {
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
KeyLayoutMap::Parser::~Parser() {
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
status_t KeyLayoutMap::Parser::parse() {
|
|
|
|
while (!mTokenizer->isEof()) {
|
|
|
|
#if DEBUG_PARSER
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
mTokenizer->peekRemainderOfLine().string());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
|
|
|
|
if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
|
|
|
|
String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
if (keywordToken == "key") {
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
status_t status = parseKey();
|
|
|
|
if (status) return status;
|
2011-02-19 09:08:02 +00:00
|
|
|
} else if (keywordToken == "axis") {
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
status_t status = parseAxis();
|
|
|
|
if (status) return status;
|
2010-11-11 00:03:06 +00:00
|
|
|
} else {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
keywordToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
if (!mTokenizer->isEol()) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected end of line, got '%s'.",
|
2010-11-11 00:03:06 +00:00
|
|
|
mTokenizer->getLocation().string(),
|
|
|
|
mTokenizer->peekRemainderOfLine().string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
mTokenizer->nextLine();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-11-11 00:03:06 +00:00
|
|
|
status_t KeyLayoutMap::Parser::parseKey() {
|
|
|
|
String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
char* end;
|
|
|
|
int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
|
|
|
|
if (*end) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected key scan code number, got '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
scanCodeToken.string());
|
|
|
|
return BAD_VALUE;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-11-11 00:03:06 +00:00
|
|
|
if (mMap->mKeys.indexOfKey(scanCode) >= 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Duplicate entry for key scan code '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
scanCodeToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string());
|
|
|
|
if (!keyCode) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
keyCodeToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
for (;;) {
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
if (mTokenizer->isEol()) break;
|
|
|
|
|
|
|
|
String8 flagToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
uint32_t flag = getKeyFlagByLabel(flagToken.string());
|
|
|
|
if (!flag) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
flagToken.string());
|
|
|
|
return BAD_VALUE;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-11-11 00:03:06 +00:00
|
|
|
if (flags & flag) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
|
2010-11-11 00:03:06 +00:00
|
|
|
flagToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
flags |= flag;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-11-11 00:03:06 +00:00
|
|
|
|
|
|
|
#if DEBUG_PARSER
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Parsed key: scanCode=%d, keyCode=%d, flags=0x%08x.", scanCode, keyCode, flags);
|
2010-11-11 00:03:06 +00:00
|
|
|
#endif
|
|
|
|
Key key;
|
|
|
|
key.keyCode = keyCode;
|
|
|
|
key.flags = flags;
|
|
|
|
mMap->mKeys.add(scanCode, key);
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-02-19 09:08:02 +00:00
|
|
|
status_t KeyLayoutMap::Parser::parseAxis() {
|
|
|
|
String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
char* end;
|
|
|
|
int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
|
|
|
|
if (*end) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
|
2011-02-19 09:08:02 +00:00
|
|
|
scanCodeToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
if (mMap->mAxes.indexOfKey(scanCode) >= 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
|
2011-02-19 09:08:02 +00:00
|
|
|
scanCodeToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2011-03-04 21:07:49 +00:00
|
|
|
AxisInfo axisInfo;
|
|
|
|
|
2011-02-19 09:08:02 +00:00
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
2011-03-04 21:07:49 +00:00
|
|
|
String8 token = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
if (token == "invert") {
|
|
|
|
axisInfo.mode = AxisInfo::MODE_INVERT;
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
String8 axisToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
axisInfo.axis = getAxisByLabel(axisToken.string());
|
|
|
|
if (axisInfo.axis < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected inverted axis label, got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), axisToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
} else if (token == "split") {
|
|
|
|
axisInfo.mode = AxisInfo::MODE_SPLIT;
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
String8 splitToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
|
|
|
|
if (*end) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected split value, got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), splitToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
axisInfo.axis = getAxisByLabel(lowAxisToken.string());
|
|
|
|
if (axisInfo.axis < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected low axis label, got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), lowAxisToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
axisInfo.highAxis = getAxisByLabel(highAxisToken.string());
|
|
|
|
if (axisInfo.highAxis < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected high axis label, got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), highAxisToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
axisInfo.axis = getAxisByLabel(token.string());
|
|
|
|
if (axisInfo.axis < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), token.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
if (mTokenizer->isEol()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
if (keywordToken == "flat") {
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
String8 flatToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
|
|
|
|
if (*end) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected flat value, got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), flatToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
} else {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("%s: Expected keyword 'flat', got '%s'.",
|
2011-03-04 21:07:49 +00:00
|
|
|
mTokenizer->getLocation().string(), keywordToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2011-02-19 09:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_PARSER
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
|
2011-03-04 21:07:49 +00:00
|
|
|
"splitValue=%d, flatOverride=%d.",
|
|
|
|
scanCode,
|
|
|
|
axisInfo.mode, axisInfo.axis, axisInfo.highAxis,
|
|
|
|
axisInfo.splitValue, axisInfo.flatOverride);
|
2011-02-19 09:08:02 +00:00
|
|
|
#endif
|
2011-03-04 21:07:49 +00:00
|
|
|
mMap->mAxes.add(scanCode, axisInfo);
|
2011-02-19 09:08:02 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
};
|