From f891939b4db79bce39c5f257771557904da4d535 Mon Sep 17 00:00:00 2001 From: Martin Herkt Date: Sat, 22 Oct 2011 16:24:16 +0200 Subject: windows: terminal: unicode, --msgcolor, size change Make mp_msg() support unicode output, --msgcolor and variable screen sizes. Patch reintegrated by wm4. --- osdep/getch2-win.c | 6 ++++++ osdep/io.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ osdep/io.h | 2 ++ 3 files changed, 66 insertions(+) (limited to 'osdep') diff --git a/osdep/getch2-win.c b/osdep/getch2-win.c index 3e6eeff086..326cf1a7d0 100644 --- a/osdep/getch2-win.c +++ b/osdep/getch2-win.c @@ -55,6 +55,12 @@ int screen_height=24; char * erase_to_end_of_line = NULL; void get_screen_size(void){ + CONSOLE_SCREEN_BUFFER_INFO cinfo; + if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cinfo)) + { + screen_width = cinfo.dwMaximumWindowSize.X; + screen_height = cinfo.dwMaximumWindowSize.Y; + } } static HANDLE in; diff --git a/osdep/io.c b/osdep/io.c index 88433184f4..e3e750e30b 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -92,6 +92,64 @@ int mp_stat(const char *path, struct stat *buf) return res; } +int mp_fprintf(FILE *stream, const char *format, ...) +{ + va_list args; + int done = 0; + + va_start(args, format); + + if (stream == stdout || stream == stderr) + { + HANDLE *wstream = GetStdHandle(stream == stdout ? + STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); + if (wstream != INVALID_HANDLE_VALUE) + { + // figure out whether we're writing to a console + unsigned int filetype = GetFileType(wstream); + if (!((filetype == FILE_TYPE_UNKNOWN) && + (GetLastError() != ERROR_SUCCESS))) + { + int isConsole; + filetype &= ~(FILE_TYPE_REMOTE); + if (filetype == FILE_TYPE_CHAR) + { + DWORD ConsoleMode; + int ret = GetConsoleMode(wstream, &ConsoleMode); + if (!ret && (GetLastError() == ERROR_INVALID_HANDLE)) + isConsole = 0; + else + isConsole = 1; + } + else + isConsole = 0; + + if (isConsole) + { + int nchars = vsnprintf(NULL, 0, format, args) + 1; + char *buf = talloc_array(NULL, char, nchars); + if (buf) + { + vsnprintf(buf, nchars, format, args); + wchar_t *out = mp_from_utf8(NULL, buf); + size_t nchars = wcslen(out); + talloc_free(buf); + done = WriteConsoleW(wstream, out, nchars, NULL, NULL); + talloc_free(out); + } + } + else + done = vfprintf(stream, format, args); + } + } + } + else + done = vfprintf(stream, format, args); + + va_end(args); + + return done; +} int mp_open(const char *filename, int oflag, ...) { diff --git a/osdep/io.h b/osdep/io.h index 514030afca..462a84917b 100644 --- a/osdep/io.h +++ b/osdep/io.h @@ -46,6 +46,7 @@ char *mp_to_utf8(void *talloc_ctx, const wchar_t *s); void mp_get_converted_argv(int *argc, char ***argv); int mp_stat(const char *path, struct stat *buf); +int mp_fprintf(FILE *stream, const char *format, ...); int mp_open(const char *filename, int oflag, ...); int mp_creat(const char *filename, int mode); FILE *mp_fopen(const char *filename, const char *mode); @@ -57,6 +58,7 @@ int mp_mkdir(const char *path, int mode); // NOTE: Stat is not overridden with mp_stat, because MinGW-w64 defines it as // macro. +#define fprintf(...) mp_fprintf(__VA_ARGS__) #define open(...) mp_open(__VA_ARGS__) #define creat(...) mp_creat(__VA_ARGS__) #define fopen(...) mp_fopen(__VA_ARGS__) -- cgit v1.2.3