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 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 32 | enum class FileType { |
| 33 | UNKNOWN, |
| 34 | KEY_LAYOUT, |
| 35 | KEY_CHARACTER_MAP, |
| 36 | VIRTUAL_KEY_DEFINITION, |
| 37 | INPUT_DEVICE_CONFIGURATION, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 40 | static void log(const char* fmt, ...) { |
| 41 | if (gQuiet) { |
| 42 | return; |
| 43 | } |
| 44 | va_list args; |
| 45 | va_start(args, fmt); |
| 46 | vfprintf(stdout, fmt, args); |
| 47 | va_end(args); |
| 48 | } |
| 49 | |
| 50 | static void error(const char* fmt, ...) { |
| 51 | va_list args; |
| 52 | va_start(args, fmt); |
| 53 | vfprintf(stderr, fmt, args); |
| 54 | va_end(args); |
| 55 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 56 | |
| 57 | static void usage() { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 58 | error("Keymap Validation Tool\n\n"); |
| 59 | error("Usage:\n"); |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 60 | error(" %s [-q] [*.kl] [*.kcm] [*.idc] [virtualkeys.*] [...]\n" |
| 61 | " Validates the specified key layouts, key character maps, \n" |
| 62 | " input device configurations, or virtual key definitions.\n\n" |
| 63 | " -q Quiet; do not write anything to standard out.\n", |
| 64 | PROG_NAME); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static FileType getFileType(const char* filename) { |
| 68 | const char *extension = strrchr(filename, '.'); |
| 69 | if (extension) { |
| 70 | if (strcmp(extension, ".kl") == 0) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 71 | return FileType::KEY_LAYOUT; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 72 | } |
| 73 | if (strcmp(extension, ".kcm") == 0) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 74 | return FileType::KEY_CHARACTER_MAP; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 75 | } |
| 76 | if (strcmp(extension, ".idc") == 0) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 77 | return FileType::INPUT_DEVICE_CONFIGURATION; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | if (strstr(filename, "virtualkeys.")) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 82 | return FileType::VIRTUAL_KEY_DEFINITION; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 85 | return FileType::UNKNOWN; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static bool validateFile(const char* filename) { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 89 | log("Validating file '%s'...\n", filename); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 90 | |
| 91 | FileType fileType = getFileType(filename); |
| 92 | switch (fileType) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 93 | case FileType::UNKNOWN: |
| 94 | error("Supported file types: *.kl, *.kcm, virtualkeys.*\n\n"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 95 | return false; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 96 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 97 | case FileType::KEY_LAYOUT: { |
| 98 | base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(filename); |
Bernie Innocenti | fbc9afb | 2020-12-22 20:10:32 +0900 | [diff] [blame^] | 99 | if (!ret.ok()) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 100 | error("Error %s parsing key layout file.\n\n", ret.error().message().c_str()); |
| 101 | return false; |
| 102 | } |
| 103 | break; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 104 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 105 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 106 | case FileType::KEY_CHARACTER_MAP: { |
| 107 | base::Result<std::shared_ptr<KeyCharacterMap>> ret = |
| 108 | KeyCharacterMap::load(filename, KeyCharacterMap::Format::ANY); |
Bernie Innocenti | fbc9afb | 2020-12-22 20:10:32 +0900 | [diff] [blame^] | 109 | if (!ret.ok()) { |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 110 | error("Error %s parsing key character map file.\n\n", |
| 111 | ret.error().message().c_str()); |
| 112 | return false; |
| 113 | } |
| 114 | break; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 115 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 116 | |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 117 | case FileType::INPUT_DEVICE_CONFIGURATION: { |
| 118 | android::base::Result<std::unique_ptr<PropertyMap>> propertyMap = |
| 119 | PropertyMap::load(String8(filename)); |
| 120 | if (!propertyMap.ok()) { |
| 121 | error("Error %d parsing input device configuration file.\n\n", |
| 122 | propertyMap.error().code()); |
| 123 | return false; |
| 124 | } |
| 125 | break; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 126 | } |
Michael Wright | 781b129 | 2020-11-04 03:55:48 +0000 | [diff] [blame] | 127 | |
| 128 | case FileType::VIRTUAL_KEY_DEFINITION: { |
| 129 | std::unique_ptr<VirtualKeyMap> map = VirtualKeyMap::load(filename); |
| 130 | if (!map) { |
| 131 | error("Error while parsing virtual key definition file.\n\n"); |
| 132 | return false; |
| 133 | } |
| 134 | break; |
| 135 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | int main(int argc, const char** argv) { |
| 142 | if (argc < 2) { |
| 143 | usage(); |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | int result = 0; |
| 148 | for (int i = 1; i < argc; i++) { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 149 | if (i == 1 && !strcmp(argv[1], "-q")) { |
| 150 | gQuiet = true; |
| 151 | continue; |
| 152 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 153 | if (!validateFile(argv[i])) { |
| 154 | result = 1; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (result) { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 159 | error("Failed!\n"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 160 | } else { |
Michael Wright | 7185881 | 2017-09-04 15:18:44 +0100 | [diff] [blame] | 161 | log("Success.\n"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 162 | } |
| 163 | return result; |
| 164 | } |