2 * Copyright © 2018 Red Hat, Inc.
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
34 #include "xkbcomp/xkbcomp-priv.h"
35 #include "xkbcomp/rules.h"
36 #include "xkbcommon/xkbcommon.h"
38 #define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"
40 static bool verbose = false;
41 static enum output_format {
44 FORMAT_KEYMAP_FROM_XKB,
45 } output_format = FORMAT_KEYMAP;
46 static darray(const char *) includes;
51 printf("Usage: %s [OPTIONS]\n"
53 "Compile the given RMLVO to a keymap and print it\n"
57 " Enable verbose debugging output\n"
59 " Print a keymap which only includes the KcCGST component names instead of the full keymap\n"
61 " Load the XKB file from stdin, ignore RMLVO options. This option\n"
62 " must not be used with --kccgst.\n"
64 " Add the given path to the include path list. This option is\n"
65 " order-dependent, include paths given first are searched first.\n"
66 " If an include path is given, the default include path list is\n"
67 " not used. Use --include-defaults to add the default include\n"
69 " --include-defaults\n"
70 " Add the default set of include directories.\n"
71 " This option is order-dependent, include paths given first\n"
72 " are searched first.\n"
74 "XKB-specific options:\n"
76 " The XKB ruleset (default: '%s')\n"
78 " The XKB model (default: '%s')\n"
79 " --layout <layout>\n"
80 " The XKB layout (default: '%s')\n"
81 " --variant <variant>\n"
82 " The XKB layout variant (default: '%s')\n"
83 " --options <options>\n"
84 " The XKB options (default: '%s')\n"
86 argv[0], DEFAULT_XKB_RULES,
87 DEFAULT_XKB_MODEL, DEFAULT_XKB_LAYOUT,
88 DEFAULT_XKB_VARIANT ? DEFAULT_XKB_VARIANT : "<none>",
89 DEFAULT_XKB_OPTIONS ? DEFAULT_XKB_OPTIONS : "<none>");
93 parse_options(int argc, char **argv, struct xkb_rule_names *names)
100 OPT_INCLUDE_DEFAULTS,
107 static struct option opts[] = {
108 {"help", no_argument, 0, 'h'},
109 {"verbose", no_argument, 0, OPT_VERBOSE},
110 {"kccgst", no_argument, 0, OPT_KCCGST},
111 {"from-xkb", no_argument, 0, OPT_FROM_XKB},
112 {"include", required_argument, 0, OPT_INCLUDE},
113 {"include-defaults", no_argument, 0, OPT_INCLUDE_DEFAULTS},
114 {"rules", required_argument, 0, OPT_RULES},
115 {"model", required_argument, 0, OPT_MODEL},
116 {"layout", required_argument, 0, OPT_LAYOUT},
117 {"variant", required_argument, 0, OPT_VARIANT},
118 {"options", required_argument, 0, OPT_OPTION},
124 int option_index = 0;
125 c = getopt_long(argc, argv, "h", opts, &option_index);
137 output_format = FORMAT_KCCGST;
140 output_format = FORMAT_KEYMAP_FROM_XKB;
143 darray_append(includes, optarg);
145 case OPT_INCLUDE_DEFAULTS:
146 darray_append(includes, DEFAULT_INCLUDE_PATH_PLACEHOLDER);
149 names->rules = optarg;
152 names->model = optarg;
155 names->layout = optarg;
158 names->variant = optarg;
161 names->options = optarg;
174 print_kccgst(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
176 struct xkb_component_names kccgst;
178 if (!xkb_components_from_rules(ctx, rmlvo, &kccgst))
181 printf("xkb_keymap {\n"
182 " xkb_keycodes { include \"%s\" };\n"
183 " xkb_types { include \"%s\" };\n"
184 " xkb_compat { include \"%s\" };\n"
185 " xkb_symbols { include \"%s\" };\n"
187 kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
188 free(kccgst.keycodes);
191 free(kccgst.symbols);
197 print_keymap(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
199 struct xkb_keymap *keymap;
201 keymap = xkb_keymap_new_from_names(ctx, rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS);
205 printf("%s\n", xkb_keymap_get_as_string(keymap,
206 XKB_KEYMAP_FORMAT_TEXT_V1));
207 xkb_keymap_unref(keymap);
212 print_keymap_from_file(struct xkb_context *ctx)
214 struct xkb_keymap *keymap = NULL;
215 char *keymap_string = NULL;
217 bool success = false;
221 fprintf(stderr, "Failed to create tmpfile\n");
229 len = fread(buf, 1, sizeof(buf), stdin);
231 fprintf(stderr, "Failed to read from stdin\n");
235 size_t wlen = fwrite(buf, 1, len, file);
237 fprintf(stderr, "Failed to write to tmpfile\n");
244 fseek(file, 0, SEEK_SET);
245 keymap = xkb_keymap_new_from_file(ctx, file,
246 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
248 fprintf(stderr, "Couldn't create xkb keymap\n");
252 keymap_string = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
253 if (!keymap_string) {
254 fprintf(stderr, "Couldn't get the keymap string\n");
258 fputs(keymap_string, stdout);
264 xkb_keymap_unref(keymap);
271 main(int argc, char **argv)
273 struct xkb_context *ctx;
274 struct xkb_rule_names names = {
289 if (!parse_options(argc, argv, &names))
292 ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
296 xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG);
297 xkb_context_set_log_verbosity(ctx, 10);
300 xkb_context_sanitize_rule_names(ctx, &names);
301 if (darray_empty(includes))
302 darray_append(includes, DEFAULT_INCLUDE_PATH_PLACEHOLDER);
304 darray_foreach(path, includes) {
305 if (streq(*path, DEFAULT_INCLUDE_PATH_PLACEHOLDER))
306 xkb_context_include_path_append_default(ctx);
308 xkb_context_include_path_append(ctx, *path);
311 if (output_format == FORMAT_KEYMAP) {
312 rc = print_keymap(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
313 } else if (output_format == FORMAT_KCCGST) {
314 rc = print_kccgst(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
315 } else if (output_format == FORMAT_KEYMAP_FROM_XKB) {
316 rc = print_keymap_from_file(ctx);
319 xkb_context_unref(ctx);