2009-03-01 13:13:25 +00:00
|
|
|
/* Windows TermIO
|
|
|
|
*
|
|
|
|
* copyright (C) 2003 Sascha Sommer
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-03-01 13:13:25 +00:00
|
|
|
*
|
2017-06-14 12:28:05 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-03-01 13:13:25 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-03-01 13:13:25 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-14 12:28:05 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2009-03-01 13:13:25 +00:00
|
|
|
*
|
2017-06-14 12:28:05 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-01 13:13:25 +00:00
|
|
|
*/
|
2003-03-30 21:20:04 +00:00
|
|
|
|
2014-01-16 10:25:52 +00:00
|
|
|
#include <fcntl.h>
|
2007-03-04 18:55:44 +00:00
|
|
|
#include <stdio.h>
|
2010-02-28 00:24:01 +00:00
|
|
|
#include <stdint.h>
|
2009-05-31 13:00:51 +00:00
|
|
|
#include <string.h>
|
2003-03-30 21:20:04 +00:00
|
|
|
#include <windows.h>
|
2013-12-18 14:03:08 +00:00
|
|
|
#include <io.h>
|
2014-08-21 20:15:32 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <assert.h>
|
2014-08-21 20:13:10 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 00:23:09 +00:00
|
|
|
#include "input/keycodes.h"
|
|
|
|
#include "input/input.h"
|
2013-12-19 20:31:27 +00:00
|
|
|
#include "terminal.h"
|
2014-08-21 20:11:38 +00:00
|
|
|
#include "osdep/io.h"
|
2014-10-19 21:32:34 +00:00
|
|
|
#include "osdep/threads.h"
|
2014-01-19 13:20:02 +00:00
|
|
|
#include "osdep/w32_keyboard.h"
|
2008-04-29 11:52:45 +00:00
|
|
|
|
2020-04-13 06:49:01 +00:00
|
|
|
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
|
|
|
|
// These values are effective on Windows 10 build 16257 (August 2017) or later
|
|
|
|
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
|
|
|
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
|
|
|
|
#endif
|
|
|
|
#ifndef DISABLE_NEWLINE_AUTO_RETURN
|
|
|
|
#define DISABLE_NEWLINE_AUTO_RETURN 0x0008
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Note: the DISABLE_NEWLINE_AUTO_RETURN docs say it enables delayed-wrap, but
|
|
|
|
// it's wrong. It does only what its names suggests - and we want it unset:
|
|
|
|
// https://github.com/microsoft/terminal/issues/4126#issuecomment-571418661
|
|
|
|
static void attempt_native_out_vt(HANDLE hOut, DWORD basemode)
|
|
|
|
{
|
2020-04-23 14:47:17 +00:00
|
|
|
DWORD vtmode = basemode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
2020-04-13 06:49:01 +00:00
|
|
|
vtmode &= ~DISABLE_NEWLINE_AUTO_RETURN;
|
|
|
|
if (!SetConsoleMode(hOut, vtmode))
|
|
|
|
SetConsoleMode(hOut, basemode);
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:47:17 +00:00
|
|
|
static bool is_native_out_vt(HANDLE hOut)
|
2020-04-13 06:49:01 +00:00
|
|
|
{
|
|
|
|
DWORD cmode;
|
|
|
|
return GetConsoleMode(hOut, &cmode) &&
|
|
|
|
(cmode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&
|
|
|
|
!(cmode & DISABLE_NEWLINE_AUTO_RETURN);
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:03:08 +00:00
|
|
|
#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE)
|
|
|
|
#define hSTDERR GetStdHandle(STD_ERROR_HANDLE)
|
2014-08-24 10:59:35 +00:00
|
|
|
|
|
|
|
#define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
|
2020-04-13 05:11:39 +00:00
|
|
|
#define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
|
2014-08-24 10:59:35 +00:00
|
|
|
|
2020-04-13 05:11:39 +00:00
|
|
|
static short stdoutAttrs = 0; // copied from the screen buffer on init
|
2013-12-18 14:03:08 +00:00
|
|
|
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,
|
|
|
|
};
|
2020-04-13 05:11:39 +00:00
|
|
|
static const unsigned char ansi2win32bg[8] = {
|
|
|
|
0,
|
|
|
|
BACKGROUND_RED,
|
|
|
|
BACKGROUND_GREEN,
|
|
|
|
BACKGROUND_GREEN | BACKGROUND_RED,
|
|
|
|
BACKGROUND_BLUE,
|
|
|
|
BACKGROUND_BLUE | BACKGROUND_RED,
|
|
|
|
BACKGROUND_BLUE | BACKGROUND_GREEN,
|
|
|
|
BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED,
|
|
|
|
};
|
2013-12-18 14:03:08 +00:00
|
|
|
|
2014-08-21 20:15:32 +00:00
|
|
|
static bool running;
|
|
|
|
static HANDLE death;
|
|
|
|
static pthread_t input_thread;
|
|
|
|
static struct input_ctx *input_ctx;
|
|
|
|
|
2014-08-21 20:13:10 +00:00
|
|
|
void terminal_get_size(int *w, int *h)
|
2013-07-14 14:08:27 +00:00
|
|
|
{
|
2011-10-22 14:24:16 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO cinfo;
|
2020-04-23 17:00:29 +00:00
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (GetConsoleScreenBufferInfo(hOut, &cinfo)) {
|
|
|
|
*w = cinfo.dwMaximumWindowSize.X - (is_native_out_vt(hOut) ? 0 : 1);
|
2014-08-21 20:13:10 +00:00
|
|
|
*h = cinfo.dwMaximumWindowSize.Y;
|
2011-10-22 14:24:16 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 10:00:08 +00:00
|
|
|
void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-13 11:40:00 +00:00
|
|
|
static bool has_input_events(HANDLE h)
|
2007-08-25 04:28:08 +00:00
|
|
|
{
|
2016-10-13 11:40:00 +00:00
|
|
|
DWORD num_events;
|
|
|
|
if (!GetNumberOfConsoleInputEvents(h, &num_events))
|
|
|
|
return false;
|
|
|
|
return !!num_events;
|
|
|
|
}
|
2013-07-14 14:08:27 +00:00
|
|
|
|
2016-10-13 11:40:00 +00:00
|
|
|
static void read_input(HANDLE in)
|
|
|
|
{
|
|
|
|
// Process any input events in the buffer
|
|
|
|
while (has_input_events(in)) {
|
|
|
|
INPUT_RECORD event;
|
|
|
|
if (!ReadConsoleInputW(in, &event, 1, &(DWORD){0}))
|
2013-07-14 14:08:27 +00:00
|
|
|
break;
|
2016-10-13 11:40:00 +00:00
|
|
|
|
|
|
|
// Only key-down events are interesting to us
|
|
|
|
if (event.EventType != KEY_EVENT)
|
|
|
|
continue;
|
|
|
|
KEY_EVENT_RECORD *record = &event.Event.KeyEvent;
|
|
|
|
if (!record->bKeyDown)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
UINT vkey = record->wVirtualKeyCode;
|
|
|
|
bool ext = record->dwControlKeyState & ENHANCED_KEY;
|
|
|
|
|
2016-10-13 11:40:12 +00:00
|
|
|
int mods = 0;
|
|
|
|
if (record->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
|
|
|
|
mods |= MP_KEY_MODIFIER_ALT;
|
|
|
|
if (record->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
|
|
|
|
mods |= MP_KEY_MODIFIER_CTRL;
|
|
|
|
if (record->dwControlKeyState & SHIFT_PRESSED)
|
|
|
|
mods |= MP_KEY_MODIFIER_SHIFT;
|
|
|
|
|
2016-10-13 11:40:00 +00:00
|
|
|
int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);
|
|
|
|
if (mpkey) {
|
2016-10-13 11:40:12 +00:00
|
|
|
mp_input_put_key(input_ctx, mpkey | mods);
|
2016-10-13 11:40:00 +00:00
|
|
|
} else {
|
|
|
|
// Only characters should be remaining
|
|
|
|
int c = record->uChar.UnicodeChar;
|
2016-10-13 11:40:12 +00:00
|
|
|
// The ctrl key always produces control characters in the console.
|
|
|
|
// Shift them back up to regular characters.
|
|
|
|
if (c > 0 && c < 0x20 && (mods & MP_KEY_MODIFIER_CTRL))
|
|
|
|
c += (mods & MP_KEY_MODIFIER_SHIFT) ? 0x40 : 0x60;
|
2016-10-13 11:40:00 +00:00
|
|
|
if (c >= 0x20)
|
2016-10-13 11:40:12 +00:00
|
|
|
mp_input_put_key(input_ctx, c | mods);
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:15:32 +00:00
|
|
|
static void *input_thread_fn(void *ptr)
|
2007-08-25 04:28:08 +00:00
|
|
|
{
|
2014-10-19 21:32:34 +00:00
|
|
|
mpthread_set_name("terminal");
|
2016-10-13 11:40:00 +00:00
|
|
|
HANDLE in = ptr;
|
2014-08-21 20:15:32 +00:00
|
|
|
HANDLE stuff[2] = {in, death};
|
|
|
|
while (1) {
|
|
|
|
DWORD r = WaitForMultipleObjects(2, stuff, FALSE, INFINITE);
|
|
|
|
if (r != WAIT_OBJECT_0)
|
|
|
|
break;
|
2016-10-13 11:40:00 +00:00
|
|
|
read_input(in);
|
2014-08-21 20:15:32 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2013-12-21 18:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void terminal_setup_getch(struct input_ctx *ictx)
|
|
|
|
{
|
2016-09-19 17:54:54 +00:00
|
|
|
if (running)
|
|
|
|
return;
|
2014-08-21 20:15:32 +00:00
|
|
|
|
2014-08-21 20:13:10 +00:00
|
|
|
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
|
2014-08-21 20:15:32 +00:00
|
|
|
if (GetNumberOfConsoleInputEvents(in, &(DWORD){0})) {
|
|
|
|
input_ctx = ictx;
|
2016-10-13 11:40:00 +00:00
|
|
|
death = CreateEventW(NULL, TRUE, FALSE, NULL);
|
2014-08-21 20:15:32 +00:00
|
|
|
if (!death)
|
|
|
|
return;
|
2016-10-13 11:40:00 +00:00
|
|
|
if (pthread_create(&input_thread, NULL, input_thread_fn, in)) {
|
2014-08-21 20:15:32 +00:00
|
|
|
CloseHandle(death);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
running = true;
|
|
|
|
}
|
2013-12-21 18:33:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 20:13:10 +00:00
|
|
|
void terminal_uninit(void)
|
2009-01-05 14:48:03 +00:00
|
|
|
{
|
2014-08-21 20:15:32 +00:00
|
|
|
if (running) {
|
|
|
|
SetEvent(death);
|
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
input_ctx = NULL;
|
|
|
|
running = false;
|
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
2013-12-18 14:03:08 +00:00
|
|
|
|
|
|
|
bool terminal_in_background(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-24 10:56:24 +00:00
|
|
|
void mp_write_console_ansi(HANDLE wstream, char *buf)
|
2014-08-21 20:11:38 +00:00
|
|
|
{
|
2020-05-27 12:52:25 +00:00
|
|
|
wchar_t *wbuf = mp_from_utf8(NULL, buf);
|
|
|
|
wchar_t *pos = wbuf;
|
|
|
|
|
|
|
|
while (*pos) {
|
2020-04-13 06:49:01 +00:00
|
|
|
if (is_native_out_vt(wstream)) {
|
2020-05-27 12:52:25 +00:00
|
|
|
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
|
2020-04-13 06:49:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-27 12:52:25 +00:00
|
|
|
wchar_t *next = wcschr(pos, '\033');
|
2014-08-21 20:11:38 +00:00
|
|
|
if (!next) {
|
2020-05-27 12:52:25 +00:00
|
|
|
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
|
2014-08-21 20:11:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-27 12:52:25 +00:00
|
|
|
next[0] = '\0';
|
|
|
|
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
|
|
|
|
if (next[1] == '[') {
|
|
|
|
// CSI - Control Sequence Introducer
|
|
|
|
next += 2;
|
|
|
|
|
|
|
|
// CSI codes generally follow this syntax:
|
|
|
|
// "\033[" [ <i> (';' <i> )* ] <c>
|
|
|
|
// where <i> are integers, and <c> a single char command code.
|
|
|
|
// Also see: http://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes
|
|
|
|
int params[16]; // 'm' might be unlimited; ignore that
|
|
|
|
int num_params = 0;
|
|
|
|
while (num_params < MP_ARRAY_SIZE(params)) {
|
|
|
|
wchar_t *end = next;
|
|
|
|
long p = wcstol(next, &end, 10);
|
|
|
|
if (end == next)
|
|
|
|
break;
|
|
|
|
next = end;
|
|
|
|
params[num_params++] = p;
|
|
|
|
if (next[0] != ';' || !next[0])
|
|
|
|
break;
|
|
|
|
next += 1;
|
|
|
|
}
|
|
|
|
wchar_t code = next[0];
|
|
|
|
if (code)
|
|
|
|
next += 1;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
GetConsoleScreenBufferInfo(wstream, &info);
|
|
|
|
switch (code) {
|
|
|
|
case 'K': { // erase to end of line
|
|
|
|
COORD at = info.dwCursorPosition;
|
|
|
|
int len = info.dwSize.X - at.X;
|
|
|
|
FillConsoleOutputCharacterW(wstream, ' ', len, at, &(DWORD){0});
|
|
|
|
SetConsoleCursorPosition(wstream, at);
|
2014-08-21 20:11:38 +00:00
|
|
|
break;
|
2020-05-27 12:52:25 +00:00
|
|
|
}
|
|
|
|
case 'A': { // cursor up
|
|
|
|
info.dwCursorPosition.Y -= 1;
|
|
|
|
SetConsoleCursorPosition(wstream, info.dwCursorPosition);
|
2014-08-21 20:11:38 +00:00
|
|
|
break;
|
2020-05-27 12:52:25 +00:00
|
|
|
}
|
|
|
|
case 'm': { // "SGR"
|
|
|
|
short attr = info.wAttributes;
|
|
|
|
if (num_params == 0) // reset
|
|
|
|
params[num_params++] = 0;
|
|
|
|
|
|
|
|
// we don't emulate italic, reverse/underline don't always work
|
|
|
|
for (int n = 0; n < num_params; n++) {
|
|
|
|
int p = params[n];
|
|
|
|
if (p == 0) {
|
|
|
|
attr = stdoutAttrs;
|
|
|
|
} else if (p == 1) {
|
|
|
|
attr |= FOREGROUND_INTENSITY;
|
|
|
|
} else if (p == 22) {
|
|
|
|
attr &= ~FOREGROUND_INTENSITY;
|
|
|
|
} else if (p == 4) {
|
|
|
|
attr |= COMMON_LVB_UNDERSCORE;
|
|
|
|
} else if (p == 24) {
|
|
|
|
attr &= ~COMMON_LVB_UNDERSCORE;
|
|
|
|
} else if (p == 7) {
|
|
|
|
attr |= COMMON_LVB_REVERSE_VIDEO;
|
|
|
|
} else if (p == 27) {
|
|
|
|
attr &= ~COMMON_LVB_REVERSE_VIDEO;
|
|
|
|
} else if (p >= 30 && p <= 37) {
|
|
|
|
attr &= ~FOREGROUND_ALL;
|
|
|
|
attr |= ansi2win32[p - 30];
|
|
|
|
} else if (p == 39) {
|
|
|
|
attr &= ~FOREGROUND_ALL;
|
|
|
|
attr |= stdoutAttrs & FOREGROUND_ALL;
|
|
|
|
} else if (p >= 40 && p <= 47) {
|
|
|
|
attr &= ~BACKGROUND_ALL;
|
|
|
|
attr |= ansi2win32bg[p - 40];
|
|
|
|
} else if (p == 49) {
|
|
|
|
attr &= ~BACKGROUND_ALL;
|
|
|
|
attr |= stdoutAttrs & BACKGROUND_ALL;
|
|
|
|
} else if (p == 38 || p == 48) { // ignore and skip sub-values
|
|
|
|
// 256 colors: <38/48>;5;N true colors: <38/48>;2;R;G;B
|
|
|
|
if (n+1 < num_params) {
|
|
|
|
n += params[n+1] == 5 ? 2
|
|
|
|
: params[n+1] == 2 ? 4
|
|
|
|
: num_params; /* unrecognized -> the rest */
|
|
|
|
}
|
2020-04-24 06:41:56 +00:00
|
|
|
}
|
2014-08-21 20:11:38 +00:00
|
|
|
}
|
2020-05-27 12:52:25 +00:00
|
|
|
|
|
|
|
if (attr != info.wAttributes)
|
|
|
|
SetConsoleTextAttribute(wstream, attr);
|
|
|
|
break;
|
|
|
|
}
|
2014-08-21 20:11:38 +00:00
|
|
|
}
|
2020-05-27 12:52:25 +00:00
|
|
|
} else if (next[1] == ']') {
|
|
|
|
// OSC - Operating System Commands
|
|
|
|
next += 2;
|
2020-04-13 05:11:39 +00:00
|
|
|
|
2020-05-27 12:52:25 +00:00
|
|
|
// OSC sequences generally follow this syntax:
|
|
|
|
// "\033]" <command> ST
|
|
|
|
// Where <command> is a string command
|
|
|
|
wchar_t *cmd = next;
|
|
|
|
while (next[0]) {
|
|
|
|
// BEL can be used instead of ST in xterm
|
|
|
|
if (next[0] == '\007' || next[0] == 0x9c) {
|
|
|
|
next[0] = '\0';
|
|
|
|
next += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next[0] == '\033' && next[1] == '\\') {
|
|
|
|
next[0] = '\0';
|
|
|
|
next += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle xterm-style OSC commands
|
|
|
|
if (cmd[0] && cmd[1] == ';') {
|
|
|
|
wchar_t code = cmd[0];
|
|
|
|
wchar_t *param = cmd + 2;
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case '0': // Change Icon Name and Window Title
|
|
|
|
case '2': // Change Window Title
|
|
|
|
SetConsoleTitleW(param);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WriteConsoleW(wstream, L"\033", 1, NULL, NULL);
|
2014-08-21 20:11:38 +00:00
|
|
|
}
|
2020-05-27 12:52:25 +00:00
|
|
|
pos = next;
|
2013-12-18 14:03:08 +00:00
|
|
|
}
|
2020-05-27 12:52:25 +00:00
|
|
|
|
|
|
|
talloc_free(wbuf);
|
2013-12-18 14:03:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 14:19:29 +00:00
|
|
|
static bool is_a_console(HANDLE h)
|
|
|
|
{
|
|
|
|
return GetConsoleMode(h, &(DWORD){0});
|
|
|
|
}
|
|
|
|
|
2015-12-05 14:33:04 +00:00
|
|
|
static void reopen_console_handle(DWORD std, int fd, FILE *stream)
|
2015-03-29 14:19:29 +00:00
|
|
|
{
|
2020-07-19 13:50:25 +00:00
|
|
|
HANDLE handle = GetStdHandle(std);
|
|
|
|
if (is_a_console(handle)) {
|
|
|
|
if (fd == 0) {
|
|
|
|
freopen("CONIN$", "rt", stream);
|
|
|
|
} else {
|
|
|
|
freopen("CONOUT$", "wt", stream);
|
|
|
|
}
|
2015-03-29 14:19:29 +00:00
|
|
|
setvbuf(stream, NULL, _IONBF, 0);
|
2020-07-19 13:50:25 +00:00
|
|
|
|
|
|
|
// Set the low-level FD to the new handle value, since mp_subprocess2
|
|
|
|
// callers might rely on low-level FDs being set. Note, with this
|
|
|
|
// method, fileno(stdin) != STDIN_FILENO, but that shouldn't matter.
|
|
|
|
int unbound_fd = -1;
|
|
|
|
if (fd == 0) {
|
|
|
|
unbound_fd = _open_osfhandle((intptr_t)handle, _O_RDONLY);
|
|
|
|
} else {
|
|
|
|
unbound_fd = _open_osfhandle((intptr_t)handle, _O_WRONLY);
|
|
|
|
}
|
|
|
|
// dup2 will duplicate the underlying handle. Don't close unbound_fd,
|
|
|
|
// since that will close the original handle.
|
|
|
|
dup2(unbound_fd, fd);
|
2015-03-29 14:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool terminal_try_attach(void)
|
2013-12-18 14:03:08 +00:00
|
|
|
{
|
2015-03-29 14:19:29 +00:00
|
|
|
// mpv.exe is a flagged as a GUI application, but it acts as a console
|
|
|
|
// application when started from the console wrapper (see
|
|
|
|
// osdep/win32-console-wrapper.c). The console wrapper sets
|
|
|
|
// _started_from_console=yes, so check that variable before trying to
|
|
|
|
// attach to the console.
|
|
|
|
wchar_t console_env[4] = { 0 };
|
|
|
|
if (!GetEnvironmentVariableW(L"_started_from_console", console_env, 4))
|
|
|
|
return false;
|
|
|
|
if (wcsncmp(console_env, L"yes", 4))
|
|
|
|
return false;
|
|
|
|
SetEnvironmentVariableW(L"_started_from_console", NULL);
|
2014-01-16 10:25:52 +00:00
|
|
|
|
2015-03-29 14:19:29 +00:00
|
|
|
if (!AttachConsole(ATTACH_PARENT_PROCESS))
|
|
|
|
return false;
|
2014-01-16 10:25:52 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
// We have a console window. Redirect input/output streams to that console's
|
|
|
|
// low-level handles, so things that use stdio work later on.
|
|
|
|
reopen_console_handle(STD_INPUT_HANDLE, STDIN_FILENO, stdin);
|
2015-12-05 14:33:04 +00:00
|
|
|
reopen_console_handle(STD_OUTPUT_HANDLE, STDOUT_FILENO, stdout);
|
|
|
|
reopen_console_handle(STD_ERROR_HANDLE, STDERR_FILENO, stderr);
|
2014-01-16 10:25:52 +00:00
|
|
|
|
2015-03-29 14:19:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-01-16 10:25:52 +00:00
|
|
|
|
2017-06-27 15:38:49 +00:00
|
|
|
void terminal_init(void)
|
2015-03-29 14:19:29 +00:00
|
|
|
{
|
2013-12-18 14:03:08 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO cinfo;
|
|
|
|
DWORD cmode = 0;
|
|
|
|
GetConsoleMode(hSTDOUT, &cmode);
|
|
|
|
cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
|
2020-04-13 06:49:01 +00:00
|
|
|
attempt_native_out_vt(hSTDOUT, cmode);
|
|
|
|
attempt_native_out_vt(hSTDERR, cmode);
|
2013-12-18 14:03:08 +00:00
|
|
|
GetConsoleScreenBufferInfo(hSTDOUT, &cinfo);
|
|
|
|
stdoutAttrs = cinfo.wAttributes;
|
|
|
|
}
|