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.
31 #include <X11/extensions/XKM.h>
33 #include "parseutils.h"
36 /* Global debugging flags */
37 unsigned int debugFlags = 0;
38 unsigned int warningLevel = 0;
40 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
43 XkbKeymapFileFromComponents(const XkbComponentNamesPtr ktcsg)
45 XkbFile *keycodes, *types, *compat, *symbols, *geometry;
49 ERROR("no components to generate keymap file from\n");
53 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
54 keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
56 inc = IncludeCreate(ktcsg->types, MergeDefault);
57 types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
58 AppendStmt(&keycodes->common, &types->common);
60 inc = IncludeCreate(ktcsg->compat, MergeDefault);
61 compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
62 AppendStmt(&keycodes->common, &compat->common);
64 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
65 symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
66 AppendStmt(&keycodes->common, &symbols->common);
68 inc = IncludeCreate(ktcsg->geometry, MergeDefault);
69 geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
70 AppendStmt(&keycodes->common, &geometry->common);
72 return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : "",
73 &keycodes->common, 0);
76 static XkbComponentNamesPtr
77 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
81 XkbRF_RulesPtr loaded;
82 XkbComponentNamesPtr names = NULL;
84 rulesFile = XkbFindFileInPath((char *)rules, XkmRulesFile, &rulesPath);
86 ERROR("could not find \"%s\" rules in XKB path\n", rules);
90 if (!(loaded = _XkbTypedCalloc(1, XkbRF_RulesRec))) {
91 ERROR("failed to allocate XKB rules\n");
95 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
96 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
100 if (!(names = _XkbTypedCalloc(1, XkbComponentNamesRec))) {
101 ERROR("failed to allocate XKB components\n");
105 if (!XkbcRF_GetComponents(loaded, defs, names)) {
106 _XkbFree(names->keymap);
107 _XkbFree(names->keycodes);
108 _XkbFree(names->types);
109 _XkbFree(names->compat);
110 _XkbFree(names->symbols);
111 _XkbFree(names->geometry);
114 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
118 XkbcRF_Free(loaded, True);
127 XkbcCompileKeymapFromRules(const XkbRMLVOSet *rmlvo)
129 XkbRF_VarDefsRec defs;
130 XkbComponentNamesPtr names;
133 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
134 ERROR("rules and layout required to generate XKB keymap\n");
138 defs.model = rmlvo->model;
139 defs.layout = rmlvo->layout;
140 defs.variant = rmlvo->variant;
141 defs.options = rmlvo->options;
143 names = XkbComponentsFromRules(rmlvo->rules, &defs);
145 ERROR("failed to generate XKB components from rules \"%s\"\n",
150 xkb = XkbcCompileKeymapFromComponents(names);
152 _XkbFree(names->keymap);
153 _XkbFree(names->keycodes);
154 _XkbFree(names->types);
155 _XkbFree(names->compat);
156 _XkbFree(names->symbols);
157 _XkbFree(names->geometry);
164 XkbChooseMap(XkbFile *file, const char *name)
171 if (map->name && strcmp(map->name, name) == 0)
173 map = (XkbFile *) map->common.next;
177 ERROR("no map named \"%s\" in input file\n", name);
179 else if (file->common.next) {
180 /* look for map with XkbLC_Default flag. */
181 for (; map; map = (XkbFile *) map->common.next) {
182 if (map->flags & XkbLC_Default)
188 WARN("no map specified, but components have several\n");
189 WARN("using the first defined map, \"%s\"\n",
190 map->name ? map->name : "");
198 XkbcCompileKeymapFromComponents(const XkbComponentNamesPtr ktcsg)
200 XkbFile *file, *mapToUse;
205 if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
206 ERROR("keycodes required to generate XKB keymap\n");
210 if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
211 ERROR("failed to generate parsed XKB file from components\n");
215 /* Find map to use */
216 if (!(mapToUse = XkbChooseMap(file, NULL)))
219 /* Compile the keyboard */
220 if (!(xkb = XkbcAllocKeyboard())) {
221 ERROR("could not allocate keyboard description\n");
225 if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
226 ERROR("failed to compile keymap\n");
232 XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
234 /* XXX: here's where we would free the XkbFile */
240 XkbcCompileKeymapFromFile(FILE *inputFile, const char *mapName)
242 XkbFile *file, *mapToUse;
246 ERROR("no file specified to generate XKB keymap\n");
250 setScanState("input", 1);
251 if (!XKBParseFile(inputFile, &file) || !file) {
252 ERROR("failed to parse input xkb file\n");
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 */