| @@ -53,20 +53,16 @@ static void attempt_native_out_vt(HANDLE hOut, DWORD basemode) · 52 unmodified lines |
| 53 | 53 | SetConsoleMode(hOut, basemode); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | | −static bool is_native_out_vt(HANDLE hOut) |
| 57 | | −{ |
| 58 | | − DWORD cmode; |
| 59 | | − return GetConsoleMode(hOut, &cmode) && |
| 60 | | − (cmode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) && |
| 61 | | − !(cmode & DISABLE_NEWLINE_AUTO_RETURN); |
| 62 | | −} |
| 63 | 56 | |
| 57 | +#define hSTDIN GetStdHandle(STD_INPUT_HANDLE) |
| 64 | 58 | #define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE) |
| 65 | 59 | #define hSTDERR GetStdHandle(STD_ERROR_HANDLE) |
| 66 | 60 | |
| 67 | 61 | #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) |
| 68 | 62 | #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) |
| 69 | 63 | |
| 64 | +static bool is_console[STDERR_FILENO + 1]; |
| 65 | +static bool is_vt[STDERR_FILENO + 1]; |
| 70 | 66 | static short stdoutAttrs = 0; // copied from the screen buffer on init |
| 71 | 67 | static const unsigned char ansi2win32[8] = { |
| 72 | 68 | 0, |
| @@ -94,6 +90,23 @@ static HANDLE death; · 21 unmodified lines |
| 94 | 90 | static mp_thread input_thread; |
| 95 | 91 | static struct input_ctx *input_ctx; |
| 96 | 92 | |
| 93 | +static bool is_native_out_vt_internal(HANDLE hOut) |
| 94 | +{ |
| 95 | + DWORD cmode; |
| 96 | + return GetConsoleMode(hOut, &cmode) && |
| 97 | + (cmode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) && |
| 98 | + !(cmode & DISABLE_NEWLINE_AUTO_RETURN); |
| 99 | +} |
| 100 | + |
| 101 | +static bool is_native_out_vt(HANDLE hOut) |
| 102 | +{ |
| 103 | + if (hOut == hSTDOUT) |
| 104 | + return is_vt[STDOUT_FILENO]; |
| 105 | + if (hOut == hSTDERR) |
| 106 | + return is_vt[STDERR_FILENO]; |
| 107 | + return is_native_out_vt_internal(hOut); |
| 108 | +} |
| 109 | + |
| 97 | 110 | void terminal_get_size(int *w, int *h) |
| 98 | 111 | { |
| 99 | 112 | CONSOLE_SCREEN_BUFFER_INFO cinfo; |
| @@ -360,6 +373,17 @@ static bool is_a_console(HANDLE h) · 260 unmodified lines |
| 360 | 373 | return GetConsoleMode(h, &(DWORD){0}); |
| 361 | 374 | } |
| 362 | 375 | |
| 376 | +bool mp_check_console(void *handle) |
| 377 | +{ |
| 378 | + if (handle == hSTDIN) |
| 379 | + return is_console[STDIN_FILENO]; |
| 380 | + if (handle == hSTDOUT) |
| 381 | + return is_console[STDOUT_FILENO]; |
| 382 | + if (handle == hSTDERR) |
| 383 | + return is_console[STDERR_FILENO]; |
| 384 | + return is_a_console(handle); |
| 385 | +} |
| 386 | + |
| 363 | 387 | static void reopen_console_handle(DWORD std, int fd, FILE *stream) |
| 364 | 388 | { |
| 365 | 389 | HANDLE handle = GetStdHandle(std); |
| @@ -419,6 +443,16 @@ void terminal_init(void) · 53 unmodified lines |
| 419 | 443 | cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 420 | 444 | attempt_native_out_vt(hSTDOUT, cmode); |
| 421 | 445 | attempt_native_out_vt(hSTDERR, cmode); |
| 446 | + |
| 447 | + // Init for mp_check_console(), this never changes during runtime |
| 448 | + is_console[STDIN_FILENO] = is_a_console(hSTDIN); |
| 449 | + is_console[STDOUT_FILENO] = is_a_console(hSTDOUT); |
| 450 | + is_console[STDERR_FILENO] = is_a_console(hSTDERR); |
| 451 | + |
| 452 | + // Init for is_native_out_vt(), this is never disabled/changed during runtime |
| 453 | + is_vt[STDOUT_FILENO] = is_native_out_vt_internal(hSTDOUT); |
| 454 | + is_vt[STDERR_FILENO] = is_native_out_vt_internal(hSTDERR); |
| 455 | + |
| 422 | 456 | GetConsoleScreenBufferInfo(hSTDOUT, &cinfo); |
| 423 | 457 | stdoutAttrs = cinfo.wAttributes; |
| 424 | 458 | } |