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.
32 #include "parseutils.h"
35 /* Global debugging flags */
36 unsigned int debugFlags = 0;
37 unsigned int warningLevel = 0;
39 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
42 XkbKeymapFileFromComponents(const struct xkb_component_names * ktcsg)
44 XkbFile *keycodes, *types, *compat, *symbols, *geometry;
48 ERROR("no components to generate keymap file from\n");
52 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
53 keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
55 inc = IncludeCreate(ktcsg->types, MergeDefault);
56 types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
57 AppendStmt(&keycodes->common, &types->common);
59 inc = IncludeCreate(ktcsg->compat, MergeDefault);
60 compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
61 AppendStmt(&keycodes->common, &compat->common);
63 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
64 symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
65 AppendStmt(&keycodes->common, &symbols->common);
67 inc = IncludeCreate(ktcsg->geometry, MergeDefault);
68 geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
69 AppendStmt(&keycodes->common, &geometry->common);
71 return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : strdup(""),
72 &keycodes->common, 0);
75 static struct xkb_component_names *
76 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
78 FILE *rulesFile = NULL;
79 char *rulesPath = NULL;
80 XkbRF_RulesPtr loaded = NULL;
81 struct xkb_component_names * names = NULL;
83 rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
85 ERROR("could not find \"%s\" rules in XKB path\n", rules);
89 if (!(loaded = _XkbTypedCalloc(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 = _XkbTypedCalloc(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);
110 free(names->geometry);
113 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
125 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rmlvo)
127 XkbRF_VarDefsRec defs;
128 struct xkb_component_names * names;
129 struct xkb_desc * xkb;
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(rmlvo->rules, &defs);
143 ERROR("failed to generate XKB components from rules \"%s\"\n",
148 xkb = xkb_compile_keymap_from_components(names);
151 free(names->keycodes);
154 free(names->symbols);
155 free(names->geometry);
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 : "");
196 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg)
198 XkbFile *file, *mapToUse;
199 struct xkb_desc * xkb = NULL;
203 if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
204 ERROR("keycodes required to generate XKB keymap\n");
208 if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
209 ERROR("failed to generate parsed XKB file from components\n");
213 /* Find map to use */
214 if (!(mapToUse = XkbChooseMap(file, NULL)))
217 /* Compile the keyboard */
218 if (!(xkb = XkbcAllocKeyboard())) {
219 ERROR("could not allocate keyboard description\n");
223 if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
224 ERROR("failed to compile keymap\n");
225 XkbcFreeKeyboard(xkb);
236 static struct xkb_desc *
237 compile_keymap(XkbFile *file, const char *mapName)
240 struct xkb_desc * xkb = NULL;
242 /* Find map to use */
243 if (!(mapToUse = XkbChooseMap(file, mapName)))
246 switch (mapToUse->type) {
247 case XkmSemanticsFile:
252 ERROR("file type %d not handled\n", mapToUse->type);
256 /* Compile the keyboard */
257 if (!(xkb = XkbcAllocKeyboard())) {
258 ERROR("could not allocate keyboard description\n");
262 if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
263 ERROR("failed to compile keymap\n");
264 XkbcFreeKeyboard(xkb);
275 xkb_compile_keymap_from_string(const char *string, const char *mapName)
280 ERROR("no string specified to generate XKB keymap\n");
284 setScanState("input", 1);
285 if (!XKBParseString(string, &file) || !file) {
286 ERROR("failed to parse input xkb file\n");
290 return compile_keymap(file, mapName);
294 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
299 ERROR("no file specified to generate XKB keymap\n");
303 setScanState("input", 1);
304 if (!XKBParseFile(inputFile, &file) || !file) {
305 ERROR("failed to parse input xkb file\n");
309 return compile_keymap(file, mapName);
313 xkb_free_keymap(struct xkb_desc *xkb)
315 XkbcFreeKeyboard(xkb);