blob: 17c3bb36e315219781af532b2deee232a0709101 [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
Jeff Brown5912f952013-07-01 19:10:31 -070019#include <android/keycodes.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080020#include <ftl/enum.h>
Michael Wright872db4f2014-04-22 15:03:51 -070021#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <input/KeyLayoutMap.h>
Chris Yef59a2f42020-10-16 12:55:26 -070023#include <input/Keyboard.h>
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070024#include <log/log.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <utils/Errors.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <utils/Timers.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <utils/Tokenizer.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028
Dominik Laskowski75788452021-02-09 18:51:25 -080029#include <cstdlib>
30#include <string_view>
31#include <unordered_map>
32
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070033/**
34 * Log debug output for the parser.
35 * Enable this via "adb shell setprop log.tag.KeyLayoutMapParser DEBUG" (requires restart)
36 */
37const bool DEBUG_PARSER =
38 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Parser", ANDROID_LOG_INFO);
Jeff Brown5912f952013-07-01 19:10:31 -070039
40// Enables debug output for parser performance.
41#define DEBUG_PARSER_PERFORMANCE 0
42
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070043/**
44 * Log debug output for mapping.
45 * Enable this via "adb shell setprop log.tag.KeyLayoutMapMapping DEBUG" (requires restart)
46 */
47const bool DEBUG_MAPPING =
48 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Mapping", ANDROID_LOG_INFO);
Jeff Brown5912f952013-07-01 19:10:31 -070049
50namespace android {
Dominik Laskowski75788452021-02-09 18:51:25 -080051namespace {
Jeff Brown5912f952013-07-01 19:10:31 -070052
Dominik Laskowski75788452021-02-09 18:51:25 -080053constexpr const char* WHITESPACE = " \t\r";
Jeff Brown5912f952013-07-01 19:10:31 -070054
Dominik Laskowski75788452021-02-09 18:51:25 -080055template <InputDeviceSensorType S>
56constexpr auto sensorPair() {
57 return std::make_pair(ftl::enum_name<S>(), S);
Jeff Brown5912f952013-07-01 19:10:31 -070058}
59
Dominik Laskowski75788452021-02-09 18:51:25 -080060static const std::unordered_map<std::string_view, InputDeviceSensorType> SENSOR_LIST =
61 {sensorPair<InputDeviceSensorType::ACCELEROMETER>(),
62 sensorPair<InputDeviceSensorType::MAGNETIC_FIELD>(),
63 sensorPair<InputDeviceSensorType::ORIENTATION>(),
64 sensorPair<InputDeviceSensorType::GYROSCOPE>(),
65 sensorPair<InputDeviceSensorType::LIGHT>(),
66 sensorPair<InputDeviceSensorType::PRESSURE>(),
67 sensorPair<InputDeviceSensorType::TEMPERATURE>(),
68 sensorPair<InputDeviceSensorType::PROXIMITY>(),
69 sensorPair<InputDeviceSensorType::GRAVITY>(),
70 sensorPair<InputDeviceSensorType::LINEAR_ACCELERATION>(),
71 sensorPair<InputDeviceSensorType::ROTATION_VECTOR>(),
72 sensorPair<InputDeviceSensorType::RELATIVE_HUMIDITY>(),
73 sensorPair<InputDeviceSensorType::AMBIENT_TEMPERATURE>(),
74 sensorPair<InputDeviceSensorType::MAGNETIC_FIELD_UNCALIBRATED>(),
75 sensorPair<InputDeviceSensorType::GAME_ROTATION_VECTOR>(),
76 sensorPair<InputDeviceSensorType::GYROSCOPE_UNCALIBRATED>(),
77 sensorPair<InputDeviceSensorType::SIGNIFICANT_MOTION>()};
78
79} // namespace
80
81KeyLayoutMap::KeyLayoutMap() = default;
82KeyLayoutMap::~KeyLayoutMap() = default;
Jeff Brown5912f952013-07-01 19:10:31 -070083
Chris Ye1abffbd2020-08-18 12:50:12 -070084base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::loadContents(const std::string& filename,
85 const char* contents) {
86 Tokenizer* tokenizer;
87 status_t status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer);
88 if (status) {
89 ALOGE("Error %d opening key layout map.", status);
90 return Errorf("Error {} opening key layout map file {}.", status, filename.c_str());
91 }
92 std::unique_ptr<Tokenizer> t(tokenizer);
93 auto ret = load(t.get());
Bernie Innocenti147c86e2020-12-21 21:01:45 +090094 if (ret.ok()) {
Chris Ye1abffbd2020-08-18 12:50:12 -070095 (*ret)->mLoadFileName = filename;
96 }
97 return ret;
98}
Jeff Brown5912f952013-07-01 19:10:31 -070099
Chris Ye1abffbd2020-08-18 12:50:12 -0700100base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(const std::string& filename) {
Jeff Brown5912f952013-07-01 19:10:31 -0700101 Tokenizer* tokenizer;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100102 status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
Jeff Brown5912f952013-07-01 19:10:31 -0700103 if (status) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100104 ALOGE("Error %d opening key layout map file %s.", status, filename.c_str());
Chris Ye1abffbd2020-08-18 12:50:12 -0700105 return Errorf("Error {} opening key layout map file {}.", status, filename.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700106 }
Chris Ye1abffbd2020-08-18 12:50:12 -0700107 std::unique_ptr<Tokenizer> t(tokenizer);
108 auto ret = load(t.get());
Bernie Innocenti147c86e2020-12-21 21:01:45 +0900109 if (ret.ok()) {
Chris Ye1abffbd2020-08-18 12:50:12 -0700110 (*ret)->mLoadFileName = filename;
111 }
112 return ret;
113}
114
115base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(Tokenizer* tokenizer) {
116 std::shared_ptr<KeyLayoutMap> map = std::shared_ptr<KeyLayoutMap>(new KeyLayoutMap());
117 status_t status = OK;
118 if (!map.get()) {
119 ALOGE("Error allocating key layout map.");
120 return Errorf("Error allocating key layout map.");
121 } else {
122#if DEBUG_PARSER_PERFORMANCE
123 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
124#endif
125 Parser parser(map.get(), tokenizer);
126 status = parser.parse();
127#if DEBUG_PARSER_PERFORMANCE
128 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
129 ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
130 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
131 elapsedTime / 1000000.0);
132#endif
133 if (!status) {
134 return std::move(map);
135 }
136 }
137 return Errorf("Load KeyLayoutMap failed {}.", status);
Jeff Brown5912f952013-07-01 19:10:31 -0700138}
139
140status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
141 int32_t* outKeyCode, uint32_t* outFlags) const {
142 const Key* key = getKey(scanCode, usageCode);
143 if (!key) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700144 ALOGD_IF(DEBUG_MAPPING, "mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode,
145 usageCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700146 *outKeyCode = AKEYCODE_UNKNOWN;
147 *outFlags = 0;
148 return NAME_NOT_FOUND;
149 }
150
151 *outKeyCode = key->keyCode;
152 *outFlags = key->flags;
153
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700154 ALOGD_IF(DEBUG_MAPPING,
155 "mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
156 scanCode, usageCode, *outKeyCode, *outFlags);
Jeff Brown5912f952013-07-01 19:10:31 -0700157 return NO_ERROR;
158}
159
Chris Yef59a2f42020-10-16 12:55:26 -0700160// Return pair of sensor type and sensor data index, for the input device abs code
161base::Result<std::pair<InputDeviceSensorType, int32_t>> KeyLayoutMap::mapSensor(int32_t absCode) {
162 auto it = mSensorsByAbsCode.find(absCode);
163 if (it == mSensorsByAbsCode.end()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700164 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, ~ Failed.", absCode);
Chris Yef59a2f42020-10-16 12:55:26 -0700165 return Errorf("Can't find abs code {}.", absCode);
166 }
167 const Sensor& sensor = it->second;
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700168 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, sensorType=%s, sensorDataIndex=0x%x.", absCode,
169 ftl::enum_string(sensor.sensorType).c_str(), sensor.sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700170 return std::make_pair(sensor.sensorType, sensor.sensorDataIndex);
171}
172
Jeff Brown5912f952013-07-01 19:10:31 -0700173const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
174 if (usageCode) {
175 ssize_t index = mKeysByUsageCode.indexOfKey(usageCode);
176 if (index >= 0) {
177 return &mKeysByUsageCode.valueAt(index);
178 }
179 }
180 if (scanCode) {
181 ssize_t index = mKeysByScanCode.indexOfKey(scanCode);
182 if (index >= 0) {
183 return &mKeysByScanCode.valueAt(index);
184 }
185 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700186 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700187}
188
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800189status_t KeyLayoutMap::findScanCodesForKey(
190 int32_t keyCode, std::vector<int32_t>* outScanCodes) const {
Jeff Brown5912f952013-07-01 19:10:31 -0700191 const size_t N = mKeysByScanCode.size();
192 for (size_t i=0; i<N; i++) {
193 if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800194 outScanCodes->push_back(mKeysByScanCode.keyAt(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700195 }
196 }
197 return NO_ERROR;
198}
199
200status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
201 ssize_t index = mAxes.indexOfKey(scanCode);
202 if (index < 0) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700203 ALOGD_IF(DEBUG_MAPPING, "mapAxis: scanCode=%d ~ Failed.", scanCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700204 return NAME_NOT_FOUND;
205 }
206
207 *outAxisInfo = mAxes.valueAt(index);
208
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700209 ALOGD_IF(DEBUG_MAPPING,
210 "mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
211 "splitValue=%d, flatOverride=%d.",
212 scanCode, outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis,
213 outAxisInfo->splitValue, outAxisInfo->flatOverride);
214
Jeff Brown5912f952013-07-01 19:10:31 -0700215 return NO_ERROR;
216}
217
Michael Wright74bdd2e2013-10-17 17:35:53 -0700218status_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const {
219 const size_t N = mLedsByScanCode.size();
220 for (size_t i = 0; i < N; i++) {
221 if (mLedsByScanCode.valueAt(i).ledCode == ledCode) {
222 *outScanCode = mLedsByScanCode.keyAt(i);
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700223 ALOGD_IF(DEBUG_MAPPING, "findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode,
224 *outScanCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700225 return NO_ERROR;
226 }
227 }
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700228 ALOGD_IF(DEBUG_MAPPING, "findScanCodeForLed: ledCode=%d ~ Not found.", ledCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700229 return NAME_NOT_FOUND;
230}
231
232status_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const {
233 const size_t N = mLedsByUsageCode.size();
234 for (size_t i = 0; i < N; i++) {
235 if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) {
236 *outUsageCode = mLedsByUsageCode.keyAt(i);
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700237 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, usage=%x.", __func__, ledCode, *outUsageCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700238 return NO_ERROR;
239 }
240 }
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700241 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
242
Michael Wright74bdd2e2013-10-17 17:35:53 -0700243 return NAME_NOT_FOUND;
244}
245
Jeff Brown5912f952013-07-01 19:10:31 -0700246
247// --- KeyLayoutMap::Parser ---
248
249KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
250 mMap(map), mTokenizer(tokenizer) {
251}
252
253KeyLayoutMap::Parser::~Parser() {
254}
255
256status_t KeyLayoutMap::Parser::parse() {
257 while (!mTokenizer->isEof()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700258 ALOGD_IF(DEBUG_PARSER, "Parsing %s: '%s'.", mTokenizer->getLocation().string(),
259 mTokenizer->peekRemainderOfLine().string());
Jeff Brown5912f952013-07-01 19:10:31 -0700260
261 mTokenizer->skipDelimiters(WHITESPACE);
262
263 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
264 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
265 if (keywordToken == "key") {
266 mTokenizer->skipDelimiters(WHITESPACE);
267 status_t status = parseKey();
268 if (status) return status;
269 } else if (keywordToken == "axis") {
270 mTokenizer->skipDelimiters(WHITESPACE);
271 status_t status = parseAxis();
272 if (status) return status;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700273 } else if (keywordToken == "led") {
274 mTokenizer->skipDelimiters(WHITESPACE);
275 status_t status = parseLed();
276 if (status) return status;
Chris Yef59a2f42020-10-16 12:55:26 -0700277 } else if (keywordToken == "sensor") {
278 mTokenizer->skipDelimiters(WHITESPACE);
279 status_t status = parseSensor();
280 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700281 } else {
282 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
283 keywordToken.string());
284 return BAD_VALUE;
285 }
286
287 mTokenizer->skipDelimiters(WHITESPACE);
288 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
289 ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
290 mTokenizer->getLocation().string(),
291 mTokenizer->peekRemainderOfLine().string());
292 return BAD_VALUE;
293 }
294 }
295
296 mTokenizer->nextLine();
297 }
298 return NO_ERROR;
299}
300
301status_t KeyLayoutMap::Parser::parseKey() {
302 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
303 bool mapUsage = false;
304 if (codeToken == "usage") {
305 mapUsage = true;
306 mTokenizer->skipDelimiters(WHITESPACE);
307 codeToken = mTokenizer->nextToken(WHITESPACE);
308 }
309
310 char* end;
311 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
312 if (*end) {
313 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
314 mapUsage ? "usage" : "scan code", codeToken.string());
315 return BAD_VALUE;
316 }
Michael Wright74bdd2e2013-10-17 17:35:53 -0700317 KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700318 if (map.indexOfKey(code) >= 0) {
319 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
320 mapUsage ? "usage" : "scan code", codeToken.string());
321 return BAD_VALUE;
322 }
323
324 mTokenizer->skipDelimiters(WHITESPACE);
325 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700326 int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700327 if (!keyCode) {
328 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
329 keyCodeToken.string());
330 return BAD_VALUE;
331 }
332
333 uint32_t flags = 0;
334 for (;;) {
335 mTokenizer->skipDelimiters(WHITESPACE);
336 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
337
338 String8 flagToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700339 uint32_t flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700340 if (!flag) {
341 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
342 flagToken.string());
343 return BAD_VALUE;
344 }
345 if (flags & flag) {
346 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
347 flagToken.string());
348 return BAD_VALUE;
349 }
350 flags |= flag;
351 }
352
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700353 ALOGD_IF(DEBUG_PARSER, "Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
354 mapUsage ? "usage" : "scan code", code, keyCode, flags);
355
Jeff Brown5912f952013-07-01 19:10:31 -0700356 Key key;
357 key.keyCode = keyCode;
358 key.flags = flags;
359 map.add(code, key);
360 return NO_ERROR;
361}
362
363status_t KeyLayoutMap::Parser::parseAxis() {
364 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
365 char* end;
366 int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
367 if (*end) {
368 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
369 scanCodeToken.string());
370 return BAD_VALUE;
371 }
372 if (mMap->mAxes.indexOfKey(scanCode) >= 0) {
373 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
374 scanCodeToken.string());
375 return BAD_VALUE;
376 }
377
378 AxisInfo axisInfo;
379
380 mTokenizer->skipDelimiters(WHITESPACE);
381 String8 token = mTokenizer->nextToken(WHITESPACE);
382 if (token == "invert") {
383 axisInfo.mode = AxisInfo::MODE_INVERT;
384
385 mTokenizer->skipDelimiters(WHITESPACE);
386 String8 axisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700387 axisInfo.axis = InputEventLookup::getAxisByLabel(axisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700388 if (axisInfo.axis < 0) {
389 ALOGE("%s: Expected inverted axis label, got '%s'.",
390 mTokenizer->getLocation().string(), axisToken.string());
391 return BAD_VALUE;
392 }
393 } else if (token == "split") {
394 axisInfo.mode = AxisInfo::MODE_SPLIT;
395
396 mTokenizer->skipDelimiters(WHITESPACE);
397 String8 splitToken = mTokenizer->nextToken(WHITESPACE);
398 axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
399 if (*end) {
400 ALOGE("%s: Expected split value, got '%s'.",
401 mTokenizer->getLocation().string(), splitToken.string());
402 return BAD_VALUE;
403 }
404
405 mTokenizer->skipDelimiters(WHITESPACE);
406 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700407 axisInfo.axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700408 if (axisInfo.axis < 0) {
409 ALOGE("%s: Expected low axis label, got '%s'.",
410 mTokenizer->getLocation().string(), lowAxisToken.string());
411 return BAD_VALUE;
412 }
413
414 mTokenizer->skipDelimiters(WHITESPACE);
415 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700416 axisInfo.highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700417 if (axisInfo.highAxis < 0) {
418 ALOGE("%s: Expected high axis label, got '%s'.",
419 mTokenizer->getLocation().string(), highAxisToken.string());
420 return BAD_VALUE;
421 }
422 } else {
Chris Ye4958d062020-08-20 13:21:10 -0700423 axisInfo.axis = InputEventLookup::getAxisByLabel(token.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700424 if (axisInfo.axis < 0) {
425 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
426 mTokenizer->getLocation().string(), token.string());
427 return BAD_VALUE;
428 }
429 }
430
431 for (;;) {
432 mTokenizer->skipDelimiters(WHITESPACE);
433 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
434 break;
435 }
436 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
437 if (keywordToken == "flat") {
438 mTokenizer->skipDelimiters(WHITESPACE);
439 String8 flatToken = mTokenizer->nextToken(WHITESPACE);
440 axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
441 if (*end) {
442 ALOGE("%s: Expected flat value, got '%s'.",
443 mTokenizer->getLocation().string(), flatToken.string());
444 return BAD_VALUE;
445 }
446 } else {
447 ALOGE("%s: Expected keyword 'flat', got '%s'.",
448 mTokenizer->getLocation().string(), keywordToken.string());
449 return BAD_VALUE;
450 }
451 }
452
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700453 ALOGD_IF(DEBUG_PARSER,
454 "Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
455 "splitValue=%d, flatOverride=%d.",
456 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
457 axisInfo.flatOverride);
458
Jeff Brown5912f952013-07-01 19:10:31 -0700459 mMap->mAxes.add(scanCode, axisInfo);
460 return NO_ERROR;
461}
462
Michael Wright74bdd2e2013-10-17 17:35:53 -0700463status_t KeyLayoutMap::Parser::parseLed() {
464 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
465 bool mapUsage = false;
466 if (codeToken == "usage") {
467 mapUsage = true;
468 mTokenizer->skipDelimiters(WHITESPACE);
469 codeToken = mTokenizer->nextToken(WHITESPACE);
470 }
471 char* end;
472 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
473 if (*end) {
474 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
475 mapUsage ? "usage" : "scan code", codeToken.string());
476 return BAD_VALUE;
477 }
478
479 KeyedVector<int32_t, Led>& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
480 if (map.indexOfKey(code) >= 0) {
481 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
482 mapUsage ? "usage" : "scan code", codeToken.string());
483 return BAD_VALUE;
484 }
485
486 mTokenizer->skipDelimiters(WHITESPACE);
487 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700488 int32_t ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
Michael Wright74bdd2e2013-10-17 17:35:53 -0700489 if (ledCode < 0) {
490 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
491 ledCodeToken.string());
492 return BAD_VALUE;
493 }
494
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700495 ALOGD_IF(DEBUG_PARSER, "Parsed led %s: code=%d, ledCode=%d.", mapUsage ? "usage" : "scan code",
496 code, ledCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700497
498 Led led;
499 led.ledCode = ledCode;
500 map.add(code, led);
501 return NO_ERROR;
502}
Chris Yef59a2f42020-10-16 12:55:26 -0700503
504static std::optional<InputDeviceSensorType> getSensorType(const char* token) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800505 auto it = SENSOR_LIST.find(token);
Chris Yef59a2f42020-10-16 12:55:26 -0700506 if (it == SENSOR_LIST.end()) {
507 return std::nullopt;
508 }
509 return it->second;
510}
511
512static std::optional<int32_t> getSensorDataIndex(String8 token) {
513 std::string tokenStr(token.string());
514 if (tokenStr == "X") {
515 return 0;
516 } else if (tokenStr == "Y") {
517 return 1;
518 } else if (tokenStr == "Z") {
519 return 2;
520 }
521 return std::nullopt;
522}
523
524// Parse sensor type and data index mapping, as below format
525// sensor <raw abs> <sensor type> <sensor data index>
526// raw abs : the linux abs code of the axis
527// sensor type : string name of InputDeviceSensorType
528// sensor data index : the data index of sensor, out of [X, Y, Z]
529// Examples:
530// sensor 0x00 ACCELEROMETER X
531// sensor 0x01 ACCELEROMETER Y
532// sensor 0x02 ACCELEROMETER Z
533// sensor 0x03 GYROSCOPE X
534// sensor 0x04 GYROSCOPE Y
535// sensor 0x05 GYROSCOPE Z
536status_t KeyLayoutMap::Parser::parseSensor() {
537 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
538 char* end;
539 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
540 if (*end) {
541 ALOGE("%s: Expected sensor %s number, got '%s'.", mTokenizer->getLocation().string(),
542 "abs code", codeToken.string());
543 return BAD_VALUE;
544 }
545
546 std::unordered_map<int32_t, Sensor>& map = mMap->mSensorsByAbsCode;
547 if (map.find(code) != map.end()) {
548 ALOGE("%s: Duplicate entry for sensor %s '%s'.", mTokenizer->getLocation().string(),
549 "abs code", codeToken.string());
550 return BAD_VALUE;
551 }
552
553 mTokenizer->skipDelimiters(WHITESPACE);
554 String8 sensorTypeToken = mTokenizer->nextToken(WHITESPACE);
555 std::optional<InputDeviceSensorType> typeOpt = getSensorType(sensorTypeToken.string());
556 if (!typeOpt) {
557 ALOGE("%s: Expected sensor code label, got '%s'.", mTokenizer->getLocation().string(),
558 sensorTypeToken.string());
559 return BAD_VALUE;
560 }
561 InputDeviceSensorType sensorType = typeOpt.value();
562 mTokenizer->skipDelimiters(WHITESPACE);
563 String8 sensorDataIndexToken = mTokenizer->nextToken(WHITESPACE);
564 std::optional<int32_t> indexOpt = getSensorDataIndex(sensorDataIndexToken);
565 if (!indexOpt) {
566 ALOGE("%s: Expected sensor data index label, got '%s'.", mTokenizer->getLocation().string(),
567 sensorDataIndexToken.string());
568 return BAD_VALUE;
569 }
570 int32_t sensorDataIndex = indexOpt.value();
571
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700572 ALOGD_IF(DEBUG_PARSER, "Parsed sensor: abs code=%d, sensorType=%s, sensorDataIndex=%d.", code,
573 ftl::enum_string(sensorType).c_str(), sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700574
575 Sensor sensor;
576 sensor.sensorType = sensorType;
577 sensor.sensorDataIndex = sensorDataIndex;
578 map.emplace(code, sensor);
579 return NO_ERROR;
580}
Dominik Laskowski75788452021-02-09 18:51:25 -0800581
582} // namespace android