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, ktcsg->keymap ? ktcsg->keymap : strdup(""),
63 &keycodes->common, 0);
66 static struct xkb_component_names *
67 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
69 FILE *rulesFile = NULL;
70 char *rulesPath = NULL;
71 XkbRF_RulesPtr loaded = NULL;
72 struct xkb_component_names * names = NULL;
74 rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
76 ERROR("could not find \"%s\" rules in XKB path\n", rules);
80 if (!(loaded = uTypedCalloc(1, XkbRF_RulesRec))) {
81 ERROR("failed to allocate XKB rules\n");
85 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
86 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
90 if (!(names = uTypedCalloc(1, struct xkb_component_names))) {
91 ERROR("failed to allocate XKB components\n");
95 if (!XkbcRF_GetComponents(loaded, defs, names)) {
97 free(names->keycodes);
100 free(names->symbols);
103 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
115 xkb_map_new_from_names(const struct xkb_rule_names *rmlvo)
117 XkbRF_VarDefsRec defs;
118 struct xkb_component_names * names;
119 struct xkb_desc * xkb;
121 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
122 ERROR("rules and layout required to generate XKB keymap\n");
126 defs.model = rmlvo->model;
127 defs.layout = rmlvo->layout;
128 defs.variant = rmlvo->variant;
129 defs.options = rmlvo->options;
131 names = XkbComponentsFromRules(rmlvo->rules, &defs);
133 ERROR("failed to generate XKB components from rules \"%s\"\n",
138 xkb = xkb_map_new_from_kccgst(names);
141 free(names->keycodes);
144 free(names->symbols);
151 XkbChooseMap(XkbFile *file, const char *name)
158 if (map->name && strcmp(map->name, name) == 0)
160 map = (XkbFile *) map->common.next;
164 ERROR("no map named \"%s\" in input file\n", name);
166 else if (file->common.next) {
167 /* look for map with XkbLC_Default flag. */
168 for (; map; map = (XkbFile *) map->common.next) {
169 if (map->flags & XkbLC_Default)
175 WARN("no map specified, but components have several\n");
176 WARN("using the first defined map, \"%s\"\n",
177 map->name ? map->name : "");
184 static struct xkb_desc *
185 compile_keymap(XkbFile *file)
188 struct xkb_desc * xkb = NULL;
190 /* Find map to use */
191 mapToUse = XkbChooseMap(file, NULL);
195 switch (mapToUse->type) {
196 case XkmSemanticsFile:
201 ERROR("file type %d not handled\n", mapToUse->type);
205 xkb = CompileKeymap(mapToUse, MergeReplace);
212 XkbFreeIncludePath();
218 xkb_map_new_from_kccgst(const struct xkb_component_names *kccgst)
223 ERROR("no components specified\n");
227 if (ISEMPTY(kccgst->keycodes)) {
228 ERROR("keycodes required to generate XKB keymap\n");
232 if (ISEMPTY(kccgst->compat)) {
233 ERROR("compat map required to generate XKB keymap\n");
237 if (ISEMPTY(kccgst->types)) {
238 ERROR("types required to generate XKB keymap\n");
242 if (ISEMPTY(kccgst->symbols)) {
243 ERROR("symbols required to generate XKB keymap\n");
247 if (!(file = XkbKeymapFileFromComponents(kccgst))) {
248 ERROR("failed to generate parsed XKB file from components\n");
252 return compile_keymap(file);
256 xkb_map_new_from_string(const char *string, enum xkb_keymap_format format)
260 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
261 ERROR("unsupported keymap format %d\n", format);
266 ERROR("no string specified to generate XKB keymap\n");
270 setScanState("input", 1);
271 if (!XKBParseString(string, &file) || !file) {
272 ERROR("failed to parse input xkb file\n");
276 return compile_keymap(file);
280 xkb_map_new_from_fd(int fd, enum xkb_keymap_format format)
285 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
286 ERROR("unsupported keymap format %d\n", format);
291 ERROR("no file specified to generate XKB keymap\n");
295 fptr = fdopen(fd, "r");
297 ERROR("couldn't associate fd with file pointer\n");
301 setScanState("input", 1);
302 if (!XKBParseFile(fptr, &file) || !file) {
303 ERROR("failed to parse input xkb file\n");
307 return compile_keymap(file);
311 xkb_map_ref(struct xkb_desc *xkb)
317 xkb_map_unref(struct xkb_desc *xkb)
319 if (--xkb->refcnt > 0)
322 XkbcFreeKeyboard(xkb);