11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the 61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 191cb0ef41Sopenharmony_ci * IN THE SOFTWARE. 201cb0ef41Sopenharmony_ci */ 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#include <assert.h> 231cb0ef41Sopenharmony_ci#include <io.h> 241cb0ef41Sopenharmony_ci#include <string.h> 251cb0ef41Sopenharmony_ci#include <stdlib.h> 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci#if defined(_MSC_VER) && _MSC_VER < 1600 281cb0ef41Sopenharmony_ci# include "uv/stdint-msvc2008.h" 291cb0ef41Sopenharmony_ci#else 301cb0ef41Sopenharmony_ci# include <stdint.h> 311cb0ef41Sopenharmony_ci#endif 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci#ifndef COMMON_LVB_REVERSE_VIDEO 341cb0ef41Sopenharmony_ci# define COMMON_LVB_REVERSE_VIDEO 0x4000 351cb0ef41Sopenharmony_ci#endif 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci#include "uv.h" 381cb0ef41Sopenharmony_ci#include "internal.h" 391cb0ef41Sopenharmony_ci#include "handle-inl.h" 401cb0ef41Sopenharmony_ci#include "stream-inl.h" 411cb0ef41Sopenharmony_ci#include "req-inl.h" 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci#ifndef InterlockedOr 441cb0ef41Sopenharmony_ci# define InterlockedOr _InterlockedOr 451cb0ef41Sopenharmony_ci#endif 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci#define UNICODE_REPLACEMENT_CHARACTER (0xfffd) 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci#define ANSI_NORMAL 0x0000 501cb0ef41Sopenharmony_ci#define ANSI_ESCAPE_SEEN 0x0002 511cb0ef41Sopenharmony_ci#define ANSI_CSI 0x0004 521cb0ef41Sopenharmony_ci#define ANSI_ST_CONTROL 0x0008 531cb0ef41Sopenharmony_ci#define ANSI_IGNORE 0x0010 541cb0ef41Sopenharmony_ci#define ANSI_IN_ARG 0x0020 551cb0ef41Sopenharmony_ci#define ANSI_IN_STRING 0x0040 561cb0ef41Sopenharmony_ci#define ANSI_BACKSLASH_SEEN 0x0080 571cb0ef41Sopenharmony_ci#define ANSI_EXTENSION 0x0100 581cb0ef41Sopenharmony_ci#define ANSI_DECSCUSR 0x0200 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci#define MAX_INPUT_BUFFER_LENGTH 8192 611cb0ef41Sopenharmony_ci#define MAX_CONSOLE_CHAR 8192 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING 641cb0ef41Sopenharmony_ci#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 651cb0ef41Sopenharmony_ci#endif 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci#define CURSOR_SIZE_SMALL 25 681cb0ef41Sopenharmony_ci#define CURSOR_SIZE_LARGE 100 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_cistatic void uv__tty_capture_initial_style( 711cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO* screen_buffer_info, 721cb0ef41Sopenharmony_ci CONSOLE_CURSOR_INFO* cursor_info); 731cb0ef41Sopenharmony_cistatic void uv__tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info); 741cb0ef41Sopenharmony_cistatic int uv__cancel_read_console(uv_tty_t* handle); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci/* Null uv_buf_t */ 781cb0ef41Sopenharmony_cistatic const uv_buf_t uv_null_buf_ = { 0, NULL }; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_cienum uv__read_console_status_e { 811cb0ef41Sopenharmony_ci NOT_STARTED, 821cb0ef41Sopenharmony_ci IN_PROGRESS, 831cb0ef41Sopenharmony_ci TRAP_REQUESTED, 841cb0ef41Sopenharmony_ci COMPLETED 851cb0ef41Sopenharmony_ci}; 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_cistatic volatile LONG uv__read_console_status = NOT_STARTED; 881cb0ef41Sopenharmony_cistatic volatile LONG uv__restore_screen_state; 891cb0ef41Sopenharmony_cistatic CONSOLE_SCREEN_BUFFER_INFO uv__saved_screen_state; 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci/* 931cb0ef41Sopenharmony_ci * The console virtual window. 941cb0ef41Sopenharmony_ci * 951cb0ef41Sopenharmony_ci * Normally cursor movement in windows is relative to the console screen buffer, 961cb0ef41Sopenharmony_ci * e.g. the application is allowed to overwrite the 'history'. This is very 971cb0ef41Sopenharmony_ci * inconvenient, it makes absolute cursor movement pretty useless. There is 981cb0ef41Sopenharmony_ci * also the concept of 'client rect' which is defined by the actual size of 991cb0ef41Sopenharmony_ci * the console window and the scroll position of the screen buffer, but it's 1001cb0ef41Sopenharmony_ci * very volatile because it changes when the user scrolls. 1011cb0ef41Sopenharmony_ci * 1021cb0ef41Sopenharmony_ci * To make cursor movement behave sensibly we define a virtual window to which 1031cb0ef41Sopenharmony_ci * cursor movement is confined. The virtual window is always as wide as the 1041cb0ef41Sopenharmony_ci * console screen buffer, but it's height is defined by the size of the 1051cb0ef41Sopenharmony_ci * console window. The top of the virtual window aligns with the position 1061cb0ef41Sopenharmony_ci * of the caret when the first stdout/err handle is created, unless that would 1071cb0ef41Sopenharmony_ci * mean that it would extend beyond the bottom of the screen buffer - in that 1081cb0ef41Sopenharmony_ci * that case it's located as far down as possible. 1091cb0ef41Sopenharmony_ci * 1101cb0ef41Sopenharmony_ci * When the user writes a long text or many newlines, such that the output 1111cb0ef41Sopenharmony_ci * reaches beyond the bottom of the virtual window, the virtual window is 1121cb0ef41Sopenharmony_ci * shifted downwards, but not resized. 1131cb0ef41Sopenharmony_ci * 1141cb0ef41Sopenharmony_ci * Since all tty i/o happens on the same console, this window is shared 1151cb0ef41Sopenharmony_ci * between all stdout/stderr handles. 1161cb0ef41Sopenharmony_ci */ 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_cistatic int uv_tty_virtual_offset = -1; 1191cb0ef41Sopenharmony_cistatic int uv_tty_virtual_height = -1; 1201cb0ef41Sopenharmony_cistatic int uv_tty_virtual_width = -1; 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci/* The console window size 1231cb0ef41Sopenharmony_ci * We keep this separate from uv_tty_virtual_*. We use those values to only 1241cb0ef41Sopenharmony_ci * handle signalling SIGWINCH 1251cb0ef41Sopenharmony_ci */ 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_cistatic HANDLE uv__tty_console_handle = INVALID_HANDLE_VALUE; 1281cb0ef41Sopenharmony_cistatic int uv__tty_console_height = -1; 1291cb0ef41Sopenharmony_cistatic int uv__tty_console_width = -1; 1301cb0ef41Sopenharmony_cistatic HANDLE uv__tty_console_resized = INVALID_HANDLE_VALUE; 1311cb0ef41Sopenharmony_cistatic uv_mutex_t uv__tty_console_resize_mutex; 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_cistatic DWORD WINAPI uv__tty_console_resize_message_loop_thread(void* param); 1341cb0ef41Sopenharmony_cistatic void CALLBACK uv__tty_console_resize_event(HWINEVENTHOOK hWinEventHook, 1351cb0ef41Sopenharmony_ci DWORD event, 1361cb0ef41Sopenharmony_ci HWND hwnd, 1371cb0ef41Sopenharmony_ci LONG idObject, 1381cb0ef41Sopenharmony_ci LONG idChild, 1391cb0ef41Sopenharmony_ci DWORD dwEventThread, 1401cb0ef41Sopenharmony_ci DWORD dwmsEventTime); 1411cb0ef41Sopenharmony_cistatic DWORD WINAPI uv__tty_console_resize_watcher_thread(void* param); 1421cb0ef41Sopenharmony_cistatic void uv__tty_console_signal_resize(void); 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci/* We use a semaphore rather than a mutex or critical section because in some 1451cb0ef41Sopenharmony_ci cases (uv__cancel_read_console) we need take the lock in the main thread and 1461cb0ef41Sopenharmony_ci release it in another thread. Using a semaphore ensures that in such 1471cb0ef41Sopenharmony_ci scenario the main thread will still block when trying to acquire the lock. */ 1481cb0ef41Sopenharmony_cistatic uv_sem_t uv_tty_output_lock; 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_cistatic WORD uv_tty_default_text_attributes = 1511cb0ef41Sopenharmony_ci FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_cistatic char uv_tty_default_fg_color = 7; 1541cb0ef41Sopenharmony_cistatic char uv_tty_default_bg_color = 0; 1551cb0ef41Sopenharmony_cistatic char uv_tty_default_fg_bright = 0; 1561cb0ef41Sopenharmony_cistatic char uv_tty_default_bg_bright = 0; 1571cb0ef41Sopenharmony_cistatic char uv_tty_default_inverse = 0; 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_cistatic CONSOLE_CURSOR_INFO uv_tty_default_cursor_info; 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci/* Determine whether or not ANSI support is enabled. */ 1621cb0ef41Sopenharmony_cistatic BOOL uv__need_check_vterm_state = TRUE; 1631cb0ef41Sopenharmony_cistatic uv_tty_vtermstate_t uv__vterm_state = UV_TTY_UNSUPPORTED; 1641cb0ef41Sopenharmony_cistatic void uv__determine_vterm_state(HANDLE handle); 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_civoid uv__console_init(void) { 1671cb0ef41Sopenharmony_ci if (uv_sem_init(&uv_tty_output_lock, 1)) 1681cb0ef41Sopenharmony_ci abort(); 1691cb0ef41Sopenharmony_ci uv__tty_console_handle = CreateFileW(L"CONOUT$", 1701cb0ef41Sopenharmony_ci GENERIC_READ | GENERIC_WRITE, 1711cb0ef41Sopenharmony_ci FILE_SHARE_WRITE, 1721cb0ef41Sopenharmony_ci 0, 1731cb0ef41Sopenharmony_ci OPEN_EXISTING, 1741cb0ef41Sopenharmony_ci 0, 1751cb0ef41Sopenharmony_ci 0); 1761cb0ef41Sopenharmony_ci if (uv__tty_console_handle != INVALID_HANDLE_VALUE) { 1771cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO sb_info; 1781cb0ef41Sopenharmony_ci QueueUserWorkItem(uv__tty_console_resize_message_loop_thread, 1791cb0ef41Sopenharmony_ci NULL, 1801cb0ef41Sopenharmony_ci WT_EXECUTELONGFUNCTION); 1811cb0ef41Sopenharmony_ci uv_mutex_init(&uv__tty_console_resize_mutex); 1821cb0ef41Sopenharmony_ci if (GetConsoleScreenBufferInfo(uv__tty_console_handle, &sb_info)) { 1831cb0ef41Sopenharmony_ci uv__tty_console_width = sb_info.dwSize.X; 1841cb0ef41Sopenharmony_ci uv__tty_console_height = sb_info.srWindow.Bottom - sb_info.srWindow.Top + 1; 1851cb0ef41Sopenharmony_ci } 1861cb0ef41Sopenharmony_ci } 1871cb0ef41Sopenharmony_ci} 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ciint uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int unused) { 1911cb0ef41Sopenharmony_ci BOOL readable; 1921cb0ef41Sopenharmony_ci DWORD NumberOfEvents; 1931cb0ef41Sopenharmony_ci HANDLE handle; 1941cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info; 1951cb0ef41Sopenharmony_ci CONSOLE_CURSOR_INFO cursor_info; 1961cb0ef41Sopenharmony_ci (void)unused; 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ci uv__once_init(); 1991cb0ef41Sopenharmony_ci handle = (HANDLE) uv__get_osfhandle(fd); 2001cb0ef41Sopenharmony_ci if (handle == INVALID_HANDLE_VALUE) 2011cb0ef41Sopenharmony_ci return UV_EBADF; 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci if (fd <= 2) { 2041cb0ef41Sopenharmony_ci /* In order to avoid closing a stdio file descriptor 0-2, duplicate the 2051cb0ef41Sopenharmony_ci * underlying OS handle and forget about the original fd. 2061cb0ef41Sopenharmony_ci * We could also opt to use the original OS handle and just never close it, 2071cb0ef41Sopenharmony_ci * but then there would be no reliable way to cancel pending read operations 2081cb0ef41Sopenharmony_ci * upon close. 2091cb0ef41Sopenharmony_ci */ 2101cb0ef41Sopenharmony_ci if (!DuplicateHandle(INVALID_HANDLE_VALUE, 2111cb0ef41Sopenharmony_ci handle, 2121cb0ef41Sopenharmony_ci INVALID_HANDLE_VALUE, 2131cb0ef41Sopenharmony_ci &handle, 2141cb0ef41Sopenharmony_ci 0, 2151cb0ef41Sopenharmony_ci FALSE, 2161cb0ef41Sopenharmony_ci DUPLICATE_SAME_ACCESS)) 2171cb0ef41Sopenharmony_ci return uv_translate_sys_error(GetLastError()); 2181cb0ef41Sopenharmony_ci fd = -1; 2191cb0ef41Sopenharmony_ci } 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci readable = GetNumberOfConsoleInputEvents(handle, &NumberOfEvents); 2221cb0ef41Sopenharmony_ci if (!readable) { 2231cb0ef41Sopenharmony_ci /* Obtain the screen buffer info with the output handle. */ 2241cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle, &screen_buffer_info)) { 2251cb0ef41Sopenharmony_ci return uv_translate_sys_error(GetLastError()); 2261cb0ef41Sopenharmony_ci } 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci /* Obtain the cursor info with the output handle. */ 2291cb0ef41Sopenharmony_ci if (!GetConsoleCursorInfo(handle, &cursor_info)) { 2301cb0ef41Sopenharmony_ci return uv_translate_sys_error(GetLastError()); 2311cb0ef41Sopenharmony_ci } 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci /* Obtain the tty_output_lock because the virtual window state is shared 2341cb0ef41Sopenharmony_ci * between all uv_tty_t handles. */ 2351cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci if (uv__need_check_vterm_state) 2381cb0ef41Sopenharmony_ci uv__determine_vterm_state(handle); 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci /* Remember the original console text attributes and cursor info. */ 2411cb0ef41Sopenharmony_ci uv__tty_capture_initial_style(&screen_buffer_info, &cursor_info); 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ci uv__tty_update_virtual_window(&screen_buffer_info); 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 2461cb0ef41Sopenharmony_ci } 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ci uv__stream_init(loop, (uv_stream_t*) tty, UV_TTY); 2501cb0ef41Sopenharmony_ci uv__connection_init((uv_stream_t*) tty); 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci tty->handle = handle; 2531cb0ef41Sopenharmony_ci tty->u.fd = fd; 2541cb0ef41Sopenharmony_ci tty->reqs_pending = 0; 2551cb0ef41Sopenharmony_ci tty->flags |= UV_HANDLE_BOUND; 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci if (readable) { 2581cb0ef41Sopenharmony_ci /* Initialize TTY input specific fields. */ 2591cb0ef41Sopenharmony_ci tty->flags |= UV_HANDLE_TTY_READABLE | UV_HANDLE_READABLE; 2601cb0ef41Sopenharmony_ci /* TODO: remove me in v2.x. */ 2611cb0ef41Sopenharmony_ci tty->tty.rd.unused_ = NULL; 2621cb0ef41Sopenharmony_ci tty->tty.rd.read_line_buffer = uv_null_buf_; 2631cb0ef41Sopenharmony_ci tty->tty.rd.read_raw_wait = NULL; 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci /* Init keycode-to-vt100 mapper state. */ 2661cb0ef41Sopenharmony_ci tty->tty.rd.last_key_len = 0; 2671cb0ef41Sopenharmony_ci tty->tty.rd.last_key_offset = 0; 2681cb0ef41Sopenharmony_ci tty->tty.rd.last_utf16_high_surrogate = 0; 2691cb0ef41Sopenharmony_ci memset(&tty->tty.rd.last_input_record, 0, sizeof tty->tty.rd.last_input_record); 2701cb0ef41Sopenharmony_ci } else { 2711cb0ef41Sopenharmony_ci /* TTY output specific fields. */ 2721cb0ef41Sopenharmony_ci tty->flags |= UV_HANDLE_WRITABLE; 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ci /* Init utf8-to-utf16 conversion state. */ 2751cb0ef41Sopenharmony_ci tty->tty.wr.utf8_bytes_left = 0; 2761cb0ef41Sopenharmony_ci tty->tty.wr.utf8_codepoint = 0; 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_ci /* Initialize eol conversion state */ 2791cb0ef41Sopenharmony_ci tty->tty.wr.previous_eol = 0; 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci /* Init ANSI parser state. */ 2821cb0ef41Sopenharmony_ci tty->tty.wr.ansi_parser_state = ANSI_NORMAL; 2831cb0ef41Sopenharmony_ci } 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ci return 0; 2861cb0ef41Sopenharmony_ci} 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci/* Set the default console text attributes based on how the console was 2901cb0ef41Sopenharmony_ci * configured when libuv started. 2911cb0ef41Sopenharmony_ci */ 2921cb0ef41Sopenharmony_cistatic void uv__tty_capture_initial_style( 2931cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO* screen_buffer_info, 2941cb0ef41Sopenharmony_ci CONSOLE_CURSOR_INFO* cursor_info) { 2951cb0ef41Sopenharmony_ci static int style_captured = 0; 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_ci /* Only do this once. 2981cb0ef41Sopenharmony_ci Assumption: Caller has acquired uv_tty_output_lock. */ 2991cb0ef41Sopenharmony_ci if (style_captured) 3001cb0ef41Sopenharmony_ci return; 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci /* Save raw win32 attributes. */ 3031cb0ef41Sopenharmony_ci uv_tty_default_text_attributes = screen_buffer_info->wAttributes; 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci /* Convert black text on black background to use white text. */ 3061cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes == 0) 3071cb0ef41Sopenharmony_ci uv_tty_default_text_attributes = 7; 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci /* Convert Win32 attributes to ANSI colors. */ 3101cb0ef41Sopenharmony_ci uv_tty_default_fg_color = 0; 3111cb0ef41Sopenharmony_ci uv_tty_default_bg_color = 0; 3121cb0ef41Sopenharmony_ci uv_tty_default_fg_bright = 0; 3131cb0ef41Sopenharmony_ci uv_tty_default_bg_bright = 0; 3141cb0ef41Sopenharmony_ci uv_tty_default_inverse = 0; 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & FOREGROUND_RED) 3171cb0ef41Sopenharmony_ci uv_tty_default_fg_color |= 1; 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & FOREGROUND_GREEN) 3201cb0ef41Sopenharmony_ci uv_tty_default_fg_color |= 2; 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & FOREGROUND_BLUE) 3231cb0ef41Sopenharmony_ci uv_tty_default_fg_color |= 4; 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & BACKGROUND_RED) 3261cb0ef41Sopenharmony_ci uv_tty_default_bg_color |= 1; 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & BACKGROUND_GREEN) 3291cb0ef41Sopenharmony_ci uv_tty_default_bg_color |= 2; 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & BACKGROUND_BLUE) 3321cb0ef41Sopenharmony_ci uv_tty_default_bg_color |= 4; 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & FOREGROUND_INTENSITY) 3351cb0ef41Sopenharmony_ci uv_tty_default_fg_bright = 1; 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & BACKGROUND_INTENSITY) 3381cb0ef41Sopenharmony_ci uv_tty_default_bg_bright = 1; 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci if (uv_tty_default_text_attributes & COMMON_LVB_REVERSE_VIDEO) 3411cb0ef41Sopenharmony_ci uv_tty_default_inverse = 1; 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ci /* Save the cursor size and the cursor state. */ 3441cb0ef41Sopenharmony_ci uv_tty_default_cursor_info = *cursor_info; 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci style_captured = 1; 3471cb0ef41Sopenharmony_ci} 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ciint uv_tty_set_mode(uv_tty_t* tty, uv_tty_mode_t mode) { 3511cb0ef41Sopenharmony_ci DWORD flags; 3521cb0ef41Sopenharmony_ci unsigned char was_reading; 3531cb0ef41Sopenharmony_ci uv_alloc_cb alloc_cb; 3541cb0ef41Sopenharmony_ci uv_read_cb read_cb; 3551cb0ef41Sopenharmony_ci int err; 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ci if (!(tty->flags & UV_HANDLE_TTY_READABLE)) { 3581cb0ef41Sopenharmony_ci return UV_EINVAL; 3591cb0ef41Sopenharmony_ci } 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci if (!!mode == !!(tty->flags & UV_HANDLE_TTY_RAW)) { 3621cb0ef41Sopenharmony_ci return 0; 3631cb0ef41Sopenharmony_ci } 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ci switch (mode) { 3661cb0ef41Sopenharmony_ci case UV_TTY_MODE_NORMAL: 3671cb0ef41Sopenharmony_ci flags = ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; 3681cb0ef41Sopenharmony_ci break; 3691cb0ef41Sopenharmony_ci case UV_TTY_MODE_RAW: 3701cb0ef41Sopenharmony_ci flags = ENABLE_WINDOW_INPUT; 3711cb0ef41Sopenharmony_ci break; 3721cb0ef41Sopenharmony_ci case UV_TTY_MODE_IO: 3731cb0ef41Sopenharmony_ci return UV_ENOTSUP; 3741cb0ef41Sopenharmony_ci default: 3751cb0ef41Sopenharmony_ci return UV_EINVAL; 3761cb0ef41Sopenharmony_ci } 3771cb0ef41Sopenharmony_ci 3781cb0ef41Sopenharmony_ci /* If currently reading, stop, and restart reading. */ 3791cb0ef41Sopenharmony_ci if (tty->flags & UV_HANDLE_READING) { 3801cb0ef41Sopenharmony_ci was_reading = 1; 3811cb0ef41Sopenharmony_ci alloc_cb = tty->alloc_cb; 3821cb0ef41Sopenharmony_ci read_cb = tty->read_cb; 3831cb0ef41Sopenharmony_ci err = uv__tty_read_stop(tty); 3841cb0ef41Sopenharmony_ci if (err) { 3851cb0ef41Sopenharmony_ci return uv_translate_sys_error(err); 3861cb0ef41Sopenharmony_ci } 3871cb0ef41Sopenharmony_ci } else { 3881cb0ef41Sopenharmony_ci was_reading = 0; 3891cb0ef41Sopenharmony_ci alloc_cb = NULL; 3901cb0ef41Sopenharmony_ci read_cb = NULL; 3911cb0ef41Sopenharmony_ci } 3921cb0ef41Sopenharmony_ci 3931cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 3941cb0ef41Sopenharmony_ci if (!SetConsoleMode(tty->handle, flags)) { 3951cb0ef41Sopenharmony_ci err = uv_translate_sys_error(GetLastError()); 3961cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 3971cb0ef41Sopenharmony_ci return err; 3981cb0ef41Sopenharmony_ci } 3991cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ci /* Update flag. */ 4021cb0ef41Sopenharmony_ci tty->flags &= ~UV_HANDLE_TTY_RAW; 4031cb0ef41Sopenharmony_ci tty->flags |= mode ? UV_HANDLE_TTY_RAW : 0; 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_ci /* If we just stopped reading, restart. */ 4061cb0ef41Sopenharmony_ci if (was_reading) { 4071cb0ef41Sopenharmony_ci err = uv__tty_read_start(tty, alloc_cb, read_cb); 4081cb0ef41Sopenharmony_ci if (err) { 4091cb0ef41Sopenharmony_ci return uv_translate_sys_error(err); 4101cb0ef41Sopenharmony_ci } 4111cb0ef41Sopenharmony_ci } 4121cb0ef41Sopenharmony_ci 4131cb0ef41Sopenharmony_ci return 0; 4141cb0ef41Sopenharmony_ci} 4151cb0ef41Sopenharmony_ci 4161cb0ef41Sopenharmony_ci 4171cb0ef41Sopenharmony_ciint uv_tty_get_winsize(uv_tty_t* tty, int* width, int* height) { 4181cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO info; 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(tty->handle, &info)) { 4211cb0ef41Sopenharmony_ci return uv_translate_sys_error(GetLastError()); 4221cb0ef41Sopenharmony_ci } 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 4251cb0ef41Sopenharmony_ci uv__tty_update_virtual_window(&info); 4261cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ci *width = uv_tty_virtual_width; 4291cb0ef41Sopenharmony_ci *height = uv_tty_virtual_height; 4301cb0ef41Sopenharmony_ci 4311cb0ef41Sopenharmony_ci return 0; 4321cb0ef41Sopenharmony_ci} 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ci 4351cb0ef41Sopenharmony_cistatic void CALLBACK uv_tty_post_raw_read(void* data, BOOLEAN didTimeout) { 4361cb0ef41Sopenharmony_ci uv_loop_t* loop; 4371cb0ef41Sopenharmony_ci uv_tty_t* handle; 4381cb0ef41Sopenharmony_ci uv_req_t* req; 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_ci assert(data); 4411cb0ef41Sopenharmony_ci assert(!didTimeout); 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci req = (uv_req_t*) data; 4441cb0ef41Sopenharmony_ci handle = (uv_tty_t*) req->data; 4451cb0ef41Sopenharmony_ci loop = handle->loop; 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ci UnregisterWait(handle->tty.rd.read_raw_wait); 4481cb0ef41Sopenharmony_ci handle->tty.rd.read_raw_wait = NULL; 4491cb0ef41Sopenharmony_ci 4501cb0ef41Sopenharmony_ci SET_REQ_SUCCESS(req); 4511cb0ef41Sopenharmony_ci POST_COMPLETION_FOR_REQ(loop, req); 4521cb0ef41Sopenharmony_ci} 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_ci 4551cb0ef41Sopenharmony_cistatic void uv__tty_queue_read_raw(uv_loop_t* loop, uv_tty_t* handle) { 4561cb0ef41Sopenharmony_ci uv_read_t* req; 4571cb0ef41Sopenharmony_ci BOOL r; 4581cb0ef41Sopenharmony_ci 4591cb0ef41Sopenharmony_ci assert(handle->flags & UV_HANDLE_READING); 4601cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_READ_PENDING)); 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ci assert(handle->handle && handle->handle != INVALID_HANDLE_VALUE); 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci handle->tty.rd.read_line_buffer = uv_null_buf_; 4651cb0ef41Sopenharmony_ci 4661cb0ef41Sopenharmony_ci req = &handle->read_req; 4671cb0ef41Sopenharmony_ci memset(&req->u.io.overlapped, 0, sizeof(req->u.io.overlapped)); 4681cb0ef41Sopenharmony_ci 4691cb0ef41Sopenharmony_ci r = RegisterWaitForSingleObject(&handle->tty.rd.read_raw_wait, 4701cb0ef41Sopenharmony_ci handle->handle, 4711cb0ef41Sopenharmony_ci uv_tty_post_raw_read, 4721cb0ef41Sopenharmony_ci (void*) req, 4731cb0ef41Sopenharmony_ci INFINITE, 4741cb0ef41Sopenharmony_ci WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE); 4751cb0ef41Sopenharmony_ci if (!r) { 4761cb0ef41Sopenharmony_ci handle->tty.rd.read_raw_wait = NULL; 4771cb0ef41Sopenharmony_ci SET_REQ_ERROR(req, GetLastError()); 4781cb0ef41Sopenharmony_ci uv__insert_pending_req(loop, (uv_req_t*)req); 4791cb0ef41Sopenharmony_ci } 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_READ_PENDING; 4821cb0ef41Sopenharmony_ci handle->reqs_pending++; 4831cb0ef41Sopenharmony_ci} 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ci 4861cb0ef41Sopenharmony_cistatic DWORD CALLBACK uv_tty_line_read_thread(void* data) { 4871cb0ef41Sopenharmony_ci uv_loop_t* loop; 4881cb0ef41Sopenharmony_ci uv_tty_t* handle; 4891cb0ef41Sopenharmony_ci uv_req_t* req; 4901cb0ef41Sopenharmony_ci DWORD bytes, read_bytes; 4911cb0ef41Sopenharmony_ci WCHAR utf16[MAX_INPUT_BUFFER_LENGTH / 3]; 4921cb0ef41Sopenharmony_ci DWORD chars, read_chars; 4931cb0ef41Sopenharmony_ci LONG status; 4941cb0ef41Sopenharmony_ci COORD pos; 4951cb0ef41Sopenharmony_ci BOOL read_console_success; 4961cb0ef41Sopenharmony_ci 4971cb0ef41Sopenharmony_ci assert(data); 4981cb0ef41Sopenharmony_ci 4991cb0ef41Sopenharmony_ci req = (uv_req_t*) data; 5001cb0ef41Sopenharmony_ci handle = (uv_tty_t*) req->data; 5011cb0ef41Sopenharmony_ci loop = handle->loop; 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_ci assert(handle->tty.rd.read_line_buffer.base != NULL); 5041cb0ef41Sopenharmony_ci assert(handle->tty.rd.read_line_buffer.len > 0); 5051cb0ef41Sopenharmony_ci 5061cb0ef41Sopenharmony_ci /* ReadConsole can't handle big buffers. */ 5071cb0ef41Sopenharmony_ci if (handle->tty.rd.read_line_buffer.len < MAX_INPUT_BUFFER_LENGTH) { 5081cb0ef41Sopenharmony_ci bytes = handle->tty.rd.read_line_buffer.len; 5091cb0ef41Sopenharmony_ci } else { 5101cb0ef41Sopenharmony_ci bytes = MAX_INPUT_BUFFER_LENGTH; 5111cb0ef41Sopenharmony_ci } 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci /* At last, unicode! One utf-16 codeunit never takes more than 3 utf-8 5141cb0ef41Sopenharmony_ci * codeunits to encode. */ 5151cb0ef41Sopenharmony_ci chars = bytes / 3; 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ci status = InterlockedExchange(&uv__read_console_status, IN_PROGRESS); 5181cb0ef41Sopenharmony_ci if (status == TRAP_REQUESTED) { 5191cb0ef41Sopenharmony_ci SET_REQ_SUCCESS(req); 5201cb0ef41Sopenharmony_ci InterlockedExchange(&uv__read_console_status, COMPLETED); 5211cb0ef41Sopenharmony_ci req->u.io.overlapped.InternalHigh = 0; 5221cb0ef41Sopenharmony_ci POST_COMPLETION_FOR_REQ(loop, req); 5231cb0ef41Sopenharmony_ci return 0; 5241cb0ef41Sopenharmony_ci } 5251cb0ef41Sopenharmony_ci 5261cb0ef41Sopenharmony_ci read_console_success = ReadConsoleW(handle->handle, 5271cb0ef41Sopenharmony_ci (void*) utf16, 5281cb0ef41Sopenharmony_ci chars, 5291cb0ef41Sopenharmony_ci &read_chars, 5301cb0ef41Sopenharmony_ci NULL); 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_ci if (read_console_success) { 5331cb0ef41Sopenharmony_ci read_bytes = WideCharToMultiByte(CP_UTF8, 5341cb0ef41Sopenharmony_ci 0, 5351cb0ef41Sopenharmony_ci utf16, 5361cb0ef41Sopenharmony_ci read_chars, 5371cb0ef41Sopenharmony_ci handle->tty.rd.read_line_buffer.base, 5381cb0ef41Sopenharmony_ci bytes, 5391cb0ef41Sopenharmony_ci NULL, 5401cb0ef41Sopenharmony_ci NULL); 5411cb0ef41Sopenharmony_ci SET_REQ_SUCCESS(req); 5421cb0ef41Sopenharmony_ci req->u.io.overlapped.InternalHigh = read_bytes; 5431cb0ef41Sopenharmony_ci } else { 5441cb0ef41Sopenharmony_ci SET_REQ_ERROR(req, GetLastError()); 5451cb0ef41Sopenharmony_ci } 5461cb0ef41Sopenharmony_ci 5471cb0ef41Sopenharmony_ci status = InterlockedExchange(&uv__read_console_status, COMPLETED); 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ci if (status == TRAP_REQUESTED) { 5501cb0ef41Sopenharmony_ci /* If we canceled the read by sending a VK_RETURN event, restore the 5511cb0ef41Sopenharmony_ci screen state to undo the visual effect of the VK_RETURN */ 5521cb0ef41Sopenharmony_ci if (read_console_success && InterlockedOr(&uv__restore_screen_state, 0)) { 5531cb0ef41Sopenharmony_ci HANDLE active_screen_buffer; 5541cb0ef41Sopenharmony_ci active_screen_buffer = CreateFileA("conout$", 5551cb0ef41Sopenharmony_ci GENERIC_READ | GENERIC_WRITE, 5561cb0ef41Sopenharmony_ci FILE_SHARE_READ | FILE_SHARE_WRITE, 5571cb0ef41Sopenharmony_ci NULL, 5581cb0ef41Sopenharmony_ci OPEN_EXISTING, 5591cb0ef41Sopenharmony_ci FILE_ATTRIBUTE_NORMAL, 5601cb0ef41Sopenharmony_ci NULL); 5611cb0ef41Sopenharmony_ci if (active_screen_buffer != INVALID_HANDLE_VALUE) { 5621cb0ef41Sopenharmony_ci pos = uv__saved_screen_state.dwCursorPosition; 5631cb0ef41Sopenharmony_ci 5641cb0ef41Sopenharmony_ci /* If the cursor was at the bottom line of the screen buffer, the 5651cb0ef41Sopenharmony_ci VK_RETURN would have caused the buffer contents to scroll up by one 5661cb0ef41Sopenharmony_ci line. The right position to reset the cursor to is therefore one line 5671cb0ef41Sopenharmony_ci higher */ 5681cb0ef41Sopenharmony_ci if (pos.Y == uv__saved_screen_state.dwSize.Y - 1) 5691cb0ef41Sopenharmony_ci pos.Y--; 5701cb0ef41Sopenharmony_ci 5711cb0ef41Sopenharmony_ci SetConsoleCursorPosition(active_screen_buffer, pos); 5721cb0ef41Sopenharmony_ci CloseHandle(active_screen_buffer); 5731cb0ef41Sopenharmony_ci } 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 5761cb0ef41Sopenharmony_ci } 5771cb0ef41Sopenharmony_ci POST_COMPLETION_FOR_REQ(loop, req); 5781cb0ef41Sopenharmony_ci return 0; 5791cb0ef41Sopenharmony_ci} 5801cb0ef41Sopenharmony_ci 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_cistatic void uv__tty_queue_read_line(uv_loop_t* loop, uv_tty_t* handle) { 5831cb0ef41Sopenharmony_ci uv_read_t* req; 5841cb0ef41Sopenharmony_ci BOOL r; 5851cb0ef41Sopenharmony_ci 5861cb0ef41Sopenharmony_ci assert(handle->flags & UV_HANDLE_READING); 5871cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_READ_PENDING)); 5881cb0ef41Sopenharmony_ci assert(handle->handle && handle->handle != INVALID_HANDLE_VALUE); 5891cb0ef41Sopenharmony_ci 5901cb0ef41Sopenharmony_ci req = &handle->read_req; 5911cb0ef41Sopenharmony_ci memset(&req->u.io.overlapped, 0, sizeof(req->u.io.overlapped)); 5921cb0ef41Sopenharmony_ci 5931cb0ef41Sopenharmony_ci handle->tty.rd.read_line_buffer = uv_buf_init(NULL, 0); 5941cb0ef41Sopenharmony_ci handle->alloc_cb((uv_handle_t*) handle, 8192, &handle->tty.rd.read_line_buffer); 5951cb0ef41Sopenharmony_ci if (handle->tty.rd.read_line_buffer.base == NULL || 5961cb0ef41Sopenharmony_ci handle->tty.rd.read_line_buffer.len == 0) { 5971cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, 5981cb0ef41Sopenharmony_ci UV_ENOBUFS, 5991cb0ef41Sopenharmony_ci &handle->tty.rd.read_line_buffer); 6001cb0ef41Sopenharmony_ci return; 6011cb0ef41Sopenharmony_ci } 6021cb0ef41Sopenharmony_ci assert(handle->tty.rd.read_line_buffer.base != NULL); 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci /* Reset flags No locking is required since there cannot be a line read 6051cb0ef41Sopenharmony_ci in progress. We are also relying on the memory barrier provided by 6061cb0ef41Sopenharmony_ci QueueUserWorkItem*/ 6071cb0ef41Sopenharmony_ci uv__restore_screen_state = FALSE; 6081cb0ef41Sopenharmony_ci uv__read_console_status = NOT_STARTED; 6091cb0ef41Sopenharmony_ci r = QueueUserWorkItem(uv_tty_line_read_thread, 6101cb0ef41Sopenharmony_ci (void*) req, 6111cb0ef41Sopenharmony_ci WT_EXECUTELONGFUNCTION); 6121cb0ef41Sopenharmony_ci if (!r) { 6131cb0ef41Sopenharmony_ci SET_REQ_ERROR(req, GetLastError()); 6141cb0ef41Sopenharmony_ci uv__insert_pending_req(loop, (uv_req_t*)req); 6151cb0ef41Sopenharmony_ci } 6161cb0ef41Sopenharmony_ci 6171cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_READ_PENDING; 6181cb0ef41Sopenharmony_ci handle->reqs_pending++; 6191cb0ef41Sopenharmony_ci} 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ci 6221cb0ef41Sopenharmony_cistatic void uv__tty_queue_read(uv_loop_t* loop, uv_tty_t* handle) { 6231cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_TTY_RAW) { 6241cb0ef41Sopenharmony_ci uv__tty_queue_read_raw(loop, handle); 6251cb0ef41Sopenharmony_ci } else { 6261cb0ef41Sopenharmony_ci uv__tty_queue_read_line(loop, handle); 6271cb0ef41Sopenharmony_ci } 6281cb0ef41Sopenharmony_ci} 6291cb0ef41Sopenharmony_ci 6301cb0ef41Sopenharmony_ci 6311cb0ef41Sopenharmony_cistatic const char* get_vt100_fn_key(DWORD code, char shift, char ctrl, 6321cb0ef41Sopenharmony_ci size_t* len) { 6331cb0ef41Sopenharmony_ci#define VK_CASE(vk, normal_str, shift_str, ctrl_str, shift_ctrl_str) \ 6341cb0ef41Sopenharmony_ci case (vk): \ 6351cb0ef41Sopenharmony_ci if (shift && ctrl) { \ 6361cb0ef41Sopenharmony_ci *len = sizeof shift_ctrl_str; \ 6371cb0ef41Sopenharmony_ci return "\033" shift_ctrl_str; \ 6381cb0ef41Sopenharmony_ci } else if (shift) { \ 6391cb0ef41Sopenharmony_ci *len = sizeof shift_str ; \ 6401cb0ef41Sopenharmony_ci return "\033" shift_str; \ 6411cb0ef41Sopenharmony_ci } else if (ctrl) { \ 6421cb0ef41Sopenharmony_ci *len = sizeof ctrl_str; \ 6431cb0ef41Sopenharmony_ci return "\033" ctrl_str; \ 6441cb0ef41Sopenharmony_ci } else { \ 6451cb0ef41Sopenharmony_ci *len = sizeof normal_str; \ 6461cb0ef41Sopenharmony_ci return "\033" normal_str; \ 6471cb0ef41Sopenharmony_ci } 6481cb0ef41Sopenharmony_ci 6491cb0ef41Sopenharmony_ci switch (code) { 6501cb0ef41Sopenharmony_ci /* These mappings are the same as Cygwin's. Unmodified and alt-modified 6511cb0ef41Sopenharmony_ci * keypad keys comply with linux console, modifiers comply with xterm 6521cb0ef41Sopenharmony_ci * modifier usage. F1. f12 and shift-f1. f10 comply with linux console, f6. 6531cb0ef41Sopenharmony_ci * f12 with and without modifiers comply with rxvt. */ 6541cb0ef41Sopenharmony_ci VK_CASE(VK_INSERT, "[2~", "[2;2~", "[2;5~", "[2;6~") 6551cb0ef41Sopenharmony_ci VK_CASE(VK_END, "[4~", "[4;2~", "[4;5~", "[4;6~") 6561cb0ef41Sopenharmony_ci VK_CASE(VK_DOWN, "[B", "[1;2B", "[1;5B", "[1;6B") 6571cb0ef41Sopenharmony_ci VK_CASE(VK_NEXT, "[6~", "[6;2~", "[6;5~", "[6;6~") 6581cb0ef41Sopenharmony_ci VK_CASE(VK_LEFT, "[D", "[1;2D", "[1;5D", "[1;6D") 6591cb0ef41Sopenharmony_ci VK_CASE(VK_CLEAR, "[G", "[1;2G", "[1;5G", "[1;6G") 6601cb0ef41Sopenharmony_ci VK_CASE(VK_RIGHT, "[C", "[1;2C", "[1;5C", "[1;6C") 6611cb0ef41Sopenharmony_ci VK_CASE(VK_UP, "[A", "[1;2A", "[1;5A", "[1;6A") 6621cb0ef41Sopenharmony_ci VK_CASE(VK_HOME, "[1~", "[1;2~", "[1;5~", "[1;6~") 6631cb0ef41Sopenharmony_ci VK_CASE(VK_PRIOR, "[5~", "[5;2~", "[5;5~", "[5;6~") 6641cb0ef41Sopenharmony_ci VK_CASE(VK_DELETE, "[3~", "[3;2~", "[3;5~", "[3;6~") 6651cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD0, "[2~", "[2;2~", "[2;5~", "[2;6~") 6661cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD1, "[4~", "[4;2~", "[4;5~", "[4;6~") 6671cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD2, "[B", "[1;2B", "[1;5B", "[1;6B") 6681cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD3, "[6~", "[6;2~", "[6;5~", "[6;6~") 6691cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD4, "[D", "[1;2D", "[1;5D", "[1;6D") 6701cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD5, "[G", "[1;2G", "[1;5G", "[1;6G") 6711cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD6, "[C", "[1;2C", "[1;5C", "[1;6C") 6721cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD7, "[A", "[1;2A", "[1;5A", "[1;6A") 6731cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD8, "[1~", "[1;2~", "[1;5~", "[1;6~") 6741cb0ef41Sopenharmony_ci VK_CASE(VK_NUMPAD9, "[5~", "[5;2~", "[5;5~", "[5;6~") 6751cb0ef41Sopenharmony_ci VK_CASE(VK_DECIMAL, "[3~", "[3;2~", "[3;5~", "[3;6~") 6761cb0ef41Sopenharmony_ci VK_CASE(VK_F1, "[[A", "[23~", "[11^", "[23^" ) 6771cb0ef41Sopenharmony_ci VK_CASE(VK_F2, "[[B", "[24~", "[12^", "[24^" ) 6781cb0ef41Sopenharmony_ci VK_CASE(VK_F3, "[[C", "[25~", "[13^", "[25^" ) 6791cb0ef41Sopenharmony_ci VK_CASE(VK_F4, "[[D", "[26~", "[14^", "[26^" ) 6801cb0ef41Sopenharmony_ci VK_CASE(VK_F5, "[[E", "[28~", "[15^", "[28^" ) 6811cb0ef41Sopenharmony_ci VK_CASE(VK_F6, "[17~", "[29~", "[17^", "[29^" ) 6821cb0ef41Sopenharmony_ci VK_CASE(VK_F7, "[18~", "[31~", "[18^", "[31^" ) 6831cb0ef41Sopenharmony_ci VK_CASE(VK_F8, "[19~", "[32~", "[19^", "[32^" ) 6841cb0ef41Sopenharmony_ci VK_CASE(VK_F9, "[20~", "[33~", "[20^", "[33^" ) 6851cb0ef41Sopenharmony_ci VK_CASE(VK_F10, "[21~", "[34~", "[21^", "[34^" ) 6861cb0ef41Sopenharmony_ci VK_CASE(VK_F11, "[23~", "[23$", "[23^", "[23@" ) 6871cb0ef41Sopenharmony_ci VK_CASE(VK_F12, "[24~", "[24$", "[24^", "[24@" ) 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ci default: 6901cb0ef41Sopenharmony_ci *len = 0; 6911cb0ef41Sopenharmony_ci return NULL; 6921cb0ef41Sopenharmony_ci } 6931cb0ef41Sopenharmony_ci#undef VK_CASE 6941cb0ef41Sopenharmony_ci} 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_civoid uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle, 6981cb0ef41Sopenharmony_ci uv_req_t* req) { 6991cb0ef41Sopenharmony_ci /* Shortcut for handle->tty.rd.last_input_record.Event.KeyEvent. */ 7001cb0ef41Sopenharmony_ci#define KEV handle->tty.rd.last_input_record.Event.KeyEvent 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci DWORD records_left, records_read; 7031cb0ef41Sopenharmony_ci uv_buf_t buf; 7041cb0ef41Sopenharmony_ci off_t buf_used; 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ci assert(handle->type == UV_TTY); 7071cb0ef41Sopenharmony_ci assert(handle->flags & UV_HANDLE_TTY_READABLE); 7081cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READ_PENDING; 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ci if (!(handle->flags & UV_HANDLE_READING) || 7111cb0ef41Sopenharmony_ci !(handle->flags & UV_HANDLE_TTY_RAW)) { 7121cb0ef41Sopenharmony_ci goto out; 7131cb0ef41Sopenharmony_ci } 7141cb0ef41Sopenharmony_ci 7151cb0ef41Sopenharmony_ci if (!REQ_SUCCESS(req)) { 7161cb0ef41Sopenharmony_ci /* An error occurred while waiting for the event. */ 7171cb0ef41Sopenharmony_ci if ((handle->flags & UV_HANDLE_READING)) { 7181cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READING; 7191cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*)handle, 7201cb0ef41Sopenharmony_ci uv_translate_sys_error(GET_REQ_ERROR(req)), 7211cb0ef41Sopenharmony_ci &uv_null_buf_); 7221cb0ef41Sopenharmony_ci } 7231cb0ef41Sopenharmony_ci goto out; 7241cb0ef41Sopenharmony_ci } 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ci /* Fetch the number of events */ 7271cb0ef41Sopenharmony_ci if (!GetNumberOfConsoleInputEvents(handle->handle, &records_left)) { 7281cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READING; 7291cb0ef41Sopenharmony_ci DECREASE_ACTIVE_COUNT(loop, handle); 7301cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*)handle, 7311cb0ef41Sopenharmony_ci uv_translate_sys_error(GetLastError()), 7321cb0ef41Sopenharmony_ci &uv_null_buf_); 7331cb0ef41Sopenharmony_ci goto out; 7341cb0ef41Sopenharmony_ci } 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_ci /* Windows sends a lot of events that we're not interested in, so buf will be 7371cb0ef41Sopenharmony_ci * allocated on demand, when there's actually something to emit. */ 7381cb0ef41Sopenharmony_ci buf = uv_null_buf_; 7391cb0ef41Sopenharmony_ci buf_used = 0; 7401cb0ef41Sopenharmony_ci 7411cb0ef41Sopenharmony_ci while ((records_left > 0 || handle->tty.rd.last_key_len > 0) && 7421cb0ef41Sopenharmony_ci (handle->flags & UV_HANDLE_READING)) { 7431cb0ef41Sopenharmony_ci if (handle->tty.rd.last_key_len == 0) { 7441cb0ef41Sopenharmony_ci /* Read the next input record */ 7451cb0ef41Sopenharmony_ci if (!ReadConsoleInputW(handle->handle, 7461cb0ef41Sopenharmony_ci &handle->tty.rd.last_input_record, 7471cb0ef41Sopenharmony_ci 1, 7481cb0ef41Sopenharmony_ci &records_read)) { 7491cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READING; 7501cb0ef41Sopenharmony_ci DECREASE_ACTIVE_COUNT(loop, handle); 7511cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, 7521cb0ef41Sopenharmony_ci uv_translate_sys_error(GetLastError()), 7531cb0ef41Sopenharmony_ci &buf); 7541cb0ef41Sopenharmony_ci goto out; 7551cb0ef41Sopenharmony_ci } 7561cb0ef41Sopenharmony_ci records_left--; 7571cb0ef41Sopenharmony_ci 7581cb0ef41Sopenharmony_ci /* We might be not subscribed to EVENT_CONSOLE_LAYOUT or we might be 7591cb0ef41Sopenharmony_ci * running under some TTY emulator that does not send those events. */ 7601cb0ef41Sopenharmony_ci if (handle->tty.rd.last_input_record.EventType == WINDOW_BUFFER_SIZE_EVENT) { 7611cb0ef41Sopenharmony_ci uv__tty_console_signal_resize(); 7621cb0ef41Sopenharmony_ci } 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ci /* Ignore other events that are not key events. */ 7651cb0ef41Sopenharmony_ci if (handle->tty.rd.last_input_record.EventType != KEY_EVENT) { 7661cb0ef41Sopenharmony_ci continue; 7671cb0ef41Sopenharmony_ci } 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci /* Ignore keyup events, unless the left alt key was held and a valid 7701cb0ef41Sopenharmony_ci * unicode character was emitted. */ 7711cb0ef41Sopenharmony_ci if (!KEV.bKeyDown && 7721cb0ef41Sopenharmony_ci (KEV.wVirtualKeyCode != VK_MENU || 7731cb0ef41Sopenharmony_ci KEV.uChar.UnicodeChar == 0)) { 7741cb0ef41Sopenharmony_ci continue; 7751cb0ef41Sopenharmony_ci } 7761cb0ef41Sopenharmony_ci 7771cb0ef41Sopenharmony_ci /* Ignore keypresses to numpad number keys if the left alt is held 7781cb0ef41Sopenharmony_ci * because the user is composing a character, or windows simulating this. 7791cb0ef41Sopenharmony_ci */ 7801cb0ef41Sopenharmony_ci if ((KEV.dwControlKeyState & LEFT_ALT_PRESSED) && 7811cb0ef41Sopenharmony_ci !(KEV.dwControlKeyState & ENHANCED_KEY) && 7821cb0ef41Sopenharmony_ci (KEV.wVirtualKeyCode == VK_INSERT || 7831cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_END || 7841cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_DOWN || 7851cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NEXT || 7861cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_LEFT || 7871cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_CLEAR || 7881cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_RIGHT || 7891cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_HOME || 7901cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_UP || 7911cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_PRIOR || 7921cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD0 || 7931cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD1 || 7941cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD2 || 7951cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD3 || 7961cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD4 || 7971cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD5 || 7981cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD6 || 7991cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD7 || 8001cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD8 || 8011cb0ef41Sopenharmony_ci KEV.wVirtualKeyCode == VK_NUMPAD9)) { 8021cb0ef41Sopenharmony_ci continue; 8031cb0ef41Sopenharmony_ci } 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci if (KEV.uChar.UnicodeChar != 0) { 8061cb0ef41Sopenharmony_ci int prefix_len, char_len; 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ci /* Character key pressed */ 8091cb0ef41Sopenharmony_ci if (KEV.uChar.UnicodeChar >= 0xD800 && 8101cb0ef41Sopenharmony_ci KEV.uChar.UnicodeChar < 0xDC00) { 8111cb0ef41Sopenharmony_ci /* UTF-16 high surrogate */ 8121cb0ef41Sopenharmony_ci handle->tty.rd.last_utf16_high_surrogate = KEV.uChar.UnicodeChar; 8131cb0ef41Sopenharmony_ci continue; 8141cb0ef41Sopenharmony_ci } 8151cb0ef41Sopenharmony_ci 8161cb0ef41Sopenharmony_ci /* Prefix with \u033 if alt was held, but alt was not used as part a 8171cb0ef41Sopenharmony_ci * compose sequence. */ 8181cb0ef41Sopenharmony_ci if ((KEV.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) 8191cb0ef41Sopenharmony_ci && !(KEV.dwControlKeyState & (LEFT_CTRL_PRESSED | 8201cb0ef41Sopenharmony_ci RIGHT_CTRL_PRESSED)) && KEV.bKeyDown) { 8211cb0ef41Sopenharmony_ci handle->tty.rd.last_key[0] = '\033'; 8221cb0ef41Sopenharmony_ci prefix_len = 1; 8231cb0ef41Sopenharmony_ci } else { 8241cb0ef41Sopenharmony_ci prefix_len = 0; 8251cb0ef41Sopenharmony_ci } 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ci if (KEV.uChar.UnicodeChar >= 0xDC00 && 8281cb0ef41Sopenharmony_ci KEV.uChar.UnicodeChar < 0xE000) { 8291cb0ef41Sopenharmony_ci /* UTF-16 surrogate pair */ 8301cb0ef41Sopenharmony_ci WCHAR utf16_buffer[2]; 8311cb0ef41Sopenharmony_ci utf16_buffer[0] = handle->tty.rd.last_utf16_high_surrogate; 8321cb0ef41Sopenharmony_ci utf16_buffer[1] = KEV.uChar.UnicodeChar; 8331cb0ef41Sopenharmony_ci char_len = WideCharToMultiByte(CP_UTF8, 8341cb0ef41Sopenharmony_ci 0, 8351cb0ef41Sopenharmony_ci utf16_buffer, 8361cb0ef41Sopenharmony_ci 2, 8371cb0ef41Sopenharmony_ci &handle->tty.rd.last_key[prefix_len], 8381cb0ef41Sopenharmony_ci sizeof handle->tty.rd.last_key, 8391cb0ef41Sopenharmony_ci NULL, 8401cb0ef41Sopenharmony_ci NULL); 8411cb0ef41Sopenharmony_ci } else { 8421cb0ef41Sopenharmony_ci /* Single UTF-16 character */ 8431cb0ef41Sopenharmony_ci char_len = WideCharToMultiByte(CP_UTF8, 8441cb0ef41Sopenharmony_ci 0, 8451cb0ef41Sopenharmony_ci &KEV.uChar.UnicodeChar, 8461cb0ef41Sopenharmony_ci 1, 8471cb0ef41Sopenharmony_ci &handle->tty.rd.last_key[prefix_len], 8481cb0ef41Sopenharmony_ci sizeof handle->tty.rd.last_key, 8491cb0ef41Sopenharmony_ci NULL, 8501cb0ef41Sopenharmony_ci NULL); 8511cb0ef41Sopenharmony_ci } 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ci /* Whatever happened, the last character wasn't a high surrogate. */ 8541cb0ef41Sopenharmony_ci handle->tty.rd.last_utf16_high_surrogate = 0; 8551cb0ef41Sopenharmony_ci 8561cb0ef41Sopenharmony_ci /* If the utf16 character(s) couldn't be converted something must be 8571cb0ef41Sopenharmony_ci * wrong. */ 8581cb0ef41Sopenharmony_ci if (!char_len) { 8591cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READING; 8601cb0ef41Sopenharmony_ci DECREASE_ACTIVE_COUNT(loop, handle); 8611cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, 8621cb0ef41Sopenharmony_ci uv_translate_sys_error(GetLastError()), 8631cb0ef41Sopenharmony_ci &buf); 8641cb0ef41Sopenharmony_ci goto out; 8651cb0ef41Sopenharmony_ci } 8661cb0ef41Sopenharmony_ci 8671cb0ef41Sopenharmony_ci handle->tty.rd.last_key_len = (unsigned char) (prefix_len + char_len); 8681cb0ef41Sopenharmony_ci handle->tty.rd.last_key_offset = 0; 8691cb0ef41Sopenharmony_ci continue; 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ci } else { 8721cb0ef41Sopenharmony_ci /* Function key pressed */ 8731cb0ef41Sopenharmony_ci const char* vt100; 8741cb0ef41Sopenharmony_ci size_t prefix_len, vt100_len; 8751cb0ef41Sopenharmony_ci 8761cb0ef41Sopenharmony_ci vt100 = get_vt100_fn_key(KEV.wVirtualKeyCode, 8771cb0ef41Sopenharmony_ci !!(KEV.dwControlKeyState & SHIFT_PRESSED), 8781cb0ef41Sopenharmony_ci !!(KEV.dwControlKeyState & ( 8791cb0ef41Sopenharmony_ci LEFT_CTRL_PRESSED | 8801cb0ef41Sopenharmony_ci RIGHT_CTRL_PRESSED)), 8811cb0ef41Sopenharmony_ci &vt100_len); 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ci /* If we were unable to map to a vt100 sequence, just ignore. */ 8841cb0ef41Sopenharmony_ci if (!vt100) { 8851cb0ef41Sopenharmony_ci continue; 8861cb0ef41Sopenharmony_ci } 8871cb0ef41Sopenharmony_ci 8881cb0ef41Sopenharmony_ci /* Prefix with \x033 when the alt key was held. */ 8891cb0ef41Sopenharmony_ci if (KEV.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) { 8901cb0ef41Sopenharmony_ci handle->tty.rd.last_key[0] = '\033'; 8911cb0ef41Sopenharmony_ci prefix_len = 1; 8921cb0ef41Sopenharmony_ci } else { 8931cb0ef41Sopenharmony_ci prefix_len = 0; 8941cb0ef41Sopenharmony_ci } 8951cb0ef41Sopenharmony_ci 8961cb0ef41Sopenharmony_ci /* Copy the vt100 sequence to the handle buffer. */ 8971cb0ef41Sopenharmony_ci assert(prefix_len + vt100_len < sizeof handle->tty.rd.last_key); 8981cb0ef41Sopenharmony_ci memcpy(&handle->tty.rd.last_key[prefix_len], vt100, vt100_len); 8991cb0ef41Sopenharmony_ci 9001cb0ef41Sopenharmony_ci handle->tty.rd.last_key_len = (unsigned char) (prefix_len + vt100_len); 9011cb0ef41Sopenharmony_ci handle->tty.rd.last_key_offset = 0; 9021cb0ef41Sopenharmony_ci continue; 9031cb0ef41Sopenharmony_ci } 9041cb0ef41Sopenharmony_ci } else { 9051cb0ef41Sopenharmony_ci /* Copy any bytes left from the last keypress to the user buffer. */ 9061cb0ef41Sopenharmony_ci if (handle->tty.rd.last_key_offset < handle->tty.rd.last_key_len) { 9071cb0ef41Sopenharmony_ci /* Allocate a buffer if needed */ 9081cb0ef41Sopenharmony_ci if (buf_used == 0) { 9091cb0ef41Sopenharmony_ci buf = uv_buf_init(NULL, 0); 9101cb0ef41Sopenharmony_ci handle->alloc_cb((uv_handle_t*) handle, 1024, &buf); 9111cb0ef41Sopenharmony_ci if (buf.base == NULL || buf.len == 0) { 9121cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, UV_ENOBUFS, &buf); 9131cb0ef41Sopenharmony_ci goto out; 9141cb0ef41Sopenharmony_ci } 9151cb0ef41Sopenharmony_ci assert(buf.base != NULL); 9161cb0ef41Sopenharmony_ci } 9171cb0ef41Sopenharmony_ci 9181cb0ef41Sopenharmony_ci buf.base[buf_used++] = handle->tty.rd.last_key[handle->tty.rd.last_key_offset++]; 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ci /* If the buffer is full, emit it */ 9211cb0ef41Sopenharmony_ci if ((size_t) buf_used == buf.len) { 9221cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, buf_used, &buf); 9231cb0ef41Sopenharmony_ci buf = uv_null_buf_; 9241cb0ef41Sopenharmony_ci buf_used = 0; 9251cb0ef41Sopenharmony_ci } 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci continue; 9281cb0ef41Sopenharmony_ci } 9291cb0ef41Sopenharmony_ci 9301cb0ef41Sopenharmony_ci /* Apply dwRepeat from the last input record. */ 9311cb0ef41Sopenharmony_ci if (--KEV.wRepeatCount > 0) { 9321cb0ef41Sopenharmony_ci handle->tty.rd.last_key_offset = 0; 9331cb0ef41Sopenharmony_ci continue; 9341cb0ef41Sopenharmony_ci } 9351cb0ef41Sopenharmony_ci 9361cb0ef41Sopenharmony_ci handle->tty.rd.last_key_len = 0; 9371cb0ef41Sopenharmony_ci continue; 9381cb0ef41Sopenharmony_ci } 9391cb0ef41Sopenharmony_ci } 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ci /* Send the buffer back to the user */ 9421cb0ef41Sopenharmony_ci if (buf_used > 0) { 9431cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, buf_used, &buf); 9441cb0ef41Sopenharmony_ci } 9451cb0ef41Sopenharmony_ci 9461cb0ef41Sopenharmony_ci out: 9471cb0ef41Sopenharmony_ci /* Wait for more input events. */ 9481cb0ef41Sopenharmony_ci if ((handle->flags & UV_HANDLE_READING) && 9491cb0ef41Sopenharmony_ci !(handle->flags & UV_HANDLE_READ_PENDING)) { 9501cb0ef41Sopenharmony_ci uv__tty_queue_read(loop, handle); 9511cb0ef41Sopenharmony_ci } 9521cb0ef41Sopenharmony_ci 9531cb0ef41Sopenharmony_ci DECREASE_PENDING_REQ_COUNT(handle); 9541cb0ef41Sopenharmony_ci 9551cb0ef41Sopenharmony_ci#undef KEV 9561cb0ef41Sopenharmony_ci} 9571cb0ef41Sopenharmony_ci 9581cb0ef41Sopenharmony_ci 9591cb0ef41Sopenharmony_ci 9601cb0ef41Sopenharmony_civoid uv_process_tty_read_line_req(uv_loop_t* loop, uv_tty_t* handle, 9611cb0ef41Sopenharmony_ci uv_req_t* req) { 9621cb0ef41Sopenharmony_ci uv_buf_t buf; 9631cb0ef41Sopenharmony_ci 9641cb0ef41Sopenharmony_ci assert(handle->type == UV_TTY); 9651cb0ef41Sopenharmony_ci assert(handle->flags & UV_HANDLE_TTY_READABLE); 9661cb0ef41Sopenharmony_ci 9671cb0ef41Sopenharmony_ci buf = handle->tty.rd.read_line_buffer; 9681cb0ef41Sopenharmony_ci 9691cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READ_PENDING; 9701cb0ef41Sopenharmony_ci handle->tty.rd.read_line_buffer = uv_null_buf_; 9711cb0ef41Sopenharmony_ci 9721cb0ef41Sopenharmony_ci if (!REQ_SUCCESS(req)) { 9731cb0ef41Sopenharmony_ci /* Read was not successful */ 9741cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_READING) { 9751cb0ef41Sopenharmony_ci /* Real error */ 9761cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READING; 9771cb0ef41Sopenharmony_ci DECREASE_ACTIVE_COUNT(loop, handle); 9781cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, 9791cb0ef41Sopenharmony_ci uv_translate_sys_error(GET_REQ_ERROR(req)), 9801cb0ef41Sopenharmony_ci &buf); 9811cb0ef41Sopenharmony_ci } 9821cb0ef41Sopenharmony_ci } else { 9831cb0ef41Sopenharmony_ci if (!(handle->flags & UV_HANDLE_CANCELLATION_PENDING) && 9841cb0ef41Sopenharmony_ci req->u.io.overlapped.InternalHigh != 0) { 9851cb0ef41Sopenharmony_ci /* Read successful. TODO: read unicode, convert to utf-8 */ 9861cb0ef41Sopenharmony_ci DWORD bytes = req->u.io.overlapped.InternalHigh; 9871cb0ef41Sopenharmony_ci handle->read_cb((uv_stream_t*) handle, bytes, &buf); 9881cb0ef41Sopenharmony_ci } 9891cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_CANCELLATION_PENDING; 9901cb0ef41Sopenharmony_ci } 9911cb0ef41Sopenharmony_ci 9921cb0ef41Sopenharmony_ci /* Wait for more input events. */ 9931cb0ef41Sopenharmony_ci if ((handle->flags & UV_HANDLE_READING) && 9941cb0ef41Sopenharmony_ci !(handle->flags & UV_HANDLE_READ_PENDING)) { 9951cb0ef41Sopenharmony_ci uv__tty_queue_read(loop, handle); 9961cb0ef41Sopenharmony_ci } 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ci DECREASE_PENDING_REQ_COUNT(handle); 9991cb0ef41Sopenharmony_ci} 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_civoid uv__process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle, 10031cb0ef41Sopenharmony_ci uv_req_t* req) { 10041cb0ef41Sopenharmony_ci assert(handle->type == UV_TTY); 10051cb0ef41Sopenharmony_ci assert(handle->flags & UV_HANDLE_TTY_READABLE); 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci /* If the read_line_buffer member is zero, it must have been an raw read. 10081cb0ef41Sopenharmony_ci * Otherwise it was a line-buffered read. FIXME: This is quite obscure. Use a 10091cb0ef41Sopenharmony_ci * flag or something. */ 10101cb0ef41Sopenharmony_ci if (handle->tty.rd.read_line_buffer.len == 0) { 10111cb0ef41Sopenharmony_ci uv_process_tty_read_raw_req(loop, handle, req); 10121cb0ef41Sopenharmony_ci } else { 10131cb0ef41Sopenharmony_ci uv_process_tty_read_line_req(loop, handle, req); 10141cb0ef41Sopenharmony_ci } 10151cb0ef41Sopenharmony_ci} 10161cb0ef41Sopenharmony_ci 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ciint uv__tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb, 10191cb0ef41Sopenharmony_ci uv_read_cb read_cb) { 10201cb0ef41Sopenharmony_ci uv_loop_t* loop = handle->loop; 10211cb0ef41Sopenharmony_ci 10221cb0ef41Sopenharmony_ci if (!(handle->flags & UV_HANDLE_TTY_READABLE)) { 10231cb0ef41Sopenharmony_ci return ERROR_INVALID_PARAMETER; 10241cb0ef41Sopenharmony_ci } 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_READING; 10271cb0ef41Sopenharmony_ci INCREASE_ACTIVE_COUNT(loop, handle); 10281cb0ef41Sopenharmony_ci handle->read_cb = read_cb; 10291cb0ef41Sopenharmony_ci handle->alloc_cb = alloc_cb; 10301cb0ef41Sopenharmony_ci 10311cb0ef41Sopenharmony_ci /* If reading was stopped and then started again, there could still be a read 10321cb0ef41Sopenharmony_ci * request pending. */ 10331cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_READ_PENDING) { 10341cb0ef41Sopenharmony_ci return 0; 10351cb0ef41Sopenharmony_ci } 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci /* Maybe the user stopped reading half-way while processing key events. 10381cb0ef41Sopenharmony_ci * Short-circuit if this could be the case. */ 10391cb0ef41Sopenharmony_ci if (handle->tty.rd.last_key_len > 0) { 10401cb0ef41Sopenharmony_ci SET_REQ_SUCCESS(&handle->read_req); 10411cb0ef41Sopenharmony_ci uv__insert_pending_req(handle->loop, (uv_req_t*) &handle->read_req); 10421cb0ef41Sopenharmony_ci /* Make sure no attempt is made to insert it again until it's handled. */ 10431cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_READ_PENDING; 10441cb0ef41Sopenharmony_ci handle->reqs_pending++; 10451cb0ef41Sopenharmony_ci return 0; 10461cb0ef41Sopenharmony_ci } 10471cb0ef41Sopenharmony_ci 10481cb0ef41Sopenharmony_ci uv__tty_queue_read(loop, handle); 10491cb0ef41Sopenharmony_ci 10501cb0ef41Sopenharmony_ci return 0; 10511cb0ef41Sopenharmony_ci} 10521cb0ef41Sopenharmony_ci 10531cb0ef41Sopenharmony_ci 10541cb0ef41Sopenharmony_ciint uv__tty_read_stop(uv_tty_t* handle) { 10551cb0ef41Sopenharmony_ci INPUT_RECORD record; 10561cb0ef41Sopenharmony_ci DWORD written, err; 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ci handle->flags &= ~UV_HANDLE_READING; 10591cb0ef41Sopenharmony_ci DECREASE_ACTIVE_COUNT(handle->loop, handle); 10601cb0ef41Sopenharmony_ci 10611cb0ef41Sopenharmony_ci if (!(handle->flags & UV_HANDLE_READ_PENDING)) 10621cb0ef41Sopenharmony_ci return 0; 10631cb0ef41Sopenharmony_ci 10641cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_TTY_RAW) { 10651cb0ef41Sopenharmony_ci /* Cancel raw read. Write some bullshit event to force the console wait to 10661cb0ef41Sopenharmony_ci * return. */ 10671cb0ef41Sopenharmony_ci memset(&record, 0, sizeof record); 10681cb0ef41Sopenharmony_ci record.EventType = FOCUS_EVENT; 10691cb0ef41Sopenharmony_ci if (!WriteConsoleInputW(handle->handle, &record, 1, &written)) { 10701cb0ef41Sopenharmony_ci return GetLastError(); 10711cb0ef41Sopenharmony_ci } 10721cb0ef41Sopenharmony_ci } else if (!(handle->flags & UV_HANDLE_CANCELLATION_PENDING)) { 10731cb0ef41Sopenharmony_ci /* Cancel line-buffered read if not already pending */ 10741cb0ef41Sopenharmony_ci err = uv__cancel_read_console(handle); 10751cb0ef41Sopenharmony_ci if (err) 10761cb0ef41Sopenharmony_ci return err; 10771cb0ef41Sopenharmony_ci 10781cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_CANCELLATION_PENDING; 10791cb0ef41Sopenharmony_ci } 10801cb0ef41Sopenharmony_ci 10811cb0ef41Sopenharmony_ci return 0; 10821cb0ef41Sopenharmony_ci} 10831cb0ef41Sopenharmony_ci 10841cb0ef41Sopenharmony_cistatic int uv__cancel_read_console(uv_tty_t* handle) { 10851cb0ef41Sopenharmony_ci HANDLE active_screen_buffer = INVALID_HANDLE_VALUE; 10861cb0ef41Sopenharmony_ci INPUT_RECORD record; 10871cb0ef41Sopenharmony_ci DWORD written; 10881cb0ef41Sopenharmony_ci DWORD err = 0; 10891cb0ef41Sopenharmony_ci LONG status; 10901cb0ef41Sopenharmony_ci 10911cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_CANCELLATION_PENDING)); 10921cb0ef41Sopenharmony_ci 10931cb0ef41Sopenharmony_ci /* Hold the output lock during the cancellation, to ensure that further 10941cb0ef41Sopenharmony_ci writes don't interfere with the screen state. It will be the ReadConsole 10951cb0ef41Sopenharmony_ci thread's responsibility to release the lock. */ 10961cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 10971cb0ef41Sopenharmony_ci status = InterlockedExchange(&uv__read_console_status, TRAP_REQUESTED); 10981cb0ef41Sopenharmony_ci if (status != IN_PROGRESS) { 10991cb0ef41Sopenharmony_ci /* Either we have managed to set a trap for the other thread before 11001cb0ef41Sopenharmony_ci ReadConsole is called, or ReadConsole has returned because the user 11011cb0ef41Sopenharmony_ci has pressed ENTER. In either case, there is nothing else to do. */ 11021cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 11031cb0ef41Sopenharmony_ci return 0; 11041cb0ef41Sopenharmony_ci } 11051cb0ef41Sopenharmony_ci 11061cb0ef41Sopenharmony_ci /* Save screen state before sending the VK_RETURN event */ 11071cb0ef41Sopenharmony_ci active_screen_buffer = CreateFileA("conout$", 11081cb0ef41Sopenharmony_ci GENERIC_READ | GENERIC_WRITE, 11091cb0ef41Sopenharmony_ci FILE_SHARE_READ | FILE_SHARE_WRITE, 11101cb0ef41Sopenharmony_ci NULL, 11111cb0ef41Sopenharmony_ci OPEN_EXISTING, 11121cb0ef41Sopenharmony_ci FILE_ATTRIBUTE_NORMAL, 11131cb0ef41Sopenharmony_ci NULL); 11141cb0ef41Sopenharmony_ci 11151cb0ef41Sopenharmony_ci if (active_screen_buffer != INVALID_HANDLE_VALUE && 11161cb0ef41Sopenharmony_ci GetConsoleScreenBufferInfo(active_screen_buffer, 11171cb0ef41Sopenharmony_ci &uv__saved_screen_state)) { 11181cb0ef41Sopenharmony_ci InterlockedOr(&uv__restore_screen_state, 1); 11191cb0ef41Sopenharmony_ci } 11201cb0ef41Sopenharmony_ci 11211cb0ef41Sopenharmony_ci /* Write enter key event to force the console wait to return. */ 11221cb0ef41Sopenharmony_ci record.EventType = KEY_EVENT; 11231cb0ef41Sopenharmony_ci record.Event.KeyEvent.bKeyDown = TRUE; 11241cb0ef41Sopenharmony_ci record.Event.KeyEvent.wRepeatCount = 1; 11251cb0ef41Sopenharmony_ci record.Event.KeyEvent.wVirtualKeyCode = VK_RETURN; 11261cb0ef41Sopenharmony_ci record.Event.KeyEvent.wVirtualScanCode = 11271cb0ef41Sopenharmony_ci MapVirtualKeyW(VK_RETURN, MAPVK_VK_TO_VSC); 11281cb0ef41Sopenharmony_ci record.Event.KeyEvent.uChar.UnicodeChar = L'\r'; 11291cb0ef41Sopenharmony_ci record.Event.KeyEvent.dwControlKeyState = 0; 11301cb0ef41Sopenharmony_ci if (!WriteConsoleInputW(handle->handle, &record, 1, &written)) 11311cb0ef41Sopenharmony_ci err = GetLastError(); 11321cb0ef41Sopenharmony_ci 11331cb0ef41Sopenharmony_ci if (active_screen_buffer != INVALID_HANDLE_VALUE) 11341cb0ef41Sopenharmony_ci CloseHandle(active_screen_buffer); 11351cb0ef41Sopenharmony_ci 11361cb0ef41Sopenharmony_ci return err; 11371cb0ef41Sopenharmony_ci} 11381cb0ef41Sopenharmony_ci 11391cb0ef41Sopenharmony_ci 11401cb0ef41Sopenharmony_cistatic void uv__tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info) { 11411cb0ef41Sopenharmony_ci uv_tty_virtual_width = info->dwSize.X; 11421cb0ef41Sopenharmony_ci uv_tty_virtual_height = info->srWindow.Bottom - info->srWindow.Top + 1; 11431cb0ef41Sopenharmony_ci 11441cb0ef41Sopenharmony_ci /* Recompute virtual window offset row. */ 11451cb0ef41Sopenharmony_ci if (uv_tty_virtual_offset == -1) { 11461cb0ef41Sopenharmony_ci uv_tty_virtual_offset = info->dwCursorPosition.Y; 11471cb0ef41Sopenharmony_ci } else if (uv_tty_virtual_offset < info->dwCursorPosition.Y - 11481cb0ef41Sopenharmony_ci uv_tty_virtual_height + 1) { 11491cb0ef41Sopenharmony_ci /* If suddenly find the cursor outside of the virtual window, it must have 11501cb0ef41Sopenharmony_ci * somehow scrolled. Update the virtual window offset. */ 11511cb0ef41Sopenharmony_ci uv_tty_virtual_offset = info->dwCursorPosition.Y - 11521cb0ef41Sopenharmony_ci uv_tty_virtual_height + 1; 11531cb0ef41Sopenharmony_ci } 11541cb0ef41Sopenharmony_ci if (uv_tty_virtual_offset + uv_tty_virtual_height > info->dwSize.Y) { 11551cb0ef41Sopenharmony_ci uv_tty_virtual_offset = info->dwSize.Y - uv_tty_virtual_height; 11561cb0ef41Sopenharmony_ci } 11571cb0ef41Sopenharmony_ci if (uv_tty_virtual_offset < 0) { 11581cb0ef41Sopenharmony_ci uv_tty_virtual_offset = 0; 11591cb0ef41Sopenharmony_ci } 11601cb0ef41Sopenharmony_ci} 11611cb0ef41Sopenharmony_ci 11621cb0ef41Sopenharmony_ci 11631cb0ef41Sopenharmony_cistatic COORD uv__tty_make_real_coord(uv_tty_t* handle, 11641cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO* info, int x, unsigned char x_relative, int y, 11651cb0ef41Sopenharmony_ci unsigned char y_relative) { 11661cb0ef41Sopenharmony_ci COORD result; 11671cb0ef41Sopenharmony_ci 11681cb0ef41Sopenharmony_ci uv__tty_update_virtual_window(info); 11691cb0ef41Sopenharmony_ci 11701cb0ef41Sopenharmony_ci /* Adjust y position */ 11711cb0ef41Sopenharmony_ci if (y_relative) { 11721cb0ef41Sopenharmony_ci y = info->dwCursorPosition.Y + y; 11731cb0ef41Sopenharmony_ci } else { 11741cb0ef41Sopenharmony_ci y = uv_tty_virtual_offset + y; 11751cb0ef41Sopenharmony_ci } 11761cb0ef41Sopenharmony_ci /* Clip y to virtual client rectangle */ 11771cb0ef41Sopenharmony_ci if (y < uv_tty_virtual_offset) { 11781cb0ef41Sopenharmony_ci y = uv_tty_virtual_offset; 11791cb0ef41Sopenharmony_ci } else if (y >= uv_tty_virtual_offset + uv_tty_virtual_height) { 11801cb0ef41Sopenharmony_ci y = uv_tty_virtual_offset + uv_tty_virtual_height - 1; 11811cb0ef41Sopenharmony_ci } 11821cb0ef41Sopenharmony_ci 11831cb0ef41Sopenharmony_ci /* Adjust x */ 11841cb0ef41Sopenharmony_ci if (x_relative) { 11851cb0ef41Sopenharmony_ci x = info->dwCursorPosition.X + x; 11861cb0ef41Sopenharmony_ci } 11871cb0ef41Sopenharmony_ci /* Clip x */ 11881cb0ef41Sopenharmony_ci if (x < 0) { 11891cb0ef41Sopenharmony_ci x = 0; 11901cb0ef41Sopenharmony_ci } else if (x >= uv_tty_virtual_width) { 11911cb0ef41Sopenharmony_ci x = uv_tty_virtual_width - 1; 11921cb0ef41Sopenharmony_ci } 11931cb0ef41Sopenharmony_ci 11941cb0ef41Sopenharmony_ci result.X = (unsigned short) x; 11951cb0ef41Sopenharmony_ci result.Y = (unsigned short) y; 11961cb0ef41Sopenharmony_ci return result; 11971cb0ef41Sopenharmony_ci} 11981cb0ef41Sopenharmony_ci 11991cb0ef41Sopenharmony_ci 12001cb0ef41Sopenharmony_cistatic int uv__tty_emit_text(uv_tty_t* handle, WCHAR buffer[], DWORD length, 12011cb0ef41Sopenharmony_ci DWORD* error) { 12021cb0ef41Sopenharmony_ci DWORD written; 12031cb0ef41Sopenharmony_ci 12041cb0ef41Sopenharmony_ci if (*error != ERROR_SUCCESS) { 12051cb0ef41Sopenharmony_ci return -1; 12061cb0ef41Sopenharmony_ci } 12071cb0ef41Sopenharmony_ci 12081cb0ef41Sopenharmony_ci if (!WriteConsoleW(handle->handle, 12091cb0ef41Sopenharmony_ci (void*) buffer, 12101cb0ef41Sopenharmony_ci length, 12111cb0ef41Sopenharmony_ci &written, 12121cb0ef41Sopenharmony_ci NULL)) { 12131cb0ef41Sopenharmony_ci *error = GetLastError(); 12141cb0ef41Sopenharmony_ci return -1; 12151cb0ef41Sopenharmony_ci } 12161cb0ef41Sopenharmony_ci 12171cb0ef41Sopenharmony_ci return 0; 12181cb0ef41Sopenharmony_ci} 12191cb0ef41Sopenharmony_ci 12201cb0ef41Sopenharmony_ci 12211cb0ef41Sopenharmony_cistatic int uv__tty_move_caret(uv_tty_t* handle, int x, unsigned char x_relative, 12221cb0ef41Sopenharmony_ci int y, unsigned char y_relative, DWORD* error) { 12231cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO info; 12241cb0ef41Sopenharmony_ci COORD pos; 12251cb0ef41Sopenharmony_ci 12261cb0ef41Sopenharmony_ci if (*error != ERROR_SUCCESS) { 12271cb0ef41Sopenharmony_ci return -1; 12281cb0ef41Sopenharmony_ci } 12291cb0ef41Sopenharmony_ci 12301cb0ef41Sopenharmony_ci retry: 12311cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle->handle, &info)) { 12321cb0ef41Sopenharmony_ci *error = GetLastError(); 12331cb0ef41Sopenharmony_ci } 12341cb0ef41Sopenharmony_ci 12351cb0ef41Sopenharmony_ci pos = uv__tty_make_real_coord(handle, &info, x, x_relative, y, y_relative); 12361cb0ef41Sopenharmony_ci 12371cb0ef41Sopenharmony_ci if (!SetConsoleCursorPosition(handle->handle, pos)) { 12381cb0ef41Sopenharmony_ci if (GetLastError() == ERROR_INVALID_PARAMETER) { 12391cb0ef41Sopenharmony_ci /* The console may be resized - retry */ 12401cb0ef41Sopenharmony_ci goto retry; 12411cb0ef41Sopenharmony_ci } else { 12421cb0ef41Sopenharmony_ci *error = GetLastError(); 12431cb0ef41Sopenharmony_ci return -1; 12441cb0ef41Sopenharmony_ci } 12451cb0ef41Sopenharmony_ci } 12461cb0ef41Sopenharmony_ci 12471cb0ef41Sopenharmony_ci return 0; 12481cb0ef41Sopenharmony_ci} 12491cb0ef41Sopenharmony_ci 12501cb0ef41Sopenharmony_ci 12511cb0ef41Sopenharmony_cistatic int uv__tty_reset(uv_tty_t* handle, DWORD* error) { 12521cb0ef41Sopenharmony_ci const COORD origin = {0, 0}; 12531cb0ef41Sopenharmony_ci const WORD char_attrs = uv_tty_default_text_attributes; 12541cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info; 12551cb0ef41Sopenharmony_ci DWORD count, written; 12561cb0ef41Sopenharmony_ci 12571cb0ef41Sopenharmony_ci if (*error != ERROR_SUCCESS) { 12581cb0ef41Sopenharmony_ci return -1; 12591cb0ef41Sopenharmony_ci } 12601cb0ef41Sopenharmony_ci 12611cb0ef41Sopenharmony_ci /* Reset original text attributes. */ 12621cb0ef41Sopenharmony_ci if (!SetConsoleTextAttribute(handle->handle, char_attrs)) { 12631cb0ef41Sopenharmony_ci *error = GetLastError(); 12641cb0ef41Sopenharmony_ci return -1; 12651cb0ef41Sopenharmony_ci } 12661cb0ef41Sopenharmony_ci 12671cb0ef41Sopenharmony_ci /* Move the cursor position to (0, 0). */ 12681cb0ef41Sopenharmony_ci if (!SetConsoleCursorPosition(handle->handle, origin)) { 12691cb0ef41Sopenharmony_ci *error = GetLastError(); 12701cb0ef41Sopenharmony_ci return -1; 12711cb0ef41Sopenharmony_ci } 12721cb0ef41Sopenharmony_ci 12731cb0ef41Sopenharmony_ci /* Clear the screen buffer. */ 12741cb0ef41Sopenharmony_ci retry: 12751cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle->handle, &screen_buffer_info)) { 12761cb0ef41Sopenharmony_ci *error = GetLastError(); 12771cb0ef41Sopenharmony_ci return -1; 12781cb0ef41Sopenharmony_ci } 12791cb0ef41Sopenharmony_ci 12801cb0ef41Sopenharmony_ci count = screen_buffer_info.dwSize.X * screen_buffer_info.dwSize.Y; 12811cb0ef41Sopenharmony_ci 12821cb0ef41Sopenharmony_ci if (!(FillConsoleOutputCharacterW(handle->handle, 12831cb0ef41Sopenharmony_ci L'\x20', 12841cb0ef41Sopenharmony_ci count, 12851cb0ef41Sopenharmony_ci origin, 12861cb0ef41Sopenharmony_ci &written) && 12871cb0ef41Sopenharmony_ci FillConsoleOutputAttribute(handle->handle, 12881cb0ef41Sopenharmony_ci char_attrs, 12891cb0ef41Sopenharmony_ci written, 12901cb0ef41Sopenharmony_ci origin, 12911cb0ef41Sopenharmony_ci &written))) { 12921cb0ef41Sopenharmony_ci if (GetLastError() == ERROR_INVALID_PARAMETER) { 12931cb0ef41Sopenharmony_ci /* The console may be resized - retry */ 12941cb0ef41Sopenharmony_ci goto retry; 12951cb0ef41Sopenharmony_ci } else { 12961cb0ef41Sopenharmony_ci *error = GetLastError(); 12971cb0ef41Sopenharmony_ci return -1; 12981cb0ef41Sopenharmony_ci } 12991cb0ef41Sopenharmony_ci } 13001cb0ef41Sopenharmony_ci 13011cb0ef41Sopenharmony_ci /* Move the virtual window up to the top. */ 13021cb0ef41Sopenharmony_ci uv_tty_virtual_offset = 0; 13031cb0ef41Sopenharmony_ci uv__tty_update_virtual_window(&screen_buffer_info); 13041cb0ef41Sopenharmony_ci 13051cb0ef41Sopenharmony_ci /* Reset the cursor size and the cursor state. */ 13061cb0ef41Sopenharmony_ci if (!SetConsoleCursorInfo(handle->handle, &uv_tty_default_cursor_info)) { 13071cb0ef41Sopenharmony_ci *error = GetLastError(); 13081cb0ef41Sopenharmony_ci return -1; 13091cb0ef41Sopenharmony_ci } 13101cb0ef41Sopenharmony_ci 13111cb0ef41Sopenharmony_ci return 0; 13121cb0ef41Sopenharmony_ci} 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci 13151cb0ef41Sopenharmony_cistatic int uv__tty_clear(uv_tty_t* handle, int dir, char entire_screen, 13161cb0ef41Sopenharmony_ci DWORD* error) { 13171cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO info; 13181cb0ef41Sopenharmony_ci COORD start, end; 13191cb0ef41Sopenharmony_ci DWORD count, written; 13201cb0ef41Sopenharmony_ci 13211cb0ef41Sopenharmony_ci int x1, x2, y1, y2; 13221cb0ef41Sopenharmony_ci int x1r, x2r, y1r, y2r; 13231cb0ef41Sopenharmony_ci 13241cb0ef41Sopenharmony_ci if (*error != ERROR_SUCCESS) { 13251cb0ef41Sopenharmony_ci return -1; 13261cb0ef41Sopenharmony_ci } 13271cb0ef41Sopenharmony_ci 13281cb0ef41Sopenharmony_ci if (dir == 0) { 13291cb0ef41Sopenharmony_ci /* Clear from current position */ 13301cb0ef41Sopenharmony_ci x1 = 0; 13311cb0ef41Sopenharmony_ci x1r = 1; 13321cb0ef41Sopenharmony_ci } else { 13331cb0ef41Sopenharmony_ci /* Clear from column 0 */ 13341cb0ef41Sopenharmony_ci x1 = 0; 13351cb0ef41Sopenharmony_ci x1r = 0; 13361cb0ef41Sopenharmony_ci } 13371cb0ef41Sopenharmony_ci 13381cb0ef41Sopenharmony_ci if (dir == 1) { 13391cb0ef41Sopenharmony_ci /* Clear to current position */ 13401cb0ef41Sopenharmony_ci x2 = 0; 13411cb0ef41Sopenharmony_ci x2r = 1; 13421cb0ef41Sopenharmony_ci } else { 13431cb0ef41Sopenharmony_ci /* Clear to end of row. We pretend the console is 65536 characters wide, 13441cb0ef41Sopenharmony_ci * uv__tty_make_real_coord will clip it to the actual console width. */ 13451cb0ef41Sopenharmony_ci x2 = 0xffff; 13461cb0ef41Sopenharmony_ci x2r = 0; 13471cb0ef41Sopenharmony_ci } 13481cb0ef41Sopenharmony_ci 13491cb0ef41Sopenharmony_ci if (!entire_screen) { 13501cb0ef41Sopenharmony_ci /* Stay on our own row */ 13511cb0ef41Sopenharmony_ci y1 = y2 = 0; 13521cb0ef41Sopenharmony_ci y1r = y2r = 1; 13531cb0ef41Sopenharmony_ci } else { 13541cb0ef41Sopenharmony_ci /* Apply columns direction to row */ 13551cb0ef41Sopenharmony_ci y1 = x1; 13561cb0ef41Sopenharmony_ci y1r = x1r; 13571cb0ef41Sopenharmony_ci y2 = x2; 13581cb0ef41Sopenharmony_ci y2r = x2r; 13591cb0ef41Sopenharmony_ci } 13601cb0ef41Sopenharmony_ci 13611cb0ef41Sopenharmony_ci retry: 13621cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle->handle, &info)) { 13631cb0ef41Sopenharmony_ci *error = GetLastError(); 13641cb0ef41Sopenharmony_ci return -1; 13651cb0ef41Sopenharmony_ci } 13661cb0ef41Sopenharmony_ci 13671cb0ef41Sopenharmony_ci start = uv__tty_make_real_coord(handle, &info, x1, x1r, y1, y1r); 13681cb0ef41Sopenharmony_ci end = uv__tty_make_real_coord(handle, &info, x2, x2r, y2, y2r); 13691cb0ef41Sopenharmony_ci count = (end.Y * info.dwSize.X + end.X) - 13701cb0ef41Sopenharmony_ci (start.Y * info.dwSize.X + start.X) + 1; 13711cb0ef41Sopenharmony_ci 13721cb0ef41Sopenharmony_ci if (!(FillConsoleOutputCharacterW(handle->handle, 13731cb0ef41Sopenharmony_ci L'\x20', 13741cb0ef41Sopenharmony_ci count, 13751cb0ef41Sopenharmony_ci start, 13761cb0ef41Sopenharmony_ci &written) && 13771cb0ef41Sopenharmony_ci FillConsoleOutputAttribute(handle->handle, 13781cb0ef41Sopenharmony_ci info.wAttributes, 13791cb0ef41Sopenharmony_ci written, 13801cb0ef41Sopenharmony_ci start, 13811cb0ef41Sopenharmony_ci &written))) { 13821cb0ef41Sopenharmony_ci if (GetLastError() == ERROR_INVALID_PARAMETER) { 13831cb0ef41Sopenharmony_ci /* The console may be resized - retry */ 13841cb0ef41Sopenharmony_ci goto retry; 13851cb0ef41Sopenharmony_ci } else { 13861cb0ef41Sopenharmony_ci *error = GetLastError(); 13871cb0ef41Sopenharmony_ci return -1; 13881cb0ef41Sopenharmony_ci } 13891cb0ef41Sopenharmony_ci } 13901cb0ef41Sopenharmony_ci 13911cb0ef41Sopenharmony_ci return 0; 13921cb0ef41Sopenharmony_ci} 13931cb0ef41Sopenharmony_ci 13941cb0ef41Sopenharmony_ci#define FLIP_FGBG \ 13951cb0ef41Sopenharmony_ci do { \ 13961cb0ef41Sopenharmony_ci WORD fg = info.wAttributes & 0xF; \ 13971cb0ef41Sopenharmony_ci WORD bg = info.wAttributes & 0xF0; \ 13981cb0ef41Sopenharmony_ci info.wAttributes &= 0xFF00; \ 13991cb0ef41Sopenharmony_ci info.wAttributes |= fg << 4; \ 14001cb0ef41Sopenharmony_ci info.wAttributes |= bg >> 4; \ 14011cb0ef41Sopenharmony_ci } while (0) 14021cb0ef41Sopenharmony_ci 14031cb0ef41Sopenharmony_cistatic int uv__tty_set_style(uv_tty_t* handle, DWORD* error) { 14041cb0ef41Sopenharmony_ci unsigned short argc = handle->tty.wr.ansi_csi_argc; 14051cb0ef41Sopenharmony_ci unsigned short* argv = handle->tty.wr.ansi_csi_argv; 14061cb0ef41Sopenharmony_ci int i; 14071cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO info; 14081cb0ef41Sopenharmony_ci 14091cb0ef41Sopenharmony_ci char fg_color = -1, bg_color = -1; 14101cb0ef41Sopenharmony_ci char fg_bright = -1, bg_bright = -1; 14111cb0ef41Sopenharmony_ci char inverse = -1; 14121cb0ef41Sopenharmony_ci 14131cb0ef41Sopenharmony_ci if (argc == 0) { 14141cb0ef41Sopenharmony_ci /* Reset mode */ 14151cb0ef41Sopenharmony_ci fg_color = uv_tty_default_fg_color; 14161cb0ef41Sopenharmony_ci bg_color = uv_tty_default_bg_color; 14171cb0ef41Sopenharmony_ci fg_bright = uv_tty_default_fg_bright; 14181cb0ef41Sopenharmony_ci bg_bright = uv_tty_default_bg_bright; 14191cb0ef41Sopenharmony_ci inverse = uv_tty_default_inverse; 14201cb0ef41Sopenharmony_ci } 14211cb0ef41Sopenharmony_ci 14221cb0ef41Sopenharmony_ci for (i = 0; i < argc; i++) { 14231cb0ef41Sopenharmony_ci short arg = argv[i]; 14241cb0ef41Sopenharmony_ci 14251cb0ef41Sopenharmony_ci if (arg == 0) { 14261cb0ef41Sopenharmony_ci /* Reset mode */ 14271cb0ef41Sopenharmony_ci fg_color = uv_tty_default_fg_color; 14281cb0ef41Sopenharmony_ci bg_color = uv_tty_default_bg_color; 14291cb0ef41Sopenharmony_ci fg_bright = uv_tty_default_fg_bright; 14301cb0ef41Sopenharmony_ci bg_bright = uv_tty_default_bg_bright; 14311cb0ef41Sopenharmony_ci inverse = uv_tty_default_inverse; 14321cb0ef41Sopenharmony_ci 14331cb0ef41Sopenharmony_ci } else if (arg == 1) { 14341cb0ef41Sopenharmony_ci /* Foreground bright on */ 14351cb0ef41Sopenharmony_ci fg_bright = 1; 14361cb0ef41Sopenharmony_ci 14371cb0ef41Sopenharmony_ci } else if (arg == 2) { 14381cb0ef41Sopenharmony_ci /* Both bright off */ 14391cb0ef41Sopenharmony_ci fg_bright = 0; 14401cb0ef41Sopenharmony_ci bg_bright = 0; 14411cb0ef41Sopenharmony_ci 14421cb0ef41Sopenharmony_ci } else if (arg == 5) { 14431cb0ef41Sopenharmony_ci /* Background bright on */ 14441cb0ef41Sopenharmony_ci bg_bright = 1; 14451cb0ef41Sopenharmony_ci 14461cb0ef41Sopenharmony_ci } else if (arg == 7) { 14471cb0ef41Sopenharmony_ci /* Inverse: on */ 14481cb0ef41Sopenharmony_ci inverse = 1; 14491cb0ef41Sopenharmony_ci 14501cb0ef41Sopenharmony_ci } else if (arg == 21 || arg == 22) { 14511cb0ef41Sopenharmony_ci /* Foreground bright off */ 14521cb0ef41Sopenharmony_ci fg_bright = 0; 14531cb0ef41Sopenharmony_ci 14541cb0ef41Sopenharmony_ci } else if (arg == 25) { 14551cb0ef41Sopenharmony_ci /* Background bright off */ 14561cb0ef41Sopenharmony_ci bg_bright = 0; 14571cb0ef41Sopenharmony_ci 14581cb0ef41Sopenharmony_ci } else if (arg == 27) { 14591cb0ef41Sopenharmony_ci /* Inverse: off */ 14601cb0ef41Sopenharmony_ci inverse = 0; 14611cb0ef41Sopenharmony_ci 14621cb0ef41Sopenharmony_ci } else if (arg >= 30 && arg <= 37) { 14631cb0ef41Sopenharmony_ci /* Set foreground color */ 14641cb0ef41Sopenharmony_ci fg_color = arg - 30; 14651cb0ef41Sopenharmony_ci 14661cb0ef41Sopenharmony_ci } else if (arg == 39) { 14671cb0ef41Sopenharmony_ci /* Default text color */ 14681cb0ef41Sopenharmony_ci fg_color = uv_tty_default_fg_color; 14691cb0ef41Sopenharmony_ci fg_bright = uv_tty_default_fg_bright; 14701cb0ef41Sopenharmony_ci 14711cb0ef41Sopenharmony_ci } else if (arg >= 40 && arg <= 47) { 14721cb0ef41Sopenharmony_ci /* Set background color */ 14731cb0ef41Sopenharmony_ci bg_color = arg - 40; 14741cb0ef41Sopenharmony_ci 14751cb0ef41Sopenharmony_ci } else if (arg == 49) { 14761cb0ef41Sopenharmony_ci /* Default background color */ 14771cb0ef41Sopenharmony_ci bg_color = uv_tty_default_bg_color; 14781cb0ef41Sopenharmony_ci bg_bright = uv_tty_default_bg_bright; 14791cb0ef41Sopenharmony_ci 14801cb0ef41Sopenharmony_ci } else if (arg >= 90 && arg <= 97) { 14811cb0ef41Sopenharmony_ci /* Set bold foreground color */ 14821cb0ef41Sopenharmony_ci fg_bright = 1; 14831cb0ef41Sopenharmony_ci fg_color = arg - 90; 14841cb0ef41Sopenharmony_ci 14851cb0ef41Sopenharmony_ci } else if (arg >= 100 && arg <= 107) { 14861cb0ef41Sopenharmony_ci /* Set bold background color */ 14871cb0ef41Sopenharmony_ci bg_bright = 1; 14881cb0ef41Sopenharmony_ci bg_color = arg - 100; 14891cb0ef41Sopenharmony_ci 14901cb0ef41Sopenharmony_ci } 14911cb0ef41Sopenharmony_ci } 14921cb0ef41Sopenharmony_ci 14931cb0ef41Sopenharmony_ci if (fg_color == -1 && bg_color == -1 && fg_bright == -1 && 14941cb0ef41Sopenharmony_ci bg_bright == -1 && inverse == -1) { 14951cb0ef41Sopenharmony_ci /* Nothing changed */ 14961cb0ef41Sopenharmony_ci return 0; 14971cb0ef41Sopenharmony_ci } 14981cb0ef41Sopenharmony_ci 14991cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle->handle, &info)) { 15001cb0ef41Sopenharmony_ci *error = GetLastError(); 15011cb0ef41Sopenharmony_ci return -1; 15021cb0ef41Sopenharmony_ci } 15031cb0ef41Sopenharmony_ci 15041cb0ef41Sopenharmony_ci if ((info.wAttributes & COMMON_LVB_REVERSE_VIDEO) > 0) { 15051cb0ef41Sopenharmony_ci FLIP_FGBG; 15061cb0ef41Sopenharmony_ci } 15071cb0ef41Sopenharmony_ci 15081cb0ef41Sopenharmony_ci if (fg_color != -1) { 15091cb0ef41Sopenharmony_ci info.wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 15101cb0ef41Sopenharmony_ci if (fg_color & 1) info.wAttributes |= FOREGROUND_RED; 15111cb0ef41Sopenharmony_ci if (fg_color & 2) info.wAttributes |= FOREGROUND_GREEN; 15121cb0ef41Sopenharmony_ci if (fg_color & 4) info.wAttributes |= FOREGROUND_BLUE; 15131cb0ef41Sopenharmony_ci } 15141cb0ef41Sopenharmony_ci 15151cb0ef41Sopenharmony_ci if (fg_bright != -1) { 15161cb0ef41Sopenharmony_ci if (fg_bright) { 15171cb0ef41Sopenharmony_ci info.wAttributes |= FOREGROUND_INTENSITY; 15181cb0ef41Sopenharmony_ci } else { 15191cb0ef41Sopenharmony_ci info.wAttributes &= ~FOREGROUND_INTENSITY; 15201cb0ef41Sopenharmony_ci } 15211cb0ef41Sopenharmony_ci } 15221cb0ef41Sopenharmony_ci 15231cb0ef41Sopenharmony_ci if (bg_color != -1) { 15241cb0ef41Sopenharmony_ci info.wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); 15251cb0ef41Sopenharmony_ci if (bg_color & 1) info.wAttributes |= BACKGROUND_RED; 15261cb0ef41Sopenharmony_ci if (bg_color & 2) info.wAttributes |= BACKGROUND_GREEN; 15271cb0ef41Sopenharmony_ci if (bg_color & 4) info.wAttributes |= BACKGROUND_BLUE; 15281cb0ef41Sopenharmony_ci } 15291cb0ef41Sopenharmony_ci 15301cb0ef41Sopenharmony_ci if (bg_bright != -1) { 15311cb0ef41Sopenharmony_ci if (bg_bright) { 15321cb0ef41Sopenharmony_ci info.wAttributes |= BACKGROUND_INTENSITY; 15331cb0ef41Sopenharmony_ci } else { 15341cb0ef41Sopenharmony_ci info.wAttributes &= ~BACKGROUND_INTENSITY; 15351cb0ef41Sopenharmony_ci } 15361cb0ef41Sopenharmony_ci } 15371cb0ef41Sopenharmony_ci 15381cb0ef41Sopenharmony_ci if (inverse != -1) { 15391cb0ef41Sopenharmony_ci if (inverse) { 15401cb0ef41Sopenharmony_ci info.wAttributes |= COMMON_LVB_REVERSE_VIDEO; 15411cb0ef41Sopenharmony_ci } else { 15421cb0ef41Sopenharmony_ci info.wAttributes &= ~COMMON_LVB_REVERSE_VIDEO; 15431cb0ef41Sopenharmony_ci } 15441cb0ef41Sopenharmony_ci } 15451cb0ef41Sopenharmony_ci 15461cb0ef41Sopenharmony_ci if ((info.wAttributes & COMMON_LVB_REVERSE_VIDEO) > 0) { 15471cb0ef41Sopenharmony_ci FLIP_FGBG; 15481cb0ef41Sopenharmony_ci } 15491cb0ef41Sopenharmony_ci 15501cb0ef41Sopenharmony_ci if (!SetConsoleTextAttribute(handle->handle, info.wAttributes)) { 15511cb0ef41Sopenharmony_ci *error = GetLastError(); 15521cb0ef41Sopenharmony_ci return -1; 15531cb0ef41Sopenharmony_ci } 15541cb0ef41Sopenharmony_ci 15551cb0ef41Sopenharmony_ci return 0; 15561cb0ef41Sopenharmony_ci} 15571cb0ef41Sopenharmony_ci 15581cb0ef41Sopenharmony_ci 15591cb0ef41Sopenharmony_cistatic int uv__tty_save_state(uv_tty_t* handle, unsigned char save_attributes, 15601cb0ef41Sopenharmony_ci DWORD* error) { 15611cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO info; 15621cb0ef41Sopenharmony_ci 15631cb0ef41Sopenharmony_ci if (*error != ERROR_SUCCESS) { 15641cb0ef41Sopenharmony_ci return -1; 15651cb0ef41Sopenharmony_ci } 15661cb0ef41Sopenharmony_ci 15671cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle->handle, &info)) { 15681cb0ef41Sopenharmony_ci *error = GetLastError(); 15691cb0ef41Sopenharmony_ci return -1; 15701cb0ef41Sopenharmony_ci } 15711cb0ef41Sopenharmony_ci 15721cb0ef41Sopenharmony_ci uv__tty_update_virtual_window(&info); 15731cb0ef41Sopenharmony_ci 15741cb0ef41Sopenharmony_ci handle->tty.wr.saved_position.X = info.dwCursorPosition.X; 15751cb0ef41Sopenharmony_ci handle->tty.wr.saved_position.Y = info.dwCursorPosition.Y - 15761cb0ef41Sopenharmony_ci uv_tty_virtual_offset; 15771cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_TTY_SAVED_POSITION; 15781cb0ef41Sopenharmony_ci 15791cb0ef41Sopenharmony_ci if (save_attributes) { 15801cb0ef41Sopenharmony_ci handle->tty.wr.saved_attributes = info.wAttributes & 15811cb0ef41Sopenharmony_ci (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY); 15821cb0ef41Sopenharmony_ci handle->flags |= UV_HANDLE_TTY_SAVED_ATTRIBUTES; 15831cb0ef41Sopenharmony_ci } 15841cb0ef41Sopenharmony_ci 15851cb0ef41Sopenharmony_ci return 0; 15861cb0ef41Sopenharmony_ci} 15871cb0ef41Sopenharmony_ci 15881cb0ef41Sopenharmony_ci 15891cb0ef41Sopenharmony_cistatic int uv__tty_restore_state(uv_tty_t* handle, 15901cb0ef41Sopenharmony_ci unsigned char restore_attributes, DWORD* error) { 15911cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO info; 15921cb0ef41Sopenharmony_ci WORD new_attributes; 15931cb0ef41Sopenharmony_ci 15941cb0ef41Sopenharmony_ci if (*error != ERROR_SUCCESS) { 15951cb0ef41Sopenharmony_ci return -1; 15961cb0ef41Sopenharmony_ci } 15971cb0ef41Sopenharmony_ci 15981cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_TTY_SAVED_POSITION) { 15991cb0ef41Sopenharmony_ci if (uv__tty_move_caret(handle, 16001cb0ef41Sopenharmony_ci handle->tty.wr.saved_position.X, 16011cb0ef41Sopenharmony_ci 0, 16021cb0ef41Sopenharmony_ci handle->tty.wr.saved_position.Y, 16031cb0ef41Sopenharmony_ci 0, 16041cb0ef41Sopenharmony_ci error) != 0) { 16051cb0ef41Sopenharmony_ci return -1; 16061cb0ef41Sopenharmony_ci } 16071cb0ef41Sopenharmony_ci } 16081cb0ef41Sopenharmony_ci 16091cb0ef41Sopenharmony_ci if (restore_attributes && 16101cb0ef41Sopenharmony_ci (handle->flags & UV_HANDLE_TTY_SAVED_ATTRIBUTES)) { 16111cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(handle->handle, &info)) { 16121cb0ef41Sopenharmony_ci *error = GetLastError(); 16131cb0ef41Sopenharmony_ci return -1; 16141cb0ef41Sopenharmony_ci } 16151cb0ef41Sopenharmony_ci 16161cb0ef41Sopenharmony_ci new_attributes = info.wAttributes; 16171cb0ef41Sopenharmony_ci new_attributes &= ~(FOREGROUND_INTENSITY | BACKGROUND_INTENSITY); 16181cb0ef41Sopenharmony_ci new_attributes |= handle->tty.wr.saved_attributes; 16191cb0ef41Sopenharmony_ci 16201cb0ef41Sopenharmony_ci if (!SetConsoleTextAttribute(handle->handle, new_attributes)) { 16211cb0ef41Sopenharmony_ci *error = GetLastError(); 16221cb0ef41Sopenharmony_ci return -1; 16231cb0ef41Sopenharmony_ci } 16241cb0ef41Sopenharmony_ci } 16251cb0ef41Sopenharmony_ci 16261cb0ef41Sopenharmony_ci return 0; 16271cb0ef41Sopenharmony_ci} 16281cb0ef41Sopenharmony_ci 16291cb0ef41Sopenharmony_cistatic int uv__tty_set_cursor_visibility(uv_tty_t* handle, 16301cb0ef41Sopenharmony_ci BOOL visible, 16311cb0ef41Sopenharmony_ci DWORD* error) { 16321cb0ef41Sopenharmony_ci CONSOLE_CURSOR_INFO cursor_info; 16331cb0ef41Sopenharmony_ci 16341cb0ef41Sopenharmony_ci if (!GetConsoleCursorInfo(handle->handle, &cursor_info)) { 16351cb0ef41Sopenharmony_ci *error = GetLastError(); 16361cb0ef41Sopenharmony_ci return -1; 16371cb0ef41Sopenharmony_ci } 16381cb0ef41Sopenharmony_ci 16391cb0ef41Sopenharmony_ci cursor_info.bVisible = visible; 16401cb0ef41Sopenharmony_ci 16411cb0ef41Sopenharmony_ci if (!SetConsoleCursorInfo(handle->handle, &cursor_info)) { 16421cb0ef41Sopenharmony_ci *error = GetLastError(); 16431cb0ef41Sopenharmony_ci return -1; 16441cb0ef41Sopenharmony_ci } 16451cb0ef41Sopenharmony_ci 16461cb0ef41Sopenharmony_ci return 0; 16471cb0ef41Sopenharmony_ci} 16481cb0ef41Sopenharmony_ci 16491cb0ef41Sopenharmony_cistatic int uv__tty_set_cursor_shape(uv_tty_t* handle, int style, DWORD* error) { 16501cb0ef41Sopenharmony_ci CONSOLE_CURSOR_INFO cursor_info; 16511cb0ef41Sopenharmony_ci 16521cb0ef41Sopenharmony_ci if (!GetConsoleCursorInfo(handle->handle, &cursor_info)) { 16531cb0ef41Sopenharmony_ci *error = GetLastError(); 16541cb0ef41Sopenharmony_ci return -1; 16551cb0ef41Sopenharmony_ci } 16561cb0ef41Sopenharmony_ci 16571cb0ef41Sopenharmony_ci if (style == 0) { 16581cb0ef41Sopenharmony_ci cursor_info.dwSize = uv_tty_default_cursor_info.dwSize; 16591cb0ef41Sopenharmony_ci } else if (style <= 2) { 16601cb0ef41Sopenharmony_ci cursor_info.dwSize = CURSOR_SIZE_LARGE; 16611cb0ef41Sopenharmony_ci } else { 16621cb0ef41Sopenharmony_ci cursor_info.dwSize = CURSOR_SIZE_SMALL; 16631cb0ef41Sopenharmony_ci } 16641cb0ef41Sopenharmony_ci 16651cb0ef41Sopenharmony_ci if (!SetConsoleCursorInfo(handle->handle, &cursor_info)) { 16661cb0ef41Sopenharmony_ci *error = GetLastError(); 16671cb0ef41Sopenharmony_ci return -1; 16681cb0ef41Sopenharmony_ci } 16691cb0ef41Sopenharmony_ci 16701cb0ef41Sopenharmony_ci return 0; 16711cb0ef41Sopenharmony_ci} 16721cb0ef41Sopenharmony_ci 16731cb0ef41Sopenharmony_ci 16741cb0ef41Sopenharmony_cistatic int uv__tty_write_bufs(uv_tty_t* handle, 16751cb0ef41Sopenharmony_ci const uv_buf_t bufs[], 16761cb0ef41Sopenharmony_ci unsigned int nbufs, 16771cb0ef41Sopenharmony_ci DWORD* error) { 16781cb0ef41Sopenharmony_ci /* We can only write 8k characters at a time. Windows can't handle much more 16791cb0ef41Sopenharmony_ci * characters in a single console write anyway. */ 16801cb0ef41Sopenharmony_ci WCHAR utf16_buf[MAX_CONSOLE_CHAR]; 16811cb0ef41Sopenharmony_ci DWORD utf16_buf_used = 0; 16821cb0ef41Sopenharmony_ci unsigned int i; 16831cb0ef41Sopenharmony_ci 16841cb0ef41Sopenharmony_ci#define FLUSH_TEXT() \ 16851cb0ef41Sopenharmony_ci do { \ 16861cb0ef41Sopenharmony_ci if (utf16_buf_used > 0) { \ 16871cb0ef41Sopenharmony_ci uv__tty_emit_text(handle, utf16_buf, utf16_buf_used, error); \ 16881cb0ef41Sopenharmony_ci utf16_buf_used = 0; \ 16891cb0ef41Sopenharmony_ci } \ 16901cb0ef41Sopenharmony_ci } while (0) 16911cb0ef41Sopenharmony_ci 16921cb0ef41Sopenharmony_ci#define ENSURE_BUFFER_SPACE(wchars_needed) \ 16931cb0ef41Sopenharmony_ci if (wchars_needed > ARRAY_SIZE(utf16_buf) - utf16_buf_used) { \ 16941cb0ef41Sopenharmony_ci FLUSH_TEXT(); \ 16951cb0ef41Sopenharmony_ci } 16961cb0ef41Sopenharmony_ci 16971cb0ef41Sopenharmony_ci /* Cache for fast access */ 16981cb0ef41Sopenharmony_ci unsigned char utf8_bytes_left = handle->tty.wr.utf8_bytes_left; 16991cb0ef41Sopenharmony_ci unsigned int utf8_codepoint = handle->tty.wr.utf8_codepoint; 17001cb0ef41Sopenharmony_ci unsigned char previous_eol = handle->tty.wr.previous_eol; 17011cb0ef41Sopenharmony_ci unsigned short ansi_parser_state = handle->tty.wr.ansi_parser_state; 17021cb0ef41Sopenharmony_ci 17031cb0ef41Sopenharmony_ci /* Store the error here. If we encounter an error, stop trying to do i/o but 17041cb0ef41Sopenharmony_ci * keep parsing the buffer so we leave the parser in a consistent state. */ 17051cb0ef41Sopenharmony_ci *error = ERROR_SUCCESS; 17061cb0ef41Sopenharmony_ci 17071cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 17081cb0ef41Sopenharmony_ci 17091cb0ef41Sopenharmony_ci for (i = 0; i < nbufs; i++) { 17101cb0ef41Sopenharmony_ci uv_buf_t buf = bufs[i]; 17111cb0ef41Sopenharmony_ci unsigned int j; 17121cb0ef41Sopenharmony_ci 17131cb0ef41Sopenharmony_ci for (j = 0; j < buf.len; j++) { 17141cb0ef41Sopenharmony_ci unsigned char c = buf.base[j]; 17151cb0ef41Sopenharmony_ci 17161cb0ef41Sopenharmony_ci /* Run the character through the utf8 decoder We happily accept non 17171cb0ef41Sopenharmony_ci * shortest form encodings and invalid code points - there's no real harm 17181cb0ef41Sopenharmony_ci * that can be done. */ 17191cb0ef41Sopenharmony_ci if (utf8_bytes_left == 0) { 17201cb0ef41Sopenharmony_ci /* Read utf-8 start byte */ 17211cb0ef41Sopenharmony_ci DWORD first_zero_bit; 17221cb0ef41Sopenharmony_ci unsigned char not_c = ~c; 17231cb0ef41Sopenharmony_ci#ifdef _MSC_VER /* msvc */ 17241cb0ef41Sopenharmony_ci if (_BitScanReverse(&first_zero_bit, not_c)) { 17251cb0ef41Sopenharmony_ci#else /* assume gcc */ 17261cb0ef41Sopenharmony_ci if (c != 0) { 17271cb0ef41Sopenharmony_ci first_zero_bit = (sizeof(int) * 8) - 1 - __builtin_clz(not_c); 17281cb0ef41Sopenharmony_ci#endif 17291cb0ef41Sopenharmony_ci if (first_zero_bit == 7) { 17301cb0ef41Sopenharmony_ci /* Ascii - pass right through */ 17311cb0ef41Sopenharmony_ci utf8_codepoint = (unsigned int) c; 17321cb0ef41Sopenharmony_ci 17331cb0ef41Sopenharmony_ci } else if (first_zero_bit <= 5) { 17341cb0ef41Sopenharmony_ci /* Multibyte sequence */ 17351cb0ef41Sopenharmony_ci utf8_codepoint = (0xff >> (8 - first_zero_bit)) & c; 17361cb0ef41Sopenharmony_ci utf8_bytes_left = (char) (6 - first_zero_bit); 17371cb0ef41Sopenharmony_ci 17381cb0ef41Sopenharmony_ci } else { 17391cb0ef41Sopenharmony_ci /* Invalid continuation */ 17401cb0ef41Sopenharmony_ci utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER; 17411cb0ef41Sopenharmony_ci } 17421cb0ef41Sopenharmony_ci 17431cb0ef41Sopenharmony_ci } else { 17441cb0ef41Sopenharmony_ci /* 0xff -- invalid */ 17451cb0ef41Sopenharmony_ci utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER; 17461cb0ef41Sopenharmony_ci } 17471cb0ef41Sopenharmony_ci 17481cb0ef41Sopenharmony_ci } else if ((c & 0xc0) == 0x80) { 17491cb0ef41Sopenharmony_ci /* Valid continuation of utf-8 multibyte sequence */ 17501cb0ef41Sopenharmony_ci utf8_bytes_left--; 17511cb0ef41Sopenharmony_ci utf8_codepoint <<= 6; 17521cb0ef41Sopenharmony_ci utf8_codepoint |= ((unsigned int) c & 0x3f); 17531cb0ef41Sopenharmony_ci 17541cb0ef41Sopenharmony_ci } else { 17551cb0ef41Sopenharmony_ci /* Start byte where continuation was expected. */ 17561cb0ef41Sopenharmony_ci utf8_bytes_left = 0; 17571cb0ef41Sopenharmony_ci utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER; 17581cb0ef41Sopenharmony_ci /* Patch buf offset so this character will be parsed again as a start 17591cb0ef41Sopenharmony_ci * byte. */ 17601cb0ef41Sopenharmony_ci j--; 17611cb0ef41Sopenharmony_ci } 17621cb0ef41Sopenharmony_ci 17631cb0ef41Sopenharmony_ci /* Maybe we need to parse more bytes to find a character. */ 17641cb0ef41Sopenharmony_ci if (utf8_bytes_left != 0) { 17651cb0ef41Sopenharmony_ci continue; 17661cb0ef41Sopenharmony_ci } 17671cb0ef41Sopenharmony_ci 17681cb0ef41Sopenharmony_ci /* Parse vt100/ansi escape codes */ 17691cb0ef41Sopenharmony_ci if (uv__vterm_state == UV_TTY_SUPPORTED) { 17701cb0ef41Sopenharmony_ci /* Pass through escape codes if conhost supports them. */ 17711cb0ef41Sopenharmony_ci } else if (ansi_parser_state == ANSI_NORMAL) { 17721cb0ef41Sopenharmony_ci switch (utf8_codepoint) { 17731cb0ef41Sopenharmony_ci case '\033': 17741cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_ESCAPE_SEEN; 17751cb0ef41Sopenharmony_ci continue; 17761cb0ef41Sopenharmony_ci 17771cb0ef41Sopenharmony_ci case 0233: 17781cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_CSI; 17791cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argc = 0; 17801cb0ef41Sopenharmony_ci continue; 17811cb0ef41Sopenharmony_ci } 17821cb0ef41Sopenharmony_ci 17831cb0ef41Sopenharmony_ci } else if (ansi_parser_state == ANSI_ESCAPE_SEEN) { 17841cb0ef41Sopenharmony_ci switch (utf8_codepoint) { 17851cb0ef41Sopenharmony_ci case '[': 17861cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_CSI; 17871cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argc = 0; 17881cb0ef41Sopenharmony_ci continue; 17891cb0ef41Sopenharmony_ci 17901cb0ef41Sopenharmony_ci case '^': 17911cb0ef41Sopenharmony_ci case '_': 17921cb0ef41Sopenharmony_ci case 'P': 17931cb0ef41Sopenharmony_ci case ']': 17941cb0ef41Sopenharmony_ci /* Not supported, but we'll have to parse until we see a stop code, 17951cb0ef41Sopenharmony_ci * e. g. ESC \ or BEL. */ 17961cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_ST_CONTROL; 17971cb0ef41Sopenharmony_ci continue; 17981cb0ef41Sopenharmony_ci 17991cb0ef41Sopenharmony_ci case '\033': 18001cb0ef41Sopenharmony_ci /* Ignore double escape. */ 18011cb0ef41Sopenharmony_ci continue; 18021cb0ef41Sopenharmony_ci 18031cb0ef41Sopenharmony_ci case 'c': 18041cb0ef41Sopenharmony_ci /* Full console reset. */ 18051cb0ef41Sopenharmony_ci FLUSH_TEXT(); 18061cb0ef41Sopenharmony_ci uv__tty_reset(handle, error); 18071cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18081cb0ef41Sopenharmony_ci continue; 18091cb0ef41Sopenharmony_ci 18101cb0ef41Sopenharmony_ci case '7': 18111cb0ef41Sopenharmony_ci /* Save the cursor position and text attributes. */ 18121cb0ef41Sopenharmony_ci FLUSH_TEXT(); 18131cb0ef41Sopenharmony_ci uv__tty_save_state(handle, 1, error); 18141cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18151cb0ef41Sopenharmony_ci continue; 18161cb0ef41Sopenharmony_ci 18171cb0ef41Sopenharmony_ci case '8': 18181cb0ef41Sopenharmony_ci /* Restore the cursor position and text attributes */ 18191cb0ef41Sopenharmony_ci FLUSH_TEXT(); 18201cb0ef41Sopenharmony_ci uv__tty_restore_state(handle, 1, error); 18211cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18221cb0ef41Sopenharmony_ci continue; 18231cb0ef41Sopenharmony_ci 18241cb0ef41Sopenharmony_ci default: 18251cb0ef41Sopenharmony_ci if (utf8_codepoint >= '@' && utf8_codepoint <= '_') { 18261cb0ef41Sopenharmony_ci /* Single-char control. */ 18271cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18281cb0ef41Sopenharmony_ci continue; 18291cb0ef41Sopenharmony_ci } else { 18301cb0ef41Sopenharmony_ci /* Invalid - proceed as normal, */ 18311cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18321cb0ef41Sopenharmony_ci } 18331cb0ef41Sopenharmony_ci } 18341cb0ef41Sopenharmony_ci 18351cb0ef41Sopenharmony_ci } else if (ansi_parser_state == ANSI_IGNORE) { 18361cb0ef41Sopenharmony_ci /* We're ignoring this command. Stop only on command character. */ 18371cb0ef41Sopenharmony_ci if (utf8_codepoint >= '@' && utf8_codepoint <= '~') { 18381cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18391cb0ef41Sopenharmony_ci } 18401cb0ef41Sopenharmony_ci continue; 18411cb0ef41Sopenharmony_ci 18421cb0ef41Sopenharmony_ci } else if (ansi_parser_state == ANSI_DECSCUSR) { 18431cb0ef41Sopenharmony_ci /* So far we've the sequence `ESC [ arg space`, and we're waiting for 18441cb0ef41Sopenharmony_ci * the final command byte. */ 18451cb0ef41Sopenharmony_ci if (utf8_codepoint >= '@' && utf8_codepoint <= '~') { 18461cb0ef41Sopenharmony_ci /* Command byte */ 18471cb0ef41Sopenharmony_ci if (utf8_codepoint == 'q') { 18481cb0ef41Sopenharmony_ci /* Change the cursor shape */ 18491cb0ef41Sopenharmony_ci int style = handle->tty.wr.ansi_csi_argc 18501cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1; 18511cb0ef41Sopenharmony_ci if (style >= 0 && style <= 6) { 18521cb0ef41Sopenharmony_ci FLUSH_TEXT(); 18531cb0ef41Sopenharmony_ci uv__tty_set_cursor_shape(handle, style, error); 18541cb0ef41Sopenharmony_ci } 18551cb0ef41Sopenharmony_ci } 18561cb0ef41Sopenharmony_ci 18571cb0ef41Sopenharmony_ci /* Sequence ended - go back to normal state. */ 18581cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 18591cb0ef41Sopenharmony_ci continue; 18601cb0ef41Sopenharmony_ci } 18611cb0ef41Sopenharmony_ci /* Unexpected character, but sequence hasn't ended yet. Ignore the rest 18621cb0ef41Sopenharmony_ci * of the sequence. */ 18631cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_IGNORE; 18641cb0ef41Sopenharmony_ci 18651cb0ef41Sopenharmony_ci } else if (ansi_parser_state & ANSI_CSI) { 18661cb0ef41Sopenharmony_ci /* So far we've seen `ESC [`, and we may or may not have already parsed 18671cb0ef41Sopenharmony_ci * some of the arguments that follow. */ 18681cb0ef41Sopenharmony_ci 18691cb0ef41Sopenharmony_ci if (utf8_codepoint >= '0' && utf8_codepoint <= '9') { 18701cb0ef41Sopenharmony_ci /* Parse a numerical argument. */ 18711cb0ef41Sopenharmony_ci if (!(ansi_parser_state & ANSI_IN_ARG)) { 18721cb0ef41Sopenharmony_ci /* We were not currently parsing a number, add a new one. */ 18731cb0ef41Sopenharmony_ci /* Check for that there are too many arguments. */ 18741cb0ef41Sopenharmony_ci if (handle->tty.wr.ansi_csi_argc >= 18751cb0ef41Sopenharmony_ci ARRAY_SIZE(handle->tty.wr.ansi_csi_argv)) { 18761cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_IGNORE; 18771cb0ef41Sopenharmony_ci continue; 18781cb0ef41Sopenharmony_ci } 18791cb0ef41Sopenharmony_ci ansi_parser_state |= ANSI_IN_ARG; 18801cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argc++; 18811cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1] = 18821cb0ef41Sopenharmony_ci (unsigned short) utf8_codepoint - '0'; 18831cb0ef41Sopenharmony_ci continue; 18841cb0ef41Sopenharmony_ci 18851cb0ef41Sopenharmony_ci } else { 18861cb0ef41Sopenharmony_ci /* We were already parsing a number. Parse next digit. */ 18871cb0ef41Sopenharmony_ci uint32_t value = 10 * 18881cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1]; 18891cb0ef41Sopenharmony_ci 18901cb0ef41Sopenharmony_ci /* Check for overflow. */ 18911cb0ef41Sopenharmony_ci if (value > UINT16_MAX) { 18921cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_IGNORE; 18931cb0ef41Sopenharmony_ci continue; 18941cb0ef41Sopenharmony_ci } 18951cb0ef41Sopenharmony_ci 18961cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1] = 18971cb0ef41Sopenharmony_ci (unsigned short) value + (utf8_codepoint - '0'); 18981cb0ef41Sopenharmony_ci continue; 18991cb0ef41Sopenharmony_ci } 19001cb0ef41Sopenharmony_ci 19011cb0ef41Sopenharmony_ci } else if (utf8_codepoint == ';') { 19021cb0ef41Sopenharmony_ci /* Denotes the end of an argument. */ 19031cb0ef41Sopenharmony_ci if (ansi_parser_state & ANSI_IN_ARG) { 19041cb0ef41Sopenharmony_ci ansi_parser_state &= ~ANSI_IN_ARG; 19051cb0ef41Sopenharmony_ci continue; 19061cb0ef41Sopenharmony_ci 19071cb0ef41Sopenharmony_ci } else { 19081cb0ef41Sopenharmony_ci /* If ANSI_IN_ARG is not set, add another argument and default 19091cb0ef41Sopenharmony_ci * it to 0. */ 19101cb0ef41Sopenharmony_ci 19111cb0ef41Sopenharmony_ci /* Check for too many arguments */ 19121cb0ef41Sopenharmony_ci if (handle->tty.wr.ansi_csi_argc >= 19131cb0ef41Sopenharmony_ci 19141cb0ef41Sopenharmony_ci ARRAY_SIZE(handle->tty.wr.ansi_csi_argv)) { 19151cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_IGNORE; 19161cb0ef41Sopenharmony_ci continue; 19171cb0ef41Sopenharmony_ci } 19181cb0ef41Sopenharmony_ci 19191cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argc++; 19201cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[handle->tty.wr.ansi_csi_argc - 1] = 0; 19211cb0ef41Sopenharmony_ci continue; 19221cb0ef41Sopenharmony_ci } 19231cb0ef41Sopenharmony_ci 19241cb0ef41Sopenharmony_ci } else if (utf8_codepoint == '?' && 19251cb0ef41Sopenharmony_ci !(ansi_parser_state & ANSI_IN_ARG) && 19261cb0ef41Sopenharmony_ci !(ansi_parser_state & ANSI_EXTENSION) && 19271cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argc == 0) { 19281cb0ef41Sopenharmony_ci /* Pass through '?' if it is the first character after CSI */ 19291cb0ef41Sopenharmony_ci /* This is an extension character from the VT100 codeset */ 19301cb0ef41Sopenharmony_ci /* that is supported and used by most ANSI terminals today. */ 19311cb0ef41Sopenharmony_ci ansi_parser_state |= ANSI_EXTENSION; 19321cb0ef41Sopenharmony_ci continue; 19331cb0ef41Sopenharmony_ci 19341cb0ef41Sopenharmony_ci } else if (utf8_codepoint == ' ' && 19351cb0ef41Sopenharmony_ci !(ansi_parser_state & ANSI_EXTENSION)) { 19361cb0ef41Sopenharmony_ci /* We expect a command byte to follow after this space. The only 19371cb0ef41Sopenharmony_ci * command that we current support is 'set cursor style'. */ 19381cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_DECSCUSR; 19391cb0ef41Sopenharmony_ci continue; 19401cb0ef41Sopenharmony_ci 19411cb0ef41Sopenharmony_ci } else if (utf8_codepoint >= '@' && utf8_codepoint <= '~') { 19421cb0ef41Sopenharmony_ci /* Command byte */ 19431cb0ef41Sopenharmony_ci if (ansi_parser_state & ANSI_EXTENSION) { 19441cb0ef41Sopenharmony_ci /* Sequence is `ESC [ ? args command`. */ 19451cb0ef41Sopenharmony_ci switch (utf8_codepoint) { 19461cb0ef41Sopenharmony_ci case 'l': 19471cb0ef41Sopenharmony_ci /* Hide the cursor */ 19481cb0ef41Sopenharmony_ci if (handle->tty.wr.ansi_csi_argc == 1 && 19491cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[0] == 25) { 19501cb0ef41Sopenharmony_ci FLUSH_TEXT(); 19511cb0ef41Sopenharmony_ci uv__tty_set_cursor_visibility(handle, 0, error); 19521cb0ef41Sopenharmony_ci } 19531cb0ef41Sopenharmony_ci break; 19541cb0ef41Sopenharmony_ci 19551cb0ef41Sopenharmony_ci case 'h': 19561cb0ef41Sopenharmony_ci /* Show the cursor */ 19571cb0ef41Sopenharmony_ci if (handle->tty.wr.ansi_csi_argc == 1 && 19581cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[0] == 25) { 19591cb0ef41Sopenharmony_ci FLUSH_TEXT(); 19601cb0ef41Sopenharmony_ci uv__tty_set_cursor_visibility(handle, 1, error); 19611cb0ef41Sopenharmony_ci } 19621cb0ef41Sopenharmony_ci break; 19631cb0ef41Sopenharmony_ci } 19641cb0ef41Sopenharmony_ci 19651cb0ef41Sopenharmony_ci } else { 19661cb0ef41Sopenharmony_ci /* Sequence is `ESC [ args command`. */ 19671cb0ef41Sopenharmony_ci int x, y, d; 19681cb0ef41Sopenharmony_ci switch (utf8_codepoint) { 19691cb0ef41Sopenharmony_ci case 'A': 19701cb0ef41Sopenharmony_ci /* cursor up */ 19711cb0ef41Sopenharmony_ci FLUSH_TEXT(); 19721cb0ef41Sopenharmony_ci y = -(handle->tty.wr.ansi_csi_argc 19731cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1); 19741cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, 0, 1, y, 1, error); 19751cb0ef41Sopenharmony_ci break; 19761cb0ef41Sopenharmony_ci 19771cb0ef41Sopenharmony_ci case 'B': 19781cb0ef41Sopenharmony_ci /* cursor down */ 19791cb0ef41Sopenharmony_ci FLUSH_TEXT(); 19801cb0ef41Sopenharmony_ci y = handle->tty.wr.ansi_csi_argc 19811cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1; 19821cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, 0, 1, y, 1, error); 19831cb0ef41Sopenharmony_ci break; 19841cb0ef41Sopenharmony_ci 19851cb0ef41Sopenharmony_ci case 'C': 19861cb0ef41Sopenharmony_ci /* cursor forward */ 19871cb0ef41Sopenharmony_ci FLUSH_TEXT(); 19881cb0ef41Sopenharmony_ci x = handle->tty.wr.ansi_csi_argc 19891cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1; 19901cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, x, 1, 0, 1, error); 19911cb0ef41Sopenharmony_ci break; 19921cb0ef41Sopenharmony_ci 19931cb0ef41Sopenharmony_ci case 'D': 19941cb0ef41Sopenharmony_ci /* cursor back */ 19951cb0ef41Sopenharmony_ci FLUSH_TEXT(); 19961cb0ef41Sopenharmony_ci x = -(handle->tty.wr.ansi_csi_argc 19971cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1); 19981cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, x, 1, 0, 1, error); 19991cb0ef41Sopenharmony_ci break; 20001cb0ef41Sopenharmony_ci 20011cb0ef41Sopenharmony_ci case 'E': 20021cb0ef41Sopenharmony_ci /* cursor next line */ 20031cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20041cb0ef41Sopenharmony_ci y = handle->tty.wr.ansi_csi_argc 20051cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1; 20061cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, 0, 0, y, 1, error); 20071cb0ef41Sopenharmony_ci break; 20081cb0ef41Sopenharmony_ci 20091cb0ef41Sopenharmony_ci case 'F': 20101cb0ef41Sopenharmony_ci /* cursor previous line */ 20111cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20121cb0ef41Sopenharmony_ci y = -(handle->tty.wr.ansi_csi_argc 20131cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 1); 20141cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, 0, 0, y, 1, error); 20151cb0ef41Sopenharmony_ci break; 20161cb0ef41Sopenharmony_ci 20171cb0ef41Sopenharmony_ci case 'G': 20181cb0ef41Sopenharmony_ci /* cursor horizontal move absolute */ 20191cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20201cb0ef41Sopenharmony_ci x = (handle->tty.wr.ansi_csi_argc >= 1 && 20211cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[0]) 20221cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] - 1 : 0; 20231cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, x, 0, 0, 1, error); 20241cb0ef41Sopenharmony_ci break; 20251cb0ef41Sopenharmony_ci 20261cb0ef41Sopenharmony_ci case 'H': 20271cb0ef41Sopenharmony_ci case 'f': 20281cb0ef41Sopenharmony_ci /* cursor move absolute */ 20291cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20301cb0ef41Sopenharmony_ci y = (handle->tty.wr.ansi_csi_argc >= 1 && 20311cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[0]) 20321cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] - 1 : 0; 20331cb0ef41Sopenharmony_ci x = (handle->tty.wr.ansi_csi_argc >= 2 && 20341cb0ef41Sopenharmony_ci handle->tty.wr.ansi_csi_argv[1]) 20351cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[1] - 1 : 0; 20361cb0ef41Sopenharmony_ci uv__tty_move_caret(handle, x, 0, y, 0, error); 20371cb0ef41Sopenharmony_ci break; 20381cb0ef41Sopenharmony_ci 20391cb0ef41Sopenharmony_ci case 'J': 20401cb0ef41Sopenharmony_ci /* Erase screen */ 20411cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20421cb0ef41Sopenharmony_ci d = handle->tty.wr.ansi_csi_argc 20431cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 0; 20441cb0ef41Sopenharmony_ci if (d >= 0 && d <= 2) { 20451cb0ef41Sopenharmony_ci uv__tty_clear(handle, d, 1, error); 20461cb0ef41Sopenharmony_ci } 20471cb0ef41Sopenharmony_ci break; 20481cb0ef41Sopenharmony_ci 20491cb0ef41Sopenharmony_ci case 'K': 20501cb0ef41Sopenharmony_ci /* Erase line */ 20511cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20521cb0ef41Sopenharmony_ci d = handle->tty.wr.ansi_csi_argc 20531cb0ef41Sopenharmony_ci ? handle->tty.wr.ansi_csi_argv[0] : 0; 20541cb0ef41Sopenharmony_ci if (d >= 0 && d <= 2) { 20551cb0ef41Sopenharmony_ci uv__tty_clear(handle, d, 0, error); 20561cb0ef41Sopenharmony_ci } 20571cb0ef41Sopenharmony_ci break; 20581cb0ef41Sopenharmony_ci 20591cb0ef41Sopenharmony_ci case 'm': 20601cb0ef41Sopenharmony_ci /* Set style */ 20611cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20621cb0ef41Sopenharmony_ci uv__tty_set_style(handle, error); 20631cb0ef41Sopenharmony_ci break; 20641cb0ef41Sopenharmony_ci 20651cb0ef41Sopenharmony_ci case 's': 20661cb0ef41Sopenharmony_ci /* Save the cursor position. */ 20671cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20681cb0ef41Sopenharmony_ci uv__tty_save_state(handle, 0, error); 20691cb0ef41Sopenharmony_ci break; 20701cb0ef41Sopenharmony_ci 20711cb0ef41Sopenharmony_ci case 'u': 20721cb0ef41Sopenharmony_ci /* Restore the cursor position */ 20731cb0ef41Sopenharmony_ci FLUSH_TEXT(); 20741cb0ef41Sopenharmony_ci uv__tty_restore_state(handle, 0, error); 20751cb0ef41Sopenharmony_ci break; 20761cb0ef41Sopenharmony_ci } 20771cb0ef41Sopenharmony_ci } 20781cb0ef41Sopenharmony_ci 20791cb0ef41Sopenharmony_ci /* Sequence ended - go back to normal state. */ 20801cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 20811cb0ef41Sopenharmony_ci continue; 20821cb0ef41Sopenharmony_ci 20831cb0ef41Sopenharmony_ci } else { 20841cb0ef41Sopenharmony_ci /* We don't support commands that use private mode characters or 20851cb0ef41Sopenharmony_ci * intermediaries. Ignore the rest of the sequence. */ 20861cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_IGNORE; 20871cb0ef41Sopenharmony_ci continue; 20881cb0ef41Sopenharmony_ci } 20891cb0ef41Sopenharmony_ci 20901cb0ef41Sopenharmony_ci } else if (ansi_parser_state & ANSI_ST_CONTROL) { 20911cb0ef41Sopenharmony_ci /* Unsupported control code. 20921cb0ef41Sopenharmony_ci * Ignore everything until we see `BEL` or `ESC \`. */ 20931cb0ef41Sopenharmony_ci if (ansi_parser_state & ANSI_IN_STRING) { 20941cb0ef41Sopenharmony_ci if (!(ansi_parser_state & ANSI_BACKSLASH_SEEN)) { 20951cb0ef41Sopenharmony_ci if (utf8_codepoint == '"') { 20961cb0ef41Sopenharmony_ci ansi_parser_state &= ~ANSI_IN_STRING; 20971cb0ef41Sopenharmony_ci } else if (utf8_codepoint == '\\') { 20981cb0ef41Sopenharmony_ci ansi_parser_state |= ANSI_BACKSLASH_SEEN; 20991cb0ef41Sopenharmony_ci } 21001cb0ef41Sopenharmony_ci } else { 21011cb0ef41Sopenharmony_ci ansi_parser_state &= ~ANSI_BACKSLASH_SEEN; 21021cb0ef41Sopenharmony_ci } 21031cb0ef41Sopenharmony_ci } else { 21041cb0ef41Sopenharmony_ci if (utf8_codepoint == '\007' || (utf8_codepoint == '\\' && 21051cb0ef41Sopenharmony_ci (ansi_parser_state & ANSI_ESCAPE_SEEN))) { 21061cb0ef41Sopenharmony_ci /* End of sequence */ 21071cb0ef41Sopenharmony_ci ansi_parser_state = ANSI_NORMAL; 21081cb0ef41Sopenharmony_ci } else if (utf8_codepoint == '\033') { 21091cb0ef41Sopenharmony_ci /* Escape character */ 21101cb0ef41Sopenharmony_ci ansi_parser_state |= ANSI_ESCAPE_SEEN; 21111cb0ef41Sopenharmony_ci } else if (utf8_codepoint == '"') { 21121cb0ef41Sopenharmony_ci /* String starting */ 21131cb0ef41Sopenharmony_ci ansi_parser_state |= ANSI_IN_STRING; 21141cb0ef41Sopenharmony_ci ansi_parser_state &= ~ANSI_ESCAPE_SEEN; 21151cb0ef41Sopenharmony_ci ansi_parser_state &= ~ANSI_BACKSLASH_SEEN; 21161cb0ef41Sopenharmony_ci } else { 21171cb0ef41Sopenharmony_ci ansi_parser_state &= ~ANSI_ESCAPE_SEEN; 21181cb0ef41Sopenharmony_ci } 21191cb0ef41Sopenharmony_ci } 21201cb0ef41Sopenharmony_ci continue; 21211cb0ef41Sopenharmony_ci } else { 21221cb0ef41Sopenharmony_ci /* Inconsistent state */ 21231cb0ef41Sopenharmony_ci abort(); 21241cb0ef41Sopenharmony_ci } 21251cb0ef41Sopenharmony_ci 21261cb0ef41Sopenharmony_ci if (utf8_codepoint == 0x0a || utf8_codepoint == 0x0d) { 21271cb0ef41Sopenharmony_ci /* EOL conversion - emit \r\n when we see \n. */ 21281cb0ef41Sopenharmony_ci 21291cb0ef41Sopenharmony_ci if (utf8_codepoint == 0x0a && previous_eol != 0x0d) { 21301cb0ef41Sopenharmony_ci /* \n was not preceded by \r; print \r\n. */ 21311cb0ef41Sopenharmony_ci ENSURE_BUFFER_SPACE(2); 21321cb0ef41Sopenharmony_ci utf16_buf[utf16_buf_used++] = L'\r'; 21331cb0ef41Sopenharmony_ci utf16_buf[utf16_buf_used++] = L'\n'; 21341cb0ef41Sopenharmony_ci } else if (utf8_codepoint == 0x0d && previous_eol == 0x0a) { 21351cb0ef41Sopenharmony_ci /* \n was followed by \r; do not print the \r, since the source was 21361cb0ef41Sopenharmony_ci * either \r\n\r (so the second \r is redundant) or was \n\r (so the 21371cb0ef41Sopenharmony_ci * \n was processed by the last case and an \r automatically 21381cb0ef41Sopenharmony_ci * inserted). */ 21391cb0ef41Sopenharmony_ci } else { 21401cb0ef41Sopenharmony_ci /* \r without \n; print \r as-is. */ 21411cb0ef41Sopenharmony_ci ENSURE_BUFFER_SPACE(1); 21421cb0ef41Sopenharmony_ci utf16_buf[utf16_buf_used++] = (WCHAR) utf8_codepoint; 21431cb0ef41Sopenharmony_ci } 21441cb0ef41Sopenharmony_ci 21451cb0ef41Sopenharmony_ci previous_eol = (char) utf8_codepoint; 21461cb0ef41Sopenharmony_ci 21471cb0ef41Sopenharmony_ci } else if (utf8_codepoint <= 0xffff) { 21481cb0ef41Sopenharmony_ci /* Encode character into utf-16 buffer. */ 21491cb0ef41Sopenharmony_ci ENSURE_BUFFER_SPACE(1); 21501cb0ef41Sopenharmony_ci utf16_buf[utf16_buf_used++] = (WCHAR) utf8_codepoint; 21511cb0ef41Sopenharmony_ci previous_eol = 0; 21521cb0ef41Sopenharmony_ci } else { 21531cb0ef41Sopenharmony_ci ENSURE_BUFFER_SPACE(2); 21541cb0ef41Sopenharmony_ci utf8_codepoint -= 0x10000; 21551cb0ef41Sopenharmony_ci utf16_buf[utf16_buf_used++] = (WCHAR) (utf8_codepoint / 0x400 + 0xD800); 21561cb0ef41Sopenharmony_ci utf16_buf[utf16_buf_used++] = (WCHAR) (utf8_codepoint % 0x400 + 0xDC00); 21571cb0ef41Sopenharmony_ci previous_eol = 0; 21581cb0ef41Sopenharmony_ci } 21591cb0ef41Sopenharmony_ci } 21601cb0ef41Sopenharmony_ci } 21611cb0ef41Sopenharmony_ci 21621cb0ef41Sopenharmony_ci /* Flush remaining characters */ 21631cb0ef41Sopenharmony_ci FLUSH_TEXT(); 21641cb0ef41Sopenharmony_ci 21651cb0ef41Sopenharmony_ci /* Copy cached values back to struct. */ 21661cb0ef41Sopenharmony_ci handle->tty.wr.utf8_bytes_left = utf8_bytes_left; 21671cb0ef41Sopenharmony_ci handle->tty.wr.utf8_codepoint = utf8_codepoint; 21681cb0ef41Sopenharmony_ci handle->tty.wr.previous_eol = previous_eol; 21691cb0ef41Sopenharmony_ci handle->tty.wr.ansi_parser_state = ansi_parser_state; 21701cb0ef41Sopenharmony_ci 21711cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 21721cb0ef41Sopenharmony_ci 21731cb0ef41Sopenharmony_ci if (*error == STATUS_SUCCESS) { 21741cb0ef41Sopenharmony_ci return 0; 21751cb0ef41Sopenharmony_ci } else { 21761cb0ef41Sopenharmony_ci return -1; 21771cb0ef41Sopenharmony_ci } 21781cb0ef41Sopenharmony_ci 21791cb0ef41Sopenharmony_ci#undef FLUSH_TEXT 21801cb0ef41Sopenharmony_ci} 21811cb0ef41Sopenharmony_ci 21821cb0ef41Sopenharmony_ci 21831cb0ef41Sopenharmony_ciint uv__tty_write(uv_loop_t* loop, 21841cb0ef41Sopenharmony_ci uv_write_t* req, 21851cb0ef41Sopenharmony_ci uv_tty_t* handle, 21861cb0ef41Sopenharmony_ci const uv_buf_t bufs[], 21871cb0ef41Sopenharmony_ci unsigned int nbufs, 21881cb0ef41Sopenharmony_ci uv_write_cb cb) { 21891cb0ef41Sopenharmony_ci DWORD error; 21901cb0ef41Sopenharmony_ci 21911cb0ef41Sopenharmony_ci UV_REQ_INIT(req, UV_WRITE); 21921cb0ef41Sopenharmony_ci req->handle = (uv_stream_t*) handle; 21931cb0ef41Sopenharmony_ci req->cb = cb; 21941cb0ef41Sopenharmony_ci 21951cb0ef41Sopenharmony_ci handle->reqs_pending++; 21961cb0ef41Sopenharmony_ci handle->stream.conn.write_reqs_pending++; 21971cb0ef41Sopenharmony_ci REGISTER_HANDLE_REQ(loop, handle, req); 21981cb0ef41Sopenharmony_ci 21991cb0ef41Sopenharmony_ci req->u.io.queued_bytes = 0; 22001cb0ef41Sopenharmony_ci 22011cb0ef41Sopenharmony_ci if (!uv__tty_write_bufs(handle, bufs, nbufs, &error)) { 22021cb0ef41Sopenharmony_ci SET_REQ_SUCCESS(req); 22031cb0ef41Sopenharmony_ci } else { 22041cb0ef41Sopenharmony_ci SET_REQ_ERROR(req, error); 22051cb0ef41Sopenharmony_ci } 22061cb0ef41Sopenharmony_ci 22071cb0ef41Sopenharmony_ci uv__insert_pending_req(loop, (uv_req_t*) req); 22081cb0ef41Sopenharmony_ci 22091cb0ef41Sopenharmony_ci return 0; 22101cb0ef41Sopenharmony_ci} 22111cb0ef41Sopenharmony_ci 22121cb0ef41Sopenharmony_ci 22131cb0ef41Sopenharmony_ciint uv__tty_try_write(uv_tty_t* handle, 22141cb0ef41Sopenharmony_ci const uv_buf_t bufs[], 22151cb0ef41Sopenharmony_ci unsigned int nbufs) { 22161cb0ef41Sopenharmony_ci DWORD error; 22171cb0ef41Sopenharmony_ci 22181cb0ef41Sopenharmony_ci if (handle->stream.conn.write_reqs_pending > 0) 22191cb0ef41Sopenharmony_ci return UV_EAGAIN; 22201cb0ef41Sopenharmony_ci 22211cb0ef41Sopenharmony_ci if (uv__tty_write_bufs(handle, bufs, nbufs, &error)) 22221cb0ef41Sopenharmony_ci return uv_translate_sys_error(error); 22231cb0ef41Sopenharmony_ci 22241cb0ef41Sopenharmony_ci return uv__count_bufs(bufs, nbufs); 22251cb0ef41Sopenharmony_ci} 22261cb0ef41Sopenharmony_ci 22271cb0ef41Sopenharmony_ci 22281cb0ef41Sopenharmony_civoid uv__process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle, 22291cb0ef41Sopenharmony_ci uv_write_t* req) { 22301cb0ef41Sopenharmony_ci int err; 22311cb0ef41Sopenharmony_ci 22321cb0ef41Sopenharmony_ci handle->write_queue_size -= req->u.io.queued_bytes; 22331cb0ef41Sopenharmony_ci UNREGISTER_HANDLE_REQ(loop, handle, req); 22341cb0ef41Sopenharmony_ci 22351cb0ef41Sopenharmony_ci if (req->cb) { 22361cb0ef41Sopenharmony_ci err = GET_REQ_ERROR(req); 22371cb0ef41Sopenharmony_ci req->cb(req, uv_translate_sys_error(err)); 22381cb0ef41Sopenharmony_ci } 22391cb0ef41Sopenharmony_ci 22401cb0ef41Sopenharmony_ci 22411cb0ef41Sopenharmony_ci handle->stream.conn.write_reqs_pending--; 22421cb0ef41Sopenharmony_ci if (handle->stream.conn.write_reqs_pending == 0) 22431cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_SHUTTING) 22441cb0ef41Sopenharmony_ci uv__process_tty_shutdown_req(loop, 22451cb0ef41Sopenharmony_ci handle, 22461cb0ef41Sopenharmony_ci handle->stream.conn.shutdown_req); 22471cb0ef41Sopenharmony_ci 22481cb0ef41Sopenharmony_ci DECREASE_PENDING_REQ_COUNT(handle); 22491cb0ef41Sopenharmony_ci} 22501cb0ef41Sopenharmony_ci 22511cb0ef41Sopenharmony_ci 22521cb0ef41Sopenharmony_civoid uv__tty_close(uv_tty_t* handle) { 22531cb0ef41Sopenharmony_ci assert(handle->u.fd == -1 || handle->u.fd > 2); 22541cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_READING) 22551cb0ef41Sopenharmony_ci uv__tty_read_stop(handle); 22561cb0ef41Sopenharmony_ci 22571cb0ef41Sopenharmony_ci if (handle->u.fd == -1) 22581cb0ef41Sopenharmony_ci CloseHandle(handle->handle); 22591cb0ef41Sopenharmony_ci else 22601cb0ef41Sopenharmony_ci close(handle->u.fd); 22611cb0ef41Sopenharmony_ci 22621cb0ef41Sopenharmony_ci handle->u.fd = -1; 22631cb0ef41Sopenharmony_ci handle->handle = INVALID_HANDLE_VALUE; 22641cb0ef41Sopenharmony_ci handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE); 22651cb0ef41Sopenharmony_ci uv__handle_closing(handle); 22661cb0ef41Sopenharmony_ci 22671cb0ef41Sopenharmony_ci if (handle->reqs_pending == 0) 22681cb0ef41Sopenharmony_ci uv__want_endgame(handle->loop, (uv_handle_t*) handle); 22691cb0ef41Sopenharmony_ci} 22701cb0ef41Sopenharmony_ci 22711cb0ef41Sopenharmony_ci 22721cb0ef41Sopenharmony_civoid uv__process_tty_shutdown_req(uv_loop_t* loop, uv_tty_t* stream, uv_shutdown_t* req) { 22731cb0ef41Sopenharmony_ci assert(stream->stream.conn.write_reqs_pending == 0); 22741cb0ef41Sopenharmony_ci assert(req); 22751cb0ef41Sopenharmony_ci 22761cb0ef41Sopenharmony_ci stream->stream.conn.shutdown_req = NULL; 22771cb0ef41Sopenharmony_ci stream->flags &= ~UV_HANDLE_SHUTTING; 22781cb0ef41Sopenharmony_ci UNREGISTER_HANDLE_REQ(loop, stream, req); 22791cb0ef41Sopenharmony_ci 22801cb0ef41Sopenharmony_ci /* TTY shutdown is really just a no-op */ 22811cb0ef41Sopenharmony_ci if (req->cb) { 22821cb0ef41Sopenharmony_ci if (stream->flags & UV_HANDLE_CLOSING) { 22831cb0ef41Sopenharmony_ci req->cb(req, UV_ECANCELED); 22841cb0ef41Sopenharmony_ci } else { 22851cb0ef41Sopenharmony_ci req->cb(req, 0); 22861cb0ef41Sopenharmony_ci } 22871cb0ef41Sopenharmony_ci } 22881cb0ef41Sopenharmony_ci 22891cb0ef41Sopenharmony_ci DECREASE_PENDING_REQ_COUNT(stream); 22901cb0ef41Sopenharmony_ci} 22911cb0ef41Sopenharmony_ci 22921cb0ef41Sopenharmony_ci 22931cb0ef41Sopenharmony_civoid uv__tty_endgame(uv_loop_t* loop, uv_tty_t* handle) { 22941cb0ef41Sopenharmony_ci assert(handle->flags & UV_HANDLE_CLOSING); 22951cb0ef41Sopenharmony_ci assert(handle->reqs_pending == 0); 22961cb0ef41Sopenharmony_ci 22971cb0ef41Sopenharmony_ci /* The wait handle used for raw reading should be unregistered when the 22981cb0ef41Sopenharmony_ci * wait callback runs. */ 22991cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_TTY_READABLE) || 23001cb0ef41Sopenharmony_ci handle->tty.rd.read_raw_wait == NULL); 23011cb0ef41Sopenharmony_ci 23021cb0ef41Sopenharmony_ci assert(!(handle->flags & UV_HANDLE_CLOSED)); 23031cb0ef41Sopenharmony_ci uv__handle_close(handle); 23041cb0ef41Sopenharmony_ci} 23051cb0ef41Sopenharmony_ci 23061cb0ef41Sopenharmony_ci 23071cb0ef41Sopenharmony_ci/* 23081cb0ef41Sopenharmony_ci * uv__process_tty_accept_req() is a stub to keep DELEGATE_STREAM_REQ working 23091cb0ef41Sopenharmony_ci * TODO: find a way to remove it 23101cb0ef41Sopenharmony_ci */ 23111cb0ef41Sopenharmony_civoid uv__process_tty_accept_req(uv_loop_t* loop, uv_tty_t* handle, 23121cb0ef41Sopenharmony_ci uv_req_t* raw_req) { 23131cb0ef41Sopenharmony_ci abort(); 23141cb0ef41Sopenharmony_ci} 23151cb0ef41Sopenharmony_ci 23161cb0ef41Sopenharmony_ci 23171cb0ef41Sopenharmony_ci/* 23181cb0ef41Sopenharmony_ci * uv__process_tty_connect_req() is a stub to keep DELEGATE_STREAM_REQ working 23191cb0ef41Sopenharmony_ci * TODO: find a way to remove it 23201cb0ef41Sopenharmony_ci */ 23211cb0ef41Sopenharmony_civoid uv__process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle, 23221cb0ef41Sopenharmony_ci uv_connect_t* req) { 23231cb0ef41Sopenharmony_ci abort(); 23241cb0ef41Sopenharmony_ci} 23251cb0ef41Sopenharmony_ci 23261cb0ef41Sopenharmony_ci 23271cb0ef41Sopenharmony_ciint uv_tty_reset_mode(void) { 23281cb0ef41Sopenharmony_ci /* Not necessary to do anything. */ 23291cb0ef41Sopenharmony_ci return 0; 23301cb0ef41Sopenharmony_ci} 23311cb0ef41Sopenharmony_ci 23321cb0ef41Sopenharmony_ci/* Determine whether or not this version of windows supports 23331cb0ef41Sopenharmony_ci * proper ANSI color codes. Should be supported as of windows 23341cb0ef41Sopenharmony_ci * 10 version 1511, build number 10.0.10586. 23351cb0ef41Sopenharmony_ci */ 23361cb0ef41Sopenharmony_cistatic void uv__determine_vterm_state(HANDLE handle) { 23371cb0ef41Sopenharmony_ci DWORD dwMode = 0; 23381cb0ef41Sopenharmony_ci 23391cb0ef41Sopenharmony_ci uv__need_check_vterm_state = FALSE; 23401cb0ef41Sopenharmony_ci if (!GetConsoleMode(handle, &dwMode)) { 23411cb0ef41Sopenharmony_ci return; 23421cb0ef41Sopenharmony_ci } 23431cb0ef41Sopenharmony_ci 23441cb0ef41Sopenharmony_ci dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; 23451cb0ef41Sopenharmony_ci if (!SetConsoleMode(handle, dwMode)) { 23461cb0ef41Sopenharmony_ci return; 23471cb0ef41Sopenharmony_ci } 23481cb0ef41Sopenharmony_ci 23491cb0ef41Sopenharmony_ci uv__vterm_state = UV_TTY_SUPPORTED; 23501cb0ef41Sopenharmony_ci} 23511cb0ef41Sopenharmony_ci 23521cb0ef41Sopenharmony_cistatic DWORD WINAPI uv__tty_console_resize_message_loop_thread(void* param) { 23531cb0ef41Sopenharmony_ci NTSTATUS status; 23541cb0ef41Sopenharmony_ci ULONG_PTR conhost_pid; 23551cb0ef41Sopenharmony_ci MSG msg; 23561cb0ef41Sopenharmony_ci 23571cb0ef41Sopenharmony_ci if (pSetWinEventHook == NULL || pNtQueryInformationProcess == NULL) 23581cb0ef41Sopenharmony_ci return 0; 23591cb0ef41Sopenharmony_ci 23601cb0ef41Sopenharmony_ci status = pNtQueryInformationProcess(GetCurrentProcess(), 23611cb0ef41Sopenharmony_ci ProcessConsoleHostProcess, 23621cb0ef41Sopenharmony_ci &conhost_pid, 23631cb0ef41Sopenharmony_ci sizeof(conhost_pid), 23641cb0ef41Sopenharmony_ci NULL); 23651cb0ef41Sopenharmony_ci 23661cb0ef41Sopenharmony_ci if (!NT_SUCCESS(status)) { 23671cb0ef41Sopenharmony_ci /* We couldn't retrieve our console host process, probably because this 23681cb0ef41Sopenharmony_ci * is a 32-bit process running on 64-bit Windows. Fall back to receiving 23691cb0ef41Sopenharmony_ci * console events from the input stream only. */ 23701cb0ef41Sopenharmony_ci return 0; 23711cb0ef41Sopenharmony_ci } 23721cb0ef41Sopenharmony_ci 23731cb0ef41Sopenharmony_ci /* Ensure the PID is a multiple of 4, which is required by SetWinEventHook */ 23741cb0ef41Sopenharmony_ci conhost_pid &= ~(ULONG_PTR)0x3; 23751cb0ef41Sopenharmony_ci 23761cb0ef41Sopenharmony_ci uv__tty_console_resized = CreateEvent(NULL, TRUE, FALSE, NULL); 23771cb0ef41Sopenharmony_ci if (uv__tty_console_resized == NULL) 23781cb0ef41Sopenharmony_ci return 0; 23791cb0ef41Sopenharmony_ci if (QueueUserWorkItem(uv__tty_console_resize_watcher_thread, 23801cb0ef41Sopenharmony_ci NULL, 23811cb0ef41Sopenharmony_ci WT_EXECUTELONGFUNCTION) == 0) 23821cb0ef41Sopenharmony_ci return 0; 23831cb0ef41Sopenharmony_ci 23841cb0ef41Sopenharmony_ci if (!pSetWinEventHook(EVENT_CONSOLE_LAYOUT, 23851cb0ef41Sopenharmony_ci EVENT_CONSOLE_LAYOUT, 23861cb0ef41Sopenharmony_ci NULL, 23871cb0ef41Sopenharmony_ci uv__tty_console_resize_event, 23881cb0ef41Sopenharmony_ci (DWORD)conhost_pid, 23891cb0ef41Sopenharmony_ci 0, 23901cb0ef41Sopenharmony_ci WINEVENT_OUTOFCONTEXT)) 23911cb0ef41Sopenharmony_ci return 0; 23921cb0ef41Sopenharmony_ci 23931cb0ef41Sopenharmony_ci while (GetMessage(&msg, NULL, 0, 0)) { 23941cb0ef41Sopenharmony_ci TranslateMessage(&msg); 23951cb0ef41Sopenharmony_ci DispatchMessage(&msg); 23961cb0ef41Sopenharmony_ci } 23971cb0ef41Sopenharmony_ci return 0; 23981cb0ef41Sopenharmony_ci} 23991cb0ef41Sopenharmony_ci 24001cb0ef41Sopenharmony_cistatic void CALLBACK uv__tty_console_resize_event(HWINEVENTHOOK hWinEventHook, 24011cb0ef41Sopenharmony_ci DWORD event, 24021cb0ef41Sopenharmony_ci HWND hwnd, 24031cb0ef41Sopenharmony_ci LONG idObject, 24041cb0ef41Sopenharmony_ci LONG idChild, 24051cb0ef41Sopenharmony_ci DWORD dwEventThread, 24061cb0ef41Sopenharmony_ci DWORD dwmsEventTime) { 24071cb0ef41Sopenharmony_ci SetEvent(uv__tty_console_resized); 24081cb0ef41Sopenharmony_ci} 24091cb0ef41Sopenharmony_ci 24101cb0ef41Sopenharmony_cistatic DWORD WINAPI uv__tty_console_resize_watcher_thread(void* param) { 24111cb0ef41Sopenharmony_ci for (;;) { 24121cb0ef41Sopenharmony_ci /* Make sure to not overwhelm the system with resize events */ 24131cb0ef41Sopenharmony_ci Sleep(33); 24141cb0ef41Sopenharmony_ci WaitForSingleObject(uv__tty_console_resized, INFINITE); 24151cb0ef41Sopenharmony_ci uv__tty_console_signal_resize(); 24161cb0ef41Sopenharmony_ci ResetEvent(uv__tty_console_resized); 24171cb0ef41Sopenharmony_ci } 24181cb0ef41Sopenharmony_ci return 0; 24191cb0ef41Sopenharmony_ci} 24201cb0ef41Sopenharmony_ci 24211cb0ef41Sopenharmony_cistatic void uv__tty_console_signal_resize(void) { 24221cb0ef41Sopenharmony_ci CONSOLE_SCREEN_BUFFER_INFO sb_info; 24231cb0ef41Sopenharmony_ci int width, height; 24241cb0ef41Sopenharmony_ci 24251cb0ef41Sopenharmony_ci if (!GetConsoleScreenBufferInfo(uv__tty_console_handle, &sb_info)) 24261cb0ef41Sopenharmony_ci return; 24271cb0ef41Sopenharmony_ci 24281cb0ef41Sopenharmony_ci width = sb_info.dwSize.X; 24291cb0ef41Sopenharmony_ci height = sb_info.srWindow.Bottom - sb_info.srWindow.Top + 1; 24301cb0ef41Sopenharmony_ci 24311cb0ef41Sopenharmony_ci uv_mutex_lock(&uv__tty_console_resize_mutex); 24321cb0ef41Sopenharmony_ci assert(uv__tty_console_width != -1 && uv__tty_console_height != -1); 24331cb0ef41Sopenharmony_ci if (width != uv__tty_console_width || height != uv__tty_console_height) { 24341cb0ef41Sopenharmony_ci uv__tty_console_width = width; 24351cb0ef41Sopenharmony_ci uv__tty_console_height = height; 24361cb0ef41Sopenharmony_ci uv_mutex_unlock(&uv__tty_console_resize_mutex); 24371cb0ef41Sopenharmony_ci uv__signal_dispatch(SIGWINCH); 24381cb0ef41Sopenharmony_ci } else { 24391cb0ef41Sopenharmony_ci uv_mutex_unlock(&uv__tty_console_resize_mutex); 24401cb0ef41Sopenharmony_ci } 24411cb0ef41Sopenharmony_ci} 24421cb0ef41Sopenharmony_ci 24431cb0ef41Sopenharmony_civoid uv_tty_set_vterm_state(uv_tty_vtermstate_t state) { 24441cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 24451cb0ef41Sopenharmony_ci uv__need_check_vterm_state = FALSE; 24461cb0ef41Sopenharmony_ci uv__vterm_state = state; 24471cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 24481cb0ef41Sopenharmony_ci} 24491cb0ef41Sopenharmony_ci 24501cb0ef41Sopenharmony_ciint uv_tty_get_vterm_state(uv_tty_vtermstate_t* state) { 24511cb0ef41Sopenharmony_ci uv_sem_wait(&uv_tty_output_lock); 24521cb0ef41Sopenharmony_ci *state = uv__vterm_state; 24531cb0ef41Sopenharmony_ci uv_sem_post(&uv_tty_output_lock); 24541cb0ef41Sopenharmony_ci return 0; 24551cb0ef41Sopenharmony_ci} 2456