tools: don't mangle the path for tools, just exec directly
[platform/upstream/libxkbcommon.git] / tools / how-to-type.c
1 /*
2  * Copyright © 2020 Ran Benita <ran@unusedvar.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "config.h"
25
26 #include <getopt.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "xkbcommon/xkbcommon.h"
32
33 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
34
35 static void
36 usage(const char *argv0, FILE *fp)
37 {
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");
42 }
43
44 int
45 main(int argc, char *argv[])
46 {
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;
54     char *endp;
55     long val;
56     uint32_t codepoint;
57     xkb_keysym_t keysym;
58     int ret;
59     char name[200];
60     struct xkb_keymap *keymap = NULL;
61     xkb_keycode_t min_keycode, max_keycode;
62     xkb_mod_index_t num_mods;
63     enum options {
64         OPT_RULES,
65         OPT_MODEL,
66         OPT_LAYOUT,
67         OPT_VARIANT,
68         OPT_OPTIONS,
69     };
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},
77         {0, 0, 0, 0},
78     };
79
80     while (1) {
81         int opt;
82         int option_index = 0;
83
84         opt = getopt_long(argc, argv, "h", opts, &option_index);
85         if (opt == -1)
86             break;
87
88         switch (opt) {
89         case OPT_RULES:
90             rules = optarg;
91             break;
92         case OPT_MODEL:
93             model = optarg;
94             break;
95         case OPT_LAYOUT:
96             layout_ = optarg;
97             break;
98         case OPT_VARIANT:
99             variant = optarg;
100             break;
101         case OPT_OPTIONS:
102             options = optarg;
103             break;
104         case 'h':
105             usage(argv[0], stdout);
106             exit(EXIT_SUCCESS);
107         default:
108             usage(argv[0], stderr);
109             exit(EXIT_INVALID_USAGE);
110         }
111     }
112     if (argc - optind != 1) {
113         usage(argv[0], stderr);
114         exit(EXIT_INVALID_USAGE);
115     }
116
117     errno = 0;
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);
122     }
123     codepoint = (uint32_t) val;
124
125     keysym = xkb_utf32_to_keysym(codepoint);
126     if (keysym == XKB_KEY_NoSymbol) {
127         fprintf(stderr, "Failed to convert codepoint to keysym\n");
128         goto err;
129     }
130
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");
134         goto err;
135     }
136
137     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
138     if (!ctx) {
139         fprintf(stderr, "Failed to create XKB context\n");
140         goto err;
141     }
142
143     struct xkb_rule_names names = {
144         .rules = rules,
145         .model = model,
146         .layout = layout_,
147         .variant = variant,
148         .options = options,
149     };
150     keymap = xkb_keymap_new_from_names(ctx, &names,
151                                        XKB_KEYMAP_COMPILE_NO_FLAGS);
152     if (!keymap) {
153         fprintf(stderr, "Failed to create XKB keymap\n");
154         goto err;
155     }
156
157     printf("keysym: %s (%#x)\n", name, keysym);
158     printf("KEYCODE\tKEY NAME\tLAYOUT#\tLAYOUT NAME\tLEVEL#\tMODIFIERS\n");
159
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;
166
167         key_name = xkb_keymap_key_get_name(keymap, keycode);
168         if (!key_name) {
169             continue;
170         }
171
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;
176
177             layout_name = xkb_keymap_layout_get_name(keymap, layout);
178             if (!layout_name) {
179                 layout_name = "?";
180             }
181
182             num_levels = xkb_keymap_num_levels_for_key(keymap, keycode, layout);
183             for (xkb_level_index_t level = 0; level < num_levels; level++) {
184                 int num_syms;
185                 const xkb_keysym_t *syms;
186                 size_t num_masks;
187                 xkb_mod_mask_t masks[100];
188
189                 num_syms = xkb_keymap_key_get_syms_by_level(
190                     keymap, keycode, layout, level, &syms
191                 );
192                 if (num_syms != 1) {
193                     continue;
194                 }
195                 if (syms[0] != keysym) {
196                     continue;
197                 }
198
199                 num_masks = xkb_keymap_key_get_mods_for_level(
200                     keymap, keycode, layout, level, masks, ARRAY_SIZE(masks)
201                 );
202                 for (size_t i = 0; i < num_masks; i++) {
203                     xkb_mod_mask_t mask = masks[i];
204
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) {
209                             continue;
210                         }
211                         printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
212                     }
213                     printf("]\n");
214                 }
215             }
216         }
217     }
218
219     err = EXIT_SUCCESS;
220 err:
221     xkb_keymap_unref(keymap);
222     xkb_context_unref(ctx);
223     return err;
224 }