blob: 950473d223fdd19926697529c55b32ec18500baa [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
Michael Wright781b1292020-11-04 03:55:48 +000032enum class FileType {
33 UNKNOWN,
34 KEY_LAYOUT,
35 KEY_CHARACTER_MAP,
36 VIRTUAL_KEY_DEFINITION,
37 INPUT_DEVICE_CONFIGURATION,
Adam Lesinski282e1812014-01-23 18:17:42 -080038};
39
Michael Wright71858812017-09-04 15:18:44 +010040static 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
50static 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 Lesinski282e1812014-01-23 18:17:42 -080056
57static void usage() {
Michael Wright71858812017-09-04 15:18:44 +010058 error("Keymap Validation Tool\n\n");
59 error("Usage:\n");
Michael Wright781b1292020-11-04 03:55:48 +000060 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 Lesinski282e1812014-01-23 18:17:42 -080065}
66
67static FileType getFileType(const char* filename) {
68 const char *extension = strrchr(filename, '.');
69 if (extension) {
70 if (strcmp(extension, ".kl") == 0) {
Michael Wright781b1292020-11-04 03:55:48 +000071 return FileType::KEY_LAYOUT;
Adam Lesinski282e1812014-01-23 18:17:42 -080072 }
73 if (strcmp(extension, ".kcm") == 0) {
Michael Wright781b1292020-11-04 03:55:48 +000074 return FileType::KEY_CHARACTER_MAP;
Adam Lesinski282e1812014-01-23 18:17:42 -080075 }
76 if (strcmp(extension, ".idc") == 0) {
Michael Wright781b1292020-11-04 03:55:48 +000077 return FileType::INPUT_DEVICE_CONFIGURATION;
Adam Lesinski282e1812014-01-23 18:17:42 -080078 }
79 }
80
81 if (strstr(filename, "virtualkeys.")) {
Michael Wright781b1292020-11-04 03:55:48 +000082 return FileType::VIRTUAL_KEY_DEFINITION;
Adam Lesinski282e1812014-01-23 18:17:42 -080083 }
84
Michael Wright781b1292020-11-04 03:55:48 +000085 return FileType::UNKNOWN;
Adam Lesinski282e1812014-01-23 18:17:42 -080086}
87
88static bool validateFile(const char* filename) {
Michael Wright71858812017-09-04 15:18:44 +010089 log("Validating file '%s'...\n", filename);
Adam Lesinski282e1812014-01-23 18:17:42 -080090
91 FileType fileType = getFileType(filename);
92 switch (fileType) {
Michael Wright781b1292020-11-04 03:55:48 +000093 case FileType::UNKNOWN:
94 error("Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
Adam Lesinski282e1812014-01-23 18:17:42 -080095 return false;
Adam Lesinski282e1812014-01-23 18:17:42 -080096
Michael Wright781b1292020-11-04 03:55:48 +000097 case FileType::KEY_LAYOUT: {
98 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(filename);
99 if (!ret) {
100 error("Error %s parsing key layout file.\n\n", ret.error().message().c_str());
101 return false;
102 }
103 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800104 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800105
Michael Wright781b1292020-11-04 03:55:48 +0000106 case FileType::KEY_CHARACTER_MAP: {
107 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
108 KeyCharacterMap::load(filename, KeyCharacterMap::Format::ANY);
109 if (!ret) {
110 error("Error %s parsing key character map file.\n\n",
111 ret.error().message().c_str());
112 return false;
113 }
114 break;
Adam Lesinski282e1812014-01-23 18:17:42 -0800115 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800116
Michael Wright781b1292020-11-04 03:55:48 +0000117 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 Lesinski282e1812014-01-23 18:17:42 -0800126 }
Michael Wright781b1292020-11-04 03:55:48 +0000127
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 Lesinski282e1812014-01-23 18:17:42 -0800136 }
137
Adam Lesinski282e1812014-01-23 18:17:42 -0800138 return true;
139}
140
141int 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 Wright71858812017-09-04 15:18:44 +0100149 if (i == 1 && !strcmp(argv[1], "-q")) {
150 gQuiet = true;
151 continue;
152 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800153 if (!validateFile(argv[i])) {
154 result = 1;
155 }
156 }
157
158 if (result) {
Michael Wright71858812017-09-04 15:18:44 +0100159 error("Failed!\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800160 } else {
Michael Wright71858812017-09-04 15:18:44 +0100161 log("Success.\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800162 }
163 return result;
164}