blob: 097b109ab1f3e5371b11011ba9804f20d6823e42 [file] [log] [blame]
Jeff Brown061cf752010-11-18 20:52:43 -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 <ui/KeyCharacterMap.h>
18#include <ui/KeyLayoutMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080019#include <ui/VirtualKeyMap.h>
Jeff Brown061cf752010-11-18 20:52:43 -080020#include <utils/String8.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26using namespace android;
27
28static const char* gProgName = "validatekeymaps";
29
30enum FileType {
31 FILETYPE_UNKNOWN,
32 FILETYPE_KEYLAYOUT,
33 FILETYPE_KEYCHARACTERMAP,
Jeff Brown90655042010-12-02 13:50:46 -080034 FILETYPE_VIRTUALKEYDEFINITION,
Jeff Brown061cf752010-11-18 20:52:43 -080035};
36
37
38static void usage() {
39 fprintf(stderr, "Keymap Validation Tool\n\n");
40 fprintf(stderr, "Usage:\n");
41 fprintf(stderr,
Jeff Brown90655042010-12-02 13:50:46 -080042 " %s [*.kl] [*.kcm] [virtualkeys.*] [...]\n"
43 " Validates the specified key layouts, key character maps \n"
44 " or virtual key definitions.\n\n",
45 gProgName);
Jeff Brown061cf752010-11-18 20:52:43 -080046}
47
48static FileType getFileType(const char* filename) {
Jeff Brownab841352010-11-23 16:29:54 -080049 const char *extension = strrchr(filename, '.');
Jeff Brown061cf752010-11-18 20:52:43 -080050 if (extension) {
51 if (strcmp(extension, ".kl") == 0) {
52 return FILETYPE_KEYLAYOUT;
53 }
54 if (strcmp(extension, ".kcm") == 0) {
55 return FILETYPE_KEYCHARACTERMAP;
56 }
57 }
Jeff Brown90655042010-12-02 13:50:46 -080058
59 if (strstr(filename, "virtualkeys.")) {
60 return FILETYPE_VIRTUALKEYDEFINITION;
61 }
62
Jeff Brown061cf752010-11-18 20:52:43 -080063 return FILETYPE_UNKNOWN;
64}
65
66static bool validateFile(const char* filename) {
67 fprintf(stdout, "Validating file '%s'...\n", filename);
68
69 FileType fileType = getFileType(filename);
70 switch (fileType) {
71 case FILETYPE_UNKNOWN:
Jeff Brown90655042010-12-02 13:50:46 -080072 fprintf(stderr, "Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
Jeff Brown061cf752010-11-18 20:52:43 -080073 return false;
74
75 case FILETYPE_KEYLAYOUT: {
76 KeyLayoutMap* map;
77 status_t status = KeyLayoutMap::load(String8(filename), &map);
78 if (status) {
79 fprintf(stderr, "Error %d parsing key layout file.\n\n", status);
80 return false;
81 }
82 break;
83 }
84
85 case FILETYPE_KEYCHARACTERMAP: {
86 KeyCharacterMap* map;
87 status_t status = KeyCharacterMap::load(String8(filename), &map);
88 if (status) {
89 fprintf(stderr, "Error %d parsing key character map file.\n\n", status);
90 return false;
91 }
92 break;
93 }
Jeff Brown90655042010-12-02 13:50:46 -080094
95 case FILETYPE_VIRTUALKEYDEFINITION: {
96 VirtualKeyMap* map;
97 status_t status = VirtualKeyMap::load(String8(filename), &map);
98 if (status) {
99 fprintf(stderr, "Error %d parsing virtual key definition file.\n\n", status);
100 return false;
101 }
102 break;
103 }
Jeff Brown061cf752010-11-18 20:52:43 -0800104 }
105
106 fputs("No errors.\n\n", stdout);
107 return true;
108}
109
110int main(int argc, const char** argv) {
111 if (argc < 2) {
112 usage();
113 return 1;
114 }
115
116 int result = 0;
117 for (int i = 1; i < argc; i++) {
118 if (!validateFile(argv[i])) {
119 result = 1;
120 }
121 }
122
123 if (result) {
124 fputs("Failed!\n", stderr);
125 } else {
126 fputs("Success.\n", stdout);
127 }
128 return result;
129}