blob: 8ab9b6ae3de679018cfbe0a094104601922586bc [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 Brown6f2fba42011-02-19 01:08:02 -080020#include <utils/PropertyMap.h>
Jeff Brown061cf752010-11-18 20:52:43 -080021#include <utils/String8.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27using namespace android;
28
29static const char* gProgName = "validatekeymaps";
30
31enum FileType {
32 FILETYPE_UNKNOWN,
33 FILETYPE_KEYLAYOUT,
34 FILETYPE_KEYCHARACTERMAP,
Jeff Brown90655042010-12-02 13:50:46 -080035 FILETYPE_VIRTUALKEYDEFINITION,
Jeff Brown6f2fba42011-02-19 01:08:02 -080036 FILETYPE_INPUTDEVICECONFIGURATION,
Jeff Brown061cf752010-11-18 20:52:43 -080037};
38
39
40static void usage() {
41 fprintf(stderr, "Keymap Validation Tool\n\n");
42 fprintf(stderr, "Usage:\n");
43 fprintf(stderr,
Jeff Brown6f2fba42011-02-19 01:08:02 -080044 " %s [*.kl] [*.kcm] [*.idc] [virtualkeys.*] [...]\n"
45 " Validates the specified key layouts, key character maps, \n"
46 " input device configurations, or virtual key definitions.\n\n",
Jeff Brown90655042010-12-02 13:50:46 -080047 gProgName);
Jeff Brown061cf752010-11-18 20:52:43 -080048}
49
50static FileType getFileType(const char* filename) {
Jeff Brownab841352010-11-23 16:29:54 -080051 const char *extension = strrchr(filename, '.');
Jeff Brown061cf752010-11-18 20:52:43 -080052 if (extension) {
53 if (strcmp(extension, ".kl") == 0) {
54 return FILETYPE_KEYLAYOUT;
55 }
56 if (strcmp(extension, ".kcm") == 0) {
57 return FILETYPE_KEYCHARACTERMAP;
58 }
Jeff Brown6f2fba42011-02-19 01:08:02 -080059 if (strcmp(extension, ".idc") == 0) {
60 return FILETYPE_INPUTDEVICECONFIGURATION;
61 }
Jeff Brown061cf752010-11-18 20:52:43 -080062 }
Jeff Brown90655042010-12-02 13:50:46 -080063
64 if (strstr(filename, "virtualkeys.")) {
65 return FILETYPE_VIRTUALKEYDEFINITION;
66 }
67
Jeff Brown061cf752010-11-18 20:52:43 -080068 return FILETYPE_UNKNOWN;
69}
70
71static bool validateFile(const char* filename) {
72 fprintf(stdout, "Validating file '%s'...\n", filename);
73
74 FileType fileType = getFileType(filename);
75 switch (fileType) {
76 case FILETYPE_UNKNOWN:
Jeff Brown90655042010-12-02 13:50:46 -080077 fprintf(stderr, "Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
Jeff Brown061cf752010-11-18 20:52:43 -080078 return false;
79
80 case FILETYPE_KEYLAYOUT: {
81 KeyLayoutMap* map;
82 status_t status = KeyLayoutMap::load(String8(filename), &map);
83 if (status) {
84 fprintf(stderr, "Error %d parsing key layout file.\n\n", status);
85 return false;
86 }
87 break;
88 }
89
90 case FILETYPE_KEYCHARACTERMAP: {
91 KeyCharacterMap* map;
92 status_t status = KeyCharacterMap::load(String8(filename), &map);
93 if (status) {
94 fprintf(stderr, "Error %d parsing key character map file.\n\n", status);
95 return false;
96 }
97 break;
98 }
Jeff Brown90655042010-12-02 13:50:46 -080099
Jeff Brown6f2fba42011-02-19 01:08:02 -0800100 case FILETYPE_INPUTDEVICECONFIGURATION: {
101 PropertyMap* map;
102 status_t status = PropertyMap::load(String8(filename), &map);
103 if (status) {
104 fprintf(stderr, "Error %d parsing input device configuration file.\n\n", status);
105 return false;
106 }
107 break;
108 }
109
Jeff Brown90655042010-12-02 13:50:46 -0800110 case FILETYPE_VIRTUALKEYDEFINITION: {
111 VirtualKeyMap* map;
112 status_t status = VirtualKeyMap::load(String8(filename), &map);
113 if (status) {
114 fprintf(stderr, "Error %d parsing virtual key definition file.\n\n", status);
115 return false;
116 }
117 break;
118 }
Jeff Brown061cf752010-11-18 20:52:43 -0800119 }
120
121 fputs("No errors.\n\n", stdout);
122 return true;
123}
124
125int main(int argc, const char** argv) {
126 if (argc < 2) {
127 usage();
128 return 1;
129 }
130
131 int result = 0;
132 for (int i = 1; i < argc; i++) {
133 if (!validateFile(argv[i])) {
134 result = 1;
135 }
136 }
137
138 if (result) {
139 fputs("Failed!\n", stderr);
140 } else {
141 fputs("Success.\n", stdout);
142 }
143 return result;
144}