Organize xkbcomp/ header files
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbcomp.c
1 /*
2  * Copyright 2009  Dan Nicholson
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Except as contained in this notice, the names of the authors or their
22  * institutions shall not be used in advertising or otherwise to promote the
23  * sale, use or other dealings in this Software without prior written
24  * authorization from the authors.
25  */
26
27 #include "xkbcomp-priv.h"
28 #include "text.h"
29 #include "rules.h"
30
31 /**
32  * Compile the given file and store the output in keymap.
33  * @param file A list of XkbFiles, each denoting one type (e.g.
34  * FILE_TYPE_KEYCODES, etc.)
35  */
36 static struct xkb_keymap *
37 compile_keymap(struct xkb_context *ctx, XkbFile *file)
38 {
39     bool ok;
40     unsigned have = 0;
41     const char *main_name;
42     struct xkb_keymap *keymap;
43     XkbFile *keycodes = NULL;
44     XkbFile *types = NULL;
45     XkbFile *compat = NULL;
46     XkbFile *symbols = NULL;
47
48     keymap = xkb_map_new(ctx);
49     if (!keymap)
50         goto err;
51
52     main_name = file->name ? file->name : "(unnamed)";
53
54     /*
55      * Other aggregate file types are converted to FILE_TYPE_KEYMAP
56      * in the parser.
57      */
58     if (file->file_type != FILE_TYPE_KEYMAP) {
59         log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
60                 FileTypeText(file->file_type));
61         goto err;
62     }
63
64     /* Check for duplicate entries in the input file */
65     for (file = (XkbFile *) file->defs; file;
66          file = (XkbFile *) file->common.next) {
67         if (have & file->file_type) {
68             log_err(ctx,
69                     "More than one %s section in a keymap file; "
70                     "All sections after the first ignored\n",
71                     FileTypeText(file->file_type));
72             continue;
73         }
74
75         switch (file->file_type) {
76         case FILE_TYPE_KEYCODES:
77             keycodes = file;
78             break;
79
80         case FILE_TYPE_TYPES:
81             types = file;
82             break;
83
84         case FILE_TYPE_SYMBOLS:
85             symbols = file;
86             break;
87
88         case FILE_TYPE_COMPAT:
89             compat = file;
90             break;
91
92         default:
93             log_err(ctx, "Cannot define %s in a keymap file\n",
94                     FileTypeText(file->file_type));
95             continue;
96         }
97
98         if (!file->topName) {
99             free(file->topName);
100             file->topName = strdup(main_name);
101         }
102
103         have |= file->file_type;
104     }
105
106     if (REQUIRED_FILE_TYPES & (~have)) {
107         enum xkb_file_type bit;
108         enum xkb_file_type missing;
109
110         missing = REQUIRED_FILE_TYPES & (~have);
111
112         for (bit = 1; missing != 0; bit <<= 1) {
113             if (missing & bit) {
114                 log_err(ctx, "Required section %s missing from keymap\n",
115                         FileTypeText(bit));
116                 missing &= ~bit;
117             }
118         }
119
120         goto err;
121     }
122
123     ok = CompileKeycodes(keycodes, keymap, MERGE_OVERRIDE);
124     if (!ok) {
125         log_err(ctx, "Failed to compile keycodes\n");
126         goto err;
127     }
128     ok = CompileKeyTypes(types, keymap, MERGE_OVERRIDE);
129     if (!ok) {
130         log_err(ctx, "Failed to compile key types\n");
131         goto err;
132     }
133     ok = CompileCompatMap(compat, keymap, MERGE_OVERRIDE);
134     if (!ok) {
135         log_err(ctx, "Failed to compile compat map\n");
136         goto err;
137     }
138     ok = CompileSymbols(symbols, keymap, MERGE_OVERRIDE);
139     if (!ok) {
140         log_err(ctx, "Failed to compile symbols\n");
141         goto err;
142     }
143
144     ok = UpdateModifiersFromCompat(keymap);
145     if (!ok)
146         goto err;
147
148     return keymap;
149
150 err:
151     log_err(ctx, "Failed to compile keymap\n");
152     xkb_map_unref(keymap);
153     return NULL;
154 }
155
156 XKB_EXPORT struct xkb_keymap *
157 xkb_map_new_from_names(struct xkb_context *ctx,
158                        const struct xkb_rule_names *rmlvo_in,
159                        enum xkb_map_compile_flags flags)
160 {
161     bool ok;
162     struct xkb_component_names kccgst;
163     struct xkb_rule_names rmlvo = *rmlvo_in;
164     XkbFile *file;
165     struct xkb_keymap *keymap;
166
167     if (isempty(rmlvo.rules))
168         rmlvo.rules = DEFAULT_XKB_RULES;
169     if (isempty(rmlvo.model))
170         rmlvo.model = DEFAULT_XKB_MODEL;
171     if (isempty(rmlvo.layout))
172         rmlvo.layout = DEFAULT_XKB_LAYOUT;
173
174     ok = xkb_components_from_rules(ctx, &rmlvo, &kccgst);
175     if (!ok) {
176         log_err(ctx,
177                 "Couldn't look up rules '%s', model '%s', layout '%s', "
178                 "variant '%s', options '%s'\n",
179                 rmlvo.rules, rmlvo.model, rmlvo.layout, rmlvo.variant,
180                 rmlvo.options);
181         return NULL;
182     }
183
184     file = XkbFileFromComponents(ctx, &kccgst);
185
186     free(kccgst.keycodes);
187     free(kccgst.types);
188     free(kccgst.compat);
189     free(kccgst.symbols);
190
191     if (!file) {
192         log_err(ctx,
193                 "Failed to generate parsed XKB file from components\n");
194         return NULL;
195     }
196
197     keymap = compile_keymap(ctx, file);
198     FreeXkbFile(file);
199     return keymap;
200 }
201
202 XKB_EXPORT struct xkb_keymap *
203 xkb_map_new_from_string(struct xkb_context *ctx,
204                         const char *string,
205                         enum xkb_keymap_format format,
206                         enum xkb_map_compile_flags flags)
207 {
208     bool ok;
209     XkbFile *file;
210     struct xkb_keymap *keymap;
211
212     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
213         log_err(ctx, "Unsupported keymap format %d\n", format);
214         return NULL;
215     }
216
217     if (!string) {
218         log_err(ctx, "No string specified to generate XKB keymap\n");
219         return NULL;
220     }
221
222     ok = XkbParseString(ctx, string, "input", &file);
223     if (!ok) {
224         log_err(ctx, "Failed to parse input xkb file\n");
225         return NULL;
226     }
227
228     keymap = compile_keymap(ctx, file);
229     FreeXkbFile(file);
230     return keymap;
231 }
232
233 XKB_EXPORT struct xkb_keymap *
234 xkb_map_new_from_file(struct xkb_context *ctx,
235                       FILE *file,
236                       enum xkb_keymap_format format,
237                       enum xkb_map_compile_flags flags)
238 {
239     bool ok;
240     XkbFile *xkb_file;
241     struct xkb_keymap *keymap;
242
243     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
244         log_err(ctx, "Unsupported keymap format %d\n", format);
245         return NULL;
246     }
247
248     if (!file) {
249         log_err(ctx, "No file specified to generate XKB keymap\n");
250         return NULL;
251     }
252
253     ok = XkbParseFile(ctx, file, "(unknown file)", &xkb_file);
254     if (!ok) {
255         log_err(ctx, "Failed to parse input xkb file\n");
256         return NULL;
257     }
258
259     keymap = compile_keymap(ctx, xkb_file);
260     FreeXkbFile(xkb_file);
261     return keymap;
262 }