Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "VirtualKeyMap" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Mathias Agopian | b93a03f | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 21 | #include <androidfw/VirtualKeyMap.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/Errors.h> |
| 24 | #include <utils/Tokenizer.h> |
| 25 | #include <utils/Timers.h> |
| 26 | |
| 27 | // Enables debug output for the parser. |
| 28 | #define DEBUG_PARSER 0 |
| 29 | |
| 30 | // Enables debug output for parser performance. |
| 31 | #define DEBUG_PARSER_PERFORMANCE 0 |
| 32 | |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | static const char* WHITESPACE = " \t\r"; |
| 37 | static const char* WHITESPACE_OR_FIELD_DELIMITER = " \t\r:"; |
| 38 | |
| 39 | |
| 40 | // --- VirtualKeyMap --- |
| 41 | |
| 42 | VirtualKeyMap::VirtualKeyMap() { |
| 43 | } |
| 44 | |
| 45 | VirtualKeyMap::~VirtualKeyMap() { |
| 46 | } |
| 47 | |
| 48 | status_t VirtualKeyMap::load(const String8& filename, VirtualKeyMap** outMap) { |
| 49 | *outMap = NULL; |
| 50 | |
| 51 | Tokenizer* tokenizer; |
| 52 | status_t status = Tokenizer::open(filename, &tokenizer); |
| 53 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 54 | ALOGE("Error %d opening virtual key map file %s.", status, filename.string()); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 55 | } else { |
| 56 | VirtualKeyMap* map = new VirtualKeyMap(); |
| 57 | if (!map) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 58 | ALOGE("Error allocating virtual key map."); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 59 | status = NO_MEMORY; |
| 60 | } else { |
| 61 | #if DEBUG_PARSER_PERFORMANCE |
| 62 | nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 63 | #endif |
| 64 | Parser parser(map, tokenizer); |
| 65 | status = parser.parse(); |
| 66 | #if DEBUG_PARSER_PERFORMANCE |
| 67 | nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 68 | ALOGD("Parsed key character map file '%s' %d lines in %0.3fms.", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 69 | tokenizer->getFilename().string(), tokenizer->getLineNumber(), |
| 70 | elapsedTime / 1000000.0); |
| 71 | #endif |
| 72 | if (status) { |
| 73 | delete map; |
| 74 | } else { |
| 75 | *outMap = map; |
| 76 | } |
| 77 | } |
| 78 | delete tokenizer; |
| 79 | } |
| 80 | return status; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | // --- VirtualKeyMap::Parser --- |
| 85 | |
| 86 | VirtualKeyMap::Parser::Parser(VirtualKeyMap* map, Tokenizer* tokenizer) : |
| 87 | mMap(map), mTokenizer(tokenizer) { |
| 88 | } |
| 89 | |
| 90 | VirtualKeyMap::Parser::~Parser() { |
| 91 | } |
| 92 | |
| 93 | status_t VirtualKeyMap::Parser::parse() { |
| 94 | while (!mTokenizer->isEof()) { |
| 95 | #if DEBUG_PARSER |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 96 | ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 97 | mTokenizer->peekRemainderOfLine().string()); |
| 98 | #endif |
| 99 | |
| 100 | mTokenizer->skipDelimiters(WHITESPACE); |
| 101 | |
| 102 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
| 103 | // Multiple keys can appear on one line or they can be broken up across multiple lines. |
| 104 | do { |
| 105 | String8 token = mTokenizer->nextToken(WHITESPACE_OR_FIELD_DELIMITER); |
| 106 | if (token != "0x01") { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 107 | ALOGE("%s: Unknown virtual key type, expected 0x01.", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 108 | mTokenizer->getLocation().string()); |
| 109 | return BAD_VALUE; |
| 110 | } |
| 111 | |
| 112 | VirtualKeyDefinition defn; |
| 113 | bool success = parseNextIntField(&defn.scanCode) |
| 114 | && parseNextIntField(&defn.centerX) |
| 115 | && parseNextIntField(&defn.centerY) |
| 116 | && parseNextIntField(&defn.width) |
| 117 | && parseNextIntField(&defn.height); |
| 118 | if (!success) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 119 | ALOGE("%s: Expected 5 colon-delimited integers in virtual key definition.", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 120 | mTokenizer->getLocation().string()); |
| 121 | return BAD_VALUE; |
| 122 | } |
| 123 | |
| 124 | #if DEBUG_PARSER |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 125 | ALOGD("Parsed virtual key: scanCode=%d, centerX=%d, centerY=%d, " |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 126 | "width=%d, height=%d", |
| 127 | defn.scanCode, defn.centerX, defn.centerY, defn.width, defn.height); |
| 128 | #endif |
| 129 | mMap->mVirtualKeys.push(defn); |
| 130 | } while (consumeFieldDelimiterAndSkipWhitespace()); |
| 131 | |
| 132 | if (!mTokenizer->isEol()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 133 | ALOGE("%s: Expected end of line, got '%s'.", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 134 | mTokenizer->getLocation().string(), |
| 135 | mTokenizer->peekRemainderOfLine().string()); |
| 136 | return BAD_VALUE; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | mTokenizer->nextLine(); |
| 141 | } |
| 142 | |
| 143 | return NO_ERROR; |
| 144 | } |
| 145 | |
| 146 | bool VirtualKeyMap::Parser::consumeFieldDelimiterAndSkipWhitespace() { |
| 147 | mTokenizer->skipDelimiters(WHITESPACE); |
| 148 | if (mTokenizer->peekChar() == ':') { |
| 149 | mTokenizer->nextChar(); |
| 150 | mTokenizer->skipDelimiters(WHITESPACE); |
| 151 | return true; |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | bool VirtualKeyMap::Parser::parseNextIntField(int32_t* outValue) { |
| 157 | if (!consumeFieldDelimiterAndSkipWhitespace()) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | String8 token = mTokenizer->nextToken(WHITESPACE_OR_FIELD_DELIMITER); |
| 162 | char* end; |
| 163 | *outValue = strtol(token.string(), &end, 0); |
| 164 | if (token.isEmpty() || *end != '\0') { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 165 | ALOGE("Expected an integer, got '%s'.", token.string()); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | } // namespace android |