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