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 debugging flags */
37 unsigned int debugFlags = 0;
38 unsigned int warningLevel = 0;
40 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
43 XkbKeymapFileFromComponents(const struct xkb_component_names * ktcsg)
45 XkbFile *keycodes, *types, *compat, *symbols;
48 inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
49 keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
51 inc = IncludeCreate(ktcsg->types, MergeDefault);
52 types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
53 AppendStmt(&keycodes->common, &types->common);
55 inc = IncludeCreate(ktcsg->compat, MergeDefault);
56 compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
57 AppendStmt(&keycodes->common, &compat->common);
59 inc = IncludeCreate(ktcsg->symbols, MergeDefault);
60 symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
61 AppendStmt(&keycodes->common, &symbols->common);
63 return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : strdup(""),
64 &keycodes->common, 0);
67 static struct xkb_component_names *
68 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
70 FILE *rulesFile = NULL;
71 char *rulesPath = NULL;
72 XkbRF_RulesPtr loaded = NULL;
73 struct xkb_component_names * names = NULL;
75 rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
77 ERROR("could not find \"%s\" rules in XKB path\n", rules);
81 if (!(loaded = _XkbTypedCalloc(1, XkbRF_RulesRec))) {
82 ERROR("failed to allocate XKB rules\n");
86 if (!XkbcRF_LoadRules(rulesFile, loaded)) {
87 ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
91 if (!(names = _XkbTypedCalloc(1, struct xkb_component_names))) {
92 ERROR("failed to allocate XKB components\n");
96 if (!XkbcRF_GetComponents(loaded, defs, names)) {
98 free(names->keycodes);
101 free(names->symbols);
104 ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
116 xkb_map_new_from_names(const struct xkb_rule_names *rmlvo)
118 XkbRF_VarDefsRec defs;
119 struct xkb_component_names * names;
120 struct xkb_desc * xkb;
122 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
123 ERROR("rules and layout required to generate XKB keymap\n");
127 defs.model = rmlvo->model;
128 defs.layout = rmlvo->layout;
129 defs.variant = rmlvo->variant;
130 defs.options = rmlvo->options;
132 names = XkbComponentsFromRules(rmlvo->rules, &defs);
134 ERROR("failed to generate XKB components from rules \"%s\"\n",
139 xkb = xkb_map_new_from_kccgst(names);
142 free(names->keycodes);
145 free(names->symbols);
152 XkbChooseMap(XkbFile *file, const char *name)
159 if (map->name && strcmp(map->name, name) == 0)
161 map = (XkbFile *) map->common.next;
165 ERROR("no map named \"%s\" in input file\n", name);
167 else if (file->common.next) {
168 /* look for map with XkbLC_Default flag. */
169 for (; map; map = (XkbFile *) map->common.next) {
170 if (map->flags & XkbLC_Default)
176 WARN("no map specified, but components have several\n");
177 WARN("using the first defined map, \"%s\"\n",
178 map->name ? map->name : "");
185 static struct xkb_desc *
186 compile_keymap(XkbFile *file)
189 struct xkb_desc * xkb = NULL;
191 /* Find map to use */
192 mapToUse = XkbChooseMap(file, NULL);
196 switch (mapToUse->type) {
197 case XkmSemanticsFile:
202 ERROR("file type %d not handled\n", mapToUse->type);
206 xkb = CompileKeymap(mapToUse, MergeReplace);
213 XkbFreeIncludePath();
219 xkb_map_new_from_kccgst(const struct xkb_component_names *kccgst)
224 ERROR("no components specified\n");
228 if (ISEMPTY(kccgst->keycodes)) {
229 ERROR("keycodes required to generate XKB keymap\n");
233 if (ISEMPTY(kccgst->compat)) {
234 ERROR("compat map required to generate XKB keymap\n");
238 if (ISEMPTY(kccgst->types)) {
239 ERROR("types required to generate XKB keymap\n");
243 if (ISEMPTY(kccgst->symbols)) {
244 ERROR("symbols required to generate XKB keymap\n");
248 if (!(file = XkbKeymapFileFromComponents(kccgst))) {
249 ERROR("failed to generate parsed XKB file from components\n");
253 return compile_keymap(file);
257 xkb_map_new_from_string(const char *string, enum xkb_keymap_format format)
261 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
262 ERROR("unsupported keymap format %d\n", format);
267 ERROR("no string specified to generate XKB keymap\n");
271 setScanState("input", 1);
272 if (!XKBParseString(string, &file) || !file) {
273 ERROR("failed to parse input xkb file\n");
277 return compile_keymap(file);
281 xkb_map_new_from_fd(int fd, enum xkb_keymap_format format)
286 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
287 ERROR("unsupported keymap format %d\n", format);
292 ERROR("no file specified to generate XKB keymap\n");
296 fptr = fdopen(fd, "r");
298 ERROR("couldn't associate fd with file pointer\n");
302 setScanState("input", 1);
303 if (!XKBParseFile(fptr, &file) || !file) {
304 ERROR("failed to parse input xkb file\n");
308 return compile_keymap(file);
312 xkb_map_ref(struct xkb_desc *xkb)
318 xkb_map_unref(struct xkb_desc *xkb)
320 if (--xkb->refcnt > 0)
323 XkbcFreeKeyboard(xkb);