blob: d6b4579a943f44f342f826f3d39e58c344b83e2f [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
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700233std::optional<AxisInfo> KeyLayoutMap::mapAxis(int32_t scanCode) const {
234 auto it = mAxes.find(scanCode);
235 if (it == mAxes.end()) {
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700236 ALOGD_IF(DEBUG_MAPPING, "mapAxis: scanCode=%d ~ Failed.", scanCode);
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700237 return std::nullopt;
Jeff Brown5912f952013-07-01 19:10:31 -0700238 }
239
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700240 const AxisInfo& axisInfo = it->second;
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700241 ALOGD_IF(DEBUG_MAPPING,
242 "mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
243 "splitValue=%d, flatOverride=%d.",
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700244 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
245 axisInfo.flatOverride);
246 return axisInfo;
Jeff Brown5912f952013-07-01 19:10:31 -0700247}
248
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700249std::optional<int32_t> KeyLayoutMap::findScanCodeForLed(int32_t ledCode) const {
250 for (const auto& [scanCode, led] : mLedsByScanCode) {
251 if (led.ledCode == ledCode) {
252 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, scanCode=%d.", __func__, ledCode, scanCode);
253 return scanCode;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700254 }
255 }
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700256 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700257 return std::nullopt;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700258}
259
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700260std::optional<int32_t> KeyLayoutMap::findUsageCodeForLed(int32_t ledCode) const {
261 for (const auto& [usageCode, led] : mLedsByUsageCode) {
262 if (led.ledCode == ledCode) {
263 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d, usage=%x.", __func__, ledCode, usageCode);
264 return usageCode;
265 }
266 }
267 ALOGD_IF(DEBUG_MAPPING, "%s: ledCode=%d ~ Not found.", __func__, ledCode);
268 return std::nullopt;
269}
Jeff Brown5912f952013-07-01 19:10:31 -0700270
271// --- KeyLayoutMap::Parser ---
272
273KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
274 mMap(map), mTokenizer(tokenizer) {
275}
276
277KeyLayoutMap::Parser::~Parser() {
278}
279
280status_t KeyLayoutMap::Parser::parse() {
281 while (!mTokenizer->isEof()) {
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700282 ALOGD_IF(DEBUG_PARSER, "Parsing %s: '%s'.", mTokenizer->getLocation().string(),
283 mTokenizer->peekRemainderOfLine().string());
Jeff Brown5912f952013-07-01 19:10:31 -0700284
285 mTokenizer->skipDelimiters(WHITESPACE);
286
287 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
288 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
289 if (keywordToken == "key") {
290 mTokenizer->skipDelimiters(WHITESPACE);
291 status_t status = parseKey();
292 if (status) return status;
293 } else if (keywordToken == "axis") {
294 mTokenizer->skipDelimiters(WHITESPACE);
295 status_t status = parseAxis();
296 if (status) return status;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700297 } else if (keywordToken == "led") {
298 mTokenizer->skipDelimiters(WHITESPACE);
299 status_t status = parseLed();
300 if (status) return status;
Chris Yef59a2f42020-10-16 12:55:26 -0700301 } else if (keywordToken == "sensor") {
302 mTokenizer->skipDelimiters(WHITESPACE);
303 status_t status = parseSensor();
304 if (status) return status;
Siarhei Vishniakoud945d3e2022-05-18 09:42:52 -0700305 } else if (keywordToken == "requires_kernel_config") {
306 mTokenizer->skipDelimiters(WHITESPACE);
307 status_t status = parseRequiredKernelConfig();
308 if (status) return status;
Jeff Brown5912f952013-07-01 19:10:31 -0700309 } else {
310 ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
311 keywordToken.string());
312 return BAD_VALUE;
313 }
314
315 mTokenizer->skipDelimiters(WHITESPACE);
316 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
317 ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
318 mTokenizer->getLocation().string(),
319 mTokenizer->peekRemainderOfLine().string());
320 return BAD_VALUE;
321 }
322 }
323
324 mTokenizer->nextLine();
325 }
326 return NO_ERROR;
327}
328
329status_t KeyLayoutMap::Parser::parseKey() {
330 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
331 bool mapUsage = false;
332 if (codeToken == "usage") {
333 mapUsage = true;
334 mTokenizer->skipDelimiters(WHITESPACE);
335 codeToken = mTokenizer->nextToken(WHITESPACE);
336 }
337
338 char* end;
339 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
340 if (*end) {
341 ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
342 mapUsage ? "usage" : "scan code", codeToken.string());
343 return BAD_VALUE;
344 }
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700345 std::unordered_map<int32_t, Key>& map =
346 mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
347 if (map.find(code) != map.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700348 ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
349 mapUsage ? "usage" : "scan code", codeToken.string());
350 return BAD_VALUE;
351 }
352
353 mTokenizer->skipDelimiters(WHITESPACE);
354 String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700355 int32_t keyCode = InputEventLookup::getKeyCodeByLabel(keyCodeToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700356 if (!keyCode) {
357 ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
358 keyCodeToken.string());
359 return BAD_VALUE;
360 }
361
362 uint32_t flags = 0;
363 for (;;) {
364 mTokenizer->skipDelimiters(WHITESPACE);
365 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
366
367 String8 flagToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700368 uint32_t flag = InputEventLookup::getKeyFlagByLabel(flagToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700369 if (!flag) {
370 ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
371 flagToken.string());
372 return BAD_VALUE;
373 }
374 if (flags & flag) {
375 ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
376 flagToken.string());
377 return BAD_VALUE;
378 }
379 flags |= flag;
380 }
381
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700382 ALOGD_IF(DEBUG_PARSER, "Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
383 mapUsage ? "usage" : "scan code", code, keyCode, flags);
384
Jeff Brown5912f952013-07-01 19:10:31 -0700385 Key key;
386 key.keyCode = keyCode;
387 key.flags = flags;
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700388 map.insert({code, key});
Jeff Brown5912f952013-07-01 19:10:31 -0700389 return NO_ERROR;
390}
391
392status_t KeyLayoutMap::Parser::parseAxis() {
393 String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
394 char* end;
395 int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
396 if (*end) {
397 ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
398 scanCodeToken.string());
399 return BAD_VALUE;
400 }
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700401 if (mMap->mAxes.find(scanCode) != mMap->mAxes.end()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700402 ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
403 scanCodeToken.string());
404 return BAD_VALUE;
405 }
406
407 AxisInfo axisInfo;
408
409 mTokenizer->skipDelimiters(WHITESPACE);
410 String8 token = mTokenizer->nextToken(WHITESPACE);
411 if (token == "invert") {
412 axisInfo.mode = AxisInfo::MODE_INVERT;
413
414 mTokenizer->skipDelimiters(WHITESPACE);
415 String8 axisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700416 axisInfo.axis = InputEventLookup::getAxisByLabel(axisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700417 if (axisInfo.axis < 0) {
418 ALOGE("%s: Expected inverted axis label, got '%s'.",
419 mTokenizer->getLocation().string(), axisToken.string());
420 return BAD_VALUE;
421 }
422 } else if (token == "split") {
423 axisInfo.mode = AxisInfo::MODE_SPLIT;
424
425 mTokenizer->skipDelimiters(WHITESPACE);
426 String8 splitToken = mTokenizer->nextToken(WHITESPACE);
427 axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
428 if (*end) {
429 ALOGE("%s: Expected split value, got '%s'.",
430 mTokenizer->getLocation().string(), splitToken.string());
431 return BAD_VALUE;
432 }
433
434 mTokenizer->skipDelimiters(WHITESPACE);
435 String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700436 axisInfo.axis = InputEventLookup::getAxisByLabel(lowAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700437 if (axisInfo.axis < 0) {
438 ALOGE("%s: Expected low axis label, got '%s'.",
439 mTokenizer->getLocation().string(), lowAxisToken.string());
440 return BAD_VALUE;
441 }
442
443 mTokenizer->skipDelimiters(WHITESPACE);
444 String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700445 axisInfo.highAxis = InputEventLookup::getAxisByLabel(highAxisToken.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700446 if (axisInfo.highAxis < 0) {
447 ALOGE("%s: Expected high axis label, got '%s'.",
448 mTokenizer->getLocation().string(), highAxisToken.string());
449 return BAD_VALUE;
450 }
451 } else {
Chris Ye4958d062020-08-20 13:21:10 -0700452 axisInfo.axis = InputEventLookup::getAxisByLabel(token.string());
Jeff Brown5912f952013-07-01 19:10:31 -0700453 if (axisInfo.axis < 0) {
454 ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
455 mTokenizer->getLocation().string(), token.string());
456 return BAD_VALUE;
457 }
458 }
459
460 for (;;) {
461 mTokenizer->skipDelimiters(WHITESPACE);
462 if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
463 break;
464 }
465 String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
466 if (keywordToken == "flat") {
467 mTokenizer->skipDelimiters(WHITESPACE);
468 String8 flatToken = mTokenizer->nextToken(WHITESPACE);
469 axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
470 if (*end) {
471 ALOGE("%s: Expected flat value, got '%s'.",
472 mTokenizer->getLocation().string(), flatToken.string());
473 return BAD_VALUE;
474 }
475 } else {
476 ALOGE("%s: Expected keyword 'flat', got '%s'.",
477 mTokenizer->getLocation().string(), keywordToken.string());
478 return BAD_VALUE;
479 }
480 }
481
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700482 ALOGD_IF(DEBUG_PARSER,
483 "Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
484 "splitValue=%d, flatOverride=%d.",
485 scanCode, axisInfo.mode, axisInfo.axis, axisInfo.highAxis, axisInfo.splitValue,
486 axisInfo.flatOverride);
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700487 mMap->mAxes.insert({scanCode, axisInfo});
Jeff Brown5912f952013-07-01 19:10:31 -0700488 return NO_ERROR;
489}
490
Michael Wright74bdd2e2013-10-17 17:35:53 -0700491status_t KeyLayoutMap::Parser::parseLed() {
492 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
493 bool mapUsage = false;
494 if (codeToken == "usage") {
495 mapUsage = true;
496 mTokenizer->skipDelimiters(WHITESPACE);
497 codeToken = mTokenizer->nextToken(WHITESPACE);
498 }
499 char* end;
500 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
501 if (*end) {
502 ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
503 mapUsage ? "usage" : "scan code", codeToken.string());
504 return BAD_VALUE;
505 }
506
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700507 std::unordered_map<int32_t, Led>& map =
508 mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
509 if (map.find(code) != map.end()) {
Michael Wright74bdd2e2013-10-17 17:35:53 -0700510 ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
511 mapUsage ? "usage" : "scan code", codeToken.string());
512 return BAD_VALUE;
513 }
514
515 mTokenizer->skipDelimiters(WHITESPACE);
516 String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
Chris Ye4958d062020-08-20 13:21:10 -0700517 int32_t ledCode = InputEventLookup::getLedByLabel(ledCodeToken.string());
Michael Wright74bdd2e2013-10-17 17:35:53 -0700518 if (ledCode < 0) {
519 ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
520 ledCodeToken.string());
521 return BAD_VALUE;
522 }
523
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700524 ALOGD_IF(DEBUG_PARSER, "Parsed led %s: code=%d, ledCode=%d.", mapUsage ? "usage" : "scan code",
525 code, ledCode);
Michael Wright74bdd2e2013-10-17 17:35:53 -0700526
527 Led led;
528 led.ledCode = ledCode;
Siarhei Vishniakou53f0a5e2022-05-18 09:45:54 -0700529 map.insert({code, led});
Michael Wright74bdd2e2013-10-17 17:35:53 -0700530 return NO_ERROR;
531}
Chris Yef59a2f42020-10-16 12:55:26 -0700532
533static std::optional<InputDeviceSensorType> getSensorType(const char* token) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800534 auto it = SENSOR_LIST.find(token);
Chris Yef59a2f42020-10-16 12:55:26 -0700535 if (it == SENSOR_LIST.end()) {
536 return std::nullopt;
537 }
538 return it->second;
539}
540
541static std::optional<int32_t> getSensorDataIndex(String8 token) {
542 std::string tokenStr(token.string());
543 if (tokenStr == "X") {
544 return 0;
545 } else if (tokenStr == "Y") {
546 return 1;
547 } else if (tokenStr == "Z") {
548 return 2;
549 }
550 return std::nullopt;
551}
552
553// Parse sensor type and data index mapping, as below format
554// sensor <raw abs> <sensor type> <sensor data index>
555// raw abs : the linux abs code of the axis
556// sensor type : string name of InputDeviceSensorType
557// sensor data index : the data index of sensor, out of [X, Y, Z]
558// Examples:
559// sensor 0x00 ACCELEROMETER X
560// sensor 0x01 ACCELEROMETER Y
561// sensor 0x02 ACCELEROMETER Z
562// sensor 0x03 GYROSCOPE X
563// sensor 0x04 GYROSCOPE Y
564// sensor 0x05 GYROSCOPE Z
565status_t KeyLayoutMap::Parser::parseSensor() {
566 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
567 char* end;
568 int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
569 if (*end) {
570 ALOGE("%s: Expected sensor %s number, got '%s'.", mTokenizer->getLocation().string(),
571 "abs code", codeToken.string());
572 return BAD_VALUE;
573 }
574
575 std::unordered_map<int32_t, Sensor>& map = mMap->mSensorsByAbsCode;
576 if (map.find(code) != map.end()) {
577 ALOGE("%s: Duplicate entry for sensor %s '%s'.", mTokenizer->getLocation().string(),
578 "abs code", codeToken.string());
579 return BAD_VALUE;
580 }
581
582 mTokenizer->skipDelimiters(WHITESPACE);
583 String8 sensorTypeToken = mTokenizer->nextToken(WHITESPACE);
584 std::optional<InputDeviceSensorType> typeOpt = getSensorType(sensorTypeToken.string());
585 if (!typeOpt) {
586 ALOGE("%s: Expected sensor code label, got '%s'.", mTokenizer->getLocation().string(),
587 sensorTypeToken.string());
588 return BAD_VALUE;
589 }
590 InputDeviceSensorType sensorType = typeOpt.value();
591 mTokenizer->skipDelimiters(WHITESPACE);
592 String8 sensorDataIndexToken = mTokenizer->nextToken(WHITESPACE);
593 std::optional<int32_t> indexOpt = getSensorDataIndex(sensorDataIndexToken);
594 if (!indexOpt) {
595 ALOGE("%s: Expected sensor data index label, got '%s'.", mTokenizer->getLocation().string(),
596 sensorDataIndexToken.string());
597 return BAD_VALUE;
598 }
599 int32_t sensorDataIndex = indexOpt.value();
600
Siarhei Vishniakouac7f2e72022-05-18 12:30:16 -0700601 ALOGD_IF(DEBUG_PARSER, "Parsed sensor: abs code=%d, sensorType=%s, sensorDataIndex=%d.", code,
602 ftl::enum_string(sensorType).c_str(), sensorDataIndex);
Chris Yef59a2f42020-10-16 12:55:26 -0700603
604 Sensor sensor;
605 sensor.sensorType = sensorType;
606 sensor.sensorDataIndex = sensorDataIndex;
607 map.emplace(code, sensor);
608 return NO_ERROR;
609}
Dominik Laskowski75788452021-02-09 18:51:25 -0800610
Siarhei Vishniakoud945d3e2022-05-18 09:42:52 -0700611// Parse the name of a required kernel config.
612// The layout won't be used if the specified kernel config is not present
613// Examples:
614// requires_kernel_config CONFIG_HID_PLAYSTATION
615status_t KeyLayoutMap::Parser::parseRequiredKernelConfig() {
616 String8 codeToken = mTokenizer->nextToken(WHITESPACE);
617 std::string configName = codeToken.string();
618
619 const auto result = mMap->mRequiredKernelConfigs.emplace(configName);
620 if (!result.second) {
621 ALOGE("%s: Duplicate entry for required kernel config %s.",
622 mTokenizer->getLocation().string(), configName.c_str());
623 return BAD_VALUE;
624 }
625
626 ALOGD_IF(DEBUG_PARSER, "Parsed required kernel config: name=%s", configName.c_str());
627 return NO_ERROR;
628}
629
Dominik Laskowski75788452021-02-09 18:51:25 -0800630} // namespace android