summaryrefslogtreecommitdiffstats
path: root/tools/validatekeymaps/Main.cpp
blob: 91e4fda3d7a7c7862116f8e9ccc3c6eb7bdc0aef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <androidfw/KeyCharacterMap.h>
#include <androidfw/KeyLayoutMap.h>
#include <androidfw/VirtualKeyMap.h>
#include <utils/PropertyMap.h>
#include <utils/String8.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace android;

static const char* gProgName = "validatekeymaps";

enum FileType {
    FILETYPE_UNKNOWN,
    FILETYPE_KEYLAYOUT,
    FILETYPE_KEYCHARACTERMAP,
    FILETYPE_VIRTUALKEYDEFINITION,
    FILETYPE_INPUTDEVICECONFIGURATION,
};


static void usage() {
    fprintf(stderr, "Keymap Validation Tool\n\n");
    fprintf(stderr, "Usage:\n");
    fprintf(stderr,
        " %s [*.kl] [*.kcm] [*.idc] [virtualkeys.*] [...]\n"
        "   Validates the specified key layouts, key character maps, \n"
        "   input device configurations, or virtual key definitions.\n\n",
        gProgName);
}

static FileType getFileType(const char* filename) {
    const char *extension = strrchr(filename, '.');
    if (extension) {
        if (strcmp(extension, ".kl") == 0) {
            return FILETYPE_KEYLAYOUT;
        }
        if (strcmp(extension, ".kcm") == 0) {
            return FILETYPE_KEYCHARACTERMAP;
        }
        if (strcmp(extension, ".idc") == 0) {
            return FILETYPE_INPUTDEVICECONFIGURATION;
        }
    }

    if (strstr(filename, "virtualkeys.")) {
        return FILETYPE_VIRTUALKEYDEFINITION;
    }

    return FILETYPE_UNKNOWN;
}

static bool validateFile(const char* filename) {
    fprintf(stdout, "Validating file '%s'...\n", filename);

    FileType fileType = getFileType(filename);
    switch (fileType) {
    case FILETYPE_UNKNOWN:
        fprintf(stderr, "Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
        return false;

    case FILETYPE_KEYLAYOUT: {
        sp<KeyLayoutMap> map;
        status_t status = KeyLayoutMap::load(String8(filename), &map);
        if (status) {
            fprintf(stderr, "Error %d parsing key layout file.\n\n", status);
            return false;
        }
        break;
    }

    case FILETYPE_KEYCHARACTERMAP: {
        sp<KeyCharacterMap> map;
        status_t status = KeyCharacterMap::load(String8(filename),
                KeyCharacterMap::FORMAT_ANY, &map);
        if (status) {
            fprintf(stderr, "Error %d parsing key character map file.\n\n", status);
            return false;
        }
        break;
    }

    case FILETYPE_INPUTDEVICECONFIGURATION: {
        PropertyMap* map;
        status_t status = PropertyMap::load(String8(filename), &map);
        if (status) {
            fprintf(stderr, "Error %d parsing input device configuration file.\n\n", status);
            return false;
        }
        delete map;
        break;
    }

    case FILETYPE_VIRTUALKEYDEFINITION: {
        VirtualKeyMap* map;
        status_t status = VirtualKeyMap::load(String8(filename), &map);
        if (status) {
            fprintf(stderr, "Error %d parsing virtual key definition file.\n\n", status);
            return false;
        }
        delete map;
        break;
    }
    }

    fputs("No errors.\n\n", stdout);
    return true;
}

int main(int argc, const char** argv) {
    if (argc < 2) {
        usage();
        return 1;
    }

    int result = 0;
    for (int i = 1; i < argc; i++) {
        if (!validateFile(argv[i])) {
            result = 1;
        }
    }

    if (result) {
        fputs("Failed!\n", stderr);
    } else {
        fputs("Success.\n", stdout);
    }
    return result;
}