Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 17 | #include <input/KeyCharacterMap.h> |
| 18 | #include <input/KeyLayoutMap.h> |
| 19 | #include <input/VirtualKeyMap.h> |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 20 | #include <utils/PropertyMap.h> |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 21 | #include <utils/String8.h> |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | static const char* gProgName = "validatekeymaps"; |
| 30 | |
| 31 | enum FileType { |
| 32 | FILETYPE_UNKNOWN, |
| 33 | FILETYPE_KEYLAYOUT, |
| 34 | FILETYPE_KEYCHARACTERMAP, |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 35 | FILETYPE_VIRTUALKEYDEFINITION, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 36 | FILETYPE_INPUTDEVICECONFIGURATION, |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | |
| 40 | static void usage() { |
| 41 | fprintf(stderr, "Keymap Validation Tool\n\n"); |
| 42 | fprintf(stderr, "Usage:\n"); |
| 43 | fprintf(stderr, |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 44 | " %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 Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 47 | gProgName); |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static FileType getFileType(const char* filename) { |
Jeff Brown | ab84135 | 2010-11-23 16:29:54 -0800 | [diff] [blame] | 51 | const char *extension = strrchr(filename, '.'); |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 52 | 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 Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 59 | if (strcmp(extension, ".idc") == 0) { |
| 60 | return FILETYPE_INPUTDEVICECONFIGURATION; |
| 61 | } |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 62 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 63 | |
| 64 | if (strstr(filename, "virtualkeys.")) { |
| 65 | return FILETYPE_VIRTUALKEYDEFINITION; |
| 66 | } |
| 67 | |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 68 | return FILETYPE_UNKNOWN; |
| 69 | } |
| 70 | |
| 71 | static 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 Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 77 | fprintf(stderr, "Supported file types: *.kl, *.kcm, virtualkeys.*\n\n"); |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 78 | return false; |
| 79 | |
| 80 | case FILETYPE_KEYLAYOUT: { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 81 | sp<KeyLayoutMap> map; |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 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: { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 91 | sp<KeyCharacterMap> map; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 92 | status_t status = KeyCharacterMap::load(String8(filename), |
| 93 | KeyCharacterMap::FORMAT_ANY, &map); |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 94 | if (status) { |
| 95 | fprintf(stderr, "Error %d parsing key character map file.\n\n", status); |
| 96 | return false; |
| 97 | } |
| 98 | break; |
| 99 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 100 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 101 | case FILETYPE_INPUTDEVICECONFIGURATION: { |
| 102 | PropertyMap* map; |
| 103 | status_t status = PropertyMap::load(String8(filename), &map); |
| 104 | if (status) { |
| 105 | fprintf(stderr, "Error %d parsing input device configuration file.\n\n", status); |
| 106 | return false; |
| 107 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 108 | delete map; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 112 | case FILETYPE_VIRTUALKEYDEFINITION: { |
| 113 | VirtualKeyMap* map; |
| 114 | status_t status = VirtualKeyMap::load(String8(filename), &map); |
| 115 | if (status) { |
| 116 | fprintf(stderr, "Error %d parsing virtual key definition file.\n\n", status); |
| 117 | return false; |
| 118 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 119 | delete map; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 120 | break; |
| 121 | } |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | fputs("No errors.\n\n", stdout); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | int main(int argc, const char** argv) { |
| 129 | if (argc < 2) { |
| 130 | usage(); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | int result = 0; |
| 135 | for (int i = 1; i < argc; i++) { |
| 136 | if (!validateFile(argv[i])) { |
| 137 | result = 1; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (result) { |
| 142 | fputs("Failed!\n", stderr); |
| 143 | } else { |
| 144 | fputs("Success.\n", stdout); |
| 145 | } |
| 146 | return result; |
| 147 | } |