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);
202 if (!mapToUse || mapToUse->type != XkmKeymapFile) {
203 ERROR("file type %d not handled\n", mapToUse->type);
207 keymap = CompileKeymap(ctx, mapToUse);
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)
224 ERROR("no components specified\n");
228 if (ISEMPTY(kccgst->keycodes)) {
229 ERROR("keycodes required to generate XKB keymap\n");
233 if (ISEMPTY(kccgst->compat)) {
234 ERROR("compat map required to generate XKB keymap\n");
238 if (ISEMPTY(kccgst->types)) {
239 ERROR("types required to generate XKB keymap\n");
243 if (ISEMPTY(kccgst->symbols)) {
244 ERROR("symbols required to generate XKB keymap\n");
248 if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
249 ERROR("failed to generate parsed XKB file from components\n");
253 return compile_keymap(ctx, file);
256 _X_EXPORT struct xkb_keymap *
257 xkb_map_new_from_string(struct xkb_ctx *ctx,
259 enum xkb_keymap_format format,
260 enum xkb_map_compile_flags flags)
264 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
265 ERROR("unsupported keymap format %d\n", format);
270 ERROR("no string specified to generate XKB keymap\n");
274 if (!XKBParseString(ctx, string, "input", &file)) {
275 ERROR("failed to parse input xkb file\n");
279 return compile_keymap(ctx, file);
282 _X_EXPORT struct xkb_keymap *
283 xkb_map_new_from_fd(struct xkb_ctx *ctx,
285 enum xkb_keymap_format format,
286 enum xkb_map_compile_flags flags)
291 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
292 ERROR("unsupported keymap format %d\n", format);
297 ERROR("no file specified to generate XKB keymap\n");
301 fptr = fdopen(fd, "r");
303 ERROR("couldn't associate fd with file pointer\n");
307 if (!XKBParseFile(ctx, fptr, "(unknown file)", &file)) {
308 ERROR("failed to parse input xkb file\n");
312 return compile_keymap(ctx, file);
315 _X_EXPORT struct xkb_keymap *
316 xkb_map_ref(struct xkb_keymap *keymap)
323 xkb_map_unref(struct xkb_keymap *keymap)
325 if (--keymap->refcnt > 0)
328 XkbcFreeKeyboard(keymap);