2 * Copyright © 2020 Ran Benita <ran@unusedvar.com>
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.
31 #include "xkbcommon/xkbcommon.h"
33 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
36 usage(const char *argv0, FILE *fp)
38 fprintf(fp, "Usage: %s [--rules <rules>] [--model <model>] "
39 "[--layout <layout>] [--variant <variant>] [--options <options>]"
40 " <unicode codepoint>\n", argv0);
41 fprintf(fp, "Pipe into `column -ts $'\\t'` for nicely aligned output.\n");
45 main(int argc, char *argv[])
47 const char *rules = NULL;
48 const char *model = NULL;
49 const char *layout_ = NULL;
50 const char *variant = NULL;
51 const char *options = NULL;
52 int err = EXIT_FAILURE;
53 struct xkb_context *ctx = NULL;
60 struct xkb_keymap *keymap = NULL;
61 xkb_keycode_t min_keycode, max_keycode;
62 xkb_mod_index_t num_mods;
70 static struct option opts[] = {
71 {"help", no_argument, 0, 'h'},
72 {"rules", required_argument, 0, OPT_RULES},
73 {"model", required_argument, 0, OPT_MODEL},
74 {"layout", required_argument, 0, OPT_LAYOUT},
75 {"variant", required_argument, 0, OPT_VARIANT},
76 {"options", required_argument, 0, OPT_OPTIONS},
84 opt = getopt_long(argc, argv, "h", opts, &option_index);
105 usage(argv[0], stdout);
108 usage(argv[0], stderr);
109 exit(EXIT_INVALID_USAGE);
112 if (argc - optind != 1) {
113 usage(argv[0], stderr);
114 exit(EXIT_INVALID_USAGE);
118 val = strtol(argv[optind], &endp, 0);
119 if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) {
120 usage(argv[0], stderr);
121 exit(EXIT_INVALID_USAGE);
123 codepoint = (uint32_t) val;
125 keysym = xkb_utf32_to_keysym(codepoint);
126 if (keysym == XKB_KEY_NoSymbol) {
127 fprintf(stderr, "Failed to convert codepoint to keysym\n");
131 ret = xkb_keysym_get_name(keysym, name, sizeof(name));
132 if (ret < 0 || (size_t) ret >= sizeof(name)) {
133 fprintf(stderr, "Failed to get name of keysym\n");
137 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
139 fprintf(stderr, "Failed to create XKB context\n");
143 struct xkb_rule_names names = {
150 keymap = xkb_keymap_new_from_names(ctx, &names,
151 XKB_KEYMAP_COMPILE_NO_FLAGS);
153 fprintf(stderr, "Failed to create XKB keymap\n");
157 printf("keysym: %s (%#x)\n", name, keysym);
158 printf("KEYCODE\tKEY NAME\tLAYOUT#\tLAYOUT NAME\tLEVEL#\tMODIFIERS\n");
160 min_keycode = xkb_keymap_min_keycode(keymap);
161 max_keycode = xkb_keymap_max_keycode(keymap);
162 num_mods = xkb_keymap_num_mods(keymap);
163 for (xkb_keycode_t keycode = min_keycode; keycode <= max_keycode; keycode++) {
164 const char *key_name;
165 xkb_layout_index_t num_layouts;
167 key_name = xkb_keymap_key_get_name(keymap, keycode);
172 num_layouts = xkb_keymap_num_layouts_for_key(keymap, keycode);
173 for (xkb_layout_index_t layout = 0; layout < num_layouts; layout++) {
174 const char *layout_name;
175 xkb_level_index_t num_levels;
177 layout_name = xkb_keymap_layout_get_name(keymap, layout);
182 num_levels = xkb_keymap_num_levels_for_key(keymap, keycode, layout);
183 for (xkb_level_index_t level = 0; level < num_levels; level++) {
185 const xkb_keysym_t *syms;
187 xkb_mod_mask_t masks[100];
189 num_syms = xkb_keymap_key_get_syms_by_level(
190 keymap, keycode, layout, level, &syms
195 if (syms[0] != keysym) {
199 num_masks = xkb_keymap_key_get_mods_for_level(
200 keymap, keycode, layout, level, masks, ARRAY_SIZE(masks)
202 for (size_t i = 0; i < num_masks; i++) {
203 xkb_mod_mask_t mask = masks[i];
205 printf("%u\t%s\t%u\t%s\t%u\t[ ",
206 keycode, key_name, layout + 1, layout_name, level + 1);
207 for (xkb_mod_index_t mod = 0; mod < num_mods; mod++) {
208 if ((mask & (1 << mod)) == 0) {
211 printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
221 xkb_keymap_unref(keymap);
222 xkb_context_unref(ctx);