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 /* Global warning level */
32 unsigned int warningLevel = 0;
34 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
37 XkbKeymapFileFromComponents(struct xkb_context *ctx,
38 const struct xkb_component_names *ktcsg)
40 XkbFile *keycodes, *types, *compat, *symbols;
43 inc = IncludeCreate(ktcsg->keycodes, MERGE_DEFAULT);
44 keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
45 (ParseCommon *)inc, 0);
47 inc = IncludeCreate(ktcsg->types, MERGE_DEFAULT);
48 types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
49 (ParseCommon *)inc, 0);
50 AppendStmt(&keycodes->common, &types->common);
52 inc = IncludeCreate(ktcsg->compat, MERGE_DEFAULT);
53 compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
54 (ParseCommon *)inc, 0);
55 AppendStmt(&keycodes->common, &compat->common);
57 inc = IncludeCreate(ktcsg->symbols, MERGE_DEFAULT);
58 symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
59 (ParseCommon *)inc, 0);
60 AppendStmt(&keycodes->common, &symbols->common);
62 return CreateXKBFile(ctx, FILE_TYPE_KEYMAP,
63 ktcsg->keymap ? ktcsg->keymap : strdup(""),
64 &keycodes->common, 0);
67 _X_EXPORT struct xkb_keymap *
68 xkb_map_new_from_names(struct xkb_context *ctx,
69 const struct xkb_rule_names *rmlvo,
70 enum xkb_map_compile_flags flags)
72 struct xkb_component_names *kkctgs;
73 struct xkb_keymap *keymap;
75 if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
76 ERROR("rules and layout required to generate XKB keymap\n");
80 kkctgs = xkb_components_from_rules(ctx, rmlvo);
82 ERROR("failed to generate XKB components from rules \"%s\"\n",
87 keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0);
90 free(kkctgs->keycodes);
93 free(kkctgs->symbols);
99 static struct xkb_keymap *
100 compile_keymap(struct xkb_context *ctx, XkbFile *file)
102 struct xkb_keymap *keymap;
104 keymap = CompileKeymap(ctx, file);
111 xkb_map_new_from_kccgst(struct xkb_context *ctx,
112 const struct xkb_component_names *kccgst,
113 enum xkb_map_compile_flags flags)
118 ERROR("no components specified\n");
122 if (ISEMPTY(kccgst->keycodes)) {
123 ERROR("keycodes required to generate XKB keymap\n");
127 if (ISEMPTY(kccgst->compat)) {
128 ERROR("compat map required to generate XKB keymap\n");
132 if (ISEMPTY(kccgst->types)) {
133 ERROR("types required to generate XKB keymap\n");
137 if (ISEMPTY(kccgst->symbols)) {
138 ERROR("symbols required to generate XKB keymap\n");
142 if (!(file = XkbKeymapFileFromComponents(ctx, kccgst))) {
143 ERROR("failed to generate parsed XKB file from components\n");
147 return compile_keymap(ctx, file);
150 _X_EXPORT struct xkb_keymap *
151 xkb_map_new_from_string(struct xkb_context *ctx,
153 enum xkb_keymap_format format,
154 enum xkb_map_compile_flags flags)
158 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
159 ERROR("unsupported keymap format %d\n", format);
164 ERROR("no string specified to generate XKB keymap\n");
168 if (!XKBParseString(ctx, string, "input", &file)) {
169 ERROR("failed to parse input xkb file\n");
173 return compile_keymap(ctx, file);
176 _X_EXPORT struct xkb_keymap *
177 xkb_map_new_from_file(struct xkb_context *ctx,
179 enum xkb_keymap_format format,
180 enum xkb_map_compile_flags flags)
184 if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
185 ERROR("unsupported keymap format %d\n", format);
190 ERROR("no file specified to generate XKB keymap\n");
194 if (!XKBParseFile(ctx, file, "(unknown file)", &xkb_file)) {
195 ERROR("failed to parse input xkb file\n");
199 return compile_keymap(ctx, xkb_file);
202 _X_EXPORT struct xkb_keymap *
203 xkb_map_ref(struct xkb_keymap *keymap)
210 xkb_map_unref(struct xkb_keymap *keymap)
212 if (--keymap->refcnt > 0)
215 XkbcFreeKeyboard(keymap);