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-unix.c | 22 +++++++++++++++++++++- osdep/terminal-win.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- osdep/terminal.h | 14 +++++++++++--- 3 files changed, 75 insertions(+), 9 deletions(-) (limited to 'osdep') diff --git a/osdep/terminal-unix.c b/osdep/terminal-unix.c index 050b97b9d1..f61cb5e367 100644 --- a/osdep/terminal-unix.c +++ b/osdep/terminal-unix.c @@ -227,7 +227,7 @@ static void termcap_add_extra_f_keys(void) { #endif -int load_termcap(char *termtype){ +static int load_termcap(char *termtype){ #if HAVE_TERMINFO || HAVE_TERMCAP #if HAVE_TERMINFO @@ -578,3 +578,23 @@ void getch2_disable(void){ getch2_enabled = 0; } + +bool terminal_in_background(void) +{ + return isatty(2) && tcgetpgrp(2) != getpgrp(); +} + +void terminal_set_foreground_color(FILE *stream, int c) +{ + if (c == -1) { + fprintf(stream, "\033[0m"); + } else { + fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7); + } +} + +int terminal_init(void) +{ + load_termcap(NULL); + return 0; +} 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; +} diff --git a/osdep/terminal.h b/osdep/terminal.h index 76cedd9928..fe4d5e32d5 100644 --- a/osdep/terminal.h +++ b/osdep/terminal.h @@ -25,6 +25,7 @@ #define MPLAYER_GETCH2_H #include +#include /* Screen size. Initialized by load_termcap() and get_screen_size() */ extern int screen_width; @@ -33,12 +34,19 @@ extern int screen_height; /* Termcap code to erase to end of line */ extern char * erase_to_end_of_line; +/* Global initialization for terminal output. */ +int terminal_init(void); + +/* Return whether the process has been backgrounded. */ +bool terminal_in_background(void); + +/* Set ANSI text foreground color. c is [-1, 7], where 0-7 are colors, and + * -1 means reset to default. stream is either stdout or stderr. */ +void terminal_set_foreground_color(FILE *stream, int c); + /* Get screen-size using IOCTL call. */ void get_screen_size(void); -/* Load key definitions from the TERMCAP database. 'termtype' can be NULL */ -int load_termcap(char *termtype); - /* Initialize getch2 */ void getch2_enable(void); void getch2_disable(void); -- cgit v1.2.3