Dump include paths when we can't find rules
[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 warning level */
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;
45     IncludeStmt *inc;
46
47     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
48     keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
49
50     inc = IncludeCreate(ktcsg->types, MergeDefault);
51     types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
52     AppendStmt(&keycodes->common, &types->common);
53
54     inc = IncludeCreate(ktcsg->compat, MergeDefault);
55     compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
56     AppendStmt(&keycodes->common, &compat->common);
57
58     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
59     symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
60     AppendStmt(&keycodes->common, &symbols->common);
61
62     return CreateXKBFile(XkmKeymapFile,
63                          ktcsg->keymap ? ktcsg->keymap : strdup(""),
64                          &keycodes->common, 0);
65 }
66
67 static struct xkb_component_names *
68 XkbComponentsFromRules(struct xkb_context *context,
69                        const char *rules,
70                        const XkbRF_VarDefsPtr defs)
71 {
72     FILE *rulesFile = NULL;
73     char *rulesPath = NULL;
74     XkbRF_RulesPtr loaded = NULL;
75     struct xkb_component_names * names = NULL;
76     int i;
77
78     rulesFile = XkbFindFileInPath(context, rules, XkmRulesFile, &rulesPath);
79     if (!rulesFile) {
80         ERROR("could not find \"%s\" rules in XKB path\n", rules);
81         ERROR("%d include paths searched:\n",
82               xkb_context_num_include_paths(context));
83         for (i = 0; i < xkb_context_num_include_paths(context); i++)
84             ERROR("\t%s\n", xkb_context_include_path_get(context, i));
85         return NULL;
86     }
87
88     if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) {
89         ERROR("failed to allocate XKB rules\n");
90         goto unwind_file;
91     }
92
93     if (!XkbcRF_LoadRules(rulesFile, loaded)) {
94         ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
95         goto unwind_file;
96     }
97
98     if (!(names = uTypedCalloc(1, struct xkb_component_names))) {
99         ERROR("failed to allocate XKB components\n");
100         goto unwind_file;
101     }
102
103     if (!XkbcRF_GetComponents(loaded, defs, names)) {
104         free(names->keymap);
105         free(names->keycodes);
106         free(names->types);
107         free(names->compat);
108         free(names->symbols);
109         free(names);
110         names = NULL;
111         ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
112     }
113
114 unwind_file:
115     XkbcRF_Free(loaded);
116     if (rulesFile)
117         fclose(rulesFile);
118     free(rulesPath);
119     return names;
120 }
121
122 _X_EXPORT struct xkb_keymap *
123 xkb_map_new_from_names(struct xkb_context *context,
124                        const struct xkb_rule_names *rmlvo)
125 {
126     XkbRF_VarDefsRec defs;
127     struct xkb_component_names *names;
128     struct xkb_keymap *xkb;
129
130     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
131         ERROR("rules and layout required to generate XKB keymap\n");
132         return NULL;
133     }
134
135     defs.model = rmlvo->model;
136     defs.layout = rmlvo->layout;
137     defs.variant = rmlvo->variant;
138     defs.options = rmlvo->options;
139
140     names = XkbComponentsFromRules(context, rmlvo->rules, &defs);
141     if (!names) {
142         ERROR("failed to generate XKB components from rules \"%s\"\n",
143               rmlvo->rules);
144         return NULL;
145     }
146
147     xkb = xkb_map_new_from_kccgst(context, names);
148
149     free(names->keymap);
150     free(names->keycodes);
151     free(names->types);
152     free(names->compat);
153     free(names->symbols);
154     free(names);
155
156     return xkb;
157 }
158
159 static XkbFile *
160 XkbChooseMap(XkbFile *file, const char *name)
161 {
162     XkbFile *map = file;
163
164     /* map specified? */
165     if (name) {
166         while (map) {
167             if (map->name && strcmp(map->name, name) == 0)
168                 break;
169             map = (XkbFile *) map->common.next;
170         }
171
172         if (!map)
173             ERROR("no map named \"%s\" in input file\n", name);
174     }
175     else if (file->common.next) {
176         /* look for map with XkbLC_Default flag. */
177         for (; map; map = (XkbFile *) map->common.next) {
178             if (map->flags & XkbLC_Default)
179                 break;
180         }
181
182         if (!map) {
183             map = file;
184             WARN("no map specified, but components have several\n");
185             WARN("using the first defined map, \"%s\"\n",
186                  map->name ? map->name : "");
187         }
188     }
189
190     return map;
191 }
192
193 static struct xkb_keymap *
194 compile_keymap(struct xkb_context *context, XkbFile *file)
195 {
196     XkbFile *mapToUse;
197     struct xkb_keymap * xkb = NULL;
198
199     /* Find map to use */
200     mapToUse = XkbChooseMap(file, NULL);
201     if (!mapToUse)
202         goto err;
203
204     switch (mapToUse->type) {
205     case XkmSemanticsFile:
206     case XkmLayoutFile:
207     case XkmKeymapFile:
208         break;
209     default:
210         ERROR("file type %d not handled\n", mapToUse->type);
211         goto err;
212     }
213
214     xkb = CompileKeymap(context, mapToUse, MergeReplace);
215     if (!xkb)
216         goto err;
217
218 err:
219     FreeXKBFile(file);
220     XkbcFreeAllAtoms();
221     return xkb;
222 }
223
224 _X_EXPORT struct xkb_keymap *
225 xkb_map_new_from_kccgst(struct xkb_context *context,
226                         const struct xkb_component_names *kccgst)
227 {
228     XkbFile *file;
229
230     if (!kccgst) {
231         ERROR("no components specified\n");
232         return NULL;
233     }
234
235     if (ISEMPTY(kccgst->keycodes)) {
236         ERROR("keycodes required to generate XKB keymap\n");
237         return NULL;
238     }
239
240     if (ISEMPTY(kccgst->compat)) {
241         ERROR("compat map required to generate XKB keymap\n");
242         return NULL;
243     }
244
245     if (ISEMPTY(kccgst->types)) {
246         ERROR("types required to generate XKB keymap\n");
247         return NULL;
248     }
249
250     if (ISEMPTY(kccgst->symbols)) {
251         ERROR("symbols required to generate XKB keymap\n");
252         return NULL;
253     }
254
255     if (!(file = XkbKeymapFileFromComponents(kccgst))) {
256         ERROR("failed to generate parsed XKB file from components\n");
257         return NULL;
258     }
259
260     return compile_keymap(context, file);
261 }
262
263 _X_EXPORT struct xkb_keymap *
264 xkb_map_new_from_string(struct xkb_context *context,
265                         const char *string,
266                         enum xkb_keymap_format format)
267 {
268     XkbFile *file;
269
270     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
271         ERROR("unsupported keymap format %d\n", format);
272         return NULL;
273     }
274
275     if (!string) {
276         ERROR("no string specified to generate XKB keymap\n");
277         return NULL;
278     }
279
280     if (!XKBParseString(string, "input", &file) || !file) {
281         ERROR("failed to parse input xkb file\n");
282         return NULL;
283     }
284
285     return compile_keymap(context, file);
286 }
287
288 _X_EXPORT struct xkb_keymap *
289 xkb_map_new_from_fd(struct xkb_context *context,
290                     int fd,
291                     enum xkb_keymap_format format)
292 {
293     XkbFile *file;
294     FILE *fptr;
295
296     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
297         ERROR("unsupported keymap format %d\n", format);
298         return NULL;
299     }
300
301     if (fd < 0) {
302         ERROR("no file specified to generate XKB keymap\n");
303         return NULL;
304     }
305
306     fptr = fdopen(fd, "r");
307     if (!fptr) {
308         ERROR("couldn't associate fd with file pointer\n");
309         return NULL;
310     }
311
312     if (!XKBParseFile(fptr, "(unknown file)", &file) || !file) {
313         ERROR("failed to parse input xkb file\n");
314         return NULL;
315     }
316
317     return compile_keymap(context, file);
318 }
319
320 _X_EXPORT struct xkb_keymap *
321 xkb_map_ref(struct xkb_keymap *xkb)
322 {
323     xkb->refcnt++;
324     return xkb;
325 }
326
327 _X_EXPORT void
328 xkb_map_unref(struct xkb_keymap *xkb)
329 {
330     if (--xkb->refcnt > 0)
331         return;
332
333     XkbcFreeKeyboard(xkb);
334 }