blob: f31f771004bdff4c2adf33e2a727340f0a5ba424 [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: {
131 VirtualKeyMap* map;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100132 status_t status = VirtualKeyMap::load(filename, &map);
Adam Lesinski282e1812014-01-23 18:17:42 -0800133 if (status) {
Michael Wright71858812017-09-04 15:18:44 +0100134 error("Error %d parsing virtual key definition file.\n\n", status);
Adam Lesinski282e1812014-01-23 18:17:42 -0800135 return false;
136 }
137 delete map;
138 break;
139 }
140 }
141
Michael Wright71858812017-09-04 15:18:44 +0100142 log("No errors.\n\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800143 return true;
144}
145
146int main(int argc, const char** argv) {
147 if (argc < 2) {
148 usage();
149 return 1;
150 }
151
152 int result = 0;
153 for (int i = 1; i < argc; i++) {
Michael Wright71858812017-09-04 15:18:44 +0100154 if (i == 1 && !strcmp(argv[1], "-q")) {
155 gQuiet = true;
156 continue;
157 }
Adam Lesinski282e1812014-01-23 18:17:42 -0800158 if (!validateFile(argv[i])) {
159 result = 1;
160 }
161 }
162
163 if (result) {
Michael Wright71858812017-09-04 15:18:44 +0100164 error("Failed!\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800165 } else {
Michael Wright71858812017-09-04 15:18:44 +0100166 log("Success.\n");
Adam Lesinski282e1812014-01-23 18:17:42 -0800167 }
168 return result;
169}