Shorten context to ctx
[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 "xkbcomp-priv.h"
28 #include "rules.h"
29 #include "parseutils.h"
30
31 /* Global warning level */
32 unsigned int warningLevel = 0;
33
34 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
35
36 static XkbFile *
37 XkbKeymapFileFromComponents(struct xkb_ctx *ctx,
38                             const struct xkb_component_names *ktcsg)
39 {
40     XkbFile *keycodes, *types, *compat, *symbols;
41     IncludeStmt *inc;
42
43     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
44     keycodes = CreateXKBFile(ctx, XkmKeyNamesIndex, NULL,
45                              (ParseCommon *)inc, 0);
46
47     inc = IncludeCreate(ktcsg->types, MergeDefault);
48     types = CreateXKBFile(ctx, XkmTypesIndex, NULL,
49                           (ParseCommon *)inc, 0);
50     AppendStmt(&keycodes->common, &types->common);
51
52     inc = IncludeCreate(ktcsg->compat, MergeDefault);
53     compat = CreateXKBFile(ctx, XkmCompatMapIndex, NULL,
54                            (ParseCommon *)inc, 0);
55     AppendStmt(&keycodes->common, &compat->common);
56
57     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
58     symbols = CreateXKBFile(ctx, XkmSymbolsIndex, NULL,
59                             (ParseCommon *)inc, 0);
60     AppendStmt(&keycodes->common, &symbols->common);
61
62     return CreateXKBFile(ctx, XkmKeymapFile,
63                          ktcsg->keymap ? ktcsg->keymap : strdup(""),
64                          &keycodes->common, 0);
65 }
66
67 static struct xkb_component_names *
68 XkbComponentsFromRules(struct xkb_ctx *ctx,
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(ctx, 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_ctx_num_include_paths(ctx));
83         for (i = 0; i < xkb_ctx_num_include_paths(ctx); i++)
84             ERROR("\t%s\n", xkb_ctx_include_path_get(ctx, 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_ctx *ctx,
124                        const struct xkb_rule_names *rmlvo,
125                        enum xkb_map_compile_flags flags)
126 {
127     XkbRF_VarDefsRec defs;
128     struct xkb_component_names *names;
129     struct xkb_keymap *keymap;
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(ctx, 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     keymap = xkb_map_new_from_kccgst(ctx, names, 0);
149
150     free(names->keymap);
151     free(names->keycodes);
152     free(names->types);
153     free(names->compat);
154     free(names->symbols);
155     free(names);
156
157     return keymap;
158 }
159
160 static XkbFile *
161 XkbChooseMap(XkbFile *file, const char *name)
162 {
163     XkbFile *map = file;
164
165     /* map specified? */
166     if (name) {
167         while (map) {
168             if (map->name && strcmp(map->name, name) == 0)
169                 break;
170             map = (XkbFile *) map->common.next;
171         }
172
173         if (!map)
174             ERROR("no map named \"%s\" in input file\n", name);
175     }
176     else if (file->common.next) {
177         /* look for map with XkbLC_Default flag. */
178         for (; map; map = (XkbFile *) map->common.next) {
179             if (map->flags & XkbLC_Default)
180                 break;
181         }
182
183         if (!map) {
184             map = file;
185             WARN("no map specified, but components have several\n");
186             WARN("using the first defined map, \"%s\"\n",
187                  map->name ? map->name : "");
188         }
189     }
190
191     return map;
192 }
193
194 static struct xkb_keymap *
195 compile_keymap(struct xkb_ctx *ctx, XkbFile *file)
196 {
197     XkbFile *mapToUse;
198     struct xkb_keymap *keymap = NULL;
199
200     /* Find map to use */
201     mapToUse = XkbChooseMap(file, NULL);
202     if (!mapToUse)
203         goto err;
204
205     switch (mapToUse->type) {
206     case XkmSemanticsFile:
207     case XkmLayoutFile:
208     case XkmKeymapFile:
209         break;
210     default:
211         ERROR("file type %d not handled\n", mapToUse->type);
212         goto err;
213     }
214
215     keymap = CompileKeymap(ctx, mapToUse);
216     if (!keymap)
217         goto err;
218
219 err:
220     FreeXKBFile(file);
221     return keymap;
222 }
223
224 _X_EXPORT struct xkb_keymap *
225 xkb_map_new_from_kccgst(struct xkb_ctx *ctx,
226                         const struct xkb_component_names *kccgst,
227                         enum xkb_map_compile_flags flags)
228 {
229     XkbFile *file;
230
231     if (!kccgst) {
232         ERROR("no components specified\n");
233         return NULL;
234     }
235
236     if (ISEMPTY(kccgst->keycodes)) {
237         ERROR("keycodes required to generate XKB keymap\n");
238         return NULL;
239     }
240
241     if (ISEMPTY(kccgst->compat)) {
242         ERROR("compat map required to generate XKB keymap\n");
243         return NULL;
244     }
245
246     if (ISEMPTY(kccgst->types)) {
247         ERROR("types required to generate XKB keymap\n");
248         return NULL;
249     }
250
251     if (ISEMPTY(kccgst->symbols)) {
252         ERROR("symbols required to generate XKB keymap\n");
253         return NULL;
254     }
255
256     if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
257         ERROR("failed to generate parsed XKB file from components\n");
258         return NULL;
259     }
260
261     return compile_keymap(ctx, file);
262 }
263
264 _X_EXPORT struct xkb_keymap *
265 xkb_map_new_from_string(struct xkb_ctx *ctx,
266                         const char *string,
267                         enum xkb_keymap_format format,
268                         enum xkb_map_compile_flags flags)
269 {
270     XkbFile *file;
271
272     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
273         ERROR("unsupported keymap format %d\n", format);
274         return NULL;
275     }
276
277     if (!string) {
278         ERROR("no string specified to generate XKB keymap\n");
279         return NULL;
280     }
281
282     if (!XKBParseString(ctx, string, "input", &file)) {
283         ERROR("failed to parse input xkb file\n");
284         return NULL;
285     }
286
287     return compile_keymap(ctx, file);
288 }
289
290 _X_EXPORT struct xkb_keymap *
291 xkb_map_new_from_fd(struct xkb_ctx *ctx,
292                     int fd,
293                     enum xkb_keymap_format format,
294                     enum xkb_map_compile_flags flags)
295 {
296     XkbFile *file;
297     FILE *fptr;
298
299     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
300         ERROR("unsupported keymap format %d\n", format);
301         return NULL;
302     }
303
304     if (fd < 0) {
305         ERROR("no file specified to generate XKB keymap\n");
306         return NULL;
307     }
308
309     fptr = fdopen(fd, "r");
310     if (!fptr) {
311         ERROR("couldn't associate fd with file pointer\n");
312         return NULL;
313     }
314
315     if (!XKBParseFile(ctx, fptr, "(unknown file)", &file)) {
316         ERROR("failed to parse input xkb file\n");
317         return NULL;
318     }
319
320     return compile_keymap(ctx, file);
321 }
322
323 _X_EXPORT struct xkb_keymap *
324 xkb_map_ref(struct xkb_keymap *keymap)
325 {
326     keymap->refcnt++;
327     return keymap;
328 }
329
330 _X_EXPORT void
331 xkb_map_unref(struct xkb_keymap *keymap)
332 {
333     if (--keymap->refcnt > 0)
334         return;
335
336     XkbcFreeKeyboard(keymap);
337 }