blob: 250c0dd9a96cf3e3237ca8acef2b83ddd6f39cc9 [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 Vishniakouac7f2e72022-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 Vishniakou36a6aa72022-08-17 16:07:40 -070028#if defined(__ANDROID__)
Siarhei Vishniakoud945d3e2022-05-18 09:42:52 -070029#include <vintf/RuntimeInfo.h>
30#include <vintf/VintfObject.h>
Siarhei Vishniakou36a6aa72022-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 Vishniakouac7f2e72022-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 Vishniakouac7f2e72022-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 Vishniakoud945d3e2022-05-18 09:42:52 -070083bool kernelConfigsArePresent(const std::set<std::string>& configs) {
Siarhei Vishniakou36a6aa72022-08-17 16:07:40 -070084#if defined(__ANDROID__)
Siarhei Vishniakoud945d3e2022-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 Vishniakou36a6aa72022-08-17 16:07:40 -0700105#else
106 (void)configs; // Suppress 'unused variable' warning
107 return true;
108#endif
Siarhei Vishniakoud945d3e2022-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 Vishniakoud945d3e2022-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 Vishniakoud945d3e2022-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 Vishniakoud945d3e2022-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 Vishniakoud945d3e2022-05-18 09:42:52 -0700136 if (!ret.ok()) {
137 return ret;
Chris Ye1abffbd2020-08-18 12:50:12 -0700138 }
Siarhei Vishniakoud945d3e2022-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 Vishniakouac7f2e72022-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 Vishniakouac7f2e72022-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
195base::Result<std::pair<InputDeviceSensorType, int32_t>> KeyLayoutMap::mapSensor(int32_t absCode) {
196 auto it = mSensorsByAbsCode.find(absCode);
197 if (it == mSensorsByAbsCode.end()) {
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700198 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, ~ Failed.", absCode);
Chris Yef59a2f42020-10-16 12:55:26 -0700199 return Errorf("Can't find abs code {}.", absCode);
200 }
201 const Sensor& sensor = it->second;
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700202 ALOGD_IF(DEBUG_MAPPING, "mapSensor: absCode=%d, sensorType=%s, sensorDataIndex=0x%x.", absCode,
203 ftl::enum_string(sensor.sensorType).c_str(), sensor.sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700204 return std::make_pair(sensor.sensorType, sensor.sensorDataIndex);
205}
206
Jeff Brown5912f952013-07-01 19:10:31 -0700207const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
208 if (usageCode) {
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700209 auto it = mKeysByUsageCode.find(usageCode);
210 if (it != mKeysByUsageCode.end()) {
211 return &it->second;
Jeff Brown5912f952013-07-01 19:10:31 -0700212 }
213 }
214 if (scanCode) {
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700215 auto it = mKeysByScanCode.find(scanCode);
216 if (it != mKeysByScanCode.end()) {
217 return &it->second;
Jeff Brown5912f952013-07-01 19:10:31 -0700218 }
219 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700220 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700221}
222
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700223std::vector<int32_t> KeyLayoutMap::findScanCodesForKey(int32_t keyCode) const {
224 std::vector<int32_t> scanCodes;
225 for (const auto& [scanCode, key] : mKeysByScanCode) {
226 if (keyCode == key.keyCode) {
227 scanCodes.push_back(scanCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700228 }
229 }
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700230 return scanCodes;
Jeff Brown5912f952013-07-01 19:10:31 -0700231}
232
Charles Linaadf8d52023-03-30 13:34:54 +0800233std::vector<int32_t> KeyLayoutMap::findUsageCodesForKey(int32_t keyCode) const {
234 std::vector<int32_t> usageCodes;
235 for (const auto& [usageCode, key] : mKeysByUsageCode) {
236 if (keyCode == key.keyCode) {
237 usageCodes.push_back(usageCode);
238 }
239 }
240 return usageCodes;
241}
242
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700243std::optional<AxisInfo> KeyLayoutMap::mapAxis(int32_t scanCode) const {
244 auto it = mAxes.find(scanCode);
245 if (it == mAxes.end()) {
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700246 ALOGD_IF(DEBUG_MAPPING, "mapAxis: scanCode=%d ~ Failed.", scanCode);
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700247 return std::nullopt;
Jeff Brown5912f952013-07-01 19:10:31 -0700248 }
249
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700250 const AxisInfo& axisInfo = it->second;
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700251 ALOGD_IF(DEBUG_MAPPING,
252 "mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
253 "splitValue=%d, flatOverride=%d.",
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700254 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
255 axisInfo.flatOverride);
256 return axisInfo;
Jeff Brown5912f952013-07-01 19:10:31 -0700257}
258
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700259std::optional<int32_t> KeyLayoutMap::findScanCodeForLed(int32_t ledCode) const {
260 for (const auto& [scanCode, led] : mLedsByScanCode) {
261 if (led.ledCode == ledCode) {
262 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, scanCode=%d.", __func__, ledCode, scanCode);
263 return scanCode;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700264 }
265 }
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700266 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700267 return std::nullopt;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700268}
269
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700270std::optional<int32_t> KeyLayoutMap::findUsageCodeForLed(int32_t ledCode) const {
271 for (const auto& [usageCode, led] : mLedsByUsageCode) {
272 if (led.ledCode == ledCode) {
273 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, usage=%x.", __func__, ledCode, usageCode);
274 return usageCode;
275 }
276 }
277 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
278 return std::nullopt;
279}
Jeff Brown5912f952013-07-01 19:10:31 -0700280
281// --- KeyLayoutMap::Parser ---
282
283KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
284 mMap(map), mTokenizer(tokenizer) {
285}
286
287KeyLayoutMap::Parser::~Parser() {
288}
289
290status_t KeyLayoutMap::Parser::parse() {
291 while (!mTokenizer->isEof()) {
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700292 ALOGD_IF(DEBUG_PARSER, "Parsing %s: '%s'.", mTokenizer->getLocation().string(),
293 mTokenizer->peekRemainderOfLine().string());
Jeff Brown5912f952013-07-01 19:10:31 -0700294
295 mTokenizer->skipDelimiters(WHITESPACE);
296
297 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
298 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
299 if (keywordToken == "key") {
300 mTokenizer->skipDelimiters(WHITESPACE);
301 status_t status = parseKey();
302 if (status) return status;
303 } else if (keywordToken == "axis") {
304 mTokenizer->skipDelimiters(WHITESPACE);
305 status_t status = parseAxis();
306 if (status) return status;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700307 } else if (keywordToken == "led") {
308 mTokenizer->skipDelimiters(WHITESPACE);
309 status_t status = parseLed();
310 if (status) return status;
Chris Yef59a2f42020-10-16 12:55:26 -0700311 } else if (keywordToken == "sensor") {
312 mTokenizer->skipDelimiters(WHITESPACE);
313 status_t status = parseSensor();
314 if (status) return status;
Siarhei Vishniakoud945d3e2022-05-18 09:42:52 -0700315 } else if (keywordToken == "requires_kernel_config") {
316 mTokenizer->skipDelimiters(WHITESPACE);
317 status_t status = parseRequiredKernelConfig();
318 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700319 } else {
320 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
321 keywordToken.string());
322 return BAD_VALUE;
323 }
324
325 mTokenizer->skipDelimiters(WHITESPACE);
326 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
327 ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
328 mTokenizer->getLocation().string(),
329 mTokenizer->peekRemainderOfLine().string());
330 return BAD_VALUE;
331 }
332 }
333
334 mTokenizer->nextLine();
335 }
336 return NO_ERROR;
337}
338
339status_t KeyLayoutMap::Parser::parseKey() {
340 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
341 bool mapUsage = false;
342 if (codeToken == "usage") {
343 mapUsage = true;
344 mTokenizer->skipDelimiters(WHITESPACE);
345 codeToken = mTokenizer->nextToken(WHITESPACE);
346 }
347
348 char* end;
349 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
350 if (*end) {
351 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
352 mapUsage ? "usage" : "scan code", codeToken.string());
353 return BAD_VALUE;
354 }
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700355 std::unordered_map<int32_t, Key>& map =
356 mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
357 if (map.find(code) != map.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700358 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
359 mapUsage ? "usage" : "scan code", codeToken.string());
360 return BAD_VALUE;
361 }
362
363 mTokenizer->skipDelimiters(WHITESPACE);
364 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700365 int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700366 if (!keyCode) {
367 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
368 keyCodeToken.string());
369 return BAD_VALUE;
370 }
371
372 uint32_t flags = 0;
373 for (;;) {
374 mTokenizer->skipDelimiters(WHITESPACE);
375 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
376
377 String8 flagToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700378 uint32_t flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700379 if (!flag) {
380 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
381 flagToken.string());
382 return BAD_VALUE;
383 }
384 if (flags & flag) {
385 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
386 flagToken.string());
387 return BAD_VALUE;
388 }
389 flags |= flag;
390 }
391
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700392 ALOGD_IF(DEBUG_PARSER, "Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
393 mapUsage ? "usage" : "scan code", code, keyCode, flags);
394
Jeff Brown5912f952013-07-01 19:10:31 -0700395 Key key;
396 key.keyCode = keyCode;
397 key.flags = flags;
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700398 map.insert({code, key});
Jeff Brown5912f952013-07-01 19:10:31 -0700399 return NO_ERROR;
400}
401
402status_t KeyLayoutMap::Parser::parseAxis() {
403 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
404 char* end;
405 int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
406 if (*end) {
407 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
408 scanCodeToken.string());
409 return BAD_VALUE;
410 }
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700411 if (mMap->mAxes.find(scanCode) != mMap->mAxes.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700412 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
413 scanCodeToken.string());
414 return BAD_VALUE;
415 }
416
417 AxisInfo axisInfo;
418
419 mTokenizer->skipDelimiters(WHITESPACE);
420 String8 token = mTokenizer->nextToken(WHITESPACE);
421 if (token == "invert") {
422 axisInfo.mode = AxisInfo::MODE_INVERT;
423
424 mTokenizer->skipDelimiters(WHITESPACE);
425 String8 axisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700426 axisInfo.axis = InputEventLookup::getAxisByLabel(axisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700427 if (axisInfo.axis < 0) {
428 ALOGE("%s: Expected inverted axis label, got '%s'.",
429 mTokenizer->getLocation().string(), axisToken.string());
430 return BAD_VALUE;
431 }
432 } else if (token == "split") {
433 axisInfo.mode = AxisInfo::MODE_SPLIT;
434
435 mTokenizer->skipDelimiters(WHITESPACE);
436 String8 splitToken = mTokenizer->nextToken(WHITESPACE);
437 axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
438 if (*end) {
439 ALOGE("%s: Expected split value, got '%s'.",
440 mTokenizer->getLocation().string(), splitToken.string());
441 return BAD_VALUE;
442 }
443
444 mTokenizer->skipDelimiters(WHITESPACE);
445 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700446 axisInfo.axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700447 if (axisInfo.axis < 0) {
448 ALOGE("%s: Expected low axis label, got '%s'.",
449 mTokenizer->getLocation().string(), lowAxisToken.string());
450 return BAD_VALUE;
451 }
452
453 mTokenizer->skipDelimiters(WHITESPACE);
454 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700455 axisInfo.highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700456 if (axisInfo.highAxis < 0) {
457 ALOGE("%s: Expected high axis label, got '%s'.",
458 mTokenizer->getLocation().string(), highAxisToken.string());
459 return BAD_VALUE;
460 }
461 } else {
Chris Ye4958d062020-08-20 13:21:10 -0700462 axisInfo.axis = InputEventLookup::getAxisByLabel(token.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700463 if (axisInfo.axis < 0) {
464 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
465 mTokenizer->getLocation().string(), token.string());
466 return BAD_VALUE;
467 }
468 }
469
470 for (;;) {
471 mTokenizer->skipDelimiters(WHITESPACE);
472 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
473 break;
474 }
475 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
476 if (keywordToken == "flat") {
477 mTokenizer->skipDelimiters(WHITESPACE);
478 String8 flatToken = mTokenizer->nextToken(WHITESPACE);
479 axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
480 if (*end) {
481 ALOGE("%s: Expected flat value, got '%s'.",
482 mTokenizer->getLocation().string(), flatToken.string());
483 return BAD_VALUE;
484 }
485 } else {
486 ALOGE("%s: Expected keyword 'flat', got '%s'.",
487 mTokenizer->getLocation().string(), keywordToken.string());
488 return BAD_VALUE;
489 }
490 }
491
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700492 ALOGD_IF(DEBUG_PARSER,
493 "Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
494 "splitValue=%d, flatOverride=%d.",
495 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
496 axisInfo.flatOverride);
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700497 mMap->mAxes.insert({scanCode, axisInfo});
Jeff Brown5912f952013-07-01 19:10:31 -0700498 return NO_ERROR;
499}
500
Michael Wright74bdd2e2013-10-17 17:35:53 -0700501status_t KeyLayoutMap::Parser::parseLed() {
502 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
503 bool mapUsage = false;
504 if (codeToken == "usage") {
505 mapUsage = true;
506 mTokenizer->skipDelimiters(WHITESPACE);
507 codeToken = mTokenizer->nextToken(WHITESPACE);
508 }
509 char* end;
510 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
511 if (*end) {
512 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
513 mapUsage ? "usage" : "scan code", codeToken.string());
514 return BAD_VALUE;
515 }
516
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700517 std::unordered_map<int32_t, Led>& map =
518 mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
519 if (map.find(code) != map.end()) {
Michael Wright74bdd2e2013-10-17 17:35:53 -0700520 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
521 mapUsage ? "usage" : "scan code", codeToken.string());
522 return BAD_VALUE;
523 }
524
525 mTokenizer->skipDelimiters(WHITESPACE);
526 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700527 int32_t ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
Michael Wright74bdd2e2013-10-17 17:35:53 -0700528 if (ledCode < 0) {
529 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
530 ledCodeToken.string());
531 return BAD_VALUE;
532 }
533
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700534 ALOGD_IF(DEBUG_PARSER, "Parsed led %s: code=%d, ledCode=%d.", mapUsage ? "usage" : "scan code",
535 code, ledCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700536
537 Led led;
538 led.ledCode = ledCode;
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700539 map.insert({code, led});
Michael Wright74bdd2e2013-10-17 17:35:53 -0700540 return NO_ERROR;
541}
Chris Yef59a2f42020-10-16 12:55:26 -0700542
543static std::optional<InputDeviceSensorType> getSensorType(const char* token) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800544 auto it = SENSOR_LIST.find(token);
Chris Yef59a2f42020-10-16 12:55:26 -0700545 if (it == SENSOR_LIST.end()) {
546 return std::nullopt;
547 }
548 return it->second;
549}
550
551static std::optional<int32_t> getSensorDataIndex(String8 token) {
552 std::string tokenStr(token.string());
553 if (tokenStr == "X") {
554 return 0;
555 } else if (tokenStr == "Y") {
556 return 1;
557 } else if (tokenStr == "Z") {
558 return 2;
559 }
560 return std::nullopt;
561}
562
563// Parse sensor type and data index mapping, as below format
564// sensor <raw abs> <sensor type> <sensor data index>
565// raw abs : the linux abs code of the axis
566// sensor type : string name of InputDeviceSensorType
567// sensor data index : the data index of sensor, out of [X, Y, Z]
568// Examples:
569// sensor 0x00 ACCELEROMETER X
570// sensor 0x01 ACCELEROMETER Y
571// sensor 0x02 ACCELEROMETER Z
572// sensor 0x03 GYROSCOPE X
573// sensor 0x04 GYROSCOPE Y
574// sensor 0x05 GYROSCOPE Z
575status_t KeyLayoutMap::Parser::parseSensor() {
576 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
577 char* end;
578 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
579 if (*end) {
580 ALOGE("%s: Expected sensor %s number, got '%s'.", mTokenizer->getLocation().string(),
581 "abs code", codeToken.string());
582 return BAD_VALUE;
583 }
584
585 std::unordered_map<int32_t, Sensor>& map = mMap->mSensorsByAbsCode;
586 if (map.find(code) != map.end()) {
587 ALOGE("%s: Duplicate entry for sensor %s '%s'.", mTokenizer->getLocation().string(),
588 "abs code", codeToken.string());
589 return BAD_VALUE;
590 }
591
592 mTokenizer->skipDelimiters(WHITESPACE);
593 String8 sensorTypeToken = mTokenizer->nextToken(WHITESPACE);
594 std::optional<InputDeviceSensorType> typeOpt = getSensorType(sensorTypeToken.string());
595 if (!typeOpt) {
596 ALOGE("%s: Expected sensor code label, got '%s'.", mTokenizer->getLocation().string(),
597 sensorTypeToken.string());
598 return BAD_VALUE;
599 }
600 InputDeviceSensorType sensorType = typeOpt.value();
601 mTokenizer->skipDelimiters(WHITESPACE);
602 String8 sensorDataIndexToken = mTokenizer->nextToken(WHITESPACE);
603 std::optional<int32_t> indexOpt = getSensorDataIndex(sensorDataIndexToken);
604 if (!indexOpt) {
605 ALOGE("%s: Expected sensor data index label, got '%s'.", mTokenizer->getLocation().string(),
606 sensorDataIndexToken.string());
607 return BAD_VALUE;
608 }
609 int32_t sensorDataIndex = indexOpt.value();
610
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700611 ALOGD_IF(DEBUG_PARSER, "Parsed sensor: abs code=%d, sensorType=%s, sensorDataIndex=%d.", code,
612 ftl::enum_string(sensorType).c_str(), sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700613
614 Sensor sensor;
615 sensor.sensorType = sensorType;
616 sensor.sensorDataIndex = sensorDataIndex;
617 map.emplace(code, sensor);
618 return NO_ERROR;
619}
Dominik Laskowski75788452021-02-09 18:51:25 -0800620
Siarhei Vishniakoud945d3e2022-05-18 09:42:52 -0700621// Parse the name of a required kernel config.
622// The layout won't be used if the specified kernel config is not present
623// Examples:
624// requires_kernel_config CONFIG_HID_PLAYSTATION
625status_t KeyLayoutMap::Parser::parseRequiredKernelConfig() {
626 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
627 std::string configName = codeToken.string();
628
629 const auto result = mMap->mRequiredKernelConfigs.emplace(configName);
630 if (!result.second) {
631 ALOGE("%s: Duplicate entry for required kernel config %s.",
632 mTokenizer->getLocation().string(), configName.c_str());
633 return BAD_VALUE;
634 }
635
636 ALOGD_IF(DEBUG_PARSER, "Parsed required kernel config: name=%s", configName.c_str());
637 return NO_ERROR;
638}
639
Dominik Laskowski75788452021-02-09 18:51:25 -0800640} // namespace android