widget/gonk/libui/VirtualKeyMap.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial