blob: 9c399b3fa12dd6e97163217a137f0a5c58bfbdab [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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>
Michael Wright872db4f2014-04-22 15:03:51 -070022#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#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
40namespace android {
41
42static const char* WHITESPACE = " \t\r";
43
44// --- KeyLayoutMap ---
45
46KeyLayoutMap::KeyLayoutMap() {
47}
48
49KeyLayoutMap::~KeyLayoutMap() {
50}
51
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010052status_t KeyLayoutMap::load(const std::string& filename, sp<KeyLayoutMap>* outMap) {
Jeff Brown5912f952013-07-01 19:10:31 -070053 outMap->clear();
54
55 Tokenizer* tokenizer;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010056 status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
Jeff Brown5912f952013-07-01 19:10:31 -070057 if (status) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010058 ALOGE("Error %d opening key layout map file %s.", status, filename.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070059 } 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
85status_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
107const 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 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700120 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700121}
122
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800123status_t KeyLayoutMap::findScanCodesForKey(
124 int32_t keyCode, std::vector<int32_t>* outScanCodes) const {
Jeff Brown5912f952013-07-01 19:10:31 -0700125 const size_t N = mKeysByScanCode.size();
126 for (size_t i=0; i<N; i++) {
127 if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800128 outScanCodes->push_back(mKeysByScanCode.keyAt(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700129 }
130 }
131 return NO_ERROR;
132}
133
134status_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
Michael Wright74bdd2e2013-10-17 17:35:53 -0700155status_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
172status_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
Jeff Brown5912f952013-07-01 19:10:31 -0700189
190// --- KeyLayoutMap::Parser ---
191
192KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
193 mMap(map), mTokenizer(tokenizer) {
194}
195
196KeyLayoutMap::Parser::~Parser() {
197}
198
199status_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;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700218 } else if (keywordToken == "led") {
219 mTokenizer->skipDelimiters(WHITESPACE);
220 status_t status = parseLed();
221 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700222 } 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
242status_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 }
Michael Wright74bdd2e2013-10-17 17:35:53 -0700258 KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700259 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);
Chris Ye4958d062020-08-20 13:21:10 -0700267 int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700268 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);
Chris Ye4958d062020-08-20 13:21:10 -0700280 uint32_t flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700281 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
305status_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);
Chris Ye4958d062020-08-20 13:21:10 -0700329 axisInfo.axis = InputEventLookup::getAxisByLabel(axisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700330 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);
Chris Ye4958d062020-08-20 13:21:10 -0700349 axisInfo.axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700350 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);
Chris Ye4958d062020-08-20 13:21:10 -0700358 axisInfo.highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700359 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 {
Chris Ye4958d062020-08-20 13:21:10 -0700365 axisInfo.axis = InputEventLookup::getAxisByLabel(token.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700366 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
Michael Wright74bdd2e2013-10-17 17:35:53 -0700406status_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);
Chris Ye4958d062020-08-20 13:21:10 -0700431 int32_t ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
Michael Wright74bdd2e2013-10-17 17:35:53 -0700432 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}
Jeff Brown5912f952013-07-01 19:10:31 -0700448};