Skip to content

Commit

Permalink
win,tty: Change to restore cursor shape with uv_tty_reset()
Browse files Browse the repository at this point in the history
PR-URL: libuv#1884
Co-authored-by: Bert Belder <bertbelder@gmail.com>
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
Reviewed-By: Jameson Nash <vtjnash@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <s@saghul.net>
(cherry picked from commit 73ca4ac)
  • Loading branch information
erw7 authored and musm committed Jul 9, 2020
1 parent d551fa2 commit 456a8cb
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/win/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif

static void uv_tty_capture_initial_style(CONSOLE_SCREEN_BUFFER_INFO* info);
static void uv_tty_capture_initial_style(
CONSOLE_SCREEN_BUFFER_INFO* screen_buffer_info,
CONSOLE_CURSOR_INFO* cursor_info);
static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info);
static int uv__cancel_read_console(uv_tty_t* handle);

Expand Down Expand Up @@ -138,6 +140,8 @@ static char uv_tty_default_fg_bright = 0;
static char uv_tty_default_bg_bright = 0;
static char uv_tty_default_inverse = 0;

static CONSOLE_CURSOR_INFO uv_tty_default_cursor_info;

/* Determine whether or not ANSI support is enabled. */
static BOOL uv__need_check_vterm_state = TRUE;
static uv_tty_vtermstate_t uv__vterm_state = UV_TTY_UNSUPPORTED;
Expand Down Expand Up @@ -165,6 +169,7 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_os_fd_t handle, int unused) {
BOOL readable;
DWORD NumberOfEvents;
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
CONSOLE_CURSOR_INFO cursor_info;
(void)unused;

if (handle == INVALID_HANDLE_VALUE)
Expand All @@ -186,15 +191,20 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_os_fd_t handle, int unused) {
return uv_translate_sys_error(GetLastError());
}

/* Obtain the cursor info with the output handle. */
if (!GetConsoleCursorInfo(handle, &cursor_info)) {
return uv_translate_sys_error(GetLastError());
}

/* Obtain the tty_output_lock because the virtual window state is shared
* between all uv_tty_t handles. */
uv_sem_wait(&uv_tty_output_lock);

if (uv__need_check_vterm_state)
uv__determine_vterm_state(handle);

/* Remember the original console text attributes. */
uv_tty_capture_initial_style(&screen_buffer_info);
/* Remember the original console text attributes and cursor info. */
uv_tty_capture_initial_style(&screen_buffer_info, &cursor_info);

uv_tty_update_virtual_window(&screen_buffer_info);

Expand Down Expand Up @@ -242,7 +252,9 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_os_fd_t handle, int unused) {
/* Set the default console text attributes based on how the console was
* configured when libuv started.
*/
static void uv_tty_capture_initial_style(CONSOLE_SCREEN_BUFFER_INFO* info) {
static void uv_tty_capture_initial_style(
CONSOLE_SCREEN_BUFFER_INFO* screen_buffer_info,
CONSOLE_CURSOR_INFO* cursor_info) {
static int style_captured = 0;

/* Only do this once.
Expand All @@ -251,7 +263,7 @@ static void uv_tty_capture_initial_style(CONSOLE_SCREEN_BUFFER_INFO* info) {
return;

/* Save raw win32 attributes. */
uv_tty_default_text_attributes = info->wAttributes;
uv_tty_default_text_attributes = screen_buffer_info->wAttributes;

/* Convert black text on black background to use white text. */
if (uv_tty_default_text_attributes == 0)
Expand Down Expand Up @@ -291,6 +303,9 @@ static void uv_tty_capture_initial_style(CONSOLE_SCREEN_BUFFER_INFO* info) {
if (uv_tty_default_text_attributes & COMMON_LVB_REVERSE_VIDEO)
uv_tty_default_inverse = 1;

/* Save the cursor size and the cursor state. */
uv_tty_default_cursor_info = *cursor_info;

style_captured = 1;
}

Expand Down Expand Up @@ -1192,7 +1207,7 @@ static int uv_tty_move_caret(uv_tty_t* handle, int x, unsigned char x_relative,
static int uv_tty_reset(uv_tty_t* handle, DWORD* error) {
const COORD origin = {0, 0};
const WORD char_attrs = uv_tty_default_text_attributes;
CONSOLE_SCREEN_BUFFER_INFO info;
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
DWORD count, written;

if (*error != ERROR_SUCCESS) {
Expand All @@ -1213,12 +1228,12 @@ static int uv_tty_reset(uv_tty_t* handle, DWORD* error) {

/* Clear the screen buffer. */
retry:
if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
*error = GetLastError();
return -1;
if (!GetConsoleScreenBufferInfo(handle->handle, &screen_buffer_info)) {
*error = GetLastError();
return -1;
}

count = info.dwSize.X * info.dwSize.Y;
count = screen_buffer_info.dwSize.X * screen_buffer_info.dwSize.Y;

if (!(FillConsoleOutputCharacterW(handle->handle,
L'\x20',
Expand All @@ -1241,7 +1256,13 @@ static int uv_tty_reset(uv_tty_t* handle, DWORD* error) {

/* Move the virtual window up to the top. */
uv_tty_virtual_offset = 0;
uv_tty_update_virtual_window(&info);
uv_tty_update_virtual_window(&screen_buffer_info);

/* Reset the cursor size and the cursor state. */
if (!SetConsoleCursorInfo(handle->handle, &uv_tty_default_cursor_info)) {
*error = GetLastError();
return -1;
}

return 0;
}
Expand Down

0 comments on commit 456a8cb

Please sign in to comment.