blob: 5ac9dfd2a5572ac7dcc53ca5b6c8e2fcc9d704ff [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>
19#include <input/VirtualKeyMap.h>
20#include <utils/PropertyMap.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: {
99 sp<KeyLayoutMap> map;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100100 status_t status = KeyLayoutMap::load(filename, &map);
Adam Lesinski282e1812014-01-23 18:17:42 -0800101 if (status) {
Michael Wright71858812017-09-04 15:18:44 +0100102 error("Error %d parsing key layout file.\n\n", status);
Adam Lesinski282e1812014-01-23 18:17:42 -0800103 return false;
104 }
105 break;
106 }
107
108 case FILETYPE_KEYCHARACTERMAP: {
109 sp<KeyCharacterMap> map;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100110 status_t status = KeyCharacterMap::load(filename,
Adam Lesinski282e1812014-01-23 18:17:42 -0800111 KeyCharacterMap::FORMAT_ANY, &map);
112 if (status) {
Michael Wright71858812017-09-04 15:18:44 +0100113 error("Error %d parsing key character map file.\n\n", status);
Adam Lesinski282e1812014-01-23 18:17:42 -0800114 return false;
115 }
116 break;
117 }
118
119 case FILETYPE_INPUTDEVICECONFIGURATION: {
120 PropertyMap* map;
121 status_t status = PropertyMap::load(String8(filename), &map);
122 if (status) {
Michael Wright71858812017-09-04 15:18:44 +0100123 error("Error %d parsing input device configuration file.\n\n", status);
Adam Lesinski282e1812014-01-23 18:17:42 -0800124 return false;
125 }
126 delete map;
127 break;
128 }
129
130 case FILETYPE_VIRTUALKEYDEFINITION: {
Siarhei Vishniakou457b1602019-02-20 16:26:12 -0600131 std::unique_ptr<VirtualKeyMap> map = VirtualKeyMap::load(filename);
132 if (!map) {
133 error("Error while parsing virtual key definition file.\n\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800134 return false;
135 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800136 break;
137 }
138 }
139
Adam Lesinski282e1812014-01-23 18:17:42 -0800140 return true;
141}
142
143int main(int argc, const char** argv) {
144 if (argc < 2) {
145 usage();
146 return 1;
147 }
148
149 int result = 0;
150 for (int i = 1; i < argc; i++) {
Michael Wright71858812017-09-04 15:18:44 +0100151 if (i == 1 && !strcmp(argv[1], "-q")) {
152 gQuiet = true;
153 continue;
154 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800155 if (!validateFile(argv[i])) {
156 result = 1;
157 }
158 }
159
160 if (result) {
Michael Wright71858812017-09-04 15:18:44 +0100161 error("Failed!\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800162 } else {
Michael Wright71858812017-09-04 15:18:44 +0100163 log("Success.\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800164 }
165 return result;
166}