Free XkbFile's when no longer needed
[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 <limits.h>
28 #include "xkbcomp.h"
29 #include "xkballoc.h"
30 #include "xkbrules.h"
31 #include "xkbpath.h"
32 #include "parseutils.h"
33 #include "utils.h"
34
35 /* Global debugging flags */
36 unsigned int debugFlags = 0;
37 unsigned int warningLevel = 0;
38
39 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
40
41 static XkbFile *
42 XkbKeymapFileFromComponents(const struct xkb_component_names * ktcsg)
43 {
44     XkbFile *keycodes, *types, *compat, *symbols, *geometry;
45     IncludeStmt *inc;
46
47     if (!ktcsg) {
48         ERROR("no components to generate keymap file from\n");
49         return NULL;
50     }
51
52     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
53     keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
54
55     inc = IncludeCreate(ktcsg->types, MergeDefault);
56     types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
57     AppendStmt(&keycodes->common, &types->common);
58
59     inc = IncludeCreate(ktcsg->compat, MergeDefault);
60     compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
61     AppendStmt(&keycodes->common, &compat->common);
62
63     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
64     symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
65     AppendStmt(&keycodes->common, &symbols->common);
66
67     inc = IncludeCreate(ktcsg->geometry, MergeDefault);
68     geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
69     AppendStmt(&keycodes->common, &geometry->common);
70
71     return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : strdup(""),
72                          &keycodes->common, 0);
73 }
74
75 static struct xkb_component_names *
76 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
77 {
78     FILE *rulesFile = NULL;
79     char *rulesPath = NULL;
80     XkbRF_RulesPtr loaded = NULL;
81     struct xkb_component_names * names = NULL;
82
83     rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
84     if (!rulesFile) {
85         ERROR("could not find \"%s\" rules in XKB path\n", rules);
86         return NULL;
87     }
88
89     if (!(loaded = _XkbTypedCalloc(1, XkbRF_RulesRec))) {
90         ERROR("failed to allocate XKB rules\n");
91         goto unwind_file;
92     }
93
94     if (!XkbcRF_LoadRules(rulesFile, loaded)) {
95         ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
96         goto unwind_file;
97     }
98
99     if (!(names = _XkbTypedCalloc(1, struct xkb_component_names))) {
100         ERROR("failed to allocate XKB components\n");
101         goto unwind_file;
102     }
103
104     if (!XkbcRF_GetComponents(loaded, defs, names)) {
105         free(names->keymap);
106         free(names->keycodes);
107         free(names->types);
108         free(names->compat);
109         free(names->symbols);
110         free(names->geometry);
111         free(names);
112         names = NULL;
113         ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
114     }
115
116 unwind_file:
117     XkbcRF_Free(loaded);
118     if (rulesFile)
119         fclose(rulesFile);
120     free(rulesPath);
121     return names;
122 }
123
124 struct xkb_desc *
125 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rmlvo)
126 {
127     XkbRF_VarDefsRec defs;
128     struct xkb_component_names * names;
129     struct xkb_desc * xkb;
130
131     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
132         ERROR("rules and layout required to generate XKB keymap\n");
133         return NULL;
134     }
135
136     defs.model = rmlvo->model;
137     defs.layout = rmlvo->layout;
138     defs.variant = rmlvo->variant;
139     defs.options = rmlvo->options;
140
141     names = XkbComponentsFromRules(rmlvo->rules, &defs);
142     if (!names) {
143         ERROR("failed to generate XKB components from rules \"%s\"\n",
144               rmlvo->rules);
145         return NULL;
146     }
147
148     xkb = xkb_compile_keymap_from_components(names);
149
150     free(names->keymap);
151     free(names->keycodes);
152     free(names->types);
153     free(names->compat);
154     free(names->symbols);
155     free(names->geometry);
156     free(names);
157
158     return xkb;
159 }
160
161 static XkbFile *
162 XkbChooseMap(XkbFile *file, const char *name)
163 {
164     XkbFile *map = file;
165
166     /* map specified? */
167     if (name) {
168         while (map) {
169             if (map->name && strcmp(map->name, name) == 0)
170                 break;
171             map = (XkbFile *) map->common.next;
172         }
173
174         if (!map)
175             ERROR("no map named \"%s\" in input file\n", name);
176     }
177     else if (file->common.next) {
178         /* look for map with XkbLC_Default flag. */
179         for (; map; map = (XkbFile *) map->common.next) {
180             if (map->flags & XkbLC_Default)
181                 break;
182         }
183
184         if (!map) {
185             map = file;
186             WARN("no map specified, but components have several\n");
187             WARN("using the first defined map, \"%s\"\n",
188                  map->name ? map->name : "");
189         }
190     }
191
192     return map;
193 }
194
195 struct xkb_desc *
196 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg)
197 {
198     XkbFile *file, *mapToUse;
199     struct xkb_desc * xkb;
200
201     uSetErrorFile(NULL);
202
203     if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
204         ERROR("keycodes required to generate XKB keymap\n");
205         goto fail;
206     }
207
208     if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
209         ERROR("failed to generate parsed XKB file from components\n");
210         goto fail;
211     }
212
213     /* Find map to use */
214     if (!(mapToUse = XkbChooseMap(file, NULL)))
215         goto unwind_file;
216
217     /* Compile the keyboard */
218     if (!(xkb = XkbcAllocKeyboard())) {
219         ERROR("could not allocate keyboard description\n");
220         goto unwind_file;
221     }
222
223     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
224         ERROR("failed to compile keymap\n");
225         goto unwind_xkb;
226     }
227
228     FreeXKBFile(file);
229     return xkb;
230 unwind_xkb:
231     XkbcFreeKeyboard(xkb);
232 unwind_file:
233     FreeXKBFile(file);
234 fail:
235     return NULL;
236 }
237
238 static struct xkb_desc *
239 compile_keymap(XkbFile *file, const char *mapName)
240 {
241     XkbFile *mapToUse;
242     struct xkb_desc * xkb;
243
244     /* Find map to use */
245     if (!(mapToUse = XkbChooseMap(file, mapName)))
246         goto unwind_file;
247
248     switch (mapToUse->type) {
249     case XkmSemanticsFile:
250     case XkmLayoutFile:
251     case XkmKeymapFile:
252         break;
253     default:
254         ERROR("file type %d not handled\n", mapToUse->type);
255         goto unwind_file;
256     }
257
258     /* Compile the keyboard */
259     if (!(xkb = XkbcAllocKeyboard())) {
260         ERROR("could not allocate keyboard description\n");
261         goto unwind_file;
262     }
263
264     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
265         ERROR("failed to compile keymap\n");
266         goto unwind_xkb;
267     }
268
269     FreeXKBFile(file);
270     return xkb;
271 unwind_xkb:
272     XkbcFreeKeyboard(xkb);
273 unwind_file:
274     FreeXKBFile(file);
275
276     return NULL;
277 }
278
279 struct xkb_desc *
280 xkb_compile_keymap_from_string(const char *string, const char *mapName)
281 {
282     XkbFile *file;
283
284     if (!string) {
285         ERROR("no string specified to generate XKB keymap\n");
286         return NULL;
287     }
288
289     setScanState("input", 1);
290     if (!XKBParseString(string, &file) || !file) {
291         ERROR("failed to parse input xkb file\n");
292         return NULL;
293     }
294
295     return compile_keymap(file, mapName);
296 }
297
298 struct xkb_desc *
299 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
300 {
301     XkbFile *file;
302
303     if (!inputFile) {
304         ERROR("no file specified to generate XKB keymap\n");
305         return NULL;
306     }
307
308     setScanState("input", 1);
309     if (!XKBParseFile(inputFile, &file) || !file) {
310         ERROR("failed to parse input xkb file\n");
311         return NULL;
312     }
313
314     return compile_keymap(file, mapName);
315 }
316
317 void
318 xkb_free_keymap(struct xkb_desc *xkb)
319 {
320     XkbcFreeKeyboard(xkb);
321 }