test-tty-duplicate-key.c revision 1.1.1.1 1 1.1 christos /* Copyright libuv project contributors. All rights reserved.
2 1.1 christos *
3 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining a copy
4 1.1 christos * of this software and associated documentation files (the "Software"), to
5 1.1 christos * deal in the Software without restriction, including without limitation the
6 1.1 christos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 1.1 christos * sell copies of the Software, and to permit persons to whom the Software is
8 1.1 christos * furnished to do so, subject to the following conditions:
9 1.1 christos *
10 1.1 christos * The above copyright notice and this permission notice shall be included in
11 1.1 christos * all copies or substantial portions of the Software.
12 1.1 christos *
13 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 1.1 christos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 1.1 christos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 1.1 christos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 1.1 christos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 1.1 christos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 1.1 christos * IN THE SOFTWARE.
20 1.1 christos */
21 1.1 christos
22 1.1 christos #ifdef _WIN32
23 1.1 christos
24 1.1 christos #include "uv.h"
25 1.1 christos #include "task.h"
26 1.1 christos
27 1.1 christos #include <errno.h>
28 1.1 christos #include <io.h>
29 1.1 christos #include <string.h>
30 1.1 christos #include <windows.h>
31 1.1 christos
32 1.1 christos #define ESC "\x1b"
33 1.1 christos #define EUR_UTF8 "\xe2\x82\xac"
34 1.1 christos #define EUR_UNICODE 0x20AC
35 1.1 christos
36 1.1 christos
37 1.1 christos const char* expect_str = NULL;
38 1.1 christos ssize_t expect_nread = 0;
39 1.1 christos
40 1.1 christos static void dump_str(const char* str, ssize_t len) {
41 1.1 christos ssize_t i;
42 1.1 christos for (i = 0; i < len; i++) {
43 1.1 christos fprintf(stderr, "%#02x ", *(str + i));
44 1.1 christos }
45 1.1 christos }
46 1.1 christos
47 1.1 christos static void print_err_msg(const char* expect, ssize_t expect_len,
48 1.1 christos const char* found, ssize_t found_len) {
49 1.1 christos fprintf(stderr, "expect ");
50 1.1 christos dump_str(expect, expect_len);
51 1.1 christos fprintf(stderr, ", but found ");
52 1.1 christos dump_str(found, found_len);
53 1.1 christos fprintf(stderr, "\n");
54 1.1 christos }
55 1.1 christos
56 1.1 christos static void tty_alloc(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
57 1.1 christos buf->base = malloc(size);
58 1.1 christos ASSERT(buf->base != NULL);
59 1.1 christos buf->len = size;
60 1.1 christos }
61 1.1 christos
62 1.1 christos static void tty_read(uv_stream_t* tty_in, ssize_t nread, const uv_buf_t* buf) {
63 1.1 christos if (nread > 0) {
64 1.1 christos if (nread != expect_nread) {
65 1.1 christos fprintf(stderr, "expected nread %ld, but found %ld\n",
66 1.1 christos (long)expect_nread, (long)nread);
67 1.1 christos print_err_msg(expect_str, expect_nread, buf->base, nread);
68 1.1 christos ASSERT(FALSE);
69 1.1 christos }
70 1.1 christos if (strncmp(buf->base, expect_str, nread) != 0) {
71 1.1 christos print_err_msg(expect_str, expect_nread, buf->base, nread);
72 1.1 christos ASSERT(FALSE);
73 1.1 christos }
74 1.1 christos uv_close((uv_handle_t*) tty_in, NULL);
75 1.1 christos } else {
76 1.1 christos ASSERT(nread == 0);
77 1.1 christos }
78 1.1 christos }
79 1.1 christos
80 1.1 christos static void make_key_event_records(WORD virt_key, DWORD ctr_key_state,
81 1.1 christos BOOL is_wsl, INPUT_RECORD* records) {
82 1.1 christos # define KEV(I) records[(I)].Event.KeyEvent
83 1.1 christos BYTE kb_state[256] = {0};
84 1.1 christos WCHAR buf[2];
85 1.1 christos int ret;
86 1.1 christos
87 1.1 christos records[0].EventType = records[1].EventType = KEY_EVENT;
88 1.1 christos KEV(0).bKeyDown = TRUE;
89 1.1 christos KEV(1).bKeyDown = FALSE;
90 1.1 christos KEV(0).wVirtualKeyCode = KEV(1).wVirtualKeyCode = virt_key;
91 1.1 christos KEV(0).wRepeatCount = KEV(1).wRepeatCount = 1;
92 1.1 christos KEV(0).wVirtualScanCode = KEV(1).wVirtualScanCode =
93 1.1 christos MapVirtualKeyW(virt_key, MAPVK_VK_TO_VSC);
94 1.1 christos KEV(0).dwControlKeyState = KEV(1).dwControlKeyState = ctr_key_state;
95 1.1 christos if (ctr_key_state & LEFT_ALT_PRESSED) {
96 1.1 christos kb_state[VK_LMENU] = 0x01;
97 1.1 christos }
98 1.1 christos if (ctr_key_state & RIGHT_ALT_PRESSED) {
99 1.1 christos kb_state[VK_RMENU] = 0x01;
100 1.1 christos }
101 1.1 christos if (ctr_key_state & LEFT_CTRL_PRESSED) {
102 1.1 christos kb_state[VK_LCONTROL] = 0x01;
103 1.1 christos }
104 1.1 christos if (ctr_key_state & RIGHT_CTRL_PRESSED) {
105 1.1 christos kb_state[VK_RCONTROL] = 0x01;
106 1.1 christos }
107 1.1 christos if (ctr_key_state & SHIFT_PRESSED) {
108 1.1 christos kb_state[VK_SHIFT] = 0x01;
109 1.1 christos }
110 1.1 christos ret = ToUnicode(virt_key, KEV(0).wVirtualScanCode, kb_state, buf, 2, 0);
111 1.1 christos if (ret == 1) {
112 1.1 christos if(!is_wsl &&
113 1.1 christos ((ctr_key_state & LEFT_ALT_PRESSED) ||
114 1.1 christos (ctr_key_state & RIGHT_ALT_PRESSED))) {
115 1.1 christos /*
116 1.1 christos * If ALT key is pressed, the UnicodeChar value of the keyup event is
117 1.1 christos * set to 0 on nomal console. Emulate this behavior.
118 1.1 christos * See https://github.com/Microsoft/console/issues/320
119 1.1 christos */
120 1.1 christos KEV(0).uChar.UnicodeChar = buf[0];
121 1.1 christos KEV(1).uChar.UnicodeChar = 0;
122 1.1 christos } else{
123 1.1 christos /*
124 1.1 christos * In WSL UnicodeChar is normally set. This behavior cause #2111.
125 1.1 christos */
126 1.1 christos KEV(0).uChar.UnicodeChar = KEV(1).uChar.UnicodeChar = buf[0];
127 1.1 christos }
128 1.1 christos } else {
129 1.1 christos KEV(0).uChar.UnicodeChar = KEV(1).uChar.UnicodeChar = 0;
130 1.1 christos }
131 1.1 christos # undef KEV
132 1.1 christos }
133 1.1 christos
134 1.1 christos TEST_IMPL(tty_duplicate_vt100_fn_key) {
135 1.1 christos int r;
136 1.1 christos int ttyin_fd;
137 1.1 christos uv_tty_t tty_in;
138 1.1 christos uv_loop_t* loop;
139 1.1 christos HANDLE handle;
140 1.1 christos INPUT_RECORD records[2];
141 1.1 christos DWORD written;
142 1.1 christos
143 1.1 christos loop = uv_default_loop();
144 1.1 christos
145 1.1 christos /* Make sure we have an FD that refers to a tty */
146 1.1 christos handle = CreateFileA("conin$",
147 1.1 christos GENERIC_READ | GENERIC_WRITE,
148 1.1 christos FILE_SHARE_READ | FILE_SHARE_WRITE,
149 1.1 christos NULL,
150 1.1 christos OPEN_EXISTING,
151 1.1 christos FILE_ATTRIBUTE_NORMAL,
152 1.1 christos NULL);
153 1.1 christos ASSERT(handle != INVALID_HANDLE_VALUE);
154 1.1 christos ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
155 1.1 christos ASSERT(ttyin_fd >= 0);
156 1.1 christos ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
157 1.1 christos
158 1.1 christos r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
159 1.1 christos ASSERT(r == 0);
160 1.1 christos ASSERT(uv_is_readable((uv_stream_t*) &tty_in));
161 1.1 christos ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));
162 1.1 christos
163 1.1 christos r = uv_read_start((uv_stream_t*)&tty_in, tty_alloc, tty_read);
164 1.1 christos ASSERT(r == 0);
165 1.1 christos
166 1.1 christos expect_str = ESC"[[A";
167 1.1 christos expect_nread = strlen(expect_str);
168 1.1 christos
169 1.1 christos /* Turn on raw mode. */
170 1.1 christos r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
171 1.1 christos ASSERT(r == 0);
172 1.1 christos
173 1.1 christos /*
174 1.1 christos * Send F1 keystrokes. Test of issue cause by #2114 that vt100 fn key
175 1.1 christos * duplicate.
176 1.1 christos */
177 1.1 christos make_key_event_records(VK_F1, 0, TRUE, records);
178 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
179 1.1 christos ASSERT(written == ARRAY_SIZE(records));
180 1.1 christos
181 1.1 christos uv_run(loop, UV_RUN_DEFAULT);
182 1.1 christos
183 1.1 christos MAKE_VALGRIND_HAPPY();
184 1.1 christos return 0;
185 1.1 christos }
186 1.1 christos
187 1.1 christos TEST_IMPL(tty_duplicate_alt_modifier_key) {
188 1.1 christos int r;
189 1.1 christos int ttyin_fd;
190 1.1 christos uv_tty_t tty_in;
191 1.1 christos uv_loop_t* loop;
192 1.1 christos HANDLE handle;
193 1.1 christos INPUT_RECORD records[2];
194 1.1 christos INPUT_RECORD alt_records[2];
195 1.1 christos DWORD written;
196 1.1 christos
197 1.1 christos loop = uv_default_loop();
198 1.1 christos
199 1.1 christos /* Make sure we have an FD that refers to a tty */
200 1.1 christos handle = CreateFileA("conin$",
201 1.1 christos GENERIC_READ | GENERIC_WRITE,
202 1.1 christos FILE_SHARE_READ | FILE_SHARE_WRITE,
203 1.1 christos NULL,
204 1.1 christos OPEN_EXISTING,
205 1.1 christos FILE_ATTRIBUTE_NORMAL,
206 1.1 christos NULL);
207 1.1 christos ASSERT(handle != INVALID_HANDLE_VALUE);
208 1.1 christos ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
209 1.1 christos ASSERT(ttyin_fd >= 0);
210 1.1 christos ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
211 1.1 christos
212 1.1 christos r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
213 1.1 christos ASSERT(r == 0);
214 1.1 christos ASSERT(uv_is_readable((uv_stream_t*) &tty_in));
215 1.1 christos ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));
216 1.1 christos
217 1.1 christos r = uv_read_start((uv_stream_t*)&tty_in, tty_alloc, tty_read);
218 1.1 christos ASSERT(r == 0);
219 1.1 christos
220 1.1 christos expect_str = ESC"a"ESC"a";
221 1.1 christos expect_nread = strlen(expect_str);
222 1.1 christos
223 1.1 christos /* Turn on raw mode. */
224 1.1 christos r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
225 1.1 christos ASSERT(r == 0);
226 1.1 christos
227 1.1 christos /* Emulate transmission of M-a at normal console */
228 1.1 christos make_key_event_records(VK_MENU, 0, TRUE, alt_records);
229 1.1 christos WriteConsoleInputW(handle, &alt_records[0], 1, &written);
230 1.1 christos ASSERT(written == 1);
231 1.1 christos make_key_event_records(L'A', LEFT_ALT_PRESSED, FALSE, records);
232 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
233 1.1 christos ASSERT(written == 2);
234 1.1 christos WriteConsoleInputW(handle, &alt_records[1], 1, &written);
235 1.1 christos ASSERT(written == 1);
236 1.1 christos
237 1.1 christos /* Emulate transmission of M-a at WSL(#2111) */
238 1.1 christos make_key_event_records(VK_MENU, 0, TRUE, alt_records);
239 1.1 christos WriteConsoleInputW(handle, &alt_records[0], 1, &written);
240 1.1 christos ASSERT(written == 1);
241 1.1 christos make_key_event_records(L'A', LEFT_ALT_PRESSED, TRUE, records);
242 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
243 1.1 christos ASSERT(written == 2);
244 1.1 christos WriteConsoleInputW(handle, &alt_records[1], 1, &written);
245 1.1 christos ASSERT(written == 1);
246 1.1 christos
247 1.1 christos uv_run(loop, UV_RUN_DEFAULT);
248 1.1 christos
249 1.1 christos MAKE_VALGRIND_HAPPY();
250 1.1 christos return 0;
251 1.1 christos }
252 1.1 christos
253 1.1 christos TEST_IMPL(tty_composing_character) {
254 1.1 christos int r;
255 1.1 christos int ttyin_fd;
256 1.1 christos uv_tty_t tty_in;
257 1.1 christos uv_loop_t* loop;
258 1.1 christos HANDLE handle;
259 1.1 christos INPUT_RECORD records[2];
260 1.1 christos INPUT_RECORD alt_records[2];
261 1.1 christos DWORD written;
262 1.1 christos
263 1.1 christos loop = uv_default_loop();
264 1.1 christos
265 1.1 christos /* Make sure we have an FD that refers to a tty */
266 1.1 christos handle = CreateFileA("conin$",
267 1.1 christos GENERIC_READ | GENERIC_WRITE,
268 1.1 christos FILE_SHARE_READ | FILE_SHARE_WRITE,
269 1.1 christos NULL,
270 1.1 christos OPEN_EXISTING,
271 1.1 christos FILE_ATTRIBUTE_NORMAL,
272 1.1 christos NULL);
273 1.1 christos ASSERT(handle != INVALID_HANDLE_VALUE);
274 1.1 christos ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
275 1.1 christos ASSERT(ttyin_fd >= 0);
276 1.1 christos ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
277 1.1 christos
278 1.1 christos r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
279 1.1 christos ASSERT(r == 0);
280 1.1 christos ASSERT(uv_is_readable((uv_stream_t*) &tty_in));
281 1.1 christos ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));
282 1.1 christos
283 1.1 christos r = uv_read_start((uv_stream_t*)&tty_in, tty_alloc, tty_read);
284 1.1 christos ASSERT(r == 0);
285 1.1 christos
286 1.1 christos expect_str = EUR_UTF8;
287 1.1 christos expect_nread = strlen(expect_str);
288 1.1 christos
289 1.1 christos /* Turn on raw mode. */
290 1.1 christos r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
291 1.1 christos ASSERT(r == 0);
292 1.1 christos
293 1.1 christos /* Emulate EUR inputs by LEFT ALT+NUMPAD ASCII KeyComos */
294 1.1 christos make_key_event_records(VK_MENU, 0, FALSE, alt_records);
295 1.1 christos alt_records[1].Event.KeyEvent.uChar.UnicodeChar = EUR_UNICODE;
296 1.1 christos WriteConsoleInputW(handle, &alt_records[0], 1, &written);
297 1.1 christos make_key_event_records(VK_NUMPAD0, LEFT_ALT_PRESSED, FALSE, records);
298 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
299 1.1 christos ASSERT(written == ARRAY_SIZE(records));
300 1.1 christos make_key_event_records(VK_NUMPAD1, LEFT_ALT_PRESSED, FALSE, records);
301 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
302 1.1 christos ASSERT(written == ARRAY_SIZE(records));
303 1.1 christos make_key_event_records(VK_NUMPAD2, LEFT_ALT_PRESSED, FALSE, records);
304 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
305 1.1 christos ASSERT(written == ARRAY_SIZE(records));
306 1.1 christos make_key_event_records(VK_NUMPAD8, LEFT_ALT_PRESSED, FALSE, records);
307 1.1 christos WriteConsoleInputW(handle, records, ARRAY_SIZE(records), &written);
308 1.1 christos ASSERT(written == ARRAY_SIZE(records));
309 1.1 christos WriteConsoleInputW(handle, &alt_records[1], 1, &written);
310 1.1 christos
311 1.1 christos uv_run(loop, UV_RUN_DEFAULT);
312 1.1 christos
313 1.1 christos MAKE_VALGRIND_HAPPY();
314 1.1 christos return 0;
315 1.1 christos }
316 1.1 christos
317 1.1 christos #else
318 1.1 christos
319 1.1 christos typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
320 1.1 christos
321 1.1 christos #endif /* ifndef _WIN32 */
322