Don't scan and parse useless maps
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbcomp.c
1 /*
2  * Copyright © 2009 Dan Nicholson
3  * Copyright © 2012 Intel Corporation
4  * Copyright © 2012 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors: Dan Nicholson <dbn.lists@gmail.com>
26  *          Ran Benita <ran234@gmail.com>
27  *          Daniel Stone <daniel@fooishbar.org>
28  */
29
30 #include "xkbcomp-priv.h"
31 #include "rules.h"
32
33 static struct xkb_keymap *
34 compile_keymap_file(struct xkb_context *ctx, XkbFile *file,
35                     enum xkb_keymap_format format,
36                     enum xkb_keymap_compile_flags flags)
37 {
38     struct xkb_keymap *keymap;
39
40     keymap = xkb_keymap_new(ctx, format, flags);
41     if (!keymap)
42         goto err;
43
44     if (file->file_type != FILE_TYPE_KEYMAP) {
45         log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
46                 xkb_file_type_to_string(file->file_type));
47         goto err;
48     }
49
50     if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
51         log_err(ctx, "Failed to compile keymap\n");
52         goto err;
53     }
54
55     return keymap;
56
57 err:
58     xkb_keymap_unref(keymap);
59     return NULL;
60 }
61
62 XKB_EXPORT struct xkb_keymap *
63 xkb_keymap_new_from_names(struct xkb_context *ctx,
64                           const struct xkb_rule_names *rmlvo_in,
65                           enum xkb_keymap_compile_flags flags)
66 {
67     bool ok;
68     struct xkb_component_names kccgst;
69     struct xkb_rule_names rmlvo = *rmlvo_in;
70     XkbFile *file;
71     struct xkb_keymap *keymap;
72
73     if (isempty(rmlvo.rules))
74         rmlvo.rules = DEFAULT_XKB_RULES;
75     if (isempty(rmlvo.model))
76         rmlvo.model = DEFAULT_XKB_MODEL;
77     if (isempty(rmlvo.layout))
78         rmlvo.layout = DEFAULT_XKB_LAYOUT;
79
80     log_dbg(ctx,
81             "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
82             "variant '%s', options '%s'\n",
83             strnull(rmlvo.rules), strnull(rmlvo.model),
84             strnull(rmlvo.layout), strnull(rmlvo.variant),
85             strnull(rmlvo.options));
86
87     ok = xkb_components_from_rules(ctx, &rmlvo, &kccgst);
88     if (!ok) {
89         log_err(ctx,
90                 "Couldn't look up rules '%s', model '%s', layout '%s', "
91                 "variant '%s', options '%s'\n",
92                 strnull(rmlvo.rules), strnull(rmlvo.model),
93                 strnull(rmlvo.layout), strnull(rmlvo.variant),
94                 strnull(rmlvo.options));
95         return NULL;
96     }
97
98     log_dbg(ctx,
99             "Compiling from KcCGST: keycodes '%s', types '%s', "
100             "compat '%s', symbols '%s'\n",
101             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
102
103     file = XkbFileFromComponents(ctx, &kccgst);
104
105     free(kccgst.keycodes);
106     free(kccgst.types);
107     free(kccgst.compat);
108     free(kccgst.symbols);
109
110     if (!file) {
111         log_err(ctx,
112                 "Failed to generate parsed XKB file from components\n");
113         return NULL;
114     }
115
116     keymap = compile_keymap_file(ctx, file, XKB_KEYMAP_FORMAT_TEXT_V1, flags);
117     FreeXkbFile(file);
118     return keymap;
119 }
120
121 XKB_EXPORT struct xkb_keymap *
122 xkb_keymap_new_from_string(struct xkb_context *ctx,
123                            const char *string,
124                            enum xkb_keymap_format format,
125                            enum xkb_keymap_compile_flags flags)
126 {
127     XkbFile *file;
128     struct xkb_keymap *keymap;
129
130     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
131         log_err(ctx, "Unsupported keymap format %d\n", format);
132         return NULL;
133     }
134
135     if (!string) {
136         log_err(ctx, "No string specified to generate XKB keymap\n");
137         return NULL;
138     }
139
140     file = XkbParseString(ctx, string, "input");
141     if (!file) {
142         log_err(ctx, "Failed to parse input xkb file\n");
143         return NULL;
144     }
145
146     keymap = compile_keymap_file(ctx, file, format, flags);
147     FreeXkbFile(file);
148     return keymap;
149 }
150
151 XKB_EXPORT struct xkb_keymap *
152 xkb_keymap_new_from_file(struct xkb_context *ctx,
153                          FILE *file,
154                          enum xkb_keymap_format format,
155                          enum xkb_keymap_compile_flags flags)
156 {
157     XkbFile *xkb_file;
158     struct xkb_keymap *keymap;
159
160     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
161         log_err(ctx, "Unsupported keymap format %d\n", format);
162         return NULL;
163     }
164
165     if (!file) {
166         log_err(ctx, "No file specified to generate XKB keymap\n");
167         return NULL;
168     }
169
170     xkb_file = XkbParseFile(ctx, file, "(unknown file)", NULL);
171     if (!xkb_file) {
172         log_err(ctx, "Failed to parse input xkb file\n");
173         return NULL;
174     }
175
176     keymap = compile_keymap_file(ctx, xkb_file, format, flags);
177     FreeXkbFile(xkb_file);
178     return keymap;
179 }