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(const struct xkb_component_names * ktcsg)
39 XkbFile *keycodes, *types, *compat, *symbols;
42 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
43 keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
45 inc = IncludeCreate(ktcsg->types, MergeDefault);
46 types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
47 AppendStmt(&keycodes->common, &types->common);
49 inc = IncludeCreate(ktcsg->compat, MergeDefault);
50 compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
51 AppendStmt(&keycodes->common, &compat->common);
53 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
54 symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
55 AppendStmt(&keycodes->common, &symbols->common);
57 return CreateXKBFile(XkmKeymapFile,
58 ktcsg->keymap ? ktcsg->keymap : strdup(""),
59 &keycodes->common, 0);
62 static struct xkb_component_names *
63 XkbComponentsFromRules(struct xkb_context *context,
65 const XkbRF_VarDefsPtr defs)
67 FILE *rulesFile = NULL;
68 char *rulesPath = NULL;
69 XkbRF_RulesPtr loaded = NULL;
70 struct xkb_component_names * names = NULL;
73 rulesFile = XkbFindFileInPath(context, rules, XkmRulesFile, &rulesPath);
75 ERROR("could not find \"%s\" rules in XKB path\n", rules);
76 ERROR("%d include paths searched:\n",
77 xkb_context_num_include_paths(context));
78 for (i = 0; i < xkb_context_num_include_paths(context); i++)
79 ERROR("\t%s\n", xkb_context_include_path_get(context, i));
83 if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) {
84 ERROR("failed to allocate XKB rules\n");
88 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
89 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
93 if (!(names = uTypedCalloc(1, struct xkb_component_names))) {
94 ERROR("failed to allocate XKB components\n");
98 if (!XkbcRF_GetComponents(loaded, defs, names)) {
100 free(names->keycodes);
103 free(names->symbols);
106 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
117 _X_EXPORT struct xkb_keymap *
118 xkb_map_new_from_names(struct xkb_context *context,
119 const struct xkb_rule_names *rmlvo)
121 XkbRF_VarDefsRec defs;
122 struct xkb_component_names *names;
123 struct xkb_keymap *xkb;
125 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
126 ERROR("rules and layout required to generate XKB keymap\n");
130 defs.model = rmlvo->model;
131 defs.layout = rmlvo->layout;
132 defs.variant = rmlvo->variant;
133 defs.options = rmlvo->options;
135 names = XkbComponentsFromRules(context, rmlvo->rules, &defs);
137 ERROR("failed to generate XKB components from rules \"%s\"\n",
142 xkb = xkb_map_new_from_kccgst(context, names);
145 free(names->keycodes);
148 free(names->symbols);
155 XkbChooseMap(XkbFile *file, const char *name)
162 if (map->name && strcmp(map->name, name) == 0)
164 map = (XkbFile *) map->common.next;
168 ERROR("no map named \"%s\" in input file\n", name);
170 else if (file->common.next) {
171 /* look for map with XkbLC_Default flag. */
172 for (; map; map = (XkbFile *) map->common.next) {
173 if (map->flags & XkbLC_Default)
179 WARN("no map specified, but components have several\n");
180 WARN("using the first defined map, \"%s\"\n",
181 map->name ? map->name : "");
188 static struct xkb_keymap *
189 compile_keymap(struct xkb_context *context, XkbFile *file)
192 struct xkb_keymap * xkb = NULL;
194 /* Find map to use */
195 mapToUse = XkbChooseMap(file, NULL);
199 switch (mapToUse->type) {
200 case XkmSemanticsFile:
205 ERROR("file type %d not handled\n", mapToUse->type);
209 xkb = CompileKeymap(context, mapToUse);
219 _X_EXPORT struct xkb_keymap *
220 xkb_map_new_from_kccgst(struct xkb_context *context,
221 const struct xkb_component_names *kccgst)
226 ERROR("no components specified\n");
230 if (ISEMPTY(kccgst->keycodes)) {
231 ERROR("keycodes required to generate XKB keymap\n");
235 if (ISEMPTY(kccgst->compat)) {
236 ERROR("compat map required to generate XKB keymap\n");
240 if (ISEMPTY(kccgst->types)) {
241 ERROR("types required to generate XKB keymap\n");
245 if (ISEMPTY(kccgst->symbols)) {
246 ERROR("symbols required to generate XKB keymap\n");
250 if (!(file = XkbKeymapFileFromComponents(kccgst))) {
251 ERROR("failed to generate parsed XKB file from components\n");
255 return compile_keymap(context, file);
258 _X_EXPORT struct xkb_keymap *
259 xkb_map_new_from_string(struct xkb_context *context,
261 enum xkb_keymap_format format)
265 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
266 ERROR("unsupported keymap format %d\n", format);
271 ERROR("no string specified to generate XKB keymap\n");
275 if (!XKBParseString(string, "input", &file) || !file) {
276 ERROR("failed to parse input xkb file\n");
280 return compile_keymap(context, file);
283 _X_EXPORT struct xkb_keymap *
284 xkb_map_new_from_fd(struct xkb_context *context,
286 enum xkb_keymap_format format)
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(fptr, "(unknown file)", &file) || !file) {
308 ERROR("failed to parse input xkb file\n");
312 return compile_keymap(context, file);
315 _X_EXPORT struct xkb_keymap *
316 xkb_map_ref(struct xkb_keymap *xkb)
323 xkb_map_unref(struct xkb_keymap *xkb)
325 if (--xkb->refcnt > 0)
328 XkbcFreeKeyboard(xkb);