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 | |
| 17 | #include <ui/KeyCharacterMap.h> |
| 18 | #include <ui/KeyLayoutMap.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 19 | #include <ui/VirtualKeyMap.h> |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 20 | #include <utils/String8.h> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | using namespace android; |
| 27 | |
| 28 | static const char* gProgName = "validatekeymaps"; |
| 29 | |
| 30 | enum FileType { |
| 31 | FILETYPE_UNKNOWN, |
| 32 | FILETYPE_KEYLAYOUT, |
| 33 | FILETYPE_KEYCHARACTERMAP, |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 34 | FILETYPE_VIRTUALKEYDEFINITION, |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | |
| 38 | static void usage() { |
| 39 | fprintf(stderr, "Keymap Validation Tool\n\n"); |
| 40 | fprintf(stderr, "Usage:\n"); |
| 41 | fprintf(stderr, |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 42 | " %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 Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static FileType getFileType(const char* filename) { |
Jeff Brown | ab84135 | 2010-11-23 16:29:54 -0800 | [diff] [blame] | 49 | const char *extension = strrchr(filename, '.'); |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 50 | 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 Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 58 | |
| 59 | if (strstr(filename, "virtualkeys.")) { |
| 60 | return FILETYPE_VIRTUALKEYDEFINITION; |
| 61 | } |
| 62 | |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 63 | return FILETYPE_UNKNOWN; |
| 64 | } |
| 65 | |
| 66 | static 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 Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 72 | fprintf(stderr, "Supported file types: *.kl, *.kcm, virtualkeys.*\n\n"); |
Jeff Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 73 | 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 Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 94 | |
| 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 Brown | 061cf75 | 2010-11-18 20:52:43 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | fputs("No errors.\n\n", stdout); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | int 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 | } |