a18451fdb25bcfb25e7b8dc36e55f0b556fe9a42
[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 || mapToUse->type != XkmKeymapFile) {
203         ERROR("file type %d not handled\n", mapToUse->type);
204         goto err;
205     }
206
207     keymap = CompileKeymap(ctx, mapToUse);
208     if (!keymap)
209         goto err;
210
211 err:
212     FreeXKBFile(file);
213     return keymap;
214 }
215
216 _X_EXPORT struct xkb_keymap *
217 xkb_map_new_from_kccgst(struct xkb_ctx *ctx,
218                         const struct xkb_component_names *kccgst,
219                         enum xkb_map_compile_flags flags)
220 {
221     XkbFile *file;
222
223     if (!kccgst) {
224         ERROR("no components specified\n");
225         return NULL;
226     }
227
228     if (ISEMPTY(kccgst->keycodes)) {
229         ERROR("keycodes required to generate XKB keymap\n");
230         return NULL;
231     }
232
233     if (ISEMPTY(kccgst->compat)) {
234         ERROR("compat map required to generate XKB keymap\n");
235         return NULL;
236     }
237
238     if (ISEMPTY(kccgst->types)) {
239         ERROR("types required to generate XKB keymap\n");
240         return NULL;
241     }
242
243     if (ISEMPTY(kccgst->symbols)) {
244         ERROR("symbols required to generate XKB keymap\n");
245         return NULL;
246     }
247
248     if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
249         ERROR("failed to generate parsed XKB file from components\n");
250         return NULL;
251     }
252
253     return compile_keymap(ctx, file);
254 }
255
256 _X_EXPORT struct xkb_keymap *
257 xkb_map_new_from_string(struct xkb_ctx *ctx,
258                         const char *string,
259                         enum xkb_keymap_format format,
260                         enum xkb_map_compile_flags flags)
261 {
262     XkbFile *file;
263
264     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
265         ERROR("unsupported keymap format %d\n", format);
266         return NULL;
267     }
268
269     if (!string) {
270         ERROR("no string specified to generate XKB keymap\n");
271         return NULL;
272     }
273
274     if (!XKBParseString(ctx, string, "input", &file)) {
275         ERROR("failed to parse input xkb file\n");
276         return NULL;
277     }
278
279     return compile_keymap(ctx, file);
280 }
281
282 _X_EXPORT struct xkb_keymap *
283 xkb_map_new_from_fd(struct xkb_ctx *ctx,
284                     int fd,
285                     enum xkb_keymap_format format,
286                     enum xkb_map_compile_flags flags)
287 {
288     XkbFile *file;
289     FILE *fptr;
290
291     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
292         ERROR("unsupported keymap format %d\n", format);
293         return NULL;
294     }
295
296     if (fd < 0) {
297         ERROR("no file specified to generate XKB keymap\n");
298         return NULL;
299     }
300
301     fptr = fdopen(fd, "r");
302     if (!fptr) {
303         ERROR("couldn't associate fd with file pointer\n");
304         return NULL;
305     }
306
307     if (!XKBParseFile(ctx, fptr, "(unknown file)", &file)) {
308         ERROR("failed to parse input xkb file\n");
309         return NULL;
310     }
311
312     return compile_keymap(ctx, file);
313 }
314
315 _X_EXPORT struct xkb_keymap *
316 xkb_map_ref(struct xkb_keymap *keymap)
317 {
318     keymap->refcnt++;
319     return keymap;
320 }
321
322 _X_EXPORT void
323 xkb_map_unref(struct xkb_keymap *keymap)
324 {
325     if (--keymap->refcnt > 0)
326         return;
327
328     XkbcFreeKeyboard(keymap);
329 }