blob: 817effd24a2dbefd7fc99f35f5352f29520a9c18 [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()) {
Michael Wright781b1292020-11-04 03:55:48 +0000144 error("Error %s parsing key layout file.\n\n", ret.error().message().c_str());
145 return false;
146 }
147 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800148 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800149
Michael Wright781b1292020-11-04 03:55:48 +0000150 case FileType::KEY_CHARACTER_MAP: {
151 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
152 KeyCharacterMap::load(filename, KeyCharacterMap::Format::ANY);
Bernie Innocentifbc9afb2020-12-22 20:10:32 +0900153 if (!ret.ok()) {
Michael Wright781b1292020-11-04 03:55:48 +0000154 error("Error %s parsing key character map file.\n\n",
155 ret.error().message().c_str());
156 return false;
157 }
158 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800159 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800160
Michael Wright781b1292020-11-04 03:55:48 +0000161 case FileType::INPUT_DEVICE_CONFIGURATION: {
162 android::base::Result<std::unique_ptr<PropertyMap>> propertyMap =
163 PropertyMap::load(String8(filename));
164 if (!propertyMap.ok()) {
165 error("Error %d parsing input device configuration file.\n\n",
166 propertyMap.error().code());
167 return false;
168 }
169 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800170 }
Michael Wright781b1292020-11-04 03:55:48 +0000171
172 case FileType::VIRTUAL_KEY_DEFINITION: {
173 std::unique_ptr<VirtualKeyMap> map = VirtualKeyMap::load(filename);
174 if (!map) {
175 error("Error while parsing virtual key definition file.\n\n");
176 return false;
177 }
178 break;
179 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800180 }
181
Adam Lesinski282e1812014-01-23 18:17:42 -0800182 return true;
183}
184
185int main(int argc, const char** argv) {
186 if (argc < 2) {
187 usage();
188 return 1;
189 }
190
191 int result = 0;
192 for (int i = 1; i < argc; i++) {
Michael Wright71858812017-09-04 15:18:44 +0100193 if (i == 1 && !strcmp(argv[1], "-q")) {
194 gQuiet = true;
195 continue;
196 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800197 if (!validateFile(argv[i])) {
198 result = 1;
199 }
200 }
201
202 if (result) {
Michael Wright71858812017-09-04 15:18:44 +0100203 error("Failed!\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800204 } else {
Michael Wright71858812017-09-04 15:18:44 +0100205 log("Success.\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800206 }
207 return result;
208}