Fix colors in TUI mode in MS-Windows build with ncurses
[deliverable/binutils-gdb.git] / gdb / tui / tui-io.c
index 5a84d08a28394f8248bcc810e6753d33aad4e906..ef1e88507aa58d6dd6322595b001463a067da41a 100644 (file)
@@ -1,6 +1,6 @@
 /* TUI support I/O functions.
 
-   Copyright (C) 1998-2018 Free Software Foundation, Inc.
+   Copyright (C) 1998-2019 Free Software Foundation, Inc.
 
    Contributed by Hewlett-Packard Company.
 
 #include "cli-out.h"
 #include <fcntl.h>
 #include <signal.h>
-#include "filestuff.h"
+#ifdef __MINGW32__
+#include <windows.h>
+#endif
+#include "common/filestuff.h"
 #include "completer.h"
 #include "gdb_curses.h"
 #include <map>
    "gdb_curses.h".  */
 #include "readline/readline.h"
 
+#ifdef __MINGW32__
+static SHORT ncurses_norm_attr;
+#endif
+
 static int tui_getc (FILE *fp);
 
 static int
@@ -322,6 +329,16 @@ apply_ansi_escape (WINDOW *w, const char *buf)
       int fgi, bgi;
       if (get_color (fg, &fgi) && get_color (bg, &bgi))
        {
+#ifdef __MINGW32__
+         /* MS-Windows port of ncurses doesn't support implicit
+            default foreground and background colors, so we must
+            specify them explicitly when needed, using the colors we
+            saw at startup.  */
+         if (fgi == -1)
+           fgi = ncurses_norm_attr & 15;
+         if (bgi == -1)
+           bgi = (ncurses_norm_attr >> 4) & 15;
+#endif
          int pair = get_color_pair (fgi, bgi);
          if (last_color_pair != -1)
            wattroff (w, COLOR_PAIR (last_color_pair));
@@ -367,9 +384,8 @@ tui_write (const char *buf, size_t length)
 }
 
 static void
-tui_puts_internal (const char *string, int *height)
+tui_puts_internal (WINDOW *w, const char *string, int *height)
 {
-  WINDOW *w = TUI_CMD_WIN->generic.handle;
   char c;
   int prev_col = 0;
 
@@ -410,9 +426,11 @@ tui_puts_internal (const char *string, int *height)
    necessary.  */
 
 void
-tui_puts (const char *string)
+tui_puts (const char *string, WINDOW *w)
 {
-  tui_puts_internal (string, nullptr);
+  if (w == nullptr)
+    w = TUI_CMD_WIN->generic.handle;
+  tui_puts_internal (w, string, nullptr);
 }
 
 /* Readline callback.
@@ -453,7 +471,7 @@ tui_redisplay_readline (void)
   prev_col = 0;
   height = 1;
   if (prompt != nullptr)
-    tui_puts_internal (prompt, &height);
+    tui_puts_internal (TUI_CMD_WIN->generic.handle, prompt, &height);
 
   prev_col = getcurx (w);
   for (in = 0; in <= rl_end; in++)
@@ -608,6 +626,12 @@ gdb_wgetch (WINDOW *win)
   nonl ();
   int r = wgetch (win);
   nl ();
+  /* In nonl mode, if the user types Enter, it will not be echoed
+     properly.  This will result in gdb output appearing immediately
+     after the command.  So, if we read \r, emit a \r now, after nl
+     mode has been re-entered, so that the output looks correct.  */
+  if (r == '\r')
+    puts ("\r");
   return r;
 }
 
@@ -800,6 +824,19 @@ tui_initialize_io (void)
 #else
   tui_rl_outstream = stdout;
 #endif
+
+#ifdef __MINGW32__
+  /* MS-Windows port of ncurses doesn't support default foreground and
+     background colors, so we must record the default colors at startup.  */
+  HANDLE hstdout = (HANDLE)_get_osfhandle (fileno (stdout));
+  DWORD cmode;
+  CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+  if (hstdout != INVALID_HANDLE_VALUE
+      && GetConsoleMode (hstdout, &cmode) != 0
+      && GetConsoleScreenBufferInfo (hstdout, &csbi))
+    ncurses_norm_attr = csbi.wAttributes;
+#endif
 }
 
 /* Get a character from the command window.  This is called from the
This page took 0.026008 seconds and 4 git commands to generate.