Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | 13257dd | 2020-09-02 20:15:40 -0700 | [diff] [blame] | 19 | #include <input/PropertyMap.h> |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 20 | #include <input/VirtualKeyMap.h> |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 21 | |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 22 | #include <stdarg.h> |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 29 | static const char* PROG_NAME = "validatekeymaps"; |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 30 | static bool gQuiet = false; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 31 | |
Siarhei Vishniakou | 8c79b6a | 2021-09-21 12:04:46 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Return true if 'str' contains 'substr', ignoring case. |
| 34 | */ |
| 35 | static 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 Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 43 | enum class FileType { |
| 44 | UNKNOWN, |
| 45 | KEY_LAYOUT, |
| 46 | KEY_CHARACTER_MAP, |
| 47 | VIRTUAL_KEY_DEFINITION, |
| 48 | INPUT_DEVICE_CONFIGURATION, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 51 | static 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 | |
| 61 | static 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 Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 67 | |
| 68 | static void usage() { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 69 | error("Keymap Validation Tool\n\n"); |
| 70 | error("Usage:\n"); |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 71 | 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 Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static FileType getFileType(const char* filename) { |
| 79 | const char *extension = strrchr(filename, '.'); |
| 80 | if (extension) { |
| 81 | if (strcmp(extension, ".kl") == 0) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 82 | return FileType::KEY_LAYOUT; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 83 | } |
| 84 | if (strcmp(extension, ".kcm") == 0) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 85 | return FileType::KEY_CHARACTER_MAP; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 86 | } |
| 87 | if (strcmp(extension, ".idc") == 0) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 88 | return FileType::INPUT_DEVICE_CONFIGURATION; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | if (strstr(filename, "virtualkeys.")) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 93 | return FileType::VIRTUAL_KEY_DEFINITION; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 96 | return FileType::UNKNOWN; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Siarhei Vishniakou | 8c79b6a | 2021-09-21 12:04:46 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Return true if the filename is allowed, false otherwise. |
| 101 | */ |
| 102 | static 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 Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 129 | static bool validateFile(const char* filename) { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 130 | log("Validating file '%s'...\n", filename); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 131 | |
| 132 | FileType fileType = getFileType(filename); |
| 133 | switch (fileType) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 134 | case FileType::UNKNOWN: |
| 135 | error("Supported file types: *.kl, *.kcm, virtualkeys.*\n\n"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 136 | return false; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 137 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 138 | case FileType::KEY_LAYOUT: { |
Siarhei Vishniakou | 8c79b6a | 2021-09-21 12:04:46 -0700 | [diff] [blame] | 139 | if (!validateKeyLayoutFileName(filename)) { |
| 140 | return false; |
| 141 | } |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 142 | base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(filename); |
Bernie Innocenti | fbc9afb | 2020-12-22 20:10:32 +0900 | [diff] [blame] | 143 | if (!ret.ok()) { |
Siarhei Vishniakou | df6630a | 2022-05-18 13:34:55 -0700 | [diff] [blame] | 144 | 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 Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 149 | error("Error %s parsing key layout file.\n\n", ret.error().message().c_str()); |
| 150 | return false; |
| 151 | } |
| 152 | break; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 153 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 154 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 155 | case FileType::KEY_CHARACTER_MAP: { |
| 156 | base::Result<std::shared_ptr<KeyCharacterMap>> ret = |
| 157 | KeyCharacterMap::load(filename, KeyCharacterMap::Format::ANY); |
Bernie Innocenti | fbc9afb | 2020-12-22 20:10:32 +0900 | [diff] [blame] | 158 | if (!ret.ok()) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 159 | error("Error %s parsing key character map file.\n\n", |
| 160 | ret.error().message().c_str()); |
| 161 | return false; |
| 162 | } |
| 163 | break; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 164 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 165 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 166 | case FileType::INPUT_DEVICE_CONFIGURATION: { |
| 167 | android::base::Result<std::unique_ptr<PropertyMap>> propertyMap = |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 168 | PropertyMap::load(String8(filename).c_str()); |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 169 | if (!propertyMap.ok()) { |
Siarhei Vishniakou | 0caeab9 | 2022-11-23 12:49:29 -0800 | [diff] [blame] | 170 | error("Error parsing input device configuration file: %s.\n\n", |
| 171 | propertyMap.error().message().c_str()); |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | break; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 175 | } |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 176 | |
| 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 Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
| 190 | int 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 Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 198 | if (i == 1 && !strcmp(argv[1], "-q")) { |
| 199 | gQuiet = true; |
| 200 | continue; |
| 201 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 202 | if (!validateFile(argv[i])) { |
| 203 | result = 1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (result) { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 208 | error("Failed!\n"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 209 | } else { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 210 | log("Success.\n"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 211 | } |
| 212 | return result; |
| 213 | } |