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.
27 #include "xkbcomp-priv.h"
29 #include "parseutils.h"
31 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
34 keymap_file_from_components(struct xkb_context *ctx,
35 const struct xkb_component_names *ktcsg)
37 XkbFile *keycodes, *types, *compat, *symbols;
40 inc = IncludeCreate(ctx, ktcsg->keycodes, MERGE_DEFAULT);
41 keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
42 (ParseCommon *) inc, 0);
44 inc = IncludeCreate(ctx, ktcsg->types, MERGE_DEFAULT);
45 types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
46 (ParseCommon *) inc, 0);
47 AppendStmt(&keycodes->common, &types->common);
49 inc = IncludeCreate(ctx, ktcsg->compat, MERGE_DEFAULT);
50 compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
51 (ParseCommon *) inc, 0);
52 AppendStmt(&keycodes->common, &compat->common);
54 inc = IncludeCreate(ctx, ktcsg->symbols, MERGE_DEFAULT);
55 symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
56 (ParseCommon *) inc, 0);
57 AppendStmt(&keycodes->common, &symbols->common);
59 return CreateXKBFile(ctx, FILE_TYPE_KEYMAP, strdup(""),
60 &keycodes->common, 0);
64 * Compile the given file and store the output in keymap.
65 * @param file A list of XkbFiles, each denoting one type (e.g.
66 * FILE_TYPE_KEYCODES, etc.)
68 static struct xkb_keymap *
69 compile_keymap(struct xkb_context *ctx, XkbFile *file)
73 const char *main_name;
74 struct xkb_keymap *keymap;
75 XkbFile *keycodes = NULL;
76 XkbFile *types = NULL;
77 XkbFile *compat = NULL;
78 XkbFile *symbols = NULL;
80 keymap = XkbcAllocKeyboard(ctx);
84 main_name = file->name ? file->name : "(unnamed)";
87 * Other aggregate file types are converted to FILE_TYPE_KEYMAP
90 if (file->type != FILE_TYPE_KEYMAP) {
91 log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
92 XkbcFileTypeText(file->type));
96 /* Check for duplicate entries in the input file */
97 for (file = (XkbFile *) file->defs; file;
98 file = (XkbFile *) file->common.next) {
99 if (have & file->type) {
101 "More than one %s section in a keymap file; "
102 "All sections after the first ignored\n",
103 XkbcFileTypeText(file->type));
107 switch (file->type) {
108 case FILE_TYPE_KEYCODES:
112 case FILE_TYPE_TYPES:
116 case FILE_TYPE_SYMBOLS:
120 case FILE_TYPE_COMPAT:
125 log_err(ctx, "Cannot define %s in a keymap file\n",
126 XkbcFileTypeText(file->type));
130 if (!file->topName) {
132 file->topName = strdup(main_name);
138 if (REQUIRED_FILE_TYPES & (~have)) {
139 enum xkb_file_type bit;
140 enum xkb_file_type missing;
142 missing = REQUIRED_FILE_TYPES & (~have);
144 for (bit = 1; missing != 0; bit <<= 1) {
146 log_err(ctx, "Required section %s missing from keymap\n",
147 XkbcFileTypeText(bit));
155 ok = CompileKeycodes(keycodes, keymap, MERGE_OVERRIDE);
157 log_err(ctx, "Failed to compile keycodes\n");
160 ok = CompileKeyTypes(types, keymap, MERGE_OVERRIDE);
162 log_err(ctx, "Failed to compile key types\n");
165 ok = CompileCompatMap(compat, keymap, MERGE_OVERRIDE);
167 log_err(ctx, "Failed to compile compat map\n");
170 ok = CompileSymbols(symbols, keymap, MERGE_OVERRIDE);
172 log_err(ctx, "Failed to compile symbols\n");
176 ok = UpdateModifiersFromCompat(keymap);
183 log_err(ctx, "Failed to compile keymap\n");
184 xkb_map_unref(keymap);
189 xkb_map_new_from_kccgst(struct xkb_context *ctx,
190 const struct xkb_component_names *kccgst,
191 enum xkb_map_compile_flags flags)
194 struct xkb_keymap *keymap;
197 log_err(ctx, "No components specified\n");
201 if (ISEMPTY(kccgst->keycodes)) {
202 log_err(ctx, "Keycodes required to generate XKB keymap\n");
206 if (ISEMPTY(kccgst->compat)) {
207 log_err(ctx, "Compat map required to generate XKB keymap\n");
211 if (ISEMPTY(kccgst->types)) {
212 log_err(ctx, "Types required to generate XKB keymap\n");
216 if (ISEMPTY(kccgst->symbols)) {
217 log_err(ctx, "Symbols required to generate XKB keymap\n");
221 file = keymap_file_from_components(ctx, kccgst);
223 log_err(ctx, "Failed to generate parsed XKB file from components\n");
227 keymap = compile_keymap(ctx, file);
232 XKB_EXPORT struct xkb_keymap *
233 xkb_map_new_from_names(struct xkb_context *ctx,
234 const struct xkb_rule_names *rmlvo,
235 enum xkb_map_compile_flags flags)
237 struct xkb_component_names *kkctgs;
238 struct xkb_keymap *keymap;
240 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
241 log_err(ctx, "rules and layout required to generate XKB keymap\n");
245 kkctgs = xkb_components_from_rules(ctx, rmlvo);
247 log_err(ctx, "failed to generate XKB components from rules \"%s\"\n",
252 keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0);
254 free(kkctgs->keycodes);
256 free(kkctgs->compat);
257 free(kkctgs->symbols);
263 XKB_EXPORT struct xkb_keymap *
264 xkb_map_new_from_string(struct xkb_context *ctx,
266 enum xkb_keymap_format format,
267 enum xkb_map_compile_flags flags)
271 struct xkb_keymap *keymap;
273 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
274 log_err(ctx, "unsupported keymap format %d\n", format);
279 log_err(ctx, "No string specified to generate XKB keymap\n");
283 ok = XKBParseString(ctx, string, "input", &file);
285 log_err(ctx, "Failed to parse input xkb file\n");
289 keymap = compile_keymap(ctx, file);
294 XKB_EXPORT struct xkb_keymap *
295 xkb_map_new_from_file(struct xkb_context *ctx,
297 enum xkb_keymap_format format,
298 enum xkb_map_compile_flags flags)
302 struct xkb_keymap *keymap;
304 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
305 log_err(ctx, "Unsupported keymap format %d\n", format);
310 log_err(ctx, "No file specified to generate XKB keymap\n");
314 ok = XKBParseFile(ctx, file, "(unknown file)", &xkb_file);
316 log_err(ctx, "Failed to parse input xkb file\n");
320 keymap = compile_keymap(ctx, xkb_file);
321 FreeXKBFile(xkb_file);
325 XKB_EXPORT struct xkb_keymap *
326 xkb_map_ref(struct xkb_keymap *keymap)
333 xkb_map_unref(struct xkb_keymap *keymap)
335 if (!keymap || --keymap->refcnt > 0)
338 XkbcFreeKeyboard(keymap);