2 Copyright 2009 Dan Nicholson
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:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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.
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.
27 #include "xkbcomp-priv.h"
29 #include "parseutils.h"
31 /* Global warning level */
32 unsigned int warningLevel = 0;
34 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
37 XkbKeymapFileFromComponents(struct xkb_ctx *ctx,
38 const struct xkb_component_names *ktcsg)
40 XkbFile *keycodes, *types, *compat, *symbols;
43 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
44 keycodes = CreateXKBFile(ctx, XkmKeyNamesIndex, NULL,
45 (ParseCommon *)inc, 0);
47 inc = IncludeCreate(ktcsg->types, MergeDefault);
48 types = CreateXKBFile(ctx, XkmTypesIndex, NULL,
49 (ParseCommon *)inc, 0);
50 AppendStmt(&keycodes->common, &types->common);
52 inc = IncludeCreate(ktcsg->compat, MergeDefault);
53 compat = CreateXKBFile(ctx, XkmCompatMapIndex, NULL,
54 (ParseCommon *)inc, 0);
55 AppendStmt(&keycodes->common, &compat->common);
57 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
58 symbols = CreateXKBFile(ctx, XkmSymbolsIndex, NULL,
59 (ParseCommon *)inc, 0);
60 AppendStmt(&keycodes->common, &symbols->common);
62 return CreateXKBFile(ctx, XkmKeymapFile,
63 ktcsg->keymap ? ktcsg->keymap : strdup(""),
64 &keycodes->common, 0);
67 static struct xkb_component_names *
68 XkbComponentsFromRules(struct xkb_ctx *ctx,
70 const XkbRF_VarDefsPtr defs)
72 FILE *rulesFile = NULL;
73 char *rulesPath = NULL;
74 XkbRF_RulesPtr loaded = NULL;
75 struct xkb_component_names * names = NULL;
78 rulesFile = XkbFindFileInPath(ctx, rules, XkmRulesFile, &rulesPath);
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));
88 if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) {
89 ERROR("failed to allocate XKB rules\n");
93 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
94 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
98 if (!(names = uTypedCalloc(1, struct xkb_component_names))) {
99 ERROR("failed to allocate XKB components\n");
103 if (!XkbcRF_GetComponents(loaded, defs, names)) {
105 free(names->keycodes);
108 free(names->symbols);
111 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
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)
127 XkbRF_VarDefsRec defs;
128 struct xkb_component_names *names;
129 struct xkb_keymap *keymap;
131 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
132 ERROR("rules and layout required to generate XKB keymap\n");
136 defs.model = rmlvo->model;
137 defs.layout = rmlvo->layout;
138 defs.variant = rmlvo->variant;
139 defs.options = rmlvo->options;
141 names = XkbComponentsFromRules(ctx, rmlvo->rules, &defs);
143 ERROR("failed to generate XKB components from rules \"%s\"\n",
148 keymap = xkb_map_new_from_kccgst(ctx, names, 0);
151 free(names->keycodes);
154 free(names->symbols);
161 XkbChooseMap(XkbFile *file, const char *name)
168 if (map->name && strcmp(map->name, name) == 0)
170 map = (XkbFile *) map->common.next;
174 ERROR("no map named \"%s\" in input file\n", name);
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)
185 WARN("no map specified, but components have several\n");
186 WARN("using the first defined map, \"%s\"\n",
187 map->name ? map->name : "");
194 static struct xkb_keymap *
195 compile_keymap(struct xkb_ctx *ctx, XkbFile *file)
198 struct xkb_keymap *keymap = NULL;
200 /* Find map to use */
201 mapToUse = XkbChooseMap(file, NULL);
205 switch (mapToUse->type) {
206 case XkmSemanticsFile:
211 ERROR("file type %d not handled\n", mapToUse->type);
215 keymap = CompileKeymap(ctx, mapToUse);
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)
232 ERROR("no components specified\n");
236 if (ISEMPTY(kccgst->keycodes)) {
237 ERROR("keycodes required to generate XKB keymap\n");
241 if (ISEMPTY(kccgst->compat)) {
242 ERROR("compat map required to generate XKB keymap\n");
246 if (ISEMPTY(kccgst->types)) {
247 ERROR("types required to generate XKB keymap\n");
251 if (ISEMPTY(kccgst->symbols)) {
252 ERROR("symbols required to generate XKB keymap\n");
256 if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
257 ERROR("failed to generate parsed XKB file from components\n");
261 return compile_keymap(ctx, file);
264 _X_EXPORT struct xkb_keymap *
265 xkb_map_new_from_string(struct xkb_ctx *ctx,
267 enum xkb_keymap_format format,
268 enum xkb_map_compile_flags flags)
272 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
273 ERROR("unsupported keymap format %d\n", format);
278 ERROR("no string specified to generate XKB keymap\n");
282 if (!XKBParseString(ctx, string, "input", &file)) {
283 ERROR("failed to parse input xkb file\n");
287 return compile_keymap(ctx, file);
290 _X_EXPORT struct xkb_keymap *
291 xkb_map_new_from_fd(struct xkb_ctx *ctx,
293 enum xkb_keymap_format format,
294 enum xkb_map_compile_flags flags)
299 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
300 ERROR("unsupported keymap format %d\n", format);
305 ERROR("no file specified to generate XKB keymap\n");
309 fptr = fdopen(fd, "r");
311 ERROR("couldn't associate fd with file pointer\n");
315 if (!XKBParseFile(ctx, fptr, "(unknown file)", &file)) {
316 ERROR("failed to parse input xkb file\n");
320 return compile_keymap(ctx, file);
323 _X_EXPORT struct xkb_keymap *
324 xkb_map_ref(struct xkb_keymap *keymap)
331 xkb_map_unref(struct xkb_keymap *keymap)
333 if (--keymap->refcnt > 0)
336 XkbcFreeKeyboard(keymap);