blob: a194513953848a42920a0d250c745f85f01fe43c [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
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080019#include <android-base/logging.h>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <android/keycodes.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080021#include <ftl/enum.h>
Michael Wright872db4f2014-04-22 15:03:51 -070022#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <input/KeyLayoutMap.h>
Chris Yef59a2f42020-10-16 12:55:26 -070024#include <input/Keyboard.h>
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070025#include <log/log.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <utils/Errors.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <utils/Timers.h>
Chris Yef59a2f42020-10-16 12:55:26 -070028#include <utils/Tokenizer.h>
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -070029#if defined(__ANDROID__)
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070030#include <vintf/RuntimeInfo.h>
31#include <vintf/VintfObject.h>
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -070032#endif
Jeff Brown5912f952013-07-01 19:10:31 -070033
Dominik Laskowski75788452021-02-09 18:51:25 -080034#include <cstdlib>
35#include <string_view>
36#include <unordered_map>
37
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070038/**
39 * Log debug output for the parser.
40 * Enable this via "adb shell setprop log.tag.KeyLayoutMapParser DEBUG" (requires restart)
41 */
42const bool DEBUG_PARSER =
43 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Parser", ANDROID_LOG_INFO);
Jeff Brown5912f952013-07-01 19:10:31 -070044
45// Enables debug output for parser performance.
46#define DEBUG_PARSER_PERFORMANCE 0
47
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070048/**
49 * Log debug output for mapping.
50 * Enable this via "adb shell setprop log.tag.KeyLayoutMapMapping DEBUG" (requires restart)
51 */
52const bool DEBUG_MAPPING =
53 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Mapping", ANDROID_LOG_INFO);
Jeff Brown5912f952013-07-01 19:10:31 -070054
55namespace android {
Dominik Laskowski75788452021-02-09 18:51:25 -080056namespace {
Jeff Brown5912f952013-07-01 19:10:31 -070057
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080058std::optional<int> parseInt(const char* str) {
59 char* end;
60 errno = 0;
61 const int value = strtol(str, &end, 0);
62 if (end == str) {
63 LOG(ERROR) << "Could not parse " << str;
64 return {};
65 }
66 if (errno == ERANGE) {
67 LOG(ERROR) << "Out of bounds: " << str;
68 return {};
69 }
70 return value;
71}
72
Dominik Laskowski75788452021-02-09 18:51:25 -080073constexpr const char* WHITESPACE = " \t\r";
Jeff Brown5912f952013-07-01 19:10:31 -070074
Dominik Laskowski75788452021-02-09 18:51:25 -080075template <InputDeviceSensorType S>
76constexpr auto sensorPair() {
77 return std::make_pair(ftl::enum_name<S>(), S);
Jeff Brown5912f952013-07-01 19:10:31 -070078}
79
Dominik Laskowski75788452021-02-09 18:51:25 -080080static const std::unordered_map<std::string_view, InputDeviceSensorType> SENSOR_LIST =
81 {sensorPair<InputDeviceSensorType::ACCELEROMETER>(),
82 sensorPair<InputDeviceSensorType::MAGNETIC_FIELD>(),
83 sensorPair<InputDeviceSensorType::ORIENTATION>(),
84 sensorPair<InputDeviceSensorType::GYROSCOPE>(),
85 sensorPair<InputDeviceSensorType::LIGHT>(),
86 sensorPair<InputDeviceSensorType::PRESSURE>(),
87 sensorPair<InputDeviceSensorType::TEMPERATURE>(),
88 sensorPair<InputDeviceSensorType::PROXIMITY>(),
89 sensorPair<InputDeviceSensorType::GRAVITY>(),
90 sensorPair<InputDeviceSensorType::LINEAR_ACCELERATION>(),
91 sensorPair<InputDeviceSensorType::ROTATION_VECTOR>(),
92 sensorPair<InputDeviceSensorType::RELATIVE_HUMIDITY>(),
93 sensorPair<InputDeviceSensorType::AMBIENT_TEMPERATURE>(),
94 sensorPair<InputDeviceSensorType::MAGNETIC_FIELD_UNCALIBRATED>(),
95 sensorPair<InputDeviceSensorType::GAME_ROTATION_VECTOR>(),
96 sensorPair<InputDeviceSensorType::GYROSCOPE_UNCALIBRATED>(),
97 sensorPair<InputDeviceSensorType::SIGNIFICANT_MOTION>()};
98
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070099bool kernelConfigsArePresent(const std::set<std::string>& configs) {
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -0700100#if defined(__ANDROID__)
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700101 std::shared_ptr<const android::vintf::RuntimeInfo> runtimeInfo =
102 android::vintf::VintfObject::GetInstance()->getRuntimeInfo(
103 vintf::RuntimeInfo::FetchFlag::CONFIG_GZ);
104 LOG_ALWAYS_FATAL_IF(runtimeInfo == nullptr, "Kernel configs could not be fetched");
105
106 const std::map<std::string, std::string>& kernelConfigs = runtimeInfo->kernelConfigs();
107 for (const std::string& requiredConfig : configs) {
108 const auto configIt = kernelConfigs.find(requiredConfig);
109 if (configIt == kernelConfigs.end()) {
110 ALOGI("Required kernel config %s is not found", requiredConfig.c_str());
111 return false;
112 }
113 const std::string& option = configIt->second;
114 if (option != "y" && option != "m") {
115 ALOGI("Required kernel config %s has option %s", requiredConfig.c_str(),
116 option.c_str());
117 return false;
118 }
119 }
120 return true;
Siarhei Vishniakou8fc145f2022-08-17 16:07:40 -0700121#else
122 (void)configs; // Suppress 'unused variable' warning
123 return true;
124#endif
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700125}
126
Dominik Laskowski75788452021-02-09 18:51:25 -0800127} // namespace
128
129KeyLayoutMap::KeyLayoutMap() = default;
130KeyLayoutMap::~KeyLayoutMap() = default;
Jeff Brown5912f952013-07-01 19:10:31 -0700131
Chris Ye1abffbd2020-08-18 12:50:12 -0700132base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::loadContents(const std::string& filename,
133 const char* contents) {
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700134 return load(filename, contents);
Chris Ye1abffbd2020-08-18 12:50:12 -0700135}
Jeff Brown5912f952013-07-01 19:10:31 -0700136
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700137base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(const std::string& filename,
138 const char* contents) {
Jeff Brown5912f952013-07-01 19:10:31 -0700139 Tokenizer* tokenizer;
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700140 status_t status;
141 if (contents == nullptr) {
142 status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
143 } else {
144 status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer);
145 }
Jeff Brown5912f952013-07-01 19:10:31 -0700146 if (status) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100147 ALOGE("Error %d opening key layout map file %s.", status, filename.c_str());
Chris Ye1abffbd2020-08-18 12:50:12 -0700148 return Errorf("Error {} opening key layout map file {}.", status, filename.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700149 }
Chris Ye1abffbd2020-08-18 12:50:12 -0700150 std::unique_ptr<Tokenizer> t(tokenizer);
151 auto ret = load(t.get());
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700152 if (!ret.ok()) {
153 return ret;
Chris Ye1abffbd2020-08-18 12:50:12 -0700154 }
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700155 const std::shared_ptr<KeyLayoutMap>& map = *ret;
156 LOG_ALWAYS_FATAL_IF(map == nullptr, "Returned map should not be null if there's no error");
157 if (!kernelConfigsArePresent(map->mRequiredKernelConfigs)) {
158 ALOGI("Not loading %s because the required kernel configs are not set", filename.c_str());
159 return Errorf("Missing kernel config");
160 }
161 map->mLoadFileName = filename;
Chris Ye1abffbd2020-08-18 12:50:12 -0700162 return ret;
163}
164
165base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(Tokenizer* tokenizer) {
166 std::shared_ptr<KeyLayoutMap> map = std::shared_ptr<KeyLayoutMap>(new KeyLayoutMap());
167 status_t status = OK;
168 if (!map.get()) {
169 ALOGE("Error allocating key layout map.");
170 return Errorf("Error allocating key layout map.");
171 } else {
172#if DEBUG_PARSER_PERFORMANCE
173 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
174#endif
175 Parser parser(map.get(), tokenizer);
176 status = parser.parse();
177#if DEBUG_PARSER_PERFORMANCE
178 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
179 ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
180 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
181 elapsedTime / 1000000.0);
182#endif
183 if (!status) {
184 return std::move(map);
185 }
186 }
187 return Errorf("Load KeyLayoutMap failed {}.", status);
Jeff Brown5912f952013-07-01 19:10:31 -0700188}
189
190status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
191 int32_t* outKeyCode, uint32_t* outFlags) const {
192 const Key* key = getKey(scanCode, usageCode);
193 if (!key) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700194 ALOGD_IF(DEBUG_MAPPING, "mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode,
195 usageCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700196 *outKeyCode = AKEYCODE_UNKNOWN;
197 *outFlags = 0;
198 return NAME_NOT_FOUND;
199 }
200
201 *outKeyCode = key->keyCode;
202 *outFlags = key->flags;
203
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700204 ALOGD_IF(DEBUG_MAPPING,
205 "mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
206 scanCode, usageCode, *outKeyCode, *outFlags);
Jeff Brown5912f952013-07-01 19:10:31 -0700207 return NO_ERROR;
208}
209
Chris Yef59a2f42020-10-16 12:55:26 -0700210// Return pair of sensor type and sensor data index, for the input device abs code
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000211base::Result<std::pair<InputDeviceSensorType, int32_t>> KeyLayoutMap::mapSensor(
212 int32_t absCode) const {
Chris Yef59a2f42020-10-16 12:55:26 -0700213 auto it = mSensorsByAbsCode.find(absCode);
214 if (it == mSensorsByAbsCode.end()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700215 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, ~ Failed.", absCode);
Chris Yef59a2f42020-10-16 12:55:26 -0700216 return Errorf("Can't find abs code {}.", absCode);
217 }
218 const Sensor& sensor = it->second;
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700219 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, sensorType=%s, sensorDataIndex=0x%x.", absCode,
220 ftl::enum_string(sensor.sensorType).c_str(), sensor.sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700221 return std::make_pair(sensor.sensorType, sensor.sensorDataIndex);
222}
223
Jeff Brown5912f952013-07-01 19:10:31 -0700224const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
225 if (usageCode) {
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700226 auto it = mKeysByUsageCode.find(usageCode);
227 if (it != mKeysByUsageCode.end()) {
228 return &it->second;
Jeff Brown5912f952013-07-01 19:10:31 -0700229 }
230 }
231 if (scanCode) {
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700232 auto it = mKeysByScanCode.find(scanCode);
233 if (it != mKeysByScanCode.end()) {
234 return &it->second;
Jeff Brown5912f952013-07-01 19:10:31 -0700235 }
236 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700237 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700238}
239
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700240std::vector<int32_t> KeyLayoutMap::findScanCodesForKey(int32_t keyCode) const {
241 std::vector<int32_t> scanCodes;
242 for (const auto& [scanCode, key] : mKeysByScanCode) {
243 if (keyCode == key.keyCode) {
244 scanCodes.push_back(scanCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700245 }
246 }
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700247 return scanCodes;
Jeff Brown5912f952013-07-01 19:10:31 -0700248}
249
Charles Linaadf8d52023-03-30 13:34:54 +0800250std::vector<int32_t> KeyLayoutMap::findUsageCodesForKey(int32_t keyCode) const {
251 std::vector<int32_t> usageCodes;
252 for (const auto& [usageCode, key] : mKeysByUsageCode) {
253 if (keyCode == key.keyCode) {
254 usageCodes.push_back(usageCode);
255 }
256 }
257 return usageCodes;
258}
259
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700260std::optional<AxisInfo> KeyLayoutMap::mapAxis(int32_t scanCode) const {
261 auto it = mAxes.find(scanCode);
262 if (it == mAxes.end()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700263 ALOGD_IF(DEBUG_MAPPING, "mapAxis: scanCode=%d ~ Failed.", scanCode);
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700264 return std::nullopt;
Jeff Brown5912f952013-07-01 19:10:31 -0700265 }
266
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700267 const AxisInfo& axisInfo = it->second;
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700268 ALOGD_IF(DEBUG_MAPPING,
269 "mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
270 "splitValue=%d, flatOverride=%d.",
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700271 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
272 axisInfo.flatOverride);
273 return axisInfo;
Jeff Brown5912f952013-07-01 19:10:31 -0700274}
275
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700276std::optional<int32_t> KeyLayoutMap::findScanCodeForLed(int32_t ledCode) const {
277 for (const auto& [scanCode, led] : mLedsByScanCode) {
278 if (led.ledCode == ledCode) {
279 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, scanCode=%d.", __func__, ledCode, scanCode);
280 return scanCode;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700281 }
282 }
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700283 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700284 return std::nullopt;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700285}
286
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700287std::optional<int32_t> KeyLayoutMap::findUsageCodeForLed(int32_t ledCode) const {
288 for (const auto& [usageCode, led] : mLedsByUsageCode) {
289 if (led.ledCode == ledCode) {
290 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, usage=%x.", __func__, ledCode, usageCode);
291 return usageCode;
292 }
293 }
294 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
295 return std::nullopt;
296}
Jeff Brown5912f952013-07-01 19:10:31 -0700297
298// --- KeyLayoutMap::Parser ---
299
300KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
301 mMap(map), mTokenizer(tokenizer) {
302}
303
304KeyLayoutMap::Parser::~Parser() {
305}
306
307status_t KeyLayoutMap::Parser::parse() {
308 while (!mTokenizer->isEof()) {
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700309 ALOGD_IF(DEBUG_PARSER, "Parsing %s: '%s'.", mTokenizer->getLocation().string(),
310 mTokenizer->peekRemainderOfLine().string());
Jeff Brown5912f952013-07-01 19:10:31 -0700311
312 mTokenizer->skipDelimiters(WHITESPACE);
313
314 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
315 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
316 if (keywordToken == "key") {
317 mTokenizer->skipDelimiters(WHITESPACE);
318 status_t status = parseKey();
319 if (status) return status;
320 } else if (keywordToken == "axis") {
321 mTokenizer->skipDelimiters(WHITESPACE);
322 status_t status = parseAxis();
323 if (status) return status;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700324 } else if (keywordToken == "led") {
325 mTokenizer->skipDelimiters(WHITESPACE);
326 status_t status = parseLed();
327 if (status) return status;
Chris Yef59a2f42020-10-16 12:55:26 -0700328 } else if (keywordToken == "sensor") {
329 mTokenizer->skipDelimiters(WHITESPACE);
330 status_t status = parseSensor();
331 if (status) return status;
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700332 } else if (keywordToken == "requires_kernel_config") {
333 mTokenizer->skipDelimiters(WHITESPACE);
334 status_t status = parseRequiredKernelConfig();
335 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700336 } else {
337 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
338 keywordToken.string());
339 return BAD_VALUE;
340 }
341
342 mTokenizer->skipDelimiters(WHITESPACE);
343 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
344 ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
345 mTokenizer->getLocation().string(),
346 mTokenizer->peekRemainderOfLine().string());
347 return BAD_VALUE;
348 }
349 }
350
351 mTokenizer->nextLine();
352 }
353 return NO_ERROR;
354}
355
356status_t KeyLayoutMap::Parser::parseKey() {
357 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
358 bool mapUsage = false;
359 if (codeToken == "usage") {
360 mapUsage = true;
361 mTokenizer->skipDelimiters(WHITESPACE);
362 codeToken = mTokenizer->nextToken(WHITESPACE);
363 }
364
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800365 std::optional<int> code = parseInt(codeToken.string());
366 if (!code) {
Jeff Brown5912f952013-07-01 19:10:31 -0700367 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
368 mapUsage ? "usage" : "scan code", codeToken.string());
369 return BAD_VALUE;
370 }
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700371 std::unordered_map<int32_t, Key>& map =
372 mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800373 if (map.find(*code) != map.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700374 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
375 mapUsage ? "usage" : "scan code", codeToken.string());
376 return BAD_VALUE;
377 }
378
379 mTokenizer->skipDelimiters(WHITESPACE);
380 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800381 std::optional<int> keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700382 if (!keyCode) {
383 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
384 keyCodeToken.string());
385 return BAD_VALUE;
386 }
387
388 uint32_t flags = 0;
389 for (;;) {
390 mTokenizer->skipDelimiters(WHITESPACE);
391 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
392
393 String8 flagToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800394 std::optional<int> flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700395 if (!flag) {
396 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
397 flagToken.string());
398 return BAD_VALUE;
399 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800400 if (flags & *flag) {
Jeff Brown5912f952013-07-01 19:10:31 -0700401 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
402 flagToken.string());
403 return BAD_VALUE;
404 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800405 flags |= *flag;
Jeff Brown5912f952013-07-01 19:10:31 -0700406 }
407
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700408 ALOGD_IF(DEBUG_PARSER, "Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800409 mapUsage ? "usage" : "scan code", *code, *keyCode, flags);
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700410
Jeff Brown5912f952013-07-01 19:10:31 -0700411 Key key;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800412 key.keyCode = *keyCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700413 key.flags = flags;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800414 map.insert({*code, key});
Jeff Brown5912f952013-07-01 19:10:31 -0700415 return NO_ERROR;
416}
417
418status_t KeyLayoutMap::Parser::parseAxis() {
419 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800420 std::optional<int> scanCode = parseInt(scanCodeToken.string());
421 if (!scanCode) {
Jeff Brown5912f952013-07-01 19:10:31 -0700422 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
423 scanCodeToken.string());
424 return BAD_VALUE;
425 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800426 if (mMap->mAxes.find(*scanCode) != mMap->mAxes.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700427 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
428 scanCodeToken.string());
429 return BAD_VALUE;
430 }
431
432 AxisInfo axisInfo;
433
434 mTokenizer->skipDelimiters(WHITESPACE);
435 String8 token = mTokenizer->nextToken(WHITESPACE);
436 if (token == "invert") {
437 axisInfo.mode = AxisInfo::MODE_INVERT;
438
439 mTokenizer->skipDelimiters(WHITESPACE);
440 String8 axisToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800441 std::optional<int> axis = InputEventLookup::getAxisByLabel(axisToken.string());
442 if (!axis) {
Jeff Brown5912f952013-07-01 19:10:31 -0700443 ALOGE("%s: Expected inverted axis label, got '%s'.",
444 mTokenizer->getLocation().string(), axisToken.string());
445 return BAD_VALUE;
446 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800447 axisInfo.axis = *axis;
Jeff Brown5912f952013-07-01 19:10:31 -0700448 } else if (token == "split") {
449 axisInfo.mode = AxisInfo::MODE_SPLIT;
450
451 mTokenizer->skipDelimiters(WHITESPACE);
452 String8 splitToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800453 std::optional<int> splitValue = parseInt(splitToken.string());
454 if (!splitValue) {
Jeff Brown5912f952013-07-01 19:10:31 -0700455 ALOGE("%s: Expected split value, got '%s'.",
456 mTokenizer->getLocation().string(), splitToken.string());
457 return BAD_VALUE;
458 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800459 axisInfo.splitValue = *splitValue;
Jeff Brown5912f952013-07-01 19:10:31 -0700460
461 mTokenizer->skipDelimiters(WHITESPACE);
462 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800463 std::optional<int> axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
464 if (!axis) {
Jeff Brown5912f952013-07-01 19:10:31 -0700465 ALOGE("%s: Expected low axis label, got '%s'.",
466 mTokenizer->getLocation().string(), lowAxisToken.string());
467 return BAD_VALUE;
468 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800469 axisInfo.axis = *axis;
Jeff Brown5912f952013-07-01 19:10:31 -0700470
471 mTokenizer->skipDelimiters(WHITESPACE);
472 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800473 std::optional<int> highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
474 if (!highAxis) {
Jeff Brown5912f952013-07-01 19:10:31 -0700475 ALOGE("%s: Expected high axis label, got '%s'.",
476 mTokenizer->getLocation().string(), highAxisToken.string());
477 return BAD_VALUE;
478 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800479 axisInfo.highAxis = *highAxis;
Jeff Brown5912f952013-07-01 19:10:31 -0700480 } else {
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800481 std::optional<int> axis = InputEventLookup::getAxisByLabel(token.string());
482 if (!axis) {
Jeff Brown5912f952013-07-01 19:10:31 -0700483 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
484 mTokenizer->getLocation().string(), token.string());
485 return BAD_VALUE;
486 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800487 axisInfo.axis = *axis;
Jeff Brown5912f952013-07-01 19:10:31 -0700488 }
489
490 for (;;) {
491 mTokenizer->skipDelimiters(WHITESPACE);
492 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
493 break;
494 }
495 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
496 if (keywordToken == "flat") {
497 mTokenizer->skipDelimiters(WHITESPACE);
498 String8 flatToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800499 std::optional<int> flatOverride = parseInt(flatToken.string());
500 if (!flatOverride) {
Jeff Brown5912f952013-07-01 19:10:31 -0700501 ALOGE("%s: Expected flat value, got '%s'.",
502 mTokenizer->getLocation().string(), flatToken.string());
503 return BAD_VALUE;
504 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800505 axisInfo.flatOverride = *flatOverride;
Jeff Brown5912f952013-07-01 19:10:31 -0700506 } else {
507 ALOGE("%s: Expected keyword 'flat', got '%s'.",
508 mTokenizer->getLocation().string(), keywordToken.string());
509 return BAD_VALUE;
510 }
511 }
512
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700513 ALOGD_IF(DEBUG_PARSER,
514 "Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
515 "splitValue=%d, flatOverride=%d.",
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800516 *scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700517 axisInfo.flatOverride);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800518 mMap->mAxes.insert({*scanCode, axisInfo});
Jeff Brown5912f952013-07-01 19:10:31 -0700519 return NO_ERROR;
520}
521
Michael Wright74bdd2e2013-10-17 17:35:53 -0700522status_t KeyLayoutMap::Parser::parseLed() {
523 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
524 bool mapUsage = false;
525 if (codeToken == "usage") {
526 mapUsage = true;
527 mTokenizer->skipDelimiters(WHITESPACE);
528 codeToken = mTokenizer->nextToken(WHITESPACE);
529 }
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800530 std::optional<int> code = parseInt(codeToken.string());
531 if (!code) {
Michael Wright74bdd2e2013-10-17 17:35:53 -0700532 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
533 mapUsage ? "usage" : "scan code", codeToken.string());
534 return BAD_VALUE;
535 }
536
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700537 std::unordered_map<int32_t, Led>& map =
538 mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800539 if (map.find(*code) != map.end()) {
Michael Wright74bdd2e2013-10-17 17:35:53 -0700540 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
541 mapUsage ? "usage" : "scan code", codeToken.string());
542 return BAD_VALUE;
543 }
544
545 mTokenizer->skipDelimiters(WHITESPACE);
546 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800547 std::optional<int> ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
548 if (!ledCode) {
Michael Wright74bdd2e2013-10-17 17:35:53 -0700549 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
550 ledCodeToken.string());
551 return BAD_VALUE;
552 }
553
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700554 ALOGD_IF(DEBUG_PARSER, "Parsed led %s: code=%d, ledCode=%d.", mapUsage ? "usage" : "scan code",
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800555 *code, *ledCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700556
557 Led led;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800558 led.ledCode = *ledCode;
559 map.insert({*code, led});
Michael Wright74bdd2e2013-10-17 17:35:53 -0700560 return NO_ERROR;
561}
Chris Yef59a2f42020-10-16 12:55:26 -0700562
563static std::optional<InputDeviceSensorType> getSensorType(const char* token) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800564 auto it = SENSOR_LIST.find(token);
Chris Yef59a2f42020-10-16 12:55:26 -0700565 if (it == SENSOR_LIST.end()) {
566 return std::nullopt;
567 }
568 return it->second;
569}
570
571static std::optional<int32_t> getSensorDataIndex(String8 token) {
572 std::string tokenStr(token.string());
573 if (tokenStr == "X") {
574 return 0;
575 } else if (tokenStr == "Y") {
576 return 1;
577 } else if (tokenStr == "Z") {
578 return 2;
579 }
580 return std::nullopt;
581}
582
583// Parse sensor type and data index mapping, as below format
584// sensor <raw abs> <sensor type> <sensor data index>
585// raw abs : the linux abs code of the axis
586// sensor type : string name of InputDeviceSensorType
587// sensor data index : the data index of sensor, out of [X, Y, Z]
588// Examples:
589// sensor 0x00 ACCELEROMETER X
590// sensor 0x01 ACCELEROMETER Y
591// sensor 0x02 ACCELEROMETER Z
592// sensor 0x03 GYROSCOPE X
593// sensor 0x04 GYROSCOPE Y
594// sensor 0x05 GYROSCOPE Z
595status_t KeyLayoutMap::Parser::parseSensor() {
596 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800597 std::optional<int> code = parseInt(codeToken.string());
598 if (!code) {
Chris Yef59a2f42020-10-16 12:55:26 -0700599 ALOGE("%s: Expected sensor %s number, got '%s'.", mTokenizer->getLocation().string(),
600 "abs code", codeToken.string());
601 return BAD_VALUE;
602 }
603
604 std::unordered_map<int32_t, Sensor>& map = mMap->mSensorsByAbsCode;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800605 if (map.find(*code) != map.end()) {
Chris Yef59a2f42020-10-16 12:55:26 -0700606 ALOGE("%s: Duplicate entry for sensor %s '%s'.", mTokenizer->getLocation().string(),
607 "abs code", codeToken.string());
608 return BAD_VALUE;
609 }
610
611 mTokenizer->skipDelimiters(WHITESPACE);
612 String8 sensorTypeToken = mTokenizer->nextToken(WHITESPACE);
613 std::optional<InputDeviceSensorType> typeOpt = getSensorType(sensorTypeToken.string());
614 if (!typeOpt) {
615 ALOGE("%s: Expected sensor code label, got '%s'.", mTokenizer->getLocation().string(),
616 sensorTypeToken.string());
617 return BAD_VALUE;
618 }
619 InputDeviceSensorType sensorType = typeOpt.value();
620 mTokenizer->skipDelimiters(WHITESPACE);
621 String8 sensorDataIndexToken = mTokenizer->nextToken(WHITESPACE);
622 std::optional<int32_t> indexOpt = getSensorDataIndex(sensorDataIndexToken);
623 if (!indexOpt) {
624 ALOGE("%s: Expected sensor data index label, got '%s'.", mTokenizer->getLocation().string(),
625 sensorDataIndexToken.string());
626 return BAD_VALUE;
627 }
628 int32_t sensorDataIndex = indexOpt.value();
629
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800630 ALOGD_IF(DEBUG_PARSER, "Parsed sensor: abs code=%d, sensorType=%s, sensorDataIndex=%d.", *code,
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -0700631 ftl::enum_string(sensorType).c_str(), sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700632
633 Sensor sensor;
634 sensor.sensorType = sensorType;
635 sensor.sensorDataIndex = sensorDataIndex;
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800636 map.emplace(*code, sensor);
Chris Yef59a2f42020-10-16 12:55:26 -0700637 return NO_ERROR;
638}
Dominik Laskowski75788452021-02-09 18:51:25 -0800639
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700640// Parse the name of a required kernel config.
641// The layout won't be used if the specified kernel config is not present
642// Examples:
643// requires_kernel_config CONFIG_HID_PLAYSTATION
644status_t KeyLayoutMap::Parser::parseRequiredKernelConfig() {
645 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
646 std::string configName = codeToken.string();
647
648 const auto result = mMap->mRequiredKernelConfigs.emplace(configName);
649 if (!result.second) {
650 ALOGE("%s: Duplicate entry for required kernel config %s.",
651 mTokenizer->getLocation().string(), configName.c_str());
652 return BAD_VALUE;
653 }
654
655 ALOGD_IF(DEBUG_PARSER, "Parsed required kernel config: name=%s", configName.c_str());
656 return NO_ERROR;
657}
658
Dominik Laskowski75788452021-02-09 18:51:25 -0800659} // namespace android