1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
/* Windows TermIO
*
* copyright (C) 2003 Sascha Sommer
*
* This file is part of mpv.
*
* 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.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <windows.h>
#include <io.h>
#include <assert.h>
#include "common/common.h"
#include "input/keycodes.h"
#include "input/input.h"
#include "terminal.h"
#include "osdep/io.h"
#include "osdep/threads.h"
#include "osdep/w32_keyboard.h"
// 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)
{
DWORD vtmode = basemode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
vtmode &= ~DISABLE_NEWLINE_AUTO_RETURN;
if (!SetConsoleMode(hOut, vtmode))
SetConsoleMode(hOut, basemode);
}
#define hSTDIN GetStdHandle(STD_INPUT_HANDLE)
#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE)
#define hSTDERR GetStdHandle(STD_ERROR_HANDLE)
#define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
static bool is_console[STDERR_FILENO + 1];
static bool is_vt[STDERR_FILENO + 1];
static bool utf8_output;
static short stdoutAttrs = 0; // copied from the screen buffer on init
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,
};
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,
};
static bool running;
static HANDLE death;
static mp_thread input_thread;
static struct input_ctx *input_ctx;
static bool is_native_out_vt_internal(HANDLE hOut)
{
DWORD cmode;
return GetConsoleMode(hOut, &cmode) &&
(cmode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&
!(cmode & DISABLE_NEWLINE_AUTO_RETURN);
}
static bool is_native_out_vt(HANDLE hOut)
{
if (hOut == hSTDOUT)
return is_vt[STDOUT_FILENO];
if (hOut == hSTDERR)
return is_vt[STDERR_FILENO];
return is_native_out_vt_internal(hOut);
}
void terminal_get_size(int *w, int *h)
{
CONSOLE_SCREEN_BUFFER_INFO cinfo;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleScreenBufferInfo(hOut, &cinfo)) {
*w = cinfo.dwMaximumWindowSize.X - (is_native_out_vt(hOut) ? 0 : 1);
*h = cinfo.dwMaximumWindowSize.Y;
}
}
void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height)
{
}
static bool has_input_events(HANDLE h)
{
DWORD num_events;
if (!GetNumberOfConsoleInputEvents(h, &num_events))
return false;
return !!num_events;
}
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}))
break;
// 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;
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;
int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);
if (mpkey) {
mp_input_put_key(input_ctx, mpkey | mods);
} else {
// Only characters should be remaining
int c = record->uChar.UnicodeChar;
// 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;
if (c >= 0x20)
mp_input_put_key(input_ctx, c | mods);
}
}
}
static MP_THREAD_VOID input_thread_fn(void *ptr)
{
mp_thread_set_name("terminal/input");
HANDLE in = ptr;
HANDLE stuff[2] = {in, death};
while (1) {
DWORD r = WaitForMultipleObjects(2, stuff, FALSE, INFINITE);
if (r != WAIT_OBJECT_0)
break;
read_input(in);
}
MP_THREAD_RETURN();
}
void terminal_setup_getch(struct input_ctx *ictx)
{
if (running)
return;
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
if (GetNumberOfConsoleInputEvents(in, &(DWORD){0})) {
input_ctx = ictx;
death = CreateEventW(NULL, TRUE, FALSE, NULL);
if (!death)
return;
if (mp_thread_create(&input_thread, input_thread_fn, in)) {
CloseHandle(death);
return;
}
running = true;
}
}
DWORD tmp_buffers_key = FLS_OUT_OF_INDEXES;
struct tmp_buffers {
bstr write_console_buf;
wchar_t *write_console_wbuf;
};
void terminal_uninit(void)
{
if (running) {
SetEvent(death);
mp_thread_join(input_thread);
input_ctx = NULL;
running = false;
}
FlsFree(tmp_buffers_key);
}
bool terminal_in_background(void)
{
return false;
}
int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args)
{
struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
bool free_buf = false;
if (!buffers) {
buffers = talloc_zero(NULL, struct tmp_buffers);
free_buf = !FlsSetValue(tmp_buffers_key, buffers);
}
buffers->write_console_buf.len = 0;
bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args);
int ret = mp_console_write(wstream, buffers->write_console_buf);
if (free_buf)
talloc_free(buffers);
return ret;
}
int mp_console_write(HANDLE wstream, bstr str)
{
struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
bool free_buf = false;
if (!buffers) {
buffers = talloc_zero(NULL, struct tmp_buffers);
free_buf = !FlsSetValue(tmp_buffers_key, buffers);
}
bool vt = is_native_out_vt(wstream);
int wlen = 0;
wchar_t *pos = NULL;
if (!utf8_output || !vt) {
wlen = bstr_to_wchar(buffers, str, &buffers->write_console_wbuf);
pos = buffers->write_console_wbuf;
}
if (vt) {
if (utf8_output) {
WriteConsoleA(wstream, str.start, str.len, NULL, NULL);
} else {
WriteConsoleW(wstream, pos, wlen, NULL, NULL);
}
goto done;
}
while (*pos) {
wchar_t *next = wcschr(pos, '\033');
if (!next) {
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
break;
}
next[0] = '\0';
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
if (next[1] == '[') {
// CSI - Control Sequence Introducer
next += 2;
// private sequences
bool priv = next[0] == '?';
next += priv;
// 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 line
COORD cursor_pos = info.dwCursorPosition;
COORD at = cursor_pos;
int len;
switch (num_params ? params[0] : 0) {
case 1:
len = at.X;
at.X = 0;
break;
case 2:
len = info.dwSize.X;
at.X = 0;
break;
case 0:
default:
len = info.dwSize.X - at.X;
}
FillConsoleOutputCharacterW(wstream, L' ', len, at, &(DWORD){0});
SetConsoleCursorPosition(wstream, cursor_pos);
break;
}
case 'B': { // cursor down
info.dwCursorPosition.Y += !num_params ? 1 : params[0];
SetConsoleCursorPosition(wstream, info.dwCursorPosition);
break;
}
case 'A': { // cursor up
info.dwCursorPosition.Y -= !num_params ? 1 : params[0];
SetConsoleCursorPosition(wstream, info.dwCursorPosition);
break;
}
case 'J': {
// Only full screen clear is supported
if (!num_params || params[0] != 2)
break;
COORD top_left = {0, 0};
FillConsoleOutputCharacterW(wstream, L' ', info.dwSize.X * info.dwSize.Y,
top_left, &(DWORD){0});
SetConsoleCursorPosition(wstream, top_left);
break;
}
case 'f': {
if (num_params != 2)
break;
SetConsoleCursorPosition(wstream, (COORD){params[0], params[1]});
break;
}
case 'l': {
if (!priv || !num_params)
break;
switch (params[0]) {
case 25:; // hide the cursor
CONSOLE_CURSOR_INFO cursor_info;
if (!GetConsoleCursorInfo(wstream, &cursor_info))
break;
cursor_info.bVisible = FALSE;
SetConsoleCursorInfo(wstream, &cursor_info);
break;
}
break;
}
case 'h': {
if (!priv || !num_params)
break;
switch (params[0]) {
case 25:; // show the cursor
CONSOLE_CURSOR_INFO cursor_info;
if (!GetConsoleCursorInfo(wstream, &cursor_info))
break;
cursor_info.bVisible = TRUE;
SetConsoleCursorInfo(wstream, &cursor_info);
break;
}
break;
}
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 */
}
}
}
if (attr != info.wAttributes)
SetConsoleTextAttribute(wstream, attr);
break;
}
}
} else if (next[1] == ']') {
// OSC - Operating System Commands
next += 2;
// 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);
}
pos = next;
}
done:;
int ret = buffers->write_console_buf.len;
if (free_buf)
talloc_free(buffers);
return ret;
}
static bool is_a_console(HANDLE h)
{
return GetConsoleMode(h, &(DWORD){0});
}
bool mp_check_console(void *handle)
{
if (handle == hSTDIN)
return is_console[STDIN_FILENO];
if (handle == hSTDOUT)
return is_console[STDOUT_FILENO];
if (handle == hSTDERR)
return is_console[STDERR_FILENO];
return is_a_console(handle);
}
static void reopen_console_handle(DWORD std, int fd, FILE *stream)
{
HANDLE handle = GetStdHandle(std);
if (is_a_console(handle)) {
if (fd == 0) {
freopen("CONIN$", "rt", stream);
} else {
freopen("CONOUT$", "wt", stream);
}
// 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);
}
}
bool terminal_try_attach(void)
{
// 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);
if (!AttachConsole(ATTACH_PARENT_PROCESS))
return false;
// 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);
reopen_console_handle(STD_OUTPUT_HANDLE, STDOUT_FILENO, stdout);
reopen_console_handle(STD_ERROR_HANDLE, STDERR_FILENO, stderr);
return true;
}
void terminal_init(void)
{
CONSOLE_SCREEN_BUFFER_INFO cinfo;
DWORD cmode = 0;
GetConsoleMode(hSTDOUT, &cmode);
cmode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
attempt_native_out_vt(hSTDOUT, cmode);
attempt_native_out_vt(hSTDERR, cmode);
// Init for mp_check_console(), this never changes during runtime
is_console[STDIN_FILENO] = is_a_console(hSTDIN);
is_console[STDOUT_FILENO] = is_a_console(hSTDOUT);
is_console[STDERR_FILENO] = is_a_console(hSTDERR);
// Init for is_native_out_vt(), this is never disabled/changed during runtime
is_vt[STDOUT_FILENO] = is_native_out_vt_internal(hSTDOUT);
is_vt[STDERR_FILENO] = is_native_out_vt_internal(hSTDERR);
GetConsoleScreenBufferInfo(hSTDOUT, &cinfo);
stdoutAttrs = cinfo.wAttributes;
tmp_buffers_key = FlsAlloc((PFLS_CALLBACK_FUNCTION)talloc_free);
utf8_output = SetConsoleOutputCP(CP_UTF8);
}
|