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.
33 #include "parseutils.h"
36 /* Global warning level */
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;
47 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
48 keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
50 inc = IncludeCreate(ktcsg->types, MergeDefault);
51 types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
52 AppendStmt(&keycodes->common, &types->common);
54 inc = IncludeCreate(ktcsg->compat, MergeDefault);
55 compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
56 AppendStmt(&keycodes->common, &compat->common);
58 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
59 symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
60 AppendStmt(&keycodes->common, &symbols->common);
62 return CreateXKBFile(XkmKeymapFile,
63 ktcsg->keymap ? ktcsg->keymap : strdup(""),
64 &keycodes->common, 0);
67 static struct xkb_component_names *
68 XkbComponentsFromRules(struct xkb_context *context,
70 const XkbRF_VarDefsPtr defs)
72 FILE *rulesFile = NULL;
73 char *rulesPath = NULL;
74 XkbRF_RulesPtr loaded = NULL;
75 struct xkb_component_names * names = NULL;
78 rulesFile = XkbFindFileInPath(context, rules, XkmRulesFile, &rulesPath);
80 ERROR("could not find \"%s\" rules in XKB path\n", rules);
81 ERROR("%d include paths searched:\n",
82 xkb_context_num_include_paths(context));
83 for (i = 0; i < xkb_context_num_include_paths(context); i++)
84 ERROR("\t%s\n", xkb_context_include_path_get(context, i));
88 if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) {
89 ERROR("failed to allocate XKB rules\n");
93 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
94 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
98 if (!(names = uTypedCalloc(1, struct xkb_component_names))) {
99 ERROR("failed to allocate XKB components\n");
103 if (!XkbcRF_GetComponents(loaded, defs, names)) {
105 free(names->keycodes);
108 free(names->symbols);
111 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
122 _X_EXPORT struct xkb_keymap *
123 xkb_map_new_from_names(struct xkb_context *context,
124 const struct xkb_rule_names *rmlvo)
126 XkbRF_VarDefsRec defs;
127 struct xkb_component_names *names;
128 struct xkb_keymap *xkb;
130 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
131 ERROR("rules and layout required to generate XKB keymap\n");
135 defs.model = rmlvo->model;
136 defs.layout = rmlvo->layout;
137 defs.variant = rmlvo->variant;
138 defs.options = rmlvo->options;
140 names = XkbComponentsFromRules(context, rmlvo->rules, &defs);
142 ERROR("failed to generate XKB components from rules \"%s\"\n",
147 xkb = xkb_map_new_from_kccgst(context, names);
150 free(names->keycodes);
153 free(names->symbols);
160 XkbChooseMap(XkbFile *file, const char *name)
167 if (map->name && strcmp(map->name, name) == 0)
169 map = (XkbFile *) map->common.next;
173 ERROR("no map named \"%s\" in input file\n", name);
175 else if (file->common.next) {
176 /* look for map with XkbLC_Default flag. */
177 for (; map; map = (XkbFile *) map->common.next) {
178 if (map->flags & XkbLC_Default)
184 WARN("no map specified, but components have several\n");
185 WARN("using the first defined map, \"%s\"\n",
186 map->name ? map->name : "");
193 static struct xkb_keymap *
194 compile_keymap(struct xkb_context *context, XkbFile *file)
197 struct xkb_keymap * xkb = NULL;
199 /* Find map to use */
200 mapToUse = XkbChooseMap(file, NULL);
204 switch (mapToUse->type) {
205 case XkmSemanticsFile:
210 ERROR("file type %d not handled\n", mapToUse->type);
214 xkb = CompileKeymap(context, mapToUse, MergeReplace);
224 _X_EXPORT struct xkb_keymap *
225 xkb_map_new_from_kccgst(struct xkb_context *context,
226 const struct xkb_component_names *kccgst)
231 ERROR("no components specified\n");
235 if (ISEMPTY(kccgst->keycodes)) {
236 ERROR("keycodes required to generate XKB keymap\n");
240 if (ISEMPTY(kccgst->compat)) {
241 ERROR("compat map required to generate XKB keymap\n");
245 if (ISEMPTY(kccgst->types)) {
246 ERROR("types required to generate XKB keymap\n");
250 if (ISEMPTY(kccgst->symbols)) {
251 ERROR("symbols required to generate XKB keymap\n");
255 if (!(file = XkbKeymapFileFromComponents(kccgst))) {
256 ERROR("failed to generate parsed XKB file from components\n");
260 return compile_keymap(context, file);
263 _X_EXPORT struct xkb_keymap *
264 xkb_map_new_from_string(struct xkb_context *context,
266 enum xkb_keymap_format format)
270 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
271 ERROR("unsupported keymap format %d\n", format);
276 ERROR("no string specified to generate XKB keymap\n");
280 if (!XKBParseString(string, "input", &file) || !file) {
281 ERROR("failed to parse input xkb file\n");
285 return compile_keymap(context, file);
288 _X_EXPORT struct xkb_keymap *
289 xkb_map_new_from_fd(struct xkb_context *context,
291 enum xkb_keymap_format format)
296 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
297 ERROR("unsupported keymap format %d\n", format);
302 ERROR("no file specified to generate XKB keymap\n");
306 fptr = fdopen(fd, "r");
308 ERROR("couldn't associate fd with file pointer\n");
312 if (!XKBParseFile(fptr, "(unknown file)", &file) || !file) {
313 ERROR("failed to parse input xkb file\n");
317 return compile_keymap(context, file);
320 _X_EXPORT struct xkb_keymap *
321 xkb_map_ref(struct xkb_keymap *xkb)
328 xkb_map_unref(struct xkb_keymap *xkb)
330 if (--xkb->refcnt > 0)
333 XkbcFreeKeyboard(xkb);