blob: 73710330d0d2fba234e4acbae117a1ffd3b0b19a [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>
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -070028#if defined(__ANDROID__)
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070029#include <vintf/RuntimeInfo.h>
30#include <vintf/VintfObject.h>
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -070031#endif
Jeff Brown5912f952013-07-01 19:10:31 -070032
Dominik Laskowski75788452021-02-09 18:51:25 -080033#include <cstdlib>
34#include <string_view>
35#include <unordered_map>
36
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070037/**
38 * Log debug output for the parser.
39 * Enable this via "adb shell setprop log.tag.KeyLayoutMapParser DEBUG" (requires restart)
40 */
41const bool DEBUG_PARSER =
42 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Parser", ANDROID_LOG_INFO);
Jeff Brown5912f952013-07-01 19:10:31 -070043
44// Enables debug output for parser performance.
45#define DEBUG_PARSER_PERFORMANCE 0
46
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070047/**
48 * Log debug output for mapping.
49 * Enable this via "adb shell setprop log.tag.KeyLayoutMapMapping DEBUG" (requires restart)
50 */
51const bool DEBUG_MAPPING =
52 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Mapping", ANDROID_LOG_INFO);
Jeff Brown5912f952013-07-01 19:10:31 -070053
54namespace android {
Dominik Laskowski75788452021-02-09 18:51:25 -080055namespace {
Jeff Brown5912f952013-07-01 19:10:31 -070056
Dominik Laskowski75788452021-02-09 18:51:25 -080057constexpr const char* WHITESPACE = " \t\r";
Jeff Brown5912f952013-07-01 19:10:31 -070058
Dominik Laskowski75788452021-02-09 18:51:25 -080059template <InputDeviceSensorType S>
60constexpr auto sensorPair() {
61 return std::make_pair(ftl::enum_name<S>(), S);
Jeff Brown5912f952013-07-01 19:10:31 -070062}
63
Dominik Laskowski75788452021-02-09 18:51:25 -080064static const std::unordered_map<std::string_view, InputDeviceSensorType> SENSOR_LIST =
65 {sensorPair<InputDeviceSensorType::ACCELEROMETER>(),
66 sensorPair<InputDeviceSensorType::MAGNETIC_FIELD>(),
67 sensorPair<InputDeviceSensorType::ORIENTATION>(),
68 sensorPair<InputDeviceSensorType::GYROSCOPE>(),
69 sensorPair<InputDeviceSensorType::LIGHT>(),
70 sensorPair<InputDeviceSensorType::PRESSURE>(),
71 sensorPair<InputDeviceSensorType::TEMPERATURE>(),
72 sensorPair<InputDeviceSensorType::PROXIMITY>(),
73 sensorPair<InputDeviceSensorType::GRAVITY>(),
74 sensorPair<InputDeviceSensorType::LINEAR_ACCELERATION>(),
75 sensorPair<InputDeviceSensorType::ROTATION_VECTOR>(),
76 sensorPair<InputDeviceSensorType::RELATIVE_HUMIDITY>(),
77 sensorPair<InputDeviceSensorType::AMBIENT_TEMPERATURE>(),
78 sensorPair<InputDeviceSensorType::MAGNETIC_FIELD_UNCALIBRATED>(),
79 sensorPair<InputDeviceSensorType::GAME_ROTATION_VECTOR>(),
80 sensorPair<InputDeviceSensorType::GYROSCOPE_UNCALIBRATED>(),
81 sensorPair<InputDeviceSensorType::SIGNIFICANT_MOTION>()};
82
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070083bool kernelConfigsArePresent(const std::set<std::string>& configs) {
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -070084#if defined(__ANDROID__)
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070085 std::shared_ptr<const android::vintf::RuntimeInfo> runtimeInfo =
86 android::vintf::VintfObject::GetInstance()->getRuntimeInfo(
87 vintf::RuntimeInfo::FetchFlag::CONFIG_GZ);
88 LOG_ALWAYS_FATAL_IF(runtimeInfo == nullptr, "Kernel configs could not be fetched");
89
90 const std::map<std::string, std::string>& kernelConfigs = runtimeInfo->kernelConfigs();
91 for (const std::string& requiredConfig : configs) {
92 const auto configIt = kernelConfigs.find(requiredConfig);
93 if (configIt == kernelConfigs.end()) {
94 ALOGI("Required kernel config %s is not found", requiredConfig.c_str());
95 return false;
96 }
97 const std::string& option = configIt->second;
98 if (option != "y" && option != "m") {
99 ALOGI("Required kernel config %s has option %s", requiredConfig.c_str(),
100 option.c_str());
101 return false;
102 }
103 }
104 return true;
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -0700105#else
106 (void)configs; // Suppress 'unused variable' warning
107 return true;
108#endif
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700109}
110
Dominik Laskowski75788452021-02-09 18:51:25 -0800111} // namespace
112
113KeyLayoutMap::KeyLayoutMap() = default;
114KeyLayoutMap::~KeyLayoutMap() = default;
Jeff Brown5912f952013-07-01 19:10:31 -0700115
Chris Ye1abffbd2020-08-18 12:50:12 -0700116base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::loadContents(const std::string& filename,
117 const char* contents) {
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700118 return load(filename, contents);
Chris Ye1abffbd2020-08-18 12:50:12 -0700119}
Jeff Brown5912f952013-07-01 19:10:31 -0700120
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700121base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(const std::string& filename,
122 const char* contents) {
Jeff Brown5912f952013-07-01 19:10:31 -0700123 Tokenizer* tokenizer;
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700124 status_t status;
125 if (contents == nullptr) {
126 status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
127 } else {
128 status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer);
129 }
Jeff Brown5912f952013-07-01 19:10:31 -0700130 if (status) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100131 ALOGE("Error %d opening key layout map file %s.", status, filename.c_str());
Chris Ye1abffbd2020-08-18 12:50:12 -0700132 return Errorf("Error {} opening key layout map file {}.", status, filename.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700133 }
Chris Ye1abffbd2020-08-18 12:50:12 -0700134 std::unique_ptr<Tokenizer> t(tokenizer);
135 auto ret = load(t.get());
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700136 if (!ret.ok()) {
137 return ret;
Chris Ye1abffbd2020-08-18 12:50:12 -0700138 }
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700139 const std::shared_ptr<KeyLayoutMap>& map = *ret;
140 LOG_ALWAYS_FATAL_IF(map == nullptr, "Returned map should not be null if there's no error");
141 if (!kernelConfigsArePresent(map->mRequiredKernelConfigs)) {
142 ALOGI("Not loading %s because the required kernel configs are not set", filename.c_str());
143 return Errorf("Missing kernel config");
144 }
145 map->mLoadFileName = filename;
Chris Ye1abffbd2020-08-18 12:50:12 -0700146 return ret;
147}
148
149base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(Tokenizer* tokenizer) {
150 std::shared_ptr<KeyLayoutMap> map = std::shared_ptr<KeyLayoutMap>(new KeyLayoutMap());
151 status_t status = OK;
152 if (!map.get()) {
153 ALOGE("Error allocating key layout map.");
154 return Errorf("Error allocating key layout map.");
155 } else {
156#if DEBUG_PARSER_PERFORMANCE
157 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
158#endif
159 Parser parser(map.get(), tokenizer);
160 status = parser.parse();
161#if DEBUG_PARSER_PERFORMANCE
162 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
163 ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
164 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
165 elapsedTime / 1000000.0);
166#endif
167 if (!status) {
168 return std::move(map);
169 }
170 }
171 return Errorf("Load KeyLayoutMap failed {}.", status);
Jeff Brown5912f952013-07-01 19:10:31 -0700172}
173
174status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
175 int32_t* outKeyCode, uint32_t* outFlags) const {
176 const Key* key = getKey(scanCode, usageCode);
177 if (!key) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700178 ALOGD_IF(DEBUG_MAPPING, "mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode,
179 usageCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700180 *outKeyCode = AKEYCODE_UNKNOWN;
181 *outFlags = 0;
182 return NAME_NOT_FOUND;
183 }
184
185 *outKeyCode = key->keyCode;
186 *outFlags = key->flags;
187
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700188 ALOGD_IF(DEBUG_MAPPING,
189 "mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
190 scanCode, usageCode, *outKeyCode, *outFlags);
Jeff Brown5912f952013-07-01 19:10:31 -0700191 return NO_ERROR;
192}
193
Chris Yef59a2f42020-10-16 12:55:26 -0700194// Return pair of sensor type and sensor data index, for the input device abs code
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000195base::Result<std::pair<InputDeviceSensorType, int32_t>> KeyLayoutMap::mapSensor(
196 int32_t absCode) const {
Chris Yef59a2f42020-10-16 12:55:26 -0700197 auto it = mSensorsByAbsCode.find(absCode);
198 if (it == mSensorsByAbsCode.end()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700199 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, ~ Failed.", absCode);
Chris Yef59a2f42020-10-16 12:55:26 -0700200 return Errorf("Can't find abs code {}.", absCode);
201 }
202 const Sensor& sensor = it->second;
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700203 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, sensorType=%s, sensorDataIndex=0x%x.", absCode,
204 ftl::enum_string(sensor.sensorType).c_str(), sensor.sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700205 return std::make_pair(sensor.sensorType, sensor.sensorDataIndex);
206}
207
Jeff Brown5912f952013-07-01 19:10:31 -0700208const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
209 if (usageCode) {
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700210 auto it = mKeysByUsageCode.find(usageCode);
211 if (it != mKeysByUsageCode.end()) {
212 return &it->second;
Jeff Brown5912f952013-07-01 19:10:31 -0700213 }
214 }
215 if (scanCode) {
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700216 auto it = mKeysByScanCode.find(scanCode);
217 if (it != mKeysByScanCode.end()) {
218 return &it->second;
Jeff Brown5912f952013-07-01 19:10:31 -0700219 }
220 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700221 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700222}
223
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700224std::vector<int32_t> KeyLayoutMap::findScanCodesForKey(int32_t keyCode) const {
225 std::vector<int32_t> scanCodes;
226 for (const auto& [scanCode, key] : mKeysByScanCode) {
227 if (keyCode == key.keyCode) {
228 scanCodes.push_back(scanCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700229 }
230 }
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700231 return scanCodes;
Jeff Brown5912f952013-07-01 19:10:31 -0700232}
233
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700234std::optional<AxisInfo> KeyLayoutMap::mapAxis(int32_t scanCode) const {
235 auto it = mAxes.find(scanCode);
236 if (it == mAxes.end()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700237 ALOGD_IF(DEBUG_MAPPING, "mapAxis: scanCode=%d ~ Failed.", scanCode);
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700238 return std::nullopt;
Jeff Brown5912f952013-07-01 19:10:31 -0700239 }
240
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700241 const AxisInfo& axisInfo = it->second;
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700242 ALOGD_IF(DEBUG_MAPPING,
243 "mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
244 "splitValue=%d, flatOverride=%d.",
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700245 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
246 axisInfo.flatOverride);
247 return axisInfo;
Jeff Brown5912f952013-07-01 19:10:31 -0700248}
249
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700250std::optional<int32_t> KeyLayoutMap::findScanCodeForLed(int32_t ledCode) const {
251 for (const auto& [scanCode, led] : mLedsByScanCode) {
252 if (led.ledCode == ledCode) {
253 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, scanCode=%d.", __func__, ledCode, scanCode);
254 return scanCode;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700255 }
256 }
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700257 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700258 return std::nullopt;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700259}
260
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700261std::optional<int32_t> KeyLayoutMap::findUsageCodeForLed(int32_t ledCode) const {
262 for (const auto& [usageCode, led] : mLedsByUsageCode) {
263 if (led.ledCode == ledCode) {
264 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, usage=%x.", __func__, ledCode, usageCode);
265 return usageCode;
266 }
267 }
268 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
269 return std::nullopt;
270}
Jeff Brown5912f952013-07-01 19:10:31 -0700271
272// --- KeyLayoutMap::Parser ---
273
274KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
275 mMap(map), mTokenizer(tokenizer) {
276}
277
278KeyLayoutMap::Parser::~Parser() {
279}
280
281status_t KeyLayoutMap::Parser::parse() {
282 while (!mTokenizer->isEof()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700283 ALOGD_IF(DEBUG_PARSER, "Parsing %s: '%s'.", mTokenizer->getLocation().string(),
284 mTokenizer->peekRemainderOfLine().string());
Jeff Brown5912f952013-07-01 19:10:31 -0700285
286 mTokenizer->skipDelimiters(WHITESPACE);
287
288 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
289 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
290 if (keywordToken == "key") {
291 mTokenizer->skipDelimiters(WHITESPACE);
292 status_t status = parseKey();
293 if (status) return status;
294 } else if (keywordToken == "axis") {
295 mTokenizer->skipDelimiters(WHITESPACE);
296 status_t status = parseAxis();
297 if (status) return status;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700298 } else if (keywordToken == "led") {
299 mTokenizer->skipDelimiters(WHITESPACE);
300 status_t status = parseLed();
301 if (status) return status;
Chris Yef59a2f42020-10-16 12:55:26 -0700302 } else if (keywordToken == "sensor") {
303 mTokenizer->skipDelimiters(WHITESPACE);
304 status_t status = parseSensor();
305 if (status) return status;
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700306 } else if (keywordToken == "requires_kernel_config") {
307 mTokenizer->skipDelimiters(WHITESPACE);
308 status_t status = parseRequiredKernelConfig();
309 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700310 } else {
311 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
312 keywordToken.string());
313 return BAD_VALUE;
314 }
315
316 mTokenizer->skipDelimiters(WHITESPACE);
317 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
318 ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
319 mTokenizer->getLocation().string(),
320 mTokenizer->peekRemainderOfLine().string());
321 return BAD_VALUE;
322 }
323 }
324
325 mTokenizer->nextLine();
326 }
327 return NO_ERROR;
328}
329
330status_t KeyLayoutMap::Parser::parseKey() {
331 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
332 bool mapUsage = false;
333 if (codeToken == "usage") {
334 mapUsage = true;
335 mTokenizer->skipDelimiters(WHITESPACE);
336 codeToken = mTokenizer->nextToken(WHITESPACE);
337 }
338
339 char* end;
340 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
341 if (*end) {
342 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
343 mapUsage ? "usage" : "scan code", codeToken.string());
344 return BAD_VALUE;
345 }
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700346 std::unordered_map<int32_t, Key>& map =
347 mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
348 if (map.find(code) != map.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700349 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
350 mapUsage ? "usage" : "scan code", codeToken.string());
351 return BAD_VALUE;
352 }
353
354 mTokenizer->skipDelimiters(WHITESPACE);
355 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700356 int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700357 if (!keyCode) {
358 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
359 keyCodeToken.string());
360 return BAD_VALUE;
361 }
362
363 uint32_t flags = 0;
364 for (;;) {
365 mTokenizer->skipDelimiters(WHITESPACE);
366 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
367
368 String8 flagToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700369 uint32_t flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700370 if (!flag) {
371 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
372 flagToken.string());
373 return BAD_VALUE;
374 }
375 if (flags & flag) {
376 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
377 flagToken.string());
378 return BAD_VALUE;
379 }
380 flags |= flag;
381 }
382
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700383 ALOGD_IF(DEBUG_PARSER, "Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
384 mapUsage ? "usage" : "scan code", code, keyCode, flags);
385
Jeff Brown5912f952013-07-01 19:10:31 -0700386 Key key;
387 key.keyCode = keyCode;
388 key.flags = flags;
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700389 map.insert({code, key});
Jeff Brown5912f952013-07-01 19:10:31 -0700390 return NO_ERROR;
391}
392
393status_t KeyLayoutMap::Parser::parseAxis() {
394 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
395 char* end;
396 int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
397 if (*end) {
398 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
399 scanCodeToken.string());
400 return BAD_VALUE;
401 }
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700402 if (mMap->mAxes.find(scanCode) != mMap->mAxes.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700403 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
404 scanCodeToken.string());
405 return BAD_VALUE;
406 }
407
408 AxisInfo axisInfo;
409
410 mTokenizer->skipDelimiters(WHITESPACE);
411 String8 token = mTokenizer->nextToken(WHITESPACE);
412 if (token == "invert") {
413 axisInfo.mode = AxisInfo::MODE_INVERT;
414
415 mTokenizer->skipDelimiters(WHITESPACE);
416 String8 axisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700417 axisInfo.axis = InputEventLookup::getAxisByLabel(axisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700418 if (axisInfo.axis < 0) {
419 ALOGE("%s: Expected inverted axis label, got '%s'.",
420 mTokenizer->getLocation().string(), axisToken.string());
421 return BAD_VALUE;
422 }
423 } else if (token == "split") {
424 axisInfo.mode = AxisInfo::MODE_SPLIT;
425
426 mTokenizer->skipDelimiters(WHITESPACE);
427 String8 splitToken = mTokenizer->nextToken(WHITESPACE);
428 axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
429 if (*end) {
430 ALOGE("%s: Expected split value, got '%s'.",
431 mTokenizer->getLocation().string(), splitToken.string());
432 return BAD_VALUE;
433 }
434
435 mTokenizer->skipDelimiters(WHITESPACE);
436 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700437 axisInfo.axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700438 if (axisInfo.axis < 0) {
439 ALOGE("%s: Expected low axis label, got '%s'.",
440 mTokenizer->getLocation().string(), lowAxisToken.string());
441 return BAD_VALUE;
442 }
443
444 mTokenizer->skipDelimiters(WHITESPACE);
445 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700446 axisInfo.highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700447 if (axisInfo.highAxis < 0) {
448 ALOGE("%s: Expected high axis label, got '%s'.",
449 mTokenizer->getLocation().string(), highAxisToken.string());
450 return BAD_VALUE;
451 }
452 } else {
Chris Ye4958d062020-08-20 13:21:10 -0700453 axisInfo.axis = InputEventLookup::getAxisByLabel(token.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700454 if (axisInfo.axis < 0) {
455 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
456 mTokenizer->getLocation().string(), token.string());
457 return BAD_VALUE;
458 }
459 }
460
461 for (;;) {
462 mTokenizer->skipDelimiters(WHITESPACE);
463 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
464 break;
465 }
466 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
467 if (keywordToken == "flat") {
468 mTokenizer->skipDelimiters(WHITESPACE);
469 String8 flatToken = mTokenizer->nextToken(WHITESPACE);
470 axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
471 if (*end) {
472 ALOGE("%s: Expected flat value, got '%s'.",
473 mTokenizer->getLocation().string(), flatToken.string());
474 return BAD_VALUE;
475 }
476 } else {
477 ALOGE("%s: Expected keyword 'flat', got '%s'.",
478 mTokenizer->getLocation().string(), keywordToken.string());
479 return BAD_VALUE;
480 }
481 }
482
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700483 ALOGD_IF(DEBUG_PARSER,
484 "Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
485 "splitValue=%d, flatOverride=%d.",
486 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
487 axisInfo.flatOverride);
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700488 mMap->mAxes.insert({scanCode, axisInfo});
Jeff Brown5912f952013-07-01 19:10:31 -0700489 return NO_ERROR;
490}
491
Michael Wright74bdd2e2013-10-17 17:35:53 -0700492status_t KeyLayoutMap::Parser::parseLed() {
493 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
494 bool mapUsage = false;
495 if (codeToken == "usage") {
496 mapUsage = true;
497 mTokenizer->skipDelimiters(WHITESPACE);
498 codeToken = mTokenizer->nextToken(WHITESPACE);
499 }
500 char* end;
501 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
502 if (*end) {
503 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
504 mapUsage ? "usage" : "scan code", codeToken.string());
505 return BAD_VALUE;
506 }
507
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700508 std::unordered_map<int32_t, Led>& map =
509 mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
510 if (map.find(code) != map.end()) {
Michael Wright74bdd2e2013-10-17 17:35:53 -0700511 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
512 mapUsage ? "usage" : "scan code", codeToken.string());
513 return BAD_VALUE;
514 }
515
516 mTokenizer->skipDelimiters(WHITESPACE);
517 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700518 int32_t ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
Michael Wright74bdd2e2013-10-17 17:35:53 -0700519 if (ledCode < 0) {
520 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
521 ledCodeToken.string());
522 return BAD_VALUE;
523 }
524
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700525 ALOGD_IF(DEBUG_PARSER, "Parsed led %s: code=%d, ledCode=%d.", mapUsage ? "usage" : "scan code",
526 code, ledCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700527
528 Led led;
529 led.ledCode = ledCode;
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700530 map.insert({code, led});
Michael Wright74bdd2e2013-10-17 17:35:53 -0700531 return NO_ERROR;
532}
Chris Yef59a2f42020-10-16 12:55:26 -0700533
534static std::optional<InputDeviceSensorType> getSensorType(const char* token) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800535 auto it = SENSOR_LIST.find(token);
Chris Yef59a2f42020-10-16 12:55:26 -0700536 if (it == SENSOR_LIST.end()) {
537 return std::nullopt;
538 }
539 return it->second;
540}
541
542static std::optional<int32_t> getSensorDataIndex(String8 token) {
543 std::string tokenStr(token.string());
544 if (tokenStr == "X") {
545 return 0;
546 } else if (tokenStr == "Y") {
547 return 1;
548 } else if (tokenStr == "Z") {
549 return 2;
550 }
551 return std::nullopt;
552}
553
554// Parse sensor type and data index mapping, as below format
555// sensor <raw abs> <sensor type> <sensor data index>
556// raw abs : the linux abs code of the axis
557// sensor type : string name of InputDeviceSensorType
558// sensor data index : the data index of sensor, out of [X, Y, Z]
559// Examples:
560// sensor 0x00 ACCELEROMETER X
561// sensor 0x01 ACCELEROMETER Y
562// sensor 0x02 ACCELEROMETER Z
563// sensor 0x03 GYROSCOPE X
564// sensor 0x04 GYROSCOPE Y
565// sensor 0x05 GYROSCOPE Z
566status_t KeyLayoutMap::Parser::parseSensor() {
567 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
568 char* end;
569 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
570 if (*end) {
571 ALOGE("%s: Expected sensor %s number, got '%s'.", mTokenizer->getLocation().string(),
572 "abs code", codeToken.string());
573 return BAD_VALUE;
574 }
575
576 std::unordered_map<int32_t, Sensor>& map = mMap->mSensorsByAbsCode;
577 if (map.find(code) != map.end()) {
578 ALOGE("%s: Duplicate entry for sensor %s '%s'.", mTokenizer->getLocation().string(),
579 "abs code", codeToken.string());
580 return BAD_VALUE;
581 }
582
583 mTokenizer->skipDelimiters(WHITESPACE);
584 String8 sensorTypeToken = mTokenizer->nextToken(WHITESPACE);
585 std::optional<InputDeviceSensorType> typeOpt = getSensorType(sensorTypeToken.string());
586 if (!typeOpt) {
587 ALOGE("%s: Expected sensor code label, got '%s'.", mTokenizer->getLocation().string(),
588 sensorTypeToken.string());
589 return BAD_VALUE;
590 }
591 InputDeviceSensorType sensorType = typeOpt.value();
592 mTokenizer->skipDelimiters(WHITESPACE);
593 String8 sensorDataIndexToken = mTokenizer->nextToken(WHITESPACE);
594 std::optional<int32_t> indexOpt = getSensorDataIndex(sensorDataIndexToken);
595 if (!indexOpt) {
596 ALOGE("%s: Expected sensor data index label, got '%s'.", mTokenizer->getLocation().string(),
597 sensorDataIndexToken.string());
598 return BAD_VALUE;
599 }
600 int32_t sensorDataIndex = indexOpt.value();
601
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700602 ALOGD_IF(DEBUG_PARSER, "Parsed sensor: abs code=%d, sensorType=%s, sensorDataIndex=%d.", code,
603 ftl::enum_string(sensorType).c_str(), sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700604
605 Sensor sensor;
606 sensor.sensorType = sensorType;
607 sensor.sensorDataIndex = sensorDataIndex;
608 map.emplace(code, sensor);
609 return NO_ERROR;
610}
Dominik Laskowski75788452021-02-09 18:51:25 -0800611
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700612// Parse the name of a required kernel config.
613// The layout won't be used if the specified kernel config is not present
614// Examples:
615// requires_kernel_config CONFIG_HID_PLAYSTATION
616status_t KeyLayoutMap::Parser::parseRequiredKernelConfig() {
617 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
618 std::string configName = codeToken.string();
619
620 const auto result = mMap->mRequiredKernelConfigs.emplace(configName);
621 if (!result.second) {
622 ALOGE("%s: Duplicate entry for required kernel config %s.",
623 mTokenizer->getLocation().string(), configName.c_str());
624 return BAD_VALUE;
625 }
626
627 ALOGD_IF(DEBUG_PARSER, "Parsed required kernel config: name=%s", configName.c_str());
628 return NO_ERROR;
629}
630
Dominik Laskowski75788452021-02-09 18:51:25 -0800631} // namespace android