blob: c365ab070e154e1084c1450f68673f5d1d5d56d1 [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>
chaviw98318de2021-05-19 16:45:23 -050022#include <ftl/NamedEnum.h>
Michael Wright872db4f2014-04-22 15:03:51 -070023#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024#include <input/KeyLayoutMap.h>
Chris Yef59a2f42020-10-16 12:55:26 -070025#include <input/Keyboard.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <utils/Errors.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <utils/Log.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <utils/Timers.h>
Chris Yef59a2f42020-10-16 12:55:26 -070029#include <utils/Tokenizer.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
31// Enables debug output for the parser.
32#define DEBUG_PARSER 0
33
34// Enables debug output for parser performance.
35#define DEBUG_PARSER_PERFORMANCE 0
36
37// Enables debug output for mapping.
38#define DEBUG_MAPPING 0
39
40
41namespace android {
42
43static const char* WHITESPACE = " \t\r";
44
Chris Yef59a2f42020-10-16 12:55:26 -070045#define SENSOR_ENTRY(type) NamedEnum::string(type), type
46static const std::unordered_map<std::string, InputDeviceSensorType> SENSOR_LIST =
47 {{SENSOR_ENTRY(InputDeviceSensorType::ACCELEROMETER)},
48 {SENSOR_ENTRY(InputDeviceSensorType::MAGNETIC_FIELD)},
49 {SENSOR_ENTRY(InputDeviceSensorType::ORIENTATION)},
50 {SENSOR_ENTRY(InputDeviceSensorType::GYROSCOPE)},
51 {SENSOR_ENTRY(InputDeviceSensorType::LIGHT)},
52 {SENSOR_ENTRY(InputDeviceSensorType::PRESSURE)},
53 {SENSOR_ENTRY(InputDeviceSensorType::TEMPERATURE)},
54 {SENSOR_ENTRY(InputDeviceSensorType::PROXIMITY)},
55 {SENSOR_ENTRY(InputDeviceSensorType::GRAVITY)},
56 {SENSOR_ENTRY(InputDeviceSensorType::LINEAR_ACCELERATION)},
57 {SENSOR_ENTRY(InputDeviceSensorType::ROTATION_VECTOR)},
58 {SENSOR_ENTRY(InputDeviceSensorType::RELATIVE_HUMIDITY)},
59 {SENSOR_ENTRY(InputDeviceSensorType::AMBIENT_TEMPERATURE)},
60 {SENSOR_ENTRY(InputDeviceSensorType::MAGNETIC_FIELD_UNCALIBRATED)},
61 {SENSOR_ENTRY(InputDeviceSensorType::GAME_ROTATION_VECTOR)},
62 {SENSOR_ENTRY(InputDeviceSensorType::GYROSCOPE_UNCALIBRATED)},
63 {SENSOR_ENTRY(InputDeviceSensorType::SIGNIFICANT_MOTION)}};
64
Jeff Brown5912f952013-07-01 19:10:31 -070065// --- KeyLayoutMap ---
66
67KeyLayoutMap::KeyLayoutMap() {
68}
69
70KeyLayoutMap::~KeyLayoutMap() {
71}
72
Chris Ye1abffbd2020-08-18 12:50:12 -070073base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::loadContents(const std::string& filename,
74 const char* contents) {
75 Tokenizer* tokenizer;
76 status_t status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer);
77 if (status) {
78 ALOGE("Error %d opening key layout map.", status);
79 return Errorf("Error {} opening key layout map file {}.", status, filename.c_str());
80 }
81 std::unique_ptr<Tokenizer> t(tokenizer);
82 auto ret = load(t.get());
Bernie Innocenti147c86e2020-12-21 21:01:45 +090083 if (ret.ok()) {
Chris Ye1abffbd2020-08-18 12:50:12 -070084 (*ret)->mLoadFileName = filename;
85 }
86 return ret;
87}
Jeff Brown5912f952013-07-01 19:10:31 -070088
Chris Ye1abffbd2020-08-18 12:50:12 -070089base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(const std::string& filename) {
Jeff Brown5912f952013-07-01 19:10:31 -070090 Tokenizer* tokenizer;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010091 status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
Jeff Brown5912f952013-07-01 19:10:31 -070092 if (status) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010093 ALOGE("Error %d opening key layout map file %s.", status, filename.c_str());
Chris Ye1abffbd2020-08-18 12:50:12 -070094 return Errorf("Error {} opening key layout map file {}.", status, filename.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070095 }
Chris Ye1abffbd2020-08-18 12:50:12 -070096 std::unique_ptr<Tokenizer> t(tokenizer);
97 auto ret = load(t.get());
Bernie Innocenti147c86e2020-12-21 21:01:45 +090098 if (ret.ok()) {
Chris Ye1abffbd2020-08-18 12:50:12 -070099 (*ret)->mLoadFileName = filename;
100 }
101 return ret;
102}
103
104base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(Tokenizer* tokenizer) {
105 std::shared_ptr<KeyLayoutMap> map = std::shared_ptr<KeyLayoutMap>(new KeyLayoutMap());
106 status_t status = OK;
107 if (!map.get()) {
108 ALOGE("Error allocating key layout map.");
109 return Errorf("Error allocating key layout map.");
110 } else {
111#if DEBUG_PARSER_PERFORMANCE
112 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
113#endif
114 Parser parser(map.get(), tokenizer);
115 status = parser.parse();
116#if DEBUG_PARSER_PERFORMANCE
117 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
118 ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
119 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
120 elapsedTime / 1000000.0);
121#endif
122 if (!status) {
123 return std::move(map);
124 }
125 }
126 return Errorf("Load KeyLayoutMap failed {}.", status);
Jeff Brown5912f952013-07-01 19:10:31 -0700127}
128
129status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
130 int32_t* outKeyCode, uint32_t* outFlags) const {
131 const Key* key = getKey(scanCode, usageCode);
132 if (!key) {
133#if DEBUG_MAPPING
134 ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode);
135#endif
136 *outKeyCode = AKEYCODE_UNKNOWN;
137 *outFlags = 0;
138 return NAME_NOT_FOUND;
139 }
140
141 *outKeyCode = key->keyCode;
142 *outFlags = key->flags;
143
144#if DEBUG_MAPPING
145 ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
146 scanCode, usageCode, *outKeyCode, *outFlags);
147#endif
148 return NO_ERROR;
149}
150
Chris Yef59a2f42020-10-16 12:55:26 -0700151// Return pair of sensor type and sensor data index, for the input device abs code
152base::Result<std::pair<InputDeviceSensorType, int32_t>> KeyLayoutMap::mapSensor(int32_t absCode) {
153 auto it = mSensorsByAbsCode.find(absCode);
154 if (it == mSensorsByAbsCode.end()) {
155#if DEBUG_MAPPING
156 ALOGD("mapSensor: absCode=%d, ~ Failed.", absCode);
157#endif
158 return Errorf("Can't find abs code {}.", absCode);
159 }
160 const Sensor& sensor = it->second;
161
162#if DEBUG_MAPPING
163 ALOGD("mapSensor: absCode=%d, sensorType=0x%0x, sensorDataIndex=0x%x.", absCode,
164 NamedEnum::string(sensor.sensorType), sensor.sensorDataIndex);
165#endif
166 return std::make_pair(sensor.sensorType, sensor.sensorDataIndex);
167}
168
Jeff Brown5912f952013-07-01 19:10:31 -0700169const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
170 if (usageCode) {
171 ssize_t index = mKeysByUsageCode.indexOfKey(usageCode);
172 if (index >= 0) {
173 return &mKeysByUsageCode.valueAt(index);
174 }
175 }
176 if (scanCode) {
177 ssize_t index = mKeysByScanCode.indexOfKey(scanCode);
178 if (index >= 0) {
179 return &mKeysByScanCode.valueAt(index);
180 }
181 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700182 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700183}
184
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800185status_t KeyLayoutMap::findScanCodesForKey(
186 int32_t keyCode, std::vector<int32_t>* outScanCodes) const {
Jeff Brown5912f952013-07-01 19:10:31 -0700187 const size_t N = mKeysByScanCode.size();
188 for (size_t i=0; i<N; i++) {
189 if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800190 outScanCodes->push_back(mKeysByScanCode.keyAt(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700191 }
192 }
193 return NO_ERROR;
194}
195
196status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
197 ssize_t index = mAxes.indexOfKey(scanCode);
198 if (index < 0) {
199#if DEBUG_MAPPING
200 ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode);
201#endif
202 return NAME_NOT_FOUND;
203 }
204
205 *outAxisInfo = mAxes.valueAt(index);
206
207#if DEBUG_MAPPING
208 ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
209 "splitValue=%d, flatOverride=%d.",
210 scanCode,
211 outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis,
212 outAxisInfo->splitValue, outAxisInfo->flatOverride);
213#endif
214 return NO_ERROR;
215}
216
Michael Wright74bdd2e2013-10-17 17:35:53 -0700217status_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const {
218 const size_t N = mLedsByScanCode.size();
219 for (size_t i = 0; i < N; i++) {
220 if (mLedsByScanCode.valueAt(i).ledCode == ledCode) {
221 *outScanCode = mLedsByScanCode.keyAt(i);
222#if DEBUG_MAPPING
223 ALOGD("findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode, *outScanCode);
224#endif
225 return NO_ERROR;
226 }
227 }
228#if DEBUG_MAPPING
229 ALOGD("findScanCodeForLed: ledCode=%d ~ Not found.", ledCode);
230#endif
231 return NAME_NOT_FOUND;
232}
233
234status_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const {
235 const size_t N = mLedsByUsageCode.size();
236 for (size_t i = 0; i < N; i++) {
237 if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) {
238 *outUsageCode = mLedsByUsageCode.keyAt(i);
239#if DEBUG_MAPPING
240 ALOGD("findUsageForLed: ledCode=%d, usage=%x.", ledCode, *outUsageCode);
241#endif
242 return NO_ERROR;
243 }
244 }
245#if DEBUG_MAPPING
246 ALOGD("findUsageForLed: ledCode=%d ~ Not found.", ledCode);
247#endif
248 return NAME_NOT_FOUND;
249}
250
Jeff Brown5912f952013-07-01 19:10:31 -0700251
252// --- KeyLayoutMap::Parser ---
253
254KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
255 mMap(map), mTokenizer(tokenizer) {
256}
257
258KeyLayoutMap::Parser::~Parser() {
259}
260
261status_t KeyLayoutMap::Parser::parse() {
262 while (!mTokenizer->isEof()) {
263#if DEBUG_PARSER
264 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
265 mTokenizer->peekRemainderOfLine().string());
266#endif
267
268 mTokenizer->skipDelimiters(WHITESPACE);
269
270 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
271 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
272 if (keywordToken == "key") {
273 mTokenizer->skipDelimiters(WHITESPACE);
274 status_t status = parseKey();
275 if (status) return status;
276 } else if (keywordToken == "axis") {
277 mTokenizer->skipDelimiters(WHITESPACE);
278 status_t status = parseAxis();
279 if (status) return status;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700280 } else if (keywordToken == "led") {
281 mTokenizer->skipDelimiters(WHITESPACE);
282 status_t status = parseLed();
283 if (status) return status;
Chris Yef59a2f42020-10-16 12:55:26 -0700284 } else if (keywordToken == "sensor") {
285 mTokenizer->skipDelimiters(WHITESPACE);
286 status_t status = parseSensor();
287 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700288 } else {
289 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
290 keywordToken.string());
291 return BAD_VALUE;
292 }
293
294 mTokenizer->skipDelimiters(WHITESPACE);
295 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
296 ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
297 mTokenizer->getLocation().string(),
298 mTokenizer->peekRemainderOfLine().string());
299 return BAD_VALUE;
300 }
301 }
302
303 mTokenizer->nextLine();
304 }
305 return NO_ERROR;
306}
307
308status_t KeyLayoutMap::Parser::parseKey() {
309 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
310 bool mapUsage = false;
311 if (codeToken == "usage") {
312 mapUsage = true;
313 mTokenizer->skipDelimiters(WHITESPACE);
314 codeToken = mTokenizer->nextToken(WHITESPACE);
315 }
316
317 char* end;
318 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
319 if (*end) {
320 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
321 mapUsage ? "usage" : "scan code", codeToken.string());
322 return BAD_VALUE;
323 }
Michael Wright74bdd2e2013-10-17 17:35:53 -0700324 KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700325 if (map.indexOfKey(code) >= 0) {
326 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
327 mapUsage ? "usage" : "scan code", codeToken.string());
328 return BAD_VALUE;
329 }
330
331 mTokenizer->skipDelimiters(WHITESPACE);
332 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700333 int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700334 if (!keyCode) {
335 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
336 keyCodeToken.string());
337 return BAD_VALUE;
338 }
339
340 uint32_t flags = 0;
341 for (;;) {
342 mTokenizer->skipDelimiters(WHITESPACE);
343 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
344
345 String8 flagToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700346 uint32_t flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700347 if (!flag) {
348 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
349 flagToken.string());
350 return BAD_VALUE;
351 }
352 if (flags & flag) {
353 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
354 flagToken.string());
355 return BAD_VALUE;
356 }
357 flags |= flag;
358 }
359
360#if DEBUG_PARSER
361 ALOGD("Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
362 mapUsage ? "usage" : "scan code", code, keyCode, flags);
363#endif
364 Key key;
365 key.keyCode = keyCode;
366 key.flags = flags;
367 map.add(code, key);
368 return NO_ERROR;
369}
370
371status_t KeyLayoutMap::Parser::parseAxis() {
372 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
373 char* end;
374 int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
375 if (*end) {
376 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
377 scanCodeToken.string());
378 return BAD_VALUE;
379 }
380 if (mMap->mAxes.indexOfKey(scanCode) >= 0) {
381 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
382 scanCodeToken.string());
383 return BAD_VALUE;
384 }
385
386 AxisInfo axisInfo;
387
388 mTokenizer->skipDelimiters(WHITESPACE);
389 String8 token = mTokenizer->nextToken(WHITESPACE);
390 if (token == "invert") {
391 axisInfo.mode = AxisInfo::MODE_INVERT;
392
393 mTokenizer->skipDelimiters(WHITESPACE);
394 String8 axisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700395 axisInfo.axis = InputEventLookup::getAxisByLabel(axisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700396 if (axisInfo.axis < 0) {
397 ALOGE("%s: Expected inverted axis label, got '%s'.",
398 mTokenizer->getLocation().string(), axisToken.string());
399 return BAD_VALUE;
400 }
401 } else if (token == "split") {
402 axisInfo.mode = AxisInfo::MODE_SPLIT;
403
404 mTokenizer->skipDelimiters(WHITESPACE);
405 String8 splitToken = mTokenizer->nextToken(WHITESPACE);
406 axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
407 if (*end) {
408 ALOGE("%s: Expected split value, got '%s'.",
409 mTokenizer->getLocation().string(), splitToken.string());
410 return BAD_VALUE;
411 }
412
413 mTokenizer->skipDelimiters(WHITESPACE);
414 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700415 axisInfo.axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700416 if (axisInfo.axis < 0) {
417 ALOGE("%s: Expected low axis label, got '%s'.",
418 mTokenizer->getLocation().string(), lowAxisToken.string());
419 return BAD_VALUE;
420 }
421
422 mTokenizer->skipDelimiters(WHITESPACE);
423 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700424 axisInfo.highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700425 if (axisInfo.highAxis < 0) {
426 ALOGE("%s: Expected high axis label, got '%s'.",
427 mTokenizer->getLocation().string(), highAxisToken.string());
428 return BAD_VALUE;
429 }
430 } else {
Chris Ye4958d062020-08-20 13:21:10 -0700431 axisInfo.axis = InputEventLookup::getAxisByLabel(token.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700432 if (axisInfo.axis < 0) {
433 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
434 mTokenizer->getLocation().string(), token.string());
435 return BAD_VALUE;
436 }
437 }
438
439 for (;;) {
440 mTokenizer->skipDelimiters(WHITESPACE);
441 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
442 break;
443 }
444 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
445 if (keywordToken == "flat") {
446 mTokenizer->skipDelimiters(WHITESPACE);
447 String8 flatToken = mTokenizer->nextToken(WHITESPACE);
448 axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
449 if (*end) {
450 ALOGE("%s: Expected flat value, got '%s'.",
451 mTokenizer->getLocation().string(), flatToken.string());
452 return BAD_VALUE;
453 }
454 } else {
455 ALOGE("%s: Expected keyword 'flat', got '%s'.",
456 mTokenizer->getLocation().string(), keywordToken.string());
457 return BAD_VALUE;
458 }
459 }
460
461#if DEBUG_PARSER
462 ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
463 "splitValue=%d, flatOverride=%d.",
464 scanCode,
465 axisInfo.mode, axisInfo.axis, axisInfo.highAxis,
466 axisInfo.splitValue, axisInfo.flatOverride);
467#endif
468 mMap->mAxes.add(scanCode, axisInfo);
469 return NO_ERROR;
470}
471
Michael Wright74bdd2e2013-10-17 17:35:53 -0700472status_t KeyLayoutMap::Parser::parseLed() {
473 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
474 bool mapUsage = false;
475 if (codeToken == "usage") {
476 mapUsage = true;
477 mTokenizer->skipDelimiters(WHITESPACE);
478 codeToken = mTokenizer->nextToken(WHITESPACE);
479 }
480 char* end;
481 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
482 if (*end) {
483 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
484 mapUsage ? "usage" : "scan code", codeToken.string());
485 return BAD_VALUE;
486 }
487
488 KeyedVector<int32_t, Led>& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
489 if (map.indexOfKey(code) >= 0) {
490 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
491 mapUsage ? "usage" : "scan code", codeToken.string());
492 return BAD_VALUE;
493 }
494
495 mTokenizer->skipDelimiters(WHITESPACE);
496 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700497 int32_t ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
Michael Wright74bdd2e2013-10-17 17:35:53 -0700498 if (ledCode < 0) {
499 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
500 ledCodeToken.string());
501 return BAD_VALUE;
502 }
503
504#if DEBUG_PARSER
505 ALOGD("Parsed led %s: code=%d, ledCode=%d.",
506 mapUsage ? "usage" : "scan code", code, ledCode);
507#endif
508
509 Led led;
510 led.ledCode = ledCode;
511 map.add(code, led);
512 return NO_ERROR;
513}
Chris Yef59a2f42020-10-16 12:55:26 -0700514
515static std::optional<InputDeviceSensorType> getSensorType(const char* token) {
516 auto it = SENSOR_LIST.find(std::string(token));
517 if (it == SENSOR_LIST.end()) {
518 return std::nullopt;
519 }
520 return it->second;
521}
522
523static std::optional<int32_t> getSensorDataIndex(String8 token) {
524 std::string tokenStr(token.string());
525 if (tokenStr == "X") {
526 return 0;
527 } else if (tokenStr == "Y") {
528 return 1;
529 } else if (tokenStr == "Z") {
530 return 2;
531 }
532 return std::nullopt;
533}
534
535// Parse sensor type and data index mapping, as below format
536// sensor <raw abs> <sensor type> <sensor data index>
537// raw abs : the linux abs code of the axis
538// sensor type : string name of InputDeviceSensorType
539// sensor data index : the data index of sensor, out of [X, Y, Z]
540// Examples:
541// sensor 0x00 ACCELEROMETER X
542// sensor 0x01 ACCELEROMETER Y
543// sensor 0x02 ACCELEROMETER Z
544// sensor 0x03 GYROSCOPE X
545// sensor 0x04 GYROSCOPE Y
546// sensor 0x05 GYROSCOPE Z
547status_t KeyLayoutMap::Parser::parseSensor() {
548 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
549 char* end;
550 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
551 if (*end) {
552 ALOGE("%s: Expected sensor %s number, got '%s'.", mTokenizer->getLocation().string(),
553 "abs code", codeToken.string());
554 return BAD_VALUE;
555 }
556
557 std::unordered_map<int32_t, Sensor>& map = mMap->mSensorsByAbsCode;
558 if (map.find(code) != map.end()) {
559 ALOGE("%s: Duplicate entry for sensor %s '%s'.", mTokenizer->getLocation().string(),
560 "abs code", codeToken.string());
561 return BAD_VALUE;
562 }
563
564 mTokenizer->skipDelimiters(WHITESPACE);
565 String8 sensorTypeToken = mTokenizer->nextToken(WHITESPACE);
566 std::optional<InputDeviceSensorType> typeOpt = getSensorType(sensorTypeToken.string());
567 if (!typeOpt) {
568 ALOGE("%s: Expected sensor code label, got '%s'.", mTokenizer->getLocation().string(),
569 sensorTypeToken.string());
570 return BAD_VALUE;
571 }
572 InputDeviceSensorType sensorType = typeOpt.value();
573 mTokenizer->skipDelimiters(WHITESPACE);
574 String8 sensorDataIndexToken = mTokenizer->nextToken(WHITESPACE);
575 std::optional<int32_t> indexOpt = getSensorDataIndex(sensorDataIndexToken);
576 if (!indexOpt) {
577 ALOGE("%s: Expected sensor data index label, got '%s'.", mTokenizer->getLocation().string(),
578 sensorDataIndexToken.string());
579 return BAD_VALUE;
580 }
581 int32_t sensorDataIndex = indexOpt.value();
582
583#if DEBUG_PARSER
584 ALOGD("Parsed sensor: abs code=%d, sensorType=%d, sensorDataIndex=%d.", code,
585 NamedEnum::string(sensorType).c_str(), sensorDataIndex);
586#endif
587
588 Sensor sensor;
589 sensor.sensorType = sensorType;
590 sensor.sensorDataIndex = sensorDataIndex;
591 map.emplace(code, sensor);
592 return NO_ERROR;
593}
Jeff Brown5912f952013-07-01 19:10:31 -0700594};