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