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