terminal-win: handle 'Change Window Title' OSC sequence

This should make --term-title work in Windows 8.1 and below.

OSC sequences are defined in ECMA-48. The 'Change Window Title' command,
as far as I can tell, is a de-facto standard defined by xterm[1]. In
either case, this code is probably still not standards-compliant.

This also changes mp_write_console_ansi to convert to UTF-16 before
parsing control sequences, because that made it easier to pass the OSC
param to SetConsoleTitleW. I think it's also more correct to do it this
way, even though it doesn't really matter much for our limited terminal
parsing. As a side-effect of this, mp_write_console_ansi no longer
mutates its argument.

[1]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
This commit is contained in:
James Ross-Gowan 2020-05-27 22:52:25 +10:00
parent 27e41c69aa
commit 102a083171
1 changed files with 138 additions and 106 deletions

View File

@ -204,44 +204,36 @@ bool terminal_in_background(void)
return false;
}
static void write_console_text(HANDLE wstream, char *buf)
{
wchar_t *out = mp_from_utf8(NULL, buf);
size_t out_len = wcslen(out);
WriteConsoleW(wstream, out, out_len, NULL, NULL);
talloc_free(out);
}
// Mutates the input argument (buf), because we're evil.
void mp_write_console_ansi(HANDLE wstream, char *buf)
{
while (*buf) {
wchar_t *wbuf = mp_from_utf8(NULL, buf);
wchar_t *pos = wbuf;
while (*pos) {
if (is_native_out_vt(wstream)) {
write_console_text(wstream, buf);
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
break;
}
char *next = strchr(buf, '\033');
wchar_t *next = wcschr(pos, '\033');
if (!next) {
write_console_text(wstream, buf);
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
break;
}
next[0] = '\0'; // mutate input for fun and profit
write_console_text(wstream, buf);
if (next[1] != '[') {
write_console_text(wstream, "\033");
buf = next;
continue;
}
next[0] = '\0';
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
if (next[1] == '[') {
// CSI - Control Sequence Introducer
next += 2;
// ANSI codes generally follow this syntax:
// 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)) {
char *end = next;
long p = strtol(next, &end, 10);
wchar_t *end = next;
long p = wcstol(next, &end, 10);
if (end == next)
break;
next = end;
@ -250,7 +242,7 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
break;
next += 1;
}
char code = next[0];
wchar_t code = next[0];
if (code)
next += 1;
CONSOLE_SCREEN_BUFFER_INFO info;
@ -317,8 +309,48 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
break;
}
}
buf = next;
} 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;
}
talloc_free(wbuf);
}
static bool is_a_console(HANDLE h)