2 * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
3 * Copyright © 2012 Intel Corporation
4 * Copyright © 2012 Ran Benita <ran234@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Except as contained in this notice, the names of the authors or their
24 * institutions shall not be used in advertising or otherwise to promote the
25 * sale, use or other dealings in this Software without prior written
26 * authorization from the authors.
28 * Author: Dan Nicholson <dbn.lists@gmail.com>
29 * Daniel Stone <daniel@fooishbar.org>
30 * Ran Benita <ran234@gmail.com>
41 #include <sys/types.h>
51 #include "tools-common.h"
54 print_keycode(struct xkb_keymap *keymap, const char* prefix,
55 xkb_keycode_t keycode, const char *suffix) {
56 const char *keyname = xkb_keymap_key_get_name(keymap, keycode);
58 printf("%s%-4s%s", prefix, keyname, suffix);
60 printf("%s%-4d%s", prefix, keycode, suffix);
64 #ifdef ENABLE_PRIVATE_APIS
65 #include "src/keymap.h"
68 print_keymap_modmaps(struct xkb_keymap *keymap) {
69 printf("Modifiers mapping:\n");
70 for (xkb_mod_index_t vmod = 0; vmod < xkb_keymap_num_mods(keymap); vmod++) {
71 if (keymap->mods.mods[vmod].type & MOD_REAL)
73 printf("- %s: ", xkb_keymap_mod_get_name(keymap, vmod));
74 if (keymap->mods.mods[vmod].mapping) {
76 for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
77 if (keymap->mods.mods[vmod].mapping & (1u << mod)) {
80 printf("%s", xkb_keymap_mod_get_name(keymap, mod));
82 printf("+ %s", xkb_keymap_mod_get_name(keymap, mod));
93 #define MODMAP_PADDING 7
94 #define VMODMAP_PADDING 9
96 print_key_modmaps(struct xkb_keymap *keymap, xkb_keycode_t keycode) {
97 const struct xkb_key *key = XkbKey(keymap, keycode);
103 for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
104 if (key->modmap & (1u << mod)) {
105 printf("%-*s", (int) MODMAP_PADDING,
106 xkb_keymap_mod_get_name(keymap, mod));
111 printf("%*c", (int) MODMAP_PADDING, ' ');
114 printf(" ] vmodmap [ ");
116 const char *mod_name;
117 for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
118 if (key->vmodmap & (1u << mod)) {
119 mod_name = xkb_keymap_mod_get_name(keymap, mod);
120 length += strlen(mod_name) + 1;
121 printf("%s ", mod_name);
124 if (length < VMODMAP_PADDING) {
125 printf("%*c", (int) VMODMAP_PADDING - length, ' ');
132 print_keys_modmaps(struct xkb_keymap *keymap) {
133 const struct xkb_key *key;
134 printf("Keys modmaps:\n");
135 xkb_keys_foreach(key, keymap) {
136 if (key->modmap || key->vmodmap) {
137 print_keycode(keymap, "- ", key->keycode, ": ");
138 print_key_modmaps(keymap, key->keycode);
146 tools_print_keycode_state(struct xkb_state *state,
147 struct xkb_compose_state *compose_state,
148 xkb_keycode_t keycode,
149 enum xkb_consumed_mode consumed_mode,
150 print_state_fields_mask_t fields)
152 struct xkb_keymap *keymap;
155 const xkb_keysym_t *syms;
158 xkb_layout_index_t layout;
159 enum xkb_compose_status status;
161 keymap = xkb_state_get_keymap(state);
163 nsyms = xkb_state_key_get_syms(state, keycode, &syms);
168 status = XKB_COMPOSE_NOTHING;
170 status = xkb_compose_state_get_status(compose_state);
172 if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
175 if (status == XKB_COMPOSE_COMPOSED) {
176 sym = xkb_compose_state_get_one_sym(compose_state);
180 else if (nsyms == 1) {
181 sym = xkb_state_key_get_one_sym(state, keycode);
185 print_keycode(keymap, "keycode [ ", keycode, " ] ");
187 #ifdef ENABLE_PRIVATE_APIS
188 if (fields & PRINT_MODMAPS) {
189 print_key_modmaps(keymap, keycode);
193 printf("keysyms [ ");
194 for (int i = 0; i < nsyms; i++) {
195 xkb_keysym_get_name(syms[i], s, sizeof(s));
196 printf("%-*s ", (int) sizeof(s), s);
200 if (fields & PRINT_UNICODE) {
201 if (status == XKB_COMPOSE_COMPOSED)
202 xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
204 xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
205 /* HACK: escape single control characters from C0 set using the
206 * Unicode codepoint convention. Ideally we would like to escape
207 * any non-printable character in the string.
210 printf("unicode [ ] ");
211 } else if (strlen(s) == 1 && (*s <= 0x1F || *s == 0x7F)) {
212 printf("unicode [ U+%04hX ] ", *s);
214 printf("unicode [ %s ] ", s);
218 layout = xkb_state_key_get_layout(state, keycode);
219 if (fields & PRINT_LAYOUT) {
220 printf("layout [ %s (%d) ] ",
221 xkb_keymap_layout_get_name(keymap, layout), layout);
224 printf("level [ %d ] ",
225 xkb_state_key_get_level(state, keycode, layout));
228 for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
229 if (xkb_state_mod_index_is_active(state, mod,
230 XKB_STATE_MODS_EFFECTIVE) <= 0)
232 if (xkb_state_mod_index_is_consumed2(state, keycode, mod,
234 printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
236 printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
241 for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
242 if (xkb_state_led_index_is_active(state, led) <= 0)
244 printf("%s ", xkb_keymap_led_get_name(keymap, led));
252 tools_print_state_changes(enum xkb_state_component changed)
257 printf("changed [ ");
258 if (changed & XKB_STATE_LAYOUT_EFFECTIVE)
259 printf("effective-layout ");
260 if (changed & XKB_STATE_LAYOUT_DEPRESSED)
261 printf("depressed-layout ");
262 if (changed & XKB_STATE_LAYOUT_LATCHED)
263 printf("latched-layout ");
264 if (changed & XKB_STATE_LAYOUT_LOCKED)
265 printf("locked-layout ");
266 if (changed & XKB_STATE_MODS_EFFECTIVE)
267 printf("effective-mods ");
268 if (changed & XKB_STATE_MODS_DEPRESSED)
269 printf("depressed-mods ");
270 if (changed & XKB_STATE_MODS_LATCHED)
271 printf("latched-mods ");
272 if (changed & XKB_STATE_MODS_LOCKED)
273 printf("locked-mods ");
274 if (changed & XKB_STATE_LEDS)
281 tools_disable_stdin_echo(void)
283 HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
285 GetConsoleMode(stdin_handle, &mode);
286 SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
290 tools_enable_stdin_echo(void)
292 HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
294 GetConsoleMode(stdin_handle, &mode);
295 SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
299 tools_disable_stdin_echo(void)
301 /* Same as `stty -echo`. */
302 struct termios termios;
303 if (tcgetattr(STDIN_FILENO, &termios) == 0) {
304 termios.c_lflag &= ~ECHO;
305 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
310 tools_enable_stdin_echo(void)
312 /* Same as `stty echo`. */
313 struct termios termios;
314 if (tcgetattr(STDIN_FILENO, &termios) == 0) {
315 termios.c_lflag |= ECHO;
316 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
323 tools_exec_command(const char *prefix, int real_argc, char **real_argv)
325 char *argv[64] = {NULL};
326 char executable[PATH_MAX];
330 if (((size_t)real_argc >= ARRAY_SIZE(argv))) {
331 fprintf(stderr, "Too many arguments\n");
332 return EXIT_INVALID_USAGE;
335 command = real_argv[0];
337 rc = snprintf(executable, sizeof(executable),
338 "%s/%s-%s", LIBXKBCOMMON_TOOL_PATH, prefix, command);
339 if (rc < 0 || (size_t) rc >= sizeof(executable)) {
340 fprintf(stderr, "Failed to assemble command\n");
344 argv[0] = executable;
345 for (int i = 1; i < real_argc; i++)
346 argv[i] = real_argv[i];
348 execv(executable, argv);
349 if (errno == ENOENT) {
350 fprintf(stderr, "Command '%s' is not available\n", command);
351 return EXIT_INVALID_USAGE;
353 fprintf(stderr, "Failed to execute '%s' (%s)\n",
354 command, strerror(errno));