blob: 70d875e394ff11978c1eccb63aec2c9120de3c60 [file] [log] [blame]
Adam Lesinski282e1812014-01-23 18:17:42 -08001/*
2 * Copyright (C) 2010 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#include <input/KeyCharacterMap.h>
18#include <input/KeyLayoutMap.h>
Siarhei Vishniakou13257dd2020-09-02 20:15:40 -070019#include <input/PropertyMap.h>
Adam Lesinski282e1812014-01-23 18:17:42 -080020#include <input/VirtualKeyMap.h>
Adam Lesinski282e1812014-01-23 18:17:42 -080021
Michael Wright71858812017-09-04 15:18:44 +010022#include <stdarg.h>
Adam Lesinski282e1812014-01-23 18:17:42 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27using namespace android;
28
Michael Wright781b1292020-11-04 03:55:48 +000029static const char* PROG_NAME = "validatekeymaps";
Michael Wright71858812017-09-04 15:18:44 +010030static bool gQuiet = false;
Adam Lesinski282e1812014-01-23 18:17:42 -080031
Siarhei Vishniakou8c79b6a2021-09-21 12:04:46 -070032/**
33 * Return true if 'str' contains 'substr', ignoring case.
34 */
35static bool containsSubstringCaseInsensitive(std::string_view str, std::string_view substr) {
36 auto it = std::search(str.begin(), str.end(), substr.begin(), substr.end(),
37 [](char left, char right) {
38 return std::tolower(left) == std::tolower(right);
39 });
40 return it != str.end();
41}
42
Michael Wright781b1292020-11-04 03:55:48 +000043enum class FileType {
44 UNKNOWN,
45 KEY_LAYOUT,
46 KEY_CHARACTER_MAP,
47 VIRTUAL_KEY_DEFINITION,
48 INPUT_DEVICE_CONFIGURATION,
Adam Lesinski282e1812014-01-23 18:17:42 -080049};
50
Michael Wright71858812017-09-04 15:18:44 +010051static void log(const char* fmt, ...) {
52 if (gQuiet) {
53 return;
54 }
55 va_list args;
56 va_start(args, fmt);
57 vfprintf(stdout, fmt, args);
58 va_end(args);
59}
60
61static void error(const char* fmt, ...) {
62 va_list args;
63 va_start(args, fmt);
64 vfprintf(stderr, fmt, args);
65 va_end(args);
66}
Adam Lesinski282e1812014-01-23 18:17:42 -080067
68static void usage() {
Michael Wright71858812017-09-04 15:18:44 +010069 error("Keymap Validation Tool\n\n");
70 error("Usage:\n");
Michael Wright781b1292020-11-04 03:55:48 +000071 error(" %s [-q] [*.kl] [*.kcm] [*.idc] [virtualkeys.*] [...]\n"
72 " Validates the specified key layouts, key character maps, \n"
73 " input device configurations, or virtual key definitions.\n\n"
74 " -q Quiet; do not write anything to standard out.\n",
75 PROG_NAME);
Adam Lesinski282e1812014-01-23 18:17:42 -080076}
77
78static FileType getFileType(const char* filename) {
79 const char *extension = strrchr(filename, '.');
80 if (extension) {
81 if (strcmp(extension, ".kl") == 0) {
Michael Wright781b1292020-11-04 03:55:48 +000082 return FileType::KEY_LAYOUT;
Adam Lesinski282e1812014-01-23 18:17:42 -080083 }
84 if (strcmp(extension, ".kcm") == 0) {
Michael Wright781b1292020-11-04 03:55:48 +000085 return FileType::KEY_CHARACTER_MAP;
Adam Lesinski282e1812014-01-23 18:17:42 -080086 }
87 if (strcmp(extension, ".idc") == 0) {
Michael Wright781b1292020-11-04 03:55:48 +000088 return FileType::INPUT_DEVICE_CONFIGURATION;
Adam Lesinski282e1812014-01-23 18:17:42 -080089 }
90 }
91
92 if (strstr(filename, "virtualkeys.")) {
Michael Wright781b1292020-11-04 03:55:48 +000093 return FileType::VIRTUAL_KEY_DEFINITION;
Adam Lesinski282e1812014-01-23 18:17:42 -080094 }
95
Michael Wright781b1292020-11-04 03:55:48 +000096 return FileType::UNKNOWN;
Adam Lesinski282e1812014-01-23 18:17:42 -080097}
98
Siarhei Vishniakou8c79b6a2021-09-21 12:04:46 -070099/**
100 * Return true if the filename is allowed, false otherwise.
101 */
102static bool validateKeyLayoutFileName(const std::string& filename) {
103 static const std::string kMicrosoftReason =
104 "Microsoft's controllers are designed to work with Generic.kl. Please check with "
105 "Microsoft prior to adding these layouts. See b/194334400";
106 static const std::vector<std::pair<std::string, std::string>> kBannedDevices{
107 std::make_pair("Vendor_0a5c_Product_8502",
108 "This vendorId/productId combination conflicts with 'SnakeByte "
109 "iDroid:con', 'BT23BK keyboard', and other keyboards. Instead, consider "
110 "matching these specific devices by name. See b/36976285, b/191720859"),
111 std::make_pair("Vendor_045e_Product_0b05", kMicrosoftReason),
112 std::make_pair("Vendor_045e_Product_0b20", kMicrosoftReason),
113 std::make_pair("Vendor_045e_Product_0b21", kMicrosoftReason),
114 std::make_pair("Vendor_045e_Product_0b22", kMicrosoftReason),
115 };
116
117 for (const auto& [filenameSubstr, reason] : kBannedDevices) {
118 if (containsSubstringCaseInsensitive(filename, filenameSubstr)) {
119 error("You are trying to add a key layout %s, which matches %s. ", filename.c_str(),
120 filenameSubstr.c_str());
121 error("This would cause some devices to function incorrectly. ");
122 error("%s. ", reason.c_str());
123 return false;
124 }
125 }
126 return true;
127}
128
Adam Lesinski282e1812014-01-23 18:17:42 -0800129static bool validateFile(const char* filename) {
Michael Wright71858812017-09-04 15:18:44 +0100130 log("Validating file '%s'...\n", filename);
Adam Lesinski282e1812014-01-23 18:17:42 -0800131
132 FileType fileType = getFileType(filename);
133 switch (fileType) {
Michael Wright781b1292020-11-04 03:55:48 +0000134 case FileType::UNKNOWN:
135 error("Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800136 return false;
Adam Lesinski282e1812014-01-23 18:17:42 -0800137
Michael Wright781b1292020-11-04 03:55:48 +0000138 case FileType::KEY_LAYOUT: {
Siarhei Vishniakou8c79b6a2021-09-21 12:04:46 -0700139 if (!validateKeyLayoutFileName(filename)) {
140 return false;
141 }
Michael Wright781b1292020-11-04 03:55:48 +0000142 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(filename);
Bernie Innocentifbc9afb2020-12-22 20:10:32 +0900143 if (!ret.ok()) {
Siarhei Vishniakoudf6630a2022-05-18 13:34:55 -0700144 if (ret.error().message() == "Missing kernel config") {
145 // It means the layout is valid, but won't be loaded on this device because
146 // this layout requires a certain kernel config.
147 return true;
148 }
Michael Wright781b1292020-11-04 03:55:48 +0000149 error("Error %s parsing key layout file.\n\n", ret.error().message().c_str());
150 return false;
151 }
152 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800153 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800154
Michael Wright781b1292020-11-04 03:55:48 +0000155 case FileType::KEY_CHARACTER_MAP: {
156 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
157 KeyCharacterMap::load(filename, KeyCharacterMap::Format::ANY);
Bernie Innocentifbc9afb2020-12-22 20:10:32 +0900158 if (!ret.ok()) {
Michael Wright781b1292020-11-04 03:55:48 +0000159 error("Error %s parsing key character map file.\n\n",
160 ret.error().message().c_str());
161 return false;
162 }
163 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800164 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800165
Michael Wright781b1292020-11-04 03:55:48 +0000166 case FileType::INPUT_DEVICE_CONFIGURATION: {
167 android::base::Result<std::unique_ptr<PropertyMap>> propertyMap =
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +0000168 PropertyMap::load(String8(filename).c_str());
Michael Wright781b1292020-11-04 03:55:48 +0000169 if (!propertyMap.ok()) {
Siarhei Vishniakou0caeab92022-11-23 12:49:29 -0800170 error("Error parsing input device configuration file: %s.\n\n",
171 propertyMap.error().message().c_str());
Michael Wright781b1292020-11-04 03:55:48 +0000172 return false;
173 }
174 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800175 }
Michael Wright781b1292020-11-04 03:55:48 +0000176
177 case FileType::VIRTUAL_KEY_DEFINITION: {
178 std::unique_ptr<VirtualKeyMap> map = VirtualKeyMap::load(filename);
179 if (!map) {
180 error("Error while parsing virtual key definition file.\n\n");
181 return false;
182 }
183 break;
184 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800185 }
186
Adam Lesinski282e1812014-01-23 18:17:42 -0800187 return true;
188}
189
190int main(int argc, const char** argv) {
191 if (argc < 2) {
192 usage();
193 return 1;
194 }
195
196 int result = 0;
197 for (int i = 1; i < argc; i++) {
Michael Wright71858812017-09-04 15:18:44 +0100198 if (i == 1 && !strcmp(argv[1], "-q")) {
199 gQuiet = true;
200 continue;
201 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800202 if (!validateFile(argv[i])) {
203 result = 1;
204 }
205 }
206
207 if (result) {
Michael Wright71858812017-09-04 15:18:44 +0100208 error("Failed!\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800209 } else {
Michael Wright71858812017-09-04 15:18:44 +0100210 log("Success.\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800211 }
212 return result;
213}