Move more of xkb_map_new_from_rmlvo into compilation
[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 "rules.h"
29 #include "parseutils.h"
30
31 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
32
33 static XkbFile *
34 keymap_file_from_names(struct xkb_context *ctx,
35                        const struct xkb_rule_names *rmlvo)
36 {
37     struct xkb_component_names *kkctgs;
38     XkbFile *keycodes, *types, *compat, *symbols;
39     IncludeStmt *inc;
40
41     kkctgs = xkb_components_from_rules(ctx, rmlvo);
42     if (!kkctgs) {
43         log_err(ctx, "failed to generate XKB components from rules \"%s\"\n",
44                 rmlvo->rules);
45         return NULL;
46     }
47
48     inc = IncludeCreate(ctx, kkctgs->keycodes, MERGE_DEFAULT);
49     keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
50                              (ParseCommon *) inc, 0);
51
52     inc = IncludeCreate(ctx, kkctgs->types, MERGE_DEFAULT);
53     types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
54                           (ParseCommon *) inc, 0);
55     AppendStmt(&keycodes->common, &types->common);
56
57     inc = IncludeCreate(ctx, kkctgs->compat, MERGE_DEFAULT);
58     compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
59                            (ParseCommon *) inc, 0);
60     AppendStmt(&keycodes->common, &compat->common);
61
62     inc = IncludeCreate(ctx, kkctgs->symbols, MERGE_DEFAULT);
63     symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
64                             (ParseCommon *) inc, 0);
65     AppendStmt(&keycodes->common, &symbols->common);
66
67     free(kkctgs->keycodes);
68     free(kkctgs->types);
69     free(kkctgs->compat);
70     free(kkctgs->symbols);
71     free(kkctgs);
72
73     return CreateXKBFile(ctx, FILE_TYPE_KEYMAP, strdup(""),
74                          &keycodes->common, 0);
75 }
76
77 static struct xkb_keymap *
78 new_keymap(struct xkb_context *ctx)
79 {
80     struct xkb_keymap *keymap;
81
82     keymap = calloc(1, sizeof(*keymap));
83     if (!keymap)
84         return NULL;
85
86     keymap->refcnt = 1;
87     keymap->ctx = xkb_context_ref(ctx);
88
89     return keymap;
90 }
91
92 /**
93  * Compile the given file and store the output in keymap.
94  * @param file A list of XkbFiles, each denoting one type (e.g.
95  * FILE_TYPE_KEYCODES, etc.)
96  */
97 static struct xkb_keymap *
98 compile_keymap(struct xkb_context *ctx, XkbFile *file)
99 {
100     bool ok;
101     unsigned have = 0;
102     const char *main_name;
103     struct xkb_keymap *keymap;
104     XkbFile *keycodes = NULL;
105     XkbFile *types = NULL;
106     XkbFile *compat = NULL;
107     XkbFile *symbols = NULL;
108
109     keymap = new_keymap(ctx);
110     if (!keymap)
111         goto err;
112
113     main_name = file->name ? file->name : "(unnamed)";
114
115     /*
116      * Other aggregate file types are converted to FILE_TYPE_KEYMAP
117      * in the parser.
118      */
119     if (file->file_type != FILE_TYPE_KEYMAP) {
120         log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
121                 FileTypeText(file->file_type));
122         goto err;
123     }
124
125     /* Check for duplicate entries in the input file */
126     for (file = (XkbFile *) file->defs; file;
127          file = (XkbFile *) file->common.next) {
128         if (have & file->file_type) {
129             log_err(ctx,
130                     "More than one %s section in a keymap file; "
131                     "All sections after the first ignored\n",
132                     FileTypeText(file->file_type));
133             continue;
134         }
135
136         switch (file->file_type) {
137         case FILE_TYPE_KEYCODES:
138             keycodes = file;
139             break;
140
141         case FILE_TYPE_TYPES:
142             types = file;
143             break;
144
145         case FILE_TYPE_SYMBOLS:
146             symbols = file;
147             break;
148
149         case FILE_TYPE_COMPAT:
150             compat = file;
151             break;
152
153         default:
154             log_err(ctx, "Cannot define %s in a keymap file\n",
155                     FileTypeText(file->file_type));
156             continue;
157         }
158
159         if (!file->topName) {
160             free(file->topName);
161             file->topName = strdup(main_name);
162         }
163
164         have |= file->file_type;
165     }
166
167     if (REQUIRED_FILE_TYPES & (~have)) {
168         enum xkb_file_type bit;
169         enum xkb_file_type missing;
170
171         missing = REQUIRED_FILE_TYPES & (~have);
172
173         for (bit = 1; missing != 0; bit <<= 1) {
174             if (missing & bit) {
175                 log_err(ctx, "Required section %s missing from keymap\n",
176                         FileTypeText(bit));
177                 missing &= ~bit;
178             }
179         }
180
181         goto err;
182     }
183
184     ok = CompileKeycodes(keycodes, keymap, MERGE_OVERRIDE);
185     if (!ok) {
186         log_err(ctx, "Failed to compile keycodes\n");
187         goto err;
188     }
189     ok = CompileKeyTypes(types, keymap, MERGE_OVERRIDE);
190     if (!ok) {
191         log_err(ctx, "Failed to compile key types\n");
192         goto err;
193     }
194     ok = CompileCompatMap(compat, keymap, MERGE_OVERRIDE);
195     if (!ok) {
196         log_err(ctx, "Failed to compile compat map\n");
197         goto err;
198     }
199     ok = CompileSymbols(symbols, keymap, MERGE_OVERRIDE);
200     if (!ok) {
201         log_err(ctx, "Failed to compile symbols\n");
202         goto err;
203     }
204
205     ok = UpdateModifiersFromCompat(keymap);
206     if (!ok)
207         goto err;
208
209     return keymap;
210
211 err:
212     log_err(ctx, "Failed to compile keymap\n");
213     xkb_map_unref(keymap);
214     return NULL;
215 }
216
217 XKB_EXPORT struct xkb_keymap *
218 xkb_map_new_from_names(struct xkb_context *ctx,
219                        const struct xkb_rule_names *rmlvo_in,
220                        enum xkb_map_compile_flags flags)
221 {
222     struct xkb_keymap *keymap = NULL;
223     struct xkb_rule_names rmlvo = *rmlvo_in;
224     XkbFile *file;
225
226     if (ISEMPTY(rmlvo.rules))
227         rmlvo.rules = DEFAULT_XKB_RULES;
228     if (ISEMPTY(rmlvo.model))
229         rmlvo.model = DEFAULT_XKB_MODEL;
230     if (ISEMPTY(rmlvo.layout))
231         rmlvo.layout = DEFAULT_XKB_LAYOUT;
232
233     file = keymap_file_from_names(ctx, &rmlvo);
234     if (!file) {
235         log_err(ctx, "Failed to generate parsed XKB file from components\n");
236         return NULL;
237     }
238
239     keymap = compile_keymap(ctx, file);
240     FreeXKBFile(file);
241
242     return keymap;
243 }
244
245 XKB_EXPORT struct xkb_keymap *
246 xkb_map_new_from_string(struct xkb_context *ctx,
247                         const char *string,
248                         enum xkb_keymap_format format,
249                         enum xkb_map_compile_flags flags)
250 {
251     bool ok;
252     XkbFile *file;
253     struct xkb_keymap *keymap;
254
255     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
256         log_err(ctx, "unsupported keymap format %d\n", format);
257         return NULL;
258     }
259
260     if (!string) {
261         log_err(ctx, "No string specified to generate XKB keymap\n");
262         return NULL;
263     }
264
265     ok = XKBParseString(ctx, string, "input", &file);
266     if (!ok) {
267         log_err(ctx, "Failed to parse input xkb file\n");
268         return NULL;
269     }
270
271     keymap = compile_keymap(ctx, file);
272     FreeXKBFile(file);
273     return keymap;
274 }
275
276 XKB_EXPORT struct xkb_keymap *
277 xkb_map_new_from_file(struct xkb_context *ctx,
278                       FILE *file,
279                       enum xkb_keymap_format format,
280                       enum xkb_map_compile_flags flags)
281 {
282     bool ok;
283     XkbFile *xkb_file;
284     struct xkb_keymap *keymap;
285
286     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
287         log_err(ctx, "Unsupported keymap format %d\n", format);
288         return NULL;
289     }
290
291     if (!file) {
292         log_err(ctx, "No file specified to generate XKB keymap\n");
293         return NULL;
294     }
295
296     ok = XKBParseFile(ctx, file, "(unknown file)", &xkb_file);
297     if (!ok) {
298         log_err(ctx, "Failed to parse input xkb file\n");
299         return NULL;
300     }
301
302     keymap = compile_keymap(ctx, xkb_file);
303     FreeXKBFile(xkb_file);
304     return keymap;
305 }
306
307 XKB_EXPORT struct xkb_keymap *
308 xkb_map_ref(struct xkb_keymap *keymap)
309 {
310     keymap->refcnt++;
311     return keymap;
312 }
313
314 XKB_EXPORT void
315 xkb_map_unref(struct xkb_keymap *keymap)
316 {
317     unsigned int i;
318     struct xkb_key *key;
319
320     if (!keymap || --keymap->refcnt > 0)
321         return;
322
323     for (i = 0; i < keymap->num_types; i++) {
324         free(keymap->types[i].map);
325         free(keymap->types[i].level_names);
326     }
327     free(keymap->types);
328
329     darray_foreach(key, keymap->keys) {
330         free(key->sym_index);
331         free(key->num_syms);
332         darray_free(key->syms);
333     }
334     darray_free(keymap->keys);
335
336     darray_free(keymap->acts);
337
338     darray_free(keymap->sym_interpret);
339
340     darray_free(keymap->key_aliases);
341
342     free(keymap->keycodes_section_name);
343     free(keymap->symbols_section_name);
344     free(keymap->types_section_name);
345     free(keymap->compat_section_name);
346
347     xkb_context_unref(keymap->ctx);
348
349     free(keymap);
350 }