xref: /aosp_15_r20/external/libxkbcommon/tools/tools-common.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2009 Dan Nicholson <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Intel Corporation
4*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Ran Benita <[email protected]>
5*2b949d04SAndroid Build Coastguard Worker  *
6*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
7*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
8*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
9*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
11*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
12*2b949d04SAndroid Build Coastguard Worker  *
13*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be included in
14*2b949d04SAndroid Build Coastguard Worker  * all copies or substantial portions of the Software.
15*2b949d04SAndroid Build Coastguard Worker  *
16*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*2b949d04SAndroid Build Coastguard Worker  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*2b949d04SAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*2b949d04SAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  *
23*2b949d04SAndroid Build Coastguard Worker  * Except as contained in this notice, the names of the authors or their
24*2b949d04SAndroid Build Coastguard Worker  * institutions shall not be used in advertising or otherwise to promote the
25*2b949d04SAndroid Build Coastguard Worker  * sale, use or other dealings in this Software without prior written
26*2b949d04SAndroid Build Coastguard Worker  * authorization from the authors.
27*2b949d04SAndroid Build Coastguard Worker  *
28*2b949d04SAndroid Build Coastguard Worker  * Author: Dan Nicholson <[email protected]>
29*2b949d04SAndroid Build Coastguard Worker  *         Daniel Stone <[email protected]>
30*2b949d04SAndroid Build Coastguard Worker  *         Ran Benita <[email protected]>
31*2b949d04SAndroid Build Coastguard Worker  */
32*2b949d04SAndroid Build Coastguard Worker 
33*2b949d04SAndroid Build Coastguard Worker #include "config.h"
34*2b949d04SAndroid Build Coastguard Worker 
35*2b949d04SAndroid Build Coastguard Worker #include <errno.h>
36*2b949d04SAndroid Build Coastguard Worker #include <limits.h>
37*2b949d04SAndroid Build Coastguard Worker #include <fcntl.h>
38*2b949d04SAndroid Build Coastguard Worker #include <stdlib.h>
39*2b949d04SAndroid Build Coastguard Worker #include <string.h>
40*2b949d04SAndroid Build Coastguard Worker #include <sys/types.h>
41*2b949d04SAndroid Build Coastguard Worker #include <sys/stat.h>
42*2b949d04SAndroid Build Coastguard Worker #ifdef _MSC_VER
43*2b949d04SAndroid Build Coastguard Worker #include <io.h>
44*2b949d04SAndroid Build Coastguard Worker #include <windows.h>
45*2b949d04SAndroid Build Coastguard Worker #else
46*2b949d04SAndroid Build Coastguard Worker #include <unistd.h>
47*2b949d04SAndroid Build Coastguard Worker #include <termios.h>
48*2b949d04SAndroid Build Coastguard Worker #endif
49*2b949d04SAndroid Build Coastguard Worker 
50*2b949d04SAndroid Build Coastguard Worker #include "tools-common.h"
51*2b949d04SAndroid Build Coastguard Worker 
52*2b949d04SAndroid Build Coastguard Worker void
tools_print_keycode_state(struct xkb_state * state,struct xkb_compose_state * compose_state,xkb_keycode_t keycode,enum xkb_consumed_mode consumed_mode)53*2b949d04SAndroid Build Coastguard Worker tools_print_keycode_state(struct xkb_state *state,
54*2b949d04SAndroid Build Coastguard Worker                           struct xkb_compose_state *compose_state,
55*2b949d04SAndroid Build Coastguard Worker                           xkb_keycode_t keycode,
56*2b949d04SAndroid Build Coastguard Worker                           enum xkb_consumed_mode consumed_mode)
57*2b949d04SAndroid Build Coastguard Worker {
58*2b949d04SAndroid Build Coastguard Worker     struct xkb_keymap *keymap;
59*2b949d04SAndroid Build Coastguard Worker 
60*2b949d04SAndroid Build Coastguard Worker     xkb_keysym_t sym;
61*2b949d04SAndroid Build Coastguard Worker     const xkb_keysym_t *syms;
62*2b949d04SAndroid Build Coastguard Worker     int nsyms;
63*2b949d04SAndroid Build Coastguard Worker     char s[16];
64*2b949d04SAndroid Build Coastguard Worker     xkb_layout_index_t layout;
65*2b949d04SAndroid Build Coastguard Worker     enum xkb_compose_status status;
66*2b949d04SAndroid Build Coastguard Worker 
67*2b949d04SAndroid Build Coastguard Worker     keymap = xkb_state_get_keymap(state);
68*2b949d04SAndroid Build Coastguard Worker 
69*2b949d04SAndroid Build Coastguard Worker     nsyms = xkb_state_key_get_syms(state, keycode, &syms);
70*2b949d04SAndroid Build Coastguard Worker 
71*2b949d04SAndroid Build Coastguard Worker     if (nsyms <= 0)
72*2b949d04SAndroid Build Coastguard Worker         return;
73*2b949d04SAndroid Build Coastguard Worker 
74*2b949d04SAndroid Build Coastguard Worker     status = XKB_COMPOSE_NOTHING;
75*2b949d04SAndroid Build Coastguard Worker     if (compose_state)
76*2b949d04SAndroid Build Coastguard Worker         status = xkb_compose_state_get_status(compose_state);
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker     if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
79*2b949d04SAndroid Build Coastguard Worker         return;
80*2b949d04SAndroid Build Coastguard Worker 
81*2b949d04SAndroid Build Coastguard Worker     if (status == XKB_COMPOSE_COMPOSED) {
82*2b949d04SAndroid Build Coastguard Worker         sym = xkb_compose_state_get_one_sym(compose_state);
83*2b949d04SAndroid Build Coastguard Worker         syms = &sym;
84*2b949d04SAndroid Build Coastguard Worker         nsyms = 1;
85*2b949d04SAndroid Build Coastguard Worker     }
86*2b949d04SAndroid Build Coastguard Worker     else if (nsyms == 1) {
87*2b949d04SAndroid Build Coastguard Worker         sym = xkb_state_key_get_one_sym(state, keycode);
88*2b949d04SAndroid Build Coastguard Worker         syms = &sym;
89*2b949d04SAndroid Build Coastguard Worker     }
90*2b949d04SAndroid Build Coastguard Worker 
91*2b949d04SAndroid Build Coastguard Worker     printf("keysyms [ ");
92*2b949d04SAndroid Build Coastguard Worker     for (int i = 0; i < nsyms; i++) {
93*2b949d04SAndroid Build Coastguard Worker         xkb_keysym_get_name(syms[i], s, sizeof(s));
94*2b949d04SAndroid Build Coastguard Worker         printf("%-*s ", (int) sizeof(s), s);
95*2b949d04SAndroid Build Coastguard Worker     }
96*2b949d04SAndroid Build Coastguard Worker     printf("] ");
97*2b949d04SAndroid Build Coastguard Worker 
98*2b949d04SAndroid Build Coastguard Worker     if (status == XKB_COMPOSE_COMPOSED)
99*2b949d04SAndroid Build Coastguard Worker         xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
100*2b949d04SAndroid Build Coastguard Worker     else
101*2b949d04SAndroid Build Coastguard Worker         xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
102*2b949d04SAndroid Build Coastguard Worker     printf("unicode [ %s ] ", s);
103*2b949d04SAndroid Build Coastguard Worker 
104*2b949d04SAndroid Build Coastguard Worker     layout = xkb_state_key_get_layout(state, keycode);
105*2b949d04SAndroid Build Coastguard Worker     printf("layout [ %s (%d) ] ",
106*2b949d04SAndroid Build Coastguard Worker            xkb_keymap_layout_get_name(keymap, layout), layout);
107*2b949d04SAndroid Build Coastguard Worker 
108*2b949d04SAndroid Build Coastguard Worker     printf("level [ %d ] ",
109*2b949d04SAndroid Build Coastguard Worker            xkb_state_key_get_level(state, keycode, layout));
110*2b949d04SAndroid Build Coastguard Worker 
111*2b949d04SAndroid Build Coastguard Worker     printf("mods [ ");
112*2b949d04SAndroid Build Coastguard Worker     for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
113*2b949d04SAndroid Build Coastguard Worker         if (xkb_state_mod_index_is_active(state, mod,
114*2b949d04SAndroid Build Coastguard Worker                                           XKB_STATE_MODS_EFFECTIVE) <= 0)
115*2b949d04SAndroid Build Coastguard Worker             continue;
116*2b949d04SAndroid Build Coastguard Worker         if (xkb_state_mod_index_is_consumed2(state, keycode, mod,
117*2b949d04SAndroid Build Coastguard Worker                                              consumed_mode))
118*2b949d04SAndroid Build Coastguard Worker             printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
119*2b949d04SAndroid Build Coastguard Worker         else
120*2b949d04SAndroid Build Coastguard Worker             printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
121*2b949d04SAndroid Build Coastguard Worker     }
122*2b949d04SAndroid Build Coastguard Worker     printf("] ");
123*2b949d04SAndroid Build Coastguard Worker 
124*2b949d04SAndroid Build Coastguard Worker     printf("leds [ ");
125*2b949d04SAndroid Build Coastguard Worker     for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
126*2b949d04SAndroid Build Coastguard Worker         if (xkb_state_led_index_is_active(state, led) <= 0)
127*2b949d04SAndroid Build Coastguard Worker             continue;
128*2b949d04SAndroid Build Coastguard Worker         printf("%s ", xkb_keymap_led_get_name(keymap, led));
129*2b949d04SAndroid Build Coastguard Worker     }
130*2b949d04SAndroid Build Coastguard Worker     printf("] ");
131*2b949d04SAndroid Build Coastguard Worker 
132*2b949d04SAndroid Build Coastguard Worker     printf("\n");
133*2b949d04SAndroid Build Coastguard Worker }
134*2b949d04SAndroid Build Coastguard Worker 
135*2b949d04SAndroid Build Coastguard Worker void
tools_print_state_changes(enum xkb_state_component changed)136*2b949d04SAndroid Build Coastguard Worker tools_print_state_changes(enum xkb_state_component changed)
137*2b949d04SAndroid Build Coastguard Worker {
138*2b949d04SAndroid Build Coastguard Worker     if (changed == 0)
139*2b949d04SAndroid Build Coastguard Worker         return;
140*2b949d04SAndroid Build Coastguard Worker 
141*2b949d04SAndroid Build Coastguard Worker     printf("changed [ ");
142*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_LAYOUT_EFFECTIVE)
143*2b949d04SAndroid Build Coastguard Worker         printf("effective-layout ");
144*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_LAYOUT_DEPRESSED)
145*2b949d04SAndroid Build Coastguard Worker         printf("depressed-layout ");
146*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_LAYOUT_LATCHED)
147*2b949d04SAndroid Build Coastguard Worker         printf("latched-layout ");
148*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_LAYOUT_LOCKED)
149*2b949d04SAndroid Build Coastguard Worker         printf("locked-layout ");
150*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_MODS_EFFECTIVE)
151*2b949d04SAndroid Build Coastguard Worker         printf("effective-mods ");
152*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_MODS_DEPRESSED)
153*2b949d04SAndroid Build Coastguard Worker         printf("depressed-mods ");
154*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_MODS_LATCHED)
155*2b949d04SAndroid Build Coastguard Worker         printf("latched-mods ");
156*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_MODS_LOCKED)
157*2b949d04SAndroid Build Coastguard Worker         printf("locked-mods ");
158*2b949d04SAndroid Build Coastguard Worker     if (changed & XKB_STATE_LEDS)
159*2b949d04SAndroid Build Coastguard Worker         printf("leds ");
160*2b949d04SAndroid Build Coastguard Worker     printf("]\n");
161*2b949d04SAndroid Build Coastguard Worker }
162*2b949d04SAndroid Build Coastguard Worker 
163*2b949d04SAndroid Build Coastguard Worker #ifdef _MSC_VER
164*2b949d04SAndroid Build Coastguard Worker void
tools_disable_stdin_echo(void)165*2b949d04SAndroid Build Coastguard Worker tools_disable_stdin_echo(void)
166*2b949d04SAndroid Build Coastguard Worker {
167*2b949d04SAndroid Build Coastguard Worker     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
168*2b949d04SAndroid Build Coastguard Worker     DWORD mode = 0;
169*2b949d04SAndroid Build Coastguard Worker     GetConsoleMode(stdin_handle, &mode);
170*2b949d04SAndroid Build Coastguard Worker     SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
171*2b949d04SAndroid Build Coastguard Worker }
172*2b949d04SAndroid Build Coastguard Worker 
173*2b949d04SAndroid Build Coastguard Worker void
tools_enable_stdin_echo(void)174*2b949d04SAndroid Build Coastguard Worker tools_enable_stdin_echo(void)
175*2b949d04SAndroid Build Coastguard Worker {
176*2b949d04SAndroid Build Coastguard Worker     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
177*2b949d04SAndroid Build Coastguard Worker     DWORD mode = 0;
178*2b949d04SAndroid Build Coastguard Worker     GetConsoleMode(stdin_handle, &mode);
179*2b949d04SAndroid Build Coastguard Worker     SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
180*2b949d04SAndroid Build Coastguard Worker }
181*2b949d04SAndroid Build Coastguard Worker #else
182*2b949d04SAndroid Build Coastguard Worker void
tools_disable_stdin_echo(void)183*2b949d04SAndroid Build Coastguard Worker tools_disable_stdin_echo(void)
184*2b949d04SAndroid Build Coastguard Worker {
185*2b949d04SAndroid Build Coastguard Worker     /* Same as `stty -echo`. */
186*2b949d04SAndroid Build Coastguard Worker     struct termios termios;
187*2b949d04SAndroid Build Coastguard Worker     if (tcgetattr(STDIN_FILENO, &termios) == 0) {
188*2b949d04SAndroid Build Coastguard Worker         termios.c_lflag &= ~ECHO;
189*2b949d04SAndroid Build Coastguard Worker         (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
190*2b949d04SAndroid Build Coastguard Worker     }
191*2b949d04SAndroid Build Coastguard Worker }
192*2b949d04SAndroid Build Coastguard Worker 
193*2b949d04SAndroid Build Coastguard Worker void
tools_enable_stdin_echo(void)194*2b949d04SAndroid Build Coastguard Worker tools_enable_stdin_echo(void)
195*2b949d04SAndroid Build Coastguard Worker {
196*2b949d04SAndroid Build Coastguard Worker     /* Same as `stty echo`. */
197*2b949d04SAndroid Build Coastguard Worker     struct termios termios;
198*2b949d04SAndroid Build Coastguard Worker     if (tcgetattr(STDIN_FILENO, &termios) == 0) {
199*2b949d04SAndroid Build Coastguard Worker         termios.c_lflag |= ECHO;
200*2b949d04SAndroid Build Coastguard Worker         (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
201*2b949d04SAndroid Build Coastguard Worker     }
202*2b949d04SAndroid Build Coastguard Worker }
203*2b949d04SAndroid Build Coastguard Worker 
204*2b949d04SAndroid Build Coastguard Worker #endif
205*2b949d04SAndroid Build Coastguard Worker 
206*2b949d04SAndroid Build Coastguard Worker int
tools_exec_command(const char * prefix,int real_argc,char ** real_argv)207*2b949d04SAndroid Build Coastguard Worker tools_exec_command(const char *prefix, int real_argc, char **real_argv)
208*2b949d04SAndroid Build Coastguard Worker {
209*2b949d04SAndroid Build Coastguard Worker     char *argv[64] = {NULL};
210*2b949d04SAndroid Build Coastguard Worker     char executable[PATH_MAX];
211*2b949d04SAndroid Build Coastguard Worker     const char *command;
212*2b949d04SAndroid Build Coastguard Worker     int rc;
213*2b949d04SAndroid Build Coastguard Worker 
214*2b949d04SAndroid Build Coastguard Worker     if (((size_t)real_argc >= ARRAY_SIZE(argv))) {
215*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Too many arguments\n");
216*2b949d04SAndroid Build Coastguard Worker         return EXIT_INVALID_USAGE;
217*2b949d04SAndroid Build Coastguard Worker     }
218*2b949d04SAndroid Build Coastguard Worker 
219*2b949d04SAndroid Build Coastguard Worker     command = real_argv[0];
220*2b949d04SAndroid Build Coastguard Worker 
221*2b949d04SAndroid Build Coastguard Worker     rc = snprintf(executable, sizeof(executable),
222*2b949d04SAndroid Build Coastguard Worker                   "%s/%s-%s", LIBXKBCOMMON_TOOL_PATH, prefix, command);
223*2b949d04SAndroid Build Coastguard Worker     if (rc < 0 || (size_t) rc >= sizeof(executable)) {
224*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Failed to assemble command\n");
225*2b949d04SAndroid Build Coastguard Worker         return EXIT_FAILURE;
226*2b949d04SAndroid Build Coastguard Worker     }
227*2b949d04SAndroid Build Coastguard Worker 
228*2b949d04SAndroid Build Coastguard Worker     argv[0] = executable;
229*2b949d04SAndroid Build Coastguard Worker     for (int i = 1; i < real_argc; i++)
230*2b949d04SAndroid Build Coastguard Worker         argv[i] = real_argv[i];
231*2b949d04SAndroid Build Coastguard Worker 
232*2b949d04SAndroid Build Coastguard Worker     execv(executable, argv);
233*2b949d04SAndroid Build Coastguard Worker     if (errno == ENOENT) {
234*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Command '%s' is not available\n", command);
235*2b949d04SAndroid Build Coastguard Worker         return EXIT_INVALID_USAGE;
236*2b949d04SAndroid Build Coastguard Worker     } else {
237*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Failed to execute '%s' (%s)\n",
238*2b949d04SAndroid Build Coastguard Worker                 command, strerror(errno));
239*2b949d04SAndroid Build Coastguard Worker     }
240*2b949d04SAndroid Build Coastguard Worker 
241*2b949d04SAndroid Build Coastguard Worker     return EXIT_FAILURE;
242*2b949d04SAndroid Build Coastguard Worker }
243