From 4d4b82217126de3160299b3aefba1f6941623d30 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 18 Dec 2013 15:03:08 +0100 Subject: terminal: abstract terminal color handling Instead of making msg.c an ifdef hell for unix vs. windows code, move the code to separate functions defined in terminal-unix.c/terminal- win.c. Drop the code that selects random colors for --msgmodule prefixes. --- osdep/terminal-win.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'osdep/terminal-win.c') diff --git a/osdep/terminal-win.c b/osdep/terminal-win.c index a56d1a409c..cee2ab395a 100644 --- a/osdep/terminal-win.c +++ b/osdep/terminal-win.c @@ -28,10 +28,25 @@ #include #include #include +#include #include "input/keycodes.h" #include "input/input.h" #include "terminal.h" +#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE) +#define hSTDERR GetStdHandle(STD_ERROR_HANDLE) +static short stdoutAttrs = 0; +static const unsigned char ansi2win32[8] = { + 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, +}; + int mp_input_slave_cmd_func(int fd, char *dest, int size) { DWORD retval; @@ -64,11 +79,6 @@ void get_screen_size(void) } } -int load_termcap(char *termtype) -{ - return 0; -} - static HANDLE in; static int getch2_status = 0; @@ -192,3 +202,31 @@ void getch2_disable(void) return; // already disabled / never enabled getch2_status = 0; } + +bool terminal_in_background(void) +{ + return false; +} + +void terminal_set_foreground_color(FILE *stream, int c) +{ + HANDLE *wstream = stream == stderr ? hSTDERR : hSTDOUT; + if (c < 0 || c >= 8) { // reset or invalid + SetConsoleTextAttribute(wstream, stdoutAttrs); + } else { + SetConsoleTextAttribute(wstream, ansi2win32[c] | FOREGROUND_INTENSITY); + } +} + +int terminal_init(void) +{ + CONSOLE_SCREEN_BUFFER_INFO cinfo; + DWORD cmode = 0; + GetConsoleMode(hSTDOUT, &cmode); + cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); + SetConsoleMode(hSTDOUT, cmode); + SetConsoleMode(hSTDERR, cmode); + GetConsoleScreenBufferInfo(hSTDOUT, &cinfo); + stdoutAttrs = cinfo.wAttributes; + return 0; +} -- cgit v1.2.3