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"
32 /* Global warning level */
33 unsigned int warningLevel = 0;
35 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
38 XkbKeymapFileFromComponents(struct xkb_context *ctx,
39 const struct xkb_component_names *ktcsg)
41 XkbFile *keycodes, *types, *compat, *symbols;
44 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
45 keycodes = CreateXKBFile(ctx, XkmKeyNamesIndex, NULL,
46 (ParseCommon *)inc, 0);
48 inc = IncludeCreate(ktcsg->types, MergeDefault);
49 types = CreateXKBFile(ctx, XkmTypesIndex, NULL,
50 (ParseCommon *)inc, 0);
51 AppendStmt(&keycodes->common, &types->common);
53 inc = IncludeCreate(ktcsg->compat, MergeDefault);
54 compat = CreateXKBFile(ctx, XkmCompatMapIndex, NULL,
55 (ParseCommon *)inc, 0);
56 AppendStmt(&keycodes->common, &compat->common);
58 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
59 symbols = CreateXKBFile(ctx, XkmSymbolsIndex, NULL,
60 (ParseCommon *)inc, 0);
61 AppendStmt(&keycodes->common, &symbols->common);
63 return CreateXKBFile(ctx, XkmKeymapFile,
64 ktcsg->keymap ? ktcsg->keymap : strdup(""),
65 &keycodes->common, 0);
68 static struct xkb_component_names *
69 XkbComponentsFromRules(struct xkb_context *ctx,
71 const XkbRF_VarDefsPtr defs)
73 FILE *rulesFile = NULL;
74 char *rulesPath = NULL;
75 XkbRF_RulesPtr loaded = NULL;
76 struct xkb_component_names * names = NULL;
79 rulesFile = XkbFindFileInPath(ctx, rules, XkmRulesFile, &rulesPath);
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));
89 if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) {
90 ERROR("failed to allocate XKB rules\n");
94 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
95 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
99 if (!(names = uTypedCalloc(1, struct xkb_component_names))) {
100 ERROR("failed to allocate XKB components\n");
104 if (!XkbcRF_GetComponents(loaded, defs, names)) {
106 free(names->keycodes);
109 free(names->symbols);
112 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
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)
128 XkbRF_VarDefsRec defs;
129 struct xkb_component_names *names;
130 struct xkb_keymap *keymap;
132 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
133 ERROR("rules and layout required to generate XKB keymap\n");
137 defs.model = rmlvo->model;
138 defs.layout = rmlvo->layout;
139 defs.variant = rmlvo->variant;
140 defs.options = rmlvo->options;
142 names = XkbComponentsFromRules(ctx, rmlvo->rules, &defs);
144 ERROR("failed to generate XKB components from rules \"%s\"\n",
149 keymap = xkb_map_new_from_kccgst(ctx, names, 0);
152 free(names->keycodes);
155 free(names->symbols);
162 XkbChooseMap(XkbFile *file, const char *name)
169 if (map->name && strcmp(map->name, name) == 0)
171 map = (XkbFile *) map->common.next;
175 ERROR("no map named \"%s\" in input file\n", name);
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)
186 WARN("no map specified, but components have several\n");
187 WARN("using the first defined map, \"%s\"\n",
188 map->name ? map->name : "");
195 static struct xkb_keymap *
196 compile_keymap(struct xkb_context *ctx, XkbFile *file)
199 struct xkb_keymap *keymap = NULL;
201 /* Find map to use */
202 mapToUse = XkbChooseMap(file, NULL);
206 if (mapToUse->type != XkmKeymapFile) {
207 ERROR("file type %d not handled\n", mapToUse->type);
211 keymap = CompileKeymap(ctx, mapToUse);
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)
228 ERROR("no components specified\n");
232 if (ISEMPTY(kccgst->keycodes)) {
233 ERROR("keycodes required to generate XKB keymap\n");
237 if (ISEMPTY(kccgst->compat)) {
238 ERROR("compat map required to generate XKB keymap\n");
242 if (ISEMPTY(kccgst->types)) {
243 ERROR("types required to generate XKB keymap\n");
247 if (ISEMPTY(kccgst->symbols)) {
248 ERROR("symbols required to generate XKB keymap\n");
252 if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
253 ERROR("failed to generate parsed XKB file from components\n");
257 return compile_keymap(ctx, file);
260 _X_EXPORT struct xkb_keymap *
261 xkb_map_new_from_string(struct xkb_context *ctx,
263 enum xkb_keymap_format format,
264 enum xkb_map_compile_flags flags)
268 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
269 ERROR("unsupported keymap format %d\n", format);
274 ERROR("no string specified to generate XKB keymap\n");
278 if (!XKBParseString(ctx, string, "input", &file)) {
279 ERROR("failed to parse input xkb file\n");
283 return compile_keymap(ctx, file);
286 _X_EXPORT struct xkb_keymap *
287 xkb_map_new_from_file(struct xkb_context *ctx,
289 enum xkb_keymap_format format,
290 enum xkb_map_compile_flags flags)
294 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
295 ERROR("unsupported keymap format %d\n", format);
300 ERROR("no file specified to generate XKB keymap\n");
304 if (!XKBParseFile(ctx, file, "(unknown file)", &xkb_file)) {
305 ERROR("failed to parse input xkb file\n");
309 return compile_keymap(ctx, xkb_file);
312 _X_EXPORT struct xkb_keymap *
313 xkb_map_ref(struct xkb_keymap *keymap)
320 xkb_map_unref(struct xkb_keymap *keymap)
322 if (--keymap->refcnt > 0)
325 XkbcFreeKeyboard(keymap);