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

Gianluigi Tiesi mplayer at netfarm.it
Sun Dec 16 10:31:39 CET 2007


Hi all,

I've made a patch to make color console work on win32
win32 console does not supports ansi escape strings
(ansi.sys needs to be loaded by it's 16bit stuff)

on win32 it works in a slightly different way
all colors are brightened because non bright colors
suck a bit on the win32 console

to achieve the same behaviour of unix
I've added in comment two different guards
for if

STATUSLINE text was changed to STATUS since it's 10 chars
while the format of the string uses %9s

The code is tested on xp sp2 and win98

I've added a check using istty() if the stream is not
attached to a terminal it skips the color code
(plus adding the "SECTION")
this may be usefull also on unix

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 --------------
diff -NuBr -x.svn -xvidix -xhelp_mp.h -xlibdha -x'*.so' -x'*.log' -x'*.a' -x'*.exe' -x'*.o' -xconfigure.log -xconfig.mak -x.cvsignore -xconfig.h -xcodecs.conf.h -xversion.h -x.depend main/mp_msg.c sherpya/mp_msg.c
--- main/mp_msg.c	2007-12-16 07:29:34.546875000 +0100
+++ sherpya/mp_msg.c	2007-12-16 10:19:00.343750000 +0100
@@ -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",
@@ -191,17 +243,52 @@
                                 "RADIO",
                                 "ASS",
                                 "LOADER",
-                                "STATUSLINE",
+                                "STATUS",
         };
 
         int c=v_colors[lev];
         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';
     }


More information about the MPlayer-dev-eng mailing list