CI: Use git master for xkeyboard-config on Linux
[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 <stdbool.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <errno.h>
31
32 #include "xkbcommon/xkbcommon.h"
33
34 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
35
36 static void
37 usage(const char *argv0, FILE *fp)
38 {
39     fprintf(fp, "Usage: %s [--keysym] [--rules <rules>] [--model <model>] "
40                 "[--layout <layout>] [--variant <variant>] [--options <options>]"
41                 " <unicode codepoint/keysym>\n", argv0);
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     bool keysym_mode = false;
53     int err = EXIT_FAILURE;
54     struct xkb_context *ctx = NULL;
55     char *endp;
56     long val;
57     uint32_t codepoint;
58     xkb_keysym_t keysym;
59     int ret;
60     char name[200];
61     struct xkb_keymap *keymap = NULL;
62     xkb_keycode_t min_keycode, max_keycode;
63     xkb_mod_index_t num_mods;
64     enum options {
65         OPT_KEYSYM,
66         OPT_RULES,
67         OPT_MODEL,
68         OPT_LAYOUT,
69         OPT_VARIANT,
70         OPT_OPTIONS,
71     };
72     static struct option opts[] = {
73         {"help",                 no_argument,            0, 'h'},
74         {"keysym",               no_argument,            0, OPT_KEYSYM},
75         {"rules",                required_argument,      0, OPT_RULES},
76         {"model",                required_argument,      0, OPT_MODEL},
77         {"layout",               required_argument,      0, OPT_LAYOUT},
78         {"variant",              required_argument,      0, OPT_VARIANT},
79         {"options",              required_argument,      0, OPT_OPTIONS},
80         {0, 0, 0, 0},
81     };
82
83     while (1) {
84         int opt;
85         int option_index = 0;
86
87         opt = getopt_long(argc, argv, "h", opts, &option_index);
88         if (opt == -1)
89             break;
90
91         switch (opt) {
92         case OPT_KEYSYM:
93             keysym_mode = true;
94             break;
95         case OPT_RULES:
96             rules = optarg;
97             break;
98         case OPT_MODEL:
99             model = optarg;
100             break;
101         case OPT_LAYOUT:
102             layout_ = optarg;
103             break;
104         case OPT_VARIANT:
105             variant = optarg;
106             break;
107         case OPT_OPTIONS:
108             options = optarg;
109             break;
110         case 'h':
111             usage(argv[0], stdout);
112             exit(EXIT_SUCCESS);
113         default:
114             usage(argv[0], stderr);
115             exit(EXIT_INVALID_USAGE);
116         }
117     }
118     if (argc - optind != 1) {
119         usage(argv[0], stderr);
120         exit(EXIT_INVALID_USAGE);
121     }
122
123     if (keysym_mode) {
124         keysym = xkb_keysym_from_name(argv[optind], XKB_KEYSYM_NO_FLAGS);
125         if (keysym == XKB_KEY_NoSymbol) {
126             fprintf(stderr, "Failed to convert argument to keysym\n");
127             goto err;
128         }
129     } else {
130         errno = 0;
131         val = strtol(argv[optind], &endp, 0);
132         if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) {
133             usage(argv[0], stderr);
134             exit(EXIT_INVALID_USAGE);
135         }
136         codepoint = (uint32_t) val;
137
138         keysym = xkb_utf32_to_keysym(codepoint);
139         if (keysym == XKB_KEY_NoSymbol) {
140             fprintf(stderr, "Failed to convert codepoint to keysym\n");
141             goto err;
142         }
143     }
144
145     ret = xkb_keysym_get_name(keysym, name, sizeof(name));
146     if (ret < 0 || (size_t) ret >= sizeof(name)) {
147         fprintf(stderr, "Failed to get name of keysym\n");
148         goto err;
149     }
150
151     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
152     if (!ctx) {
153         fprintf(stderr, "Failed to create XKB context\n");
154         goto err;
155     }
156
157     struct xkb_rule_names names = {
158         .rules = rules,
159         .model = model,
160         .layout = layout_,
161         .variant = variant,
162         .options = options,
163     };
164     keymap = xkb_keymap_new_from_names(ctx, &names,
165                                        XKB_KEYMAP_COMPILE_NO_FLAGS);
166     if (!keymap) {
167         fprintf(stderr, "Failed to create XKB keymap\n");
168         goto err;
169     }
170
171     printf("keysym: %s (%#x)\n", name, keysym);
172     printf("%-8s %-9s %-8s %-20s %-7s %-s\n",
173            "KEYCODE", "KEY NAME", "LAYOUT", "LAYOUT NAME", "LEVEL#", "MODIFIERS");
174
175     min_keycode = xkb_keymap_min_keycode(keymap);
176     max_keycode = xkb_keymap_max_keycode(keymap);
177     num_mods = xkb_keymap_num_mods(keymap);
178     for (xkb_keycode_t keycode = min_keycode; keycode <= max_keycode; keycode++) {
179         const char *key_name;
180         xkb_layout_index_t num_layouts;
181
182         key_name = xkb_keymap_key_get_name(keymap, keycode);
183         if (!key_name) {
184             continue;
185         }
186
187         num_layouts = xkb_keymap_num_layouts_for_key(keymap, keycode);
188         for (xkb_layout_index_t layout = 0; layout < num_layouts; layout++) {
189             const char *layout_name;
190             xkb_level_index_t num_levels;
191
192             layout_name = xkb_keymap_layout_get_name(keymap, layout);
193             if (!layout_name) {
194                 layout_name = "?";
195             }
196
197             num_levels = xkb_keymap_num_levels_for_key(keymap, keycode, layout);
198             for (xkb_level_index_t level = 0; level < num_levels; level++) {
199                 int num_syms;
200                 const xkb_keysym_t *syms;
201                 size_t num_masks;
202                 xkb_mod_mask_t masks[100];
203
204                 num_syms = xkb_keymap_key_get_syms_by_level(
205                     keymap, keycode, layout, level, &syms
206                 );
207                 if (num_syms != 1) {
208                     continue;
209                 }
210                 if (syms[0] != keysym) {
211                     continue;
212                 }
213
214                 num_masks = xkb_keymap_key_get_mods_for_level(
215                     keymap, keycode, layout, level, masks, ARRAY_SIZE(masks)
216                 );
217                 for (size_t i = 0; i < num_masks; i++) {
218                     xkb_mod_mask_t mask = masks[i];
219
220                     printf("%-8u %-9s %-8u %-20s %-7u [ ",
221                            keycode, key_name, layout + 1, layout_name, level + 1);
222                     for (xkb_mod_index_t mod = 0; mod < num_mods; mod++) {
223                         if ((mask & (1 << mod)) == 0) {
224                             continue;
225                         }
226                         printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
227                     }
228                     printf("]\n");
229                 }
230             }
231         }
232     }
233
234     err = EXIT_SUCCESS;
235 err:
236     xkb_keymap_unref(keymap);
237     xkb_context_unref(ctx);
238     return err;
239 }