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