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.
28 #include "X11/extensions/XKBcommon.h"
29 #include <X11/extensions/XKM.h>
30 #include "XKBcommonint.h"
32 #include "parseutils.h"
35 #ifndef DFLT_XKB_CONFIG_ROOT
36 #define DFLT_XKB_CONFIG_ROOT "/usr/share/X11/xkb"
39 /* Global debugging flags */
40 unsigned int debugFlags = 0;
41 unsigned int warningLevel = 0;
43 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
46 XkbKeymapFileFromComponents(const XkbComponentNamesPtr ktcsg)
48 XkbFile *keycodes, *types, *compat, *symbols, *geometry;
52 ERROR("no components to generate keymap file from\n");
56 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
57 keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
59 inc = IncludeCreate(ktcsg->types, MergeDefault);
60 types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
61 AppendStmt(&keycodes->common, &types->common);
63 inc = IncludeCreate(ktcsg->compat, MergeDefault);
64 compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
65 AppendStmt(&keycodes->common, &compat->common);
67 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
68 symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
69 AppendStmt(&keycodes->common, &symbols->common);
71 inc = IncludeCreate(ktcsg->geometry, MergeDefault);
72 geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
73 AppendStmt(&keycodes->common, &geometry->common);
75 return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : "",
76 &keycodes->common, 0);
79 static XkbComponentNamesPtr
80 XkbComponentsFromRules(const char *rulesPath, const XkbRF_VarDefsPtr defs)
83 XkbComponentNamesPtr names = NULL;
85 if (!(rules = XkbcRF_Load((char *)rulesPath, NULL, False, True))) {
86 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
90 if (!(names = _XkbTypedCalloc(1, XkbComponentNamesRec))) {
91 ERROR("failed to allocate XKB components\n");
95 if (!XkbcRF_GetComponents(rules, defs, names)) {
96 _XkbFree(names->keymap);
97 _XkbFree(names->keycodes);
98 _XkbFree(names->types);
99 _XkbFree(names->compat);
100 _XkbFree(names->symbols);
101 _XkbFree(names->geometry);
104 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
108 XkbcRF_Free(rules, True);
114 XkbcCompileKeymapFromRules(const char *rules, XkbRF_VarDefsPtr defs)
116 char rulesPath[PATH_MAX];
118 XkbComponentNamesPtr names;
121 if (ISEMPTY(rules) || !defs || ISEMPTY(defs->layout)) {
122 ERROR("rules and layout required to generate XKB keymap\n");
126 pathlen = snprintf(rulesPath, sizeof(rulesPath),
127 DFLT_XKB_CONFIG_ROOT "/rules/%s", rules);
128 if (pathlen >= sizeof(rulesPath)) {
129 ERROR("XKB rules path truncated\n");
133 names = XkbComponentsFromRules(rulesPath, defs);
135 ERROR("failed to generate XKB components from rules \"%s\"\n",
140 xkb = XkbcCompileKeymapFromComponents(names);
142 _XkbFree(names->keymap);
143 _XkbFree(names->keycodes);
144 _XkbFree(names->types);
145 _XkbFree(names->compat);
146 _XkbFree(names->symbols);
147 _XkbFree(names->geometry);
154 XkbcCompileKeymapFromComponents(XkbComponentNamesPtr ktcsg)
156 XkbFile *file, *mapToUse;
159 if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
160 ERROR("keycodes required to generate XKB keymap\n");
164 if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
165 ERROR("failed to generate parsed XKB file from components\n");
169 /* Find map to use */
171 if (file->common.next) {
172 for (; mapToUse; mapToUse = (XkbFile *)mapToUse->common.next) {
173 if (mapToUse->flags & XkbLC_Default)
178 WARN("no map specified, but components have several\n");
182 /* Compile the keyboard */
183 if (!(xkb = XkbcAllocKeyboard())) {
184 ERROR("could not allocate keyboard description\n");
188 if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
189 ERROR("failed to compile keymap\n");
195 XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
197 /* XXX: here's where we would free the XkbFile */