test: add "how to type" demo program
[platform/upstream/libxkbcommon.git] / test / 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 <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include <xkbcommon/xkbcommon.h>
31
32 static void
33 usage(const char *argv0)
34 {
35     fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
36             "[-l <layout>] [-v <variant>] [-o <options>] <unicode codepoint>\n",
37             argv0);
38     fprintf(stderr, "Pipe into `column -ts $'\\t'` for nice aligned output.\n");
39     exit(2);
40 }
41
42 int
43 main(int argc, char *argv[])
44 {
45     int opt;
46     const char *rules = NULL;
47     const char *model = NULL;
48     const char *layout_ = NULL;
49     const char *variant = NULL;
50     const char *options = NULL;
51     int exit = EXIT_FAILURE;
52     struct xkb_context *ctx;
53     char *endp;
54     long val;
55     uint32_t codepoint;
56     xkb_keysym_t keysym;
57     int ret;
58     char name[200];
59     struct xkb_keymap *keymap;
60     xkb_keycode_t min_keycode, max_keycode;
61     xkb_mod_index_t num_mods;
62
63     while ((opt = getopt(argc, argv, "r:m:l:v:o:")) != -1) {
64         switch (opt) {
65         case 'r':
66             rules = optarg;
67             break;
68         case 'm':
69             model = optarg;
70             break;
71         case 'l':
72             layout_ = optarg;
73             break;
74         case 'v':
75             variant = optarg;
76             break;
77         case 'o':
78             options = optarg;
79             break;
80         default:
81             usage(argv[0]);
82         }
83     }
84     if (argc - optind != 1) {
85         usage(argv[0]);
86     }
87
88     errno = 0;
89     val = strtol(argv[optind], &endp, 0);
90     if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) {
91         fprintf(stderr, "usage: %s <unicode codepoint>\n", argv[0]);
92         goto err_exit;
93     }
94     codepoint = (uint32_t) val;
95
96     keysym = xkb_utf32_to_keysym(codepoint);
97     if (keysym == XKB_KEY_NoSymbol) {
98         fprintf(stderr, "Failed to convert codepoint to keysym");
99         goto err_exit;
100     }
101
102     ret = xkb_keysym_get_name(keysym, name, sizeof(name));
103     if (ret < 0 || (size_t) ret >= sizeof(name)) {
104         fprintf(stderr, "Failed to get name of keysym");
105         goto err_exit;
106     }
107
108     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
109     if (!ctx) {
110         fprintf(stderr, "Failed to create XKB context\n");
111         goto err_exit;
112     }
113
114     struct xkb_rule_names names = {
115         .rules = rules,
116         .model = model,
117         .layout = layout_,
118         .variant = variant,
119         .options = options,
120     };
121     keymap = xkb_keymap_new_from_names(ctx, &names,
122                                        XKB_KEYMAP_COMPILE_NO_FLAGS);
123     if (!keymap) {
124         fprintf(stderr, "Failed to create XKB keymap\n");
125         goto err_ctx;
126     }
127
128     printf("keysym: %s (%#x)\n", name, keysym);
129     printf("KEYCODE\tKEY NAME\tLAYOUT#\tLAYOUT NAME\tLEVEL#\tMODIFIERS\n");
130
131     min_keycode = xkb_keymap_min_keycode(keymap);
132     max_keycode = xkb_keymap_max_keycode(keymap);
133     num_mods = xkb_keymap_num_mods(keymap);
134     for (xkb_keycode_t keycode = min_keycode; keycode <= max_keycode; keycode++) {
135         const char *key_name;
136         xkb_layout_index_t num_layouts;
137
138         key_name = xkb_keymap_key_get_name(keymap, keycode);
139         if (!key_name) {
140             continue;
141         }
142
143         num_layouts = xkb_keymap_num_layouts_for_key(keymap, keycode);
144         for (xkb_layout_index_t layout = 0; layout < num_layouts; layout++) {
145             const char *layout_name;
146             xkb_level_index_t num_levels;
147
148             layout_name = xkb_keymap_layout_get_name(keymap, layout);
149             if (!layout_name) {
150                 layout_name = "?";
151             }
152
153             num_levels = xkb_keymap_num_levels_for_key(keymap, keycode, layout);
154             for (xkb_level_index_t level = 0; level < num_levels; level++) {
155                 int num_syms;
156                 const xkb_keysym_t *syms;
157                 size_t num_masks;
158                 xkb_mod_mask_t masks[100];
159
160                 num_syms = xkb_keymap_key_get_syms_by_level(
161                     keymap, keycode, layout, level, &syms
162                 );
163                 if (num_syms != 1) {
164                     continue;
165                 }
166                 if (syms[0] != keysym) {
167                     continue;
168                 }
169
170                 num_masks = xkb_keymap_key_get_mods_for_level(
171                     keymap, keycode, layout, level, masks, 100
172                 );
173                 for (size_t i = 0; i < num_masks; i++) {
174                     xkb_mod_mask_t mask = masks[i];
175
176                     printf("%u\t%s\t%u\t%s\t%u\t[ ",
177                            keycode, key_name, layout + 1, layout_name, level + 1);
178                     for (xkb_mod_index_t mod = 0; mod < num_mods; mod++) {
179                         if ((mask & (1 << mod)) == 0) {
180                             continue;
181                         }
182                         printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
183                     }
184                     printf("]\n");
185                 }
186             }
187         }
188     }
189
190     exit = EXIT_SUCCESS;
191     xkb_keymap_unref(keymap);
192 err_ctx:
193     xkb_context_unref(ctx);
194 err_exit:
195     return exit;
196 }