[MPlayer-dev-eng] [PATCH] color console for win32

Gianluigi Tiesi mplayer at netfarm.it
Mon Dec 17 01:36:51 CET 2007


On Sun, Dec 16, 2007 at 03:44:15PM +0100, Reimar D?ffinger wrote:
> Hello,
> On Sun, Dec 16, 2007 at 10:31:39AM +0100, Gianluigi Tiesi wrote:
> > @@ -191,17 +243,52 @@
> >                                  "RADIO",
> >                                  "ASS",
> >                                  "LOADER",
> > -                                "STATUSLINE",
> > +                                "STATUS",
> 
> Unrelated?
> I did not review it otherwise though...
> 

99_color_console_win32.diff - win32 color console port

xx_statusline.diff - STATUSLINE to STATUS because the fprintf
uses %9s

xx_trailspace.diff - remove trailing spaces in the file

Regards

-- 
Gianluigi Tiesi <sherpya at netfarm.it>
EDP Project Leader
Netfarm S.r.l. - http://www.netfarm.it/
Free Software: http://oss.netfarm.it/
-------------- next part --------------
Index: mp_msg.c
===================================================================
--- mp_msg.c	(revision 25427)
+++ mp_msg.c	(working copy)
@@ -24,6 +24,13 @@
 #endif
 #include "mp_msg.h"
 
+#if defined(MSG_USE_COLORS) && defined(_WIN32)
+#include <windows.h>
+#include <io.h>
+short stdoutAttrs = 0, stderrAttrs;
+HANDLE hConOut = INVALID_HANDLE_VALUE, hConErr = INVALID_HANDLE_VALUE;
+#endif
+
 /* maximum message length of mp_msg */
 #define MSGSIZE_MAX 3072
 
@@ -79,6 +86,36 @@
     if (!mp_msg_charset)
       mp_msg_charset = get_term_charset();
 #endif
+#if defined(MSG_USE_COLORS) && defined(_WIN32)
+    {
+        CONSOLE_SCREEN_BUFFER_INFO cinfo;
+        long cmode = 0;
+
+        hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
+        hConErr = GetStdHandle(STD_ERROR_HANDLE);
+        if ((hConOut == NULL) || (hConOut == INVALID_HANDLE_VALUE)
+            || (hConErr == NULL) || (hConErr == INVALID_HANDLE_VALUE))
+        {
+            hConOut = NULL;
+            hConErr = NULL;
+            fprintf(stderr, "Cannot get handles of stdout/stderr\n");
+            return;
+        }
+
+        GetConsoleMode(hConOut, &cmode);
+        cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
+        SetConsoleMode(hConOut, cmode);
+
+        GetConsoleMode(hConErr, &cmode);
+        cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
+        SetConsoleMode(hConErr, cmode);
+
+        GetConsoleScreenBufferInfo(hConOut, &cinfo);
+        stdoutAttrs = cinfo.wAttributes;
+        GetConsoleScreenBufferInfo(hConErr, &cinfo);
+        stderrAttrs = cinfo.wAttributes;
+    }
+#endif
 }
 
 int mp_msg_test(int mod, int lev)
@@ -145,6 +182,21 @@
     }
 #endif    
     {   unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
+#ifdef _WIN32
+        unsigned char ansi2win32[10]=
+        {
+            0,
+            FOREGROUND_RED,
+            FOREGROUND_GREEN,
+            FOREGROUND_GREEN | FOREGROUND_RED,
+            FOREGROUND_BLUE,
+            FOREGROUND_BLUE  | FOREGROUND_RED,
+            FOREGROUND_BLUE  | FOREGROUND_GREEN,
+            FOREGROUND_BLUE  | FOREGROUND_GREEN | FOREGROUND_RED,
+            FOREGROUND_BLUE  | FOREGROUND_GREEN | FOREGROUND_RED,
+            FOREGROUND_BLUE  | FOREGROUND_GREEN | FOREGROUND_RED
+        };
+#endif
         static const char *mod_text[MSGT_MAX]= {
                                 "GLOBAL",
                                 "CPLAYER",
@@ -198,10 +250,45 @@
         int c2=(mod+1)%15+1;
         static int header=1;
         FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
+#ifdef _WIN32
+        if(isatty(fileno(stream)) && hConOut && hConErr)
+        {
+            HANDLE hCon;
+            short stdAttrs, color = 0;
+            if (lev <= MSGL_WARN)
+            {
+                hCon = hConErr;
+                stdAttrs = stderrAttrs;
+            }
+            else
+            {
+                hCon = hConOut;
+                stdAttrs = stdoutAttrs;
+            }
+            if(header)
+            {
+                color = ansi2win32[c2&7];
+                /* if (c2>>3) */
+                if (c2!=7) color |= FOREGROUND_INTENSITY;
+                SetConsoleTextAttribute(hCon, color);
+                fprintf(stream, "%9s", mod_text[mod]);
+                SetConsoleTextAttribute(hCon, stdAttrs);
+                fprintf(stream, ": ");
+            }
+            if (c2&7)
+            {
+                color = ansi2win32[c&7];
+                /* if (c>>3) */
+                if (c!=7) color |= FOREGROUND_INTENSITY;
+                SetConsoleTextAttribute(hCon, color);
+            }
+        }
+#else /* _WIN32 */
         if(header){
             fprintf(stream, "\033[%d;3%dm%9s\033[0;37m: ",c2>>3,c2&7, mod_text[mod]);
         }
         fprintf(stream, "\033[%d;3%dm",c>>3,c&7);
+#endif /* _WIN32 */
         header=    tmp[strlen(tmp)-1] == '\n'
                  ||tmp[strlen(tmp)-1] == '\r';
     }
-------------- next part --------------
Index: mp_msg.c
===================================================================
--- mp_msg.c	(revision 25427)
+++ mp_msg.c	(working copy)
@@ -191,7 +191,7 @@
                                 "RADIO",
                                 "ASS",
                                 "LOADER",
-                                "STATUSLINE",
+                                "STATUS",
         };
 
         int c=v_colors[lev];
-------------- next part --------------
Index: mp_msg.c
===================================================================
--- mp_msg.c	(revision 25427)
+++ mp_msg.c	(working copy)
@@ -89,7 +89,7 @@
 void mp_msg(int mod, int lev, const char *format, ... ){
     va_list va;
     char tmp[MSGSIZE_MAX];
-    
+
     if (!mp_msg_test(mod, lev)) return; // do not display
     va_start(va, format);
     vsnprintf(tmp, MSGSIZE_MAX, format, va);
@@ -143,7 +143,7 @@
           printf("\033[%d;3%dm***  COLOR TEST %d  ***\n",(c>7),c&7,c);
       flag=0;
     }
-#endif    
+#endif
     {   unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
         static const char *mod_text[MSGT_MAX]= {
                                 "GLOBAL",


More information about the MPlayer-dev-eng mailing list