test: untangle interactive-evdev from the test headers
[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 <limits.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef _MSC_VER
40 #include <io.h>
41 #include <windows.h>
42 #else
43 #include <unistd.h>
44 #include <termios.h>
45 #endif
46
47 #include "tools-common.h"
48
49 void
50 tools_print_keycode_state(struct xkb_state *state,
51                           struct xkb_compose_state *compose_state,
52                           xkb_keycode_t keycode,
53                           enum xkb_consumed_mode consumed_mode)
54 {
55     struct xkb_keymap *keymap;
56
57     xkb_keysym_t sym;
58     const xkb_keysym_t *syms;
59     int nsyms;
60     char s[16];
61     xkb_layout_index_t layout;
62     enum xkb_compose_status status;
63
64     keymap = xkb_state_get_keymap(state);
65
66     nsyms = xkb_state_key_get_syms(state, keycode, &syms);
67
68     if (nsyms <= 0)
69         return;
70
71     status = XKB_COMPOSE_NOTHING;
72     if (compose_state)
73         status = xkb_compose_state_get_status(compose_state);
74
75     if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
76         return;
77
78     if (status == XKB_COMPOSE_COMPOSED) {
79         sym = xkb_compose_state_get_one_sym(compose_state);
80         syms = &sym;
81         nsyms = 1;
82     }
83     else if (nsyms == 1) {
84         sym = xkb_state_key_get_one_sym(state, keycode);
85         syms = &sym;
86     }
87
88     printf("keysyms [ ");
89     for (int i = 0; i < nsyms; i++) {
90         xkb_keysym_get_name(syms[i], s, sizeof(s));
91         printf("%-*s ", (int) sizeof(s), s);
92     }
93     printf("] ");
94
95     if (status == XKB_COMPOSE_COMPOSED)
96         xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
97     else
98         xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
99     printf("unicode [ %s ] ", s);
100
101     layout = xkb_state_key_get_layout(state, keycode);
102     printf("layout [ %s (%d) ] ",
103            xkb_keymap_layout_get_name(keymap, layout), layout);
104
105     printf("level [ %d ] ",
106            xkb_state_key_get_level(state, keycode, layout));
107
108     printf("mods [ ");
109     for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
110         if (xkb_state_mod_index_is_active(state, mod,
111                                           XKB_STATE_MODS_EFFECTIVE) <= 0)
112             continue;
113         if (xkb_state_mod_index_is_consumed2(state, keycode, mod,
114                                              consumed_mode))
115             printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
116         else
117             printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
118     }
119     printf("] ");
120
121     printf("leds [ ");
122     for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
123         if (xkb_state_led_index_is_active(state, led) <= 0)
124             continue;
125         printf("%s ", xkb_keymap_led_get_name(keymap, led));
126     }
127     printf("] ");
128
129     printf("\n");
130 }
131
132 void
133 tools_print_state_changes(enum xkb_state_component changed)
134 {
135     if (changed == 0)
136         return;
137
138     printf("changed [ ");
139     if (changed & XKB_STATE_LAYOUT_EFFECTIVE)
140         printf("effective-layout ");
141     if (changed & XKB_STATE_LAYOUT_DEPRESSED)
142         printf("depressed-layout ");
143     if (changed & XKB_STATE_LAYOUT_LATCHED)
144         printf("latched-layout ");
145     if (changed & XKB_STATE_LAYOUT_LOCKED)
146         printf("locked-layout ");
147     if (changed & XKB_STATE_MODS_EFFECTIVE)
148         printf("effective-mods ");
149     if (changed & XKB_STATE_MODS_DEPRESSED)
150         printf("depressed-mods ");
151     if (changed & XKB_STATE_MODS_LATCHED)
152         printf("latched-mods ");
153     if (changed & XKB_STATE_MODS_LOCKED)
154         printf("locked-mods ");
155     if (changed & XKB_STATE_LEDS)
156         printf("leds ");
157     printf("]\n");
158 }
159
160 #ifdef _MSC_VER
161 void
162 tools_disable_stdin_echo(void)
163 {
164     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
165     DWORD mode = 0;
166     GetConsoleMode(stdin_handle, &mode);
167     SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
168 }
169
170 void
171 tools_enable_stdin_echo(void)
172 {
173     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
174     DWORD mode = 0;
175     GetConsoleMode(stdin_handle, &mode);
176     SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
177 }
178 #else
179 void
180 tools_disable_stdin_echo(void)
181 {
182     /* Same as `stty -echo`. */
183     struct termios termios;
184     if (tcgetattr(STDIN_FILENO, &termios) == 0) {
185         termios.c_lflag &= ~ECHO;
186         (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
187     }
188 }
189
190 void
191 tools_enable_stdin_echo(void)
192 {
193     /* Same as `stty echo`. */
194     struct termios termios;
195     if (tcgetattr(STDIN_FILENO, &termios) == 0) {
196         termios.c_lflag |= ECHO;
197         (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
198     }
199 }
200 #endif