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