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 static XkbRF_RulesPtr loaded = NULL;
81 static char *cached_name = NULL;
82 struct xkb_component_names * names = NULL;
84 if (!cached_name || strcmp(rules, cached_name) != 0) {
86 XkbcRF_Free(loaded, True);
93 rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
95 ERROR("could not find \"%s\" rules in XKB path\n", rules);
99 if (!(loaded = _XkbTypedCalloc(1, XkbRF_RulesRec))) {
100 ERROR("failed to allocate XKB rules\n");
104 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
105 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
109 cached_name = strdup(rules);
112 if (!(names = _XkbTypedCalloc(1, struct xkb_component_names))) {
113 ERROR("failed to allocate XKB components\n");
117 if (!XkbcRF_GetComponents(loaded, defs, names)) {
119 free(names->keycodes);
122 free(names->symbols);
123 free(names->geometry);
126 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
138 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rmlvo)
140 XkbRF_VarDefsRec defs;
141 struct xkb_component_names * names;
142 struct xkb_desc * xkb;
144 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
145 ERROR("rules and layout required to generate XKB keymap\n");
149 defs.model = rmlvo->model;
150 defs.layout = rmlvo->layout;
151 defs.variant = rmlvo->variant;
152 defs.options = rmlvo->options;
154 names = XkbComponentsFromRules(rmlvo->rules, &defs);
156 ERROR("failed to generate XKB components from rules \"%s\"\n",
161 xkb = xkb_compile_keymap_from_components(names);
164 free(names->keycodes);
167 free(names->symbols);
168 free(names->geometry);
175 XkbChooseMap(XkbFile *file, const char *name)
182 if (map->name && strcmp(map->name, name) == 0)
184 map = (XkbFile *) map->common.next;
188 ERROR("no map named \"%s\" in input file\n", name);
190 else if (file->common.next) {
191 /* look for map with XkbLC_Default flag. */
192 for (; map; map = (XkbFile *) map->common.next) {
193 if (map->flags & XkbLC_Default)
199 WARN("no map specified, but components have several\n");
200 WARN("using the first defined map, \"%s\"\n",
201 map->name ? map->name : "");
209 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg)
211 XkbFile *file, *mapToUse;
212 struct xkb_desc * xkb;
216 if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
217 ERROR("keycodes required to generate XKB keymap\n");
221 if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
222 ERROR("failed to generate parsed XKB file from components\n");
226 /* Find map to use */
227 if (!(mapToUse = XkbChooseMap(file, NULL)))
230 /* Compile the keyboard */
231 if (!(xkb = XkbcAllocKeyboard())) {
232 ERROR("could not allocate keyboard description\n");
236 if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
237 ERROR("failed to compile keymap\n");
243 XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
245 /* XXX: here's where we would free the XkbFile */
250 static struct xkb_desc *
251 compile_keymap(XkbFile *file, const char *mapName)
254 struct xkb_desc * xkb;
256 /* Find map to use */
257 if (!(mapToUse = XkbChooseMap(file, mapName)))
260 switch (mapToUse->type) {
261 case XkmSemanticsFile:
266 ERROR("file type %d not handled\n", mapToUse->type);
270 /* Compile the keyboard */
271 if (!(xkb = XkbcAllocKeyboard())) {
272 ERROR("could not allocate keyboard description\n");
276 if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
277 ERROR("failed to compile keymap\n");
283 XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
285 /* XXX: here's where we would free the XkbFile */
291 xkb_compile_keymap_from_string(const char *string, const char *mapName)
296 ERROR("no string specified to generate XKB keymap\n");
300 setScanState("input", 1);
301 if (!XKBParseString(string, &file) || !file) {
302 ERROR("failed to parse input xkb file\n");
306 return compile_keymap(file, mapName);
310 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
315 ERROR("no file specified to generate XKB keymap\n");
319 setScanState("input", 1);
320 if (!XKBParseFile(inputFile, &file) || !file) {
321 ERROR("failed to parse input xkb file\n");
325 return compile_keymap(file, mapName);
329 xkb_free_keymap(struct xkb_desc *xkb)
331 XkbcFreeKeyboard(xkb, 0, True);