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/io.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'osdep/io.c') 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, ...) { -- cgit v1.2.3