]> sourceware.org Git - newlib-cygwin.git/commitdiff
Cygwin: console: Add code to restore console mode on close.
authorTakashi Yano <takashi.yano@nifty.ne.jp>
Thu, 2 Jan 2020 13:17:16 +0000 (22:17 +0900)
committerCorinna Vinschen <corinna@vinschen.de>
Tue, 14 Jan 2020 16:18:25 +0000 (17:18 +0100)
- The console with 24bit color support has a problem that console
  mode is changed if cygwin process is executed in cmd.exe which
  started in cygwin shell. For example, cursor keys become not
  working if bash -> cmd -> true are executed in this order.
  This patch fixes the issue.

winsup/cygwin/fhandler_console.cc

index 337331be2a5ce02155e490713b3cc8cd1ac69d72..b3f2cb65af6094d9add8973b26e331ac284b167c 100644 (file)
@@ -53,6 +53,9 @@ fhandler_console::console_state NO_COPY *fhandler_console::shared_console_info;
 
 bool NO_COPY fhandler_console::invisible_console;
 
+static DWORD orig_conin_mode = (DWORD) -1;
+static DWORD orig_conout_mode = (DWORD) -1;
+
 static void
 beep ()
 {
@@ -1010,6 +1013,11 @@ fhandler_console::open (int flags, mode_t)
   get_ttyp ()->rstcons (false);
   set_open_status ();
 
+  if (orig_conin_mode == (DWORD) -1)
+    GetConsoleMode (get_handle (), &orig_conin_mode);
+  if (orig_conout_mode == (DWORD) -1)
+    GetConsoleMode (get_output_handle (), &orig_conout_mode);
+
   if (getpid () == con.owner && wincap.has_con_24bit_colors ())
     {
       DWORD dwMode;
@@ -1079,6 +1087,19 @@ fhandler_console::close ()
       SetConsoleMode (get_output_handle (), dwMode);
     }
 
+  /* Restore console mode if this is the last closure. */
+  OBJECT_BASIC_INFORMATION obi;
+  NTSTATUS status;
+  status = NtQueryObject (get_handle (), ObjectBasicInformation,
+                         &obi, sizeof obi, NULL);
+  if (NT_SUCCESS (status) && obi.HandleCount == 1)
+    {
+      if (orig_conin_mode != (DWORD) -1)
+       SetConsoleMode (get_handle (), orig_conin_mode);
+      if (orig_conout_mode != (DWORD) -1)
+       SetConsoleMode (get_handle (), orig_conout_mode);
+    }
+
   CloseHandle (get_handle ());
   CloseHandle (get_output_handle ());
 
This page took 0.035955 seconds and 5 git commands to generate.