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 {
45 FORMAT_KEYMAP_FROM_XKB,
46 } output_format = FORMAT_KEYMAP;
47 static darray(const char *) includes;
52 printf("Usage: %s [OPTIONS]\n"
54 "Compile the given RMLVO to a keymap and print it\n"
58 " Enable verbose debugging output\n"
59 #if ENABLE_PRIVATE_APIS
61 " Print a keymap which only includes the KcCGST component names instead of the full keymap\n"
64 " Print the full RMLVO with the defaults filled in for missing elements\n"
66 " Load the XKB file from stdin, ignore RMLVO options. This option\n"
67 " must not be used with --kccgst.\n"
69 " Add the given path to the include path list. This option is\n"
70 " order-dependent, include paths given first are searched first.\n"
71 " If an include path is given, the default include path list is\n"
72 " not used. Use --include-defaults to add the default include\n"
74 " --include-defaults\n"
75 " Add the default set of include directories.\n"
76 " This option is order-dependent, include paths given first\n"
77 " are searched first.\n"
79 "XKB-specific options:\n"
81 " The XKB ruleset (default: '%s')\n"
83 " The XKB model (default: '%s')\n"
84 " --layout <layout>\n"
85 " The XKB layout (default: '%s')\n"
86 " --variant <variant>\n"
87 " The XKB layout variant (default: '%s')\n"
88 " --options <options>\n"
89 " The XKB options (default: '%s')\n"
91 argv[0], DEFAULT_XKB_RULES,
92 DEFAULT_XKB_MODEL, DEFAULT_XKB_LAYOUT,
93 DEFAULT_XKB_VARIANT ? DEFAULT_XKB_VARIANT : "<none>",
94 DEFAULT_XKB_OPTIONS ? DEFAULT_XKB_OPTIONS : "<none>");
98 parse_options(int argc, char **argv, struct xkb_rule_names *names)
106 OPT_INCLUDE_DEFAULTS,
113 static struct option opts[] = {
114 {"help", no_argument, 0, 'h'},
115 {"verbose", no_argument, 0, OPT_VERBOSE},
116 #if ENABLE_PRIVATE_APIS
117 {"kccgst", no_argument, 0, OPT_KCCGST},
119 {"rmlvo", no_argument, 0, OPT_RMLVO},
120 {"from-xkb", no_argument, 0, OPT_FROM_XKB},
121 {"include", required_argument, 0, OPT_INCLUDE},
122 {"include-defaults", no_argument, 0, OPT_INCLUDE_DEFAULTS},
123 {"rules", required_argument, 0, OPT_RULES},
124 {"model", required_argument, 0, OPT_MODEL},
125 {"layout", required_argument, 0, OPT_LAYOUT},
126 {"variant", required_argument, 0, OPT_VARIANT},
127 {"options", required_argument, 0, OPT_OPTION},
133 int option_index = 0;
134 c = getopt_long(argc, argv, "h", opts, &option_index);
146 output_format = FORMAT_KCCGST;
149 output_format = FORMAT_RMLVO;
152 output_format = FORMAT_KEYMAP_FROM_XKB;
155 darray_append(includes, optarg);
157 case OPT_INCLUDE_DEFAULTS:
158 darray_append(includes, DEFAULT_INCLUDE_PATH_PLACEHOLDER);
161 names->rules = optarg;
164 names->model = optarg;
167 names->layout = optarg;
170 names->variant = optarg;
173 names->options = optarg;
177 exit(EXIT_INVALID_USAGE);
186 print_rmlvo(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
188 printf("rules: \"%s\"\nmodel: \"%s\"\nlayout: \"%s\"\nvariant: \"%s\"\noptions: \"%s\"\n",
189 rmlvo->rules, rmlvo->model, rmlvo->layout,
190 rmlvo->variant ? rmlvo->variant : "",
191 rmlvo->options ? rmlvo->options : "");
196 print_kccgst(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
198 #if ENABLE_PRIVATE_APIS
199 struct xkb_component_names kccgst;
201 if (!xkb_components_from_rules(ctx, rmlvo, &kccgst))
204 printf("xkb_keymap {\n"
205 " xkb_keycodes { include \"%s\" };\n"
206 " xkb_types { include \"%s\" };\n"
207 " xkb_compat { include \"%s\" };\n"
208 " xkb_symbols { include \"%s\" };\n"
210 kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
211 free(kccgst.keycodes);
214 free(kccgst.symbols);
223 print_keymap(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
225 struct xkb_keymap *keymap;
227 keymap = xkb_keymap_new_from_names(ctx, rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS);
231 printf("%s\n", xkb_keymap_get_as_string(keymap,
232 XKB_KEYMAP_FORMAT_TEXT_V1));
233 xkb_keymap_unref(keymap);
238 print_keymap_from_file(struct xkb_context *ctx)
240 struct xkb_keymap *keymap = NULL;
241 char *keymap_string = NULL;
243 bool success = false;
247 fprintf(stderr, "Failed to create tmpfile\n");
255 len = fread(buf, 1, sizeof(buf), stdin);
257 fprintf(stderr, "Failed to read from stdin\n");
261 size_t wlen = fwrite(buf, 1, len, file);
263 fprintf(stderr, "Failed to write to tmpfile\n");
270 fseek(file, 0, SEEK_SET);
271 keymap = xkb_keymap_new_from_file(ctx, file,
272 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
274 fprintf(stderr, "Couldn't create xkb keymap\n");
278 keymap_string = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
279 if (!keymap_string) {
280 fprintf(stderr, "Couldn't get the keymap string\n");
284 fputs(keymap_string, stdout);
290 xkb_keymap_unref(keymap);
297 main(int argc, char **argv)
299 struct xkb_context *ctx;
300 struct xkb_rule_names names = {
301 .rules = DEFAULT_XKB_RULES,
302 .model = DEFAULT_XKB_MODEL,
303 /* layout and variant are tied together, so we either get user-supplied for
304 * both or default for both, see below */
307 .options = DEFAULT_XKB_OPTIONS,
314 return EXIT_INVALID_USAGE;
317 if (!parse_options(argc, argv, &names))
318 return EXIT_INVALID_USAGE;
320 /* Now fill in the layout */
321 if (isempty(names.layout)) {
322 if (!isempty(names.variant)) {
323 fprintf(stderr, "Error: a variant requires a layout\n");
324 return EXIT_INVALID_USAGE;
326 names.layout = DEFAULT_XKB_LAYOUT;
327 names.variant = DEFAULT_XKB_VARIANT;
330 ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
334 xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG);
335 xkb_context_set_log_verbosity(ctx, 10);
338 if (darray_empty(includes))
339 darray_append(includes, DEFAULT_INCLUDE_PATH_PLACEHOLDER);
341 darray_foreach(path, includes) {
342 if (streq(*path, DEFAULT_INCLUDE_PATH_PLACEHOLDER))
343 xkb_context_include_path_append_default(ctx);
345 xkb_context_include_path_append(ctx, *path);
348 if (output_format == FORMAT_RMLVO) {
349 rc = print_rmlvo(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
350 } else if (output_format == FORMAT_KEYMAP) {
351 rc = print_keymap(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
352 } else if (output_format == FORMAT_KCCGST) {
353 rc = print_kccgst(ctx, &names) ? EXIT_SUCCESS : EXIT_FAILURE;
354 } else if (output_format == FORMAT_KEYMAP_FROM_XKB) {
355 rc = print_keymap_from_file(ctx);
358 xkb_context_unref(ctx);