Removed build dependency on kbproto.
[platform/upstream/libxkbcommon.git] / tools / tools-common.c
1 /*
2  * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
3  * Copyright © 2012 Intel Corporation
4  * Copyright © 2012 Ran Benita <ran234@gmail.com>
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
22  *
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.
27  *
28  * Author: Dan Nicholson <dbn.lists@gmail.com>
29  *         Daniel Stone <daniel@fooishbar.org>
30  *         Ran Benita <ran234@gmail.com>
31  */
32
33 #include "config.h"
34
35 #include <ctype.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #ifdef _WIN32
44 #include <io.h>
45 #include <windows.h>
46 #else
47 #include <unistd.h>
48 #include <termios.h>
49 #endif
50
51 #include "tools-common.h"
52
53 static void
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);
57     if (keyname) {
58         printf("%s%-4s%s", prefix, keyname, suffix);
59     } else {
60         printf("%s%-4d%s", prefix, keycode, suffix);
61     }
62 }
63
64 #ifdef ENABLE_PRIVATE_APIS
65 #include "src/keymap.h"
66
67 void
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)
72             continue;
73         printf("- %s: ", xkb_keymap_mod_get_name(keymap, vmod));
74         if (keymap->mods.mods[vmod].mapping) {
75             bool first = true;
76             for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
77                 if (keymap->mods.mods[vmod].mapping & (1u << mod)) {
78                     if (first) {
79                         first = false;
80                         printf("%s", xkb_keymap_mod_get_name(keymap, mod));
81                     } else {
82                         printf("+ %s", xkb_keymap_mod_get_name(keymap, mod));
83                     }
84                 }
85             }
86         } else {
87             printf("(unmapped)");
88         }
89         printf("\n");
90     }
91 }
92
93 #define MODMAP_PADDING  7
94 #define VMODMAP_PADDING 9
95 static void
96 print_key_modmaps(struct xkb_keymap *keymap, xkb_keycode_t keycode) {
97     const struct xkb_key *key = XkbKey(keymap, keycode);
98     if (key != NULL) {
99         xkb_mod_index_t mod;
100
101         printf("modmap [ ");
102         if (key->modmap) {
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));
107                     break;
108                 }
109             }
110         } else {
111             printf("%*c", (int) MODMAP_PADDING, ' ');
112         }
113
114         printf(" ] vmodmap [ ");
115         int length = 0;
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);
122             }
123         }
124         if (length < VMODMAP_PADDING) {
125             printf("%*c", (int) VMODMAP_PADDING - length, ' ');
126         }
127         printf("] ");
128     }
129 }
130
131 void
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);
139             putchar('\n');
140         }
141     }
142 }
143 #endif
144
145 void
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)
151 {
152     struct xkb_keymap *keymap;
153
154     xkb_keysym_t sym;
155     const xkb_keysym_t *syms;
156     int nsyms;
157     char s[16];
158     xkb_layout_index_t layout;
159     enum xkb_compose_status status;
160
161     keymap = xkb_state_get_keymap(state);
162
163     nsyms = xkb_state_key_get_syms(state, keycode, &syms);
164
165     if (nsyms <= 0)
166         return;
167
168     status = XKB_COMPOSE_NOTHING;
169     if (compose_state)
170         status = xkb_compose_state_get_status(compose_state);
171
172     if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
173         return;
174
175     if (status == XKB_COMPOSE_COMPOSED) {
176         sym = xkb_compose_state_get_one_sym(compose_state);
177         syms = &sym;
178         nsyms = 1;
179     }
180     else if (nsyms == 1) {
181         sym = xkb_state_key_get_one_sym(state, keycode);
182         syms = &sym;
183     }
184
185     print_keycode(keymap, "keycode [ ", keycode, " ] ");
186
187 #ifdef ENABLE_PRIVATE_APIS
188     if (fields & PRINT_MODMAPS) {
189         print_key_modmaps(keymap, keycode);
190     }
191 #endif
192
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);
197     }
198     printf("] ");
199
200     if (fields & PRINT_UNICODE) {
201         if (status == XKB_COMPOSE_COMPOSED)
202             xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
203         else
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.
208         */
209         if (!*s) {
210             printf("unicode [   ] ");
211         } else if (strlen(s) == 1 && (*s <= 0x1F || *s == 0x7F)) {
212             printf("unicode [ U+%04hX ] ", *s);
213         } else {
214             printf("unicode [ %s ] ", s);
215         }
216     }
217
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);
222     }
223
224     printf("level [ %d ] ",
225            xkb_state_key_get_level(state, keycode, layout));
226
227     printf("mods [ ");
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)
231             continue;
232         if (xkb_state_mod_index_is_consumed2(state, keycode, mod,
233                                              consumed_mode))
234             printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
235         else
236             printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
237     }
238     printf("] ");
239
240     printf("leds [ ");
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)
243             continue;
244         printf("%s ", xkb_keymap_led_get_name(keymap, led));
245     }
246     printf("] ");
247
248     printf("\n");
249 }
250
251 void
252 tools_print_state_changes(enum xkb_state_component changed)
253 {
254     if (changed == 0)
255         return;
256
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)
275         printf("leds ");
276     printf("]\n");
277 }
278
279 #ifdef _WIN32
280 void
281 tools_disable_stdin_echo(void)
282 {
283     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
284     DWORD mode = 0;
285     GetConsoleMode(stdin_handle, &mode);
286     SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
287 }
288
289 void
290 tools_enable_stdin_echo(void)
291 {
292     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
293     DWORD mode = 0;
294     GetConsoleMode(stdin_handle, &mode);
295     SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
296 }
297 #else
298 void
299 tools_disable_stdin_echo(void)
300 {
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);
306     }
307 }
308
309 void
310 tools_enable_stdin_echo(void)
311 {
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);
317     }
318 }
319
320 #endif
321
322 int
323 tools_exec_command(const char *prefix, int real_argc, char **real_argv)
324 {
325     char *argv[64] = {NULL};
326     char executable[PATH_MAX];
327     const char *command;
328     int rc;
329
330     if (((size_t)real_argc >= ARRAY_SIZE(argv))) {
331         fprintf(stderr, "Too many arguments\n");
332         return EXIT_INVALID_USAGE;
333     }
334
335     command = real_argv[0];
336
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");
341         return EXIT_FAILURE;
342     }
343
344     argv[0] = executable;
345     for (int i = 1; i < real_argc; i++)
346         argv[i] = real_argv[i];
347
348     execv(executable, argv);
349     if (errno == ENOENT) {
350         fprintf(stderr, "Command '%s' is not available\n", command);
351         return EXIT_INVALID_USAGE;
352     } else {
353         fprintf(stderr, "Failed to execute '%s' (%s)\n",
354                 command, strerror(errno));
355     }
356
357     return EXIT_FAILURE;
358 }