blob: 0af6266d16425f081394104cce60f1fa5c1d9ace [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 Wright71858812017-09-04 15:18:44 +010029static const char* kProgName = "validatekeymaps";
30static bool gQuiet = false;
Adam Lesinski282e1812014-01-23 18:17:42 -080031
32enum FileType {
33 FILETYPE_UNKNOWN,
34 FILETYPE_KEYLAYOUT,
35 FILETYPE_KEYCHARACTERMAP,
36 FILETYPE_VIRTUALKEYDEFINITION,
37 FILETYPE_INPUTDEVICECONFIGURATION,
38};
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");
60 error(
61 " %s [-q] [*.kl] [*.kcm] [*.idc] [virtualkeys.*] [...]\n"
Adam Lesinski282e1812014-01-23 18:17:42 -080062 " Validates the specified key layouts, key character maps, \n"
Michael Wright71858812017-09-04 15:18:44 +010063 " input device configurations, or virtual key definitions.\n\n"
64 " -q Quiet; do not write anything to standard out.\n",
65 kProgName);
Adam Lesinski282e1812014-01-23 18:17:42 -080066}
67
68static FileType getFileType(const char* filename) {
69 const char *extension = strrchr(filename, '.');
70 if (extension) {
71 if (strcmp(extension, ".kl") == 0) {
72 return FILETYPE_KEYLAYOUT;
73 }
74 if (strcmp(extension, ".kcm") == 0) {
75 return FILETYPE_KEYCHARACTERMAP;
76 }
77 if (strcmp(extension, ".idc") == 0) {
78 return FILETYPE_INPUTDEVICECONFIGURATION;
79 }
80 }
81
82 if (strstr(filename, "virtualkeys.")) {
83 return FILETYPE_VIRTUALKEYDEFINITION;
84 }
85
86 return FILETYPE_UNKNOWN;
87}
88
89static bool validateFile(const char* filename) {
Michael Wright71858812017-09-04 15:18:44 +010090 log("Validating file '%s'...\n", filename);
Adam Lesinski282e1812014-01-23 18:17:42 -080091
92 FileType fileType = getFileType(filename);
93 switch (fileType) {
94 case FILETYPE_UNKNOWN:
Michael Wright71858812017-09-04 15:18:44 +010095 error("Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
Adam Lesinski282e1812014-01-23 18:17:42 -080096 return false;
97
98 case FILETYPE_KEYLAYOUT: {
Chris Yedc275f82020-08-18 12:42:22 -070099 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(filename);
100 if (!ret) {
101 error("Error %s parsing key layout file.\n\n", ret.error().message().c_str());
Adam Lesinski282e1812014-01-23 18:17:42 -0800102 return false;
103 }
104 break;
105 }
106
107 case FILETYPE_KEYCHARACTERMAP: {
Chris Yee8da2d32020-08-18 12:15:28 -0700108 base::Result<std::shared_ptr<KeyCharacterMap>> ret = KeyCharacterMap::load(filename,
109 KeyCharacterMap::FORMAT_ANY);
110 if (!ret) {
111 error("Error %s parsing key character map file.\n\n", ret.error().message().c_str());
Adam Lesinski282e1812014-01-23 18:17:42 -0800112 return false;
113 }
114 break;
115 }
116
117 case FILETYPE_INPUTDEVICECONFIGURATION: {
Siarhei Vishniakou2da17762020-09-08 22:43:35 -0500118 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());
Adam Lesinski282e1812014-01-23 18:17:42 -0800123 return false;
124 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800125 break;
126 }
127
128 case FILETYPE_VIRTUALKEYDEFINITION: {
Siarhei Vishniakou457b1602019-02-20 16:26:12 -0600129 std::unique_ptr<VirtualKeyMap> map = VirtualKeyMap::load(filename);
130 if (!map) {
131 error("Error while parsing virtual key definition file.\n\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800132 return false;
133 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800134 break;
135 }
136 }
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}