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