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