From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 47E40388B012; Mon, 1 Jun 2020 08:46:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 47E40388B012 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pty: Fix screen distortion after using less for native apps. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano via Cygwin-patches X-Git-Refname: refs/heads/master X-Git-Oldrev: d212bdc40096fde87b086290ac34b1cc6517b19f X-Git-Newrev: c4b060e3fe3bed05b3a69ccbcc20993ad85e163d Message-Id: <20200601084604.47E40388B012@sourceware.org> Date: Mon, 1 Jun 2020 08:46:04 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Jun 2020 08:46:04 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c4b060e3fe3bed05b3a69ccbcc20993ad85e163d commit c4b060e3fe3bed05b3a69ccbcc20993ad85e163d Author: Takashi Yano via Cygwin-patches Date: Mon Jun 1 15:16:18 2020 +0900 Cygwin: pty: Fix screen distortion after using less for native apps. - If the output of non-cygwin apps is browsed using less, screen is ocasionally distorted after less exits. This frequently happens if cmd.exe is executed after less. This patch fixes the issue. Diff: --- winsup/cygwin/fhandler_tty.cc | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index e434b7878..bcc7648f3 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -1372,7 +1372,7 @@ fhandler_pty_slave::push_to_pcon_screenbuffer (const char *ptr, size_t len, p0 = (char *) memmem (p1, nlen - (p1-buf), "\033[?1049h", 8); if (p0) { - //p0 += 8; + p0 += 8; get_ttyp ()->screen_alternated = true; if (get_ttyp ()->switch_to_pcon_out) do_not_reset_switch_to_pcon = true; @@ -1384,7 +1384,7 @@ fhandler_pty_slave::push_to_pcon_screenbuffer (const char *ptr, size_t len, p1 = (char *) memmem (p0, nlen - (p0-buf), "\033[?1049l", 8); if (p1) { - p1 += 8; + //p1 += 8; get_ttyp ()->screen_alternated = false; do_not_reset_switch_to_pcon = false; memmove (p0, p1, buf+nlen - p1); @@ -1504,7 +1504,10 @@ fhandler_pty_slave::write (const void *ptr, size_t len) reset_switch_to_pcon (); - UINT target_code_page = get_ttyp ()->switch_to_pcon_out ? + bool output_to_pcon = + get_ttyp ()->switch_to_pcon_out && !get_ttyp ()->screen_alternated; + + UINT target_code_page = output_to_pcon ? GetConsoleOutputCP () : get_ttyp ()->term_code_page; ssize_t nlen; char *buf = convert_mb_str (target_code_page, (size_t *) &nlen, @@ -1513,11 +1516,11 @@ fhandler_pty_slave::write (const void *ptr, size_t len) /* If not attached to this pseudo console, try to attach temporarily. */ pid_restore = 0; bool fallback = false; - if (get_ttyp ()->switch_to_pcon_out && pcon_attached_to != get_minor ()) + if (output_to_pcon && pcon_attached_to != get_minor ()) if (!try_reattach_pcon ()) fallback = true; - if (get_ttyp ()->switch_to_pcon_out && !fallback && + if (output_to_pcon && !fallback && (memmem (buf, nlen, "\033[6n", 4) || memmem (buf, nlen, "\033[0c", 4))) { get_ttyp ()->pcon_in_empty = false; @@ -1530,12 +1533,12 @@ fhandler_pty_slave::write (const void *ptr, size_t len) if (!(get_ttyp ()->ti.c_oflag & OPOST) || !(get_ttyp ()->ti.c_oflag & ONLCR)) flags |= DISABLE_NEWLINE_AUTO_RETURN; - if (get_ttyp ()->switch_to_pcon_out && !fallback) + if (output_to_pcon && !fallback) { GetConsoleMode (get_output_handle (), &dwMode); SetConsoleMode (get_output_handle (), dwMode | flags); } - HANDLE to = (get_ttyp ()->switch_to_pcon_out && !fallback) ? + HANDLE to = (output_to_pcon && !fallback) ? get_output_handle () : get_output_handle_cyg (); acquire_output_mutex (INFINITE); if (!process_opost_output (to, buf, nlen, false)) @@ -1555,7 +1558,7 @@ fhandler_pty_slave::write (const void *ptr, size_t len) release_output_mutex (); mb_str_free (buf); flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING; - if (get_ttyp ()->switch_to_pcon_out && !fallback) + if (output_to_pcon && !fallback) SetConsoleMode (get_output_handle (), dwMode | flags); restore_reattach_pcon ();