summaryrefslogtreecommitdiffstats
path: root/osdep/terminal-win.c
diff options
context:
space:
mode:
Diffstat (limited to 'osdep/terminal-win.c')
-rw-r--r--osdep/terminal-win.c355
1 files changed, 295 insertions, 60 deletions
diff --git a/osdep/terminal-win.c b/osdep/terminal-win.c
index 3672f3a763..435354155a 100644
--- a/osdep/terminal-win.c
+++ b/osdep/terminal-win.c
@@ -18,14 +18,12 @@
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "config.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <windows.h>
#include <io.h>
-#include <pthread.h>
#include <assert.h>
#include "common/common.h"
#include "input/keycodes.h"
@@ -55,20 +53,17 @@ static void attempt_native_out_vt(HANDLE hOut, DWORD basemode)
SetConsoleMode(hOut, basemode);
}
-static bool is_native_out_vt(HANDLE hOut)
-{
- DWORD cmode;
- return GetConsoleMode(hOut, &cmode) &&
- (cmode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&
- !(cmode & DISABLE_NEWLINE_AUTO_RETURN);
-}
+#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,
@@ -93,19 +88,60 @@ static const unsigned char ansi2win32bg[8] = {
static bool running;
static HANDLE death;
-static pthread_t input_thread;
+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);
+ HANDLE hOut = hSTDOUT;
if (GetConsoleScreenBufferInfo(hOut, &cinfo)) {
*w = cinfo.dwMaximumWindowSize.X - (is_native_out_vt(hOut) ? 0 : 1);
*h = cinfo.dwMaximumWindowSize.Y;
}
}
+static bool get_font_size(int *w, int *h)
+{
+ CONSOLE_FONT_INFO finfo;
+ HANDLE hOut = hSTDOUT;
+ BOOL res = GetCurrentConsoleFont(hOut, FALSE, &finfo);
+ if (res) {
+ *w = finfo.dwFontSize.X;
+ *h = finfo.dwFontSize.Y;
+ }
+ return res;
+}
+
+void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height)
+{
+ int w = 0, h = 0, fw = 0, fh = 0;
+ terminal_get_size(&w, &h);
+ if (get_font_size(&fw, &fh)) {
+ *px_width = fw * w;
+ *px_height = fh * h;
+ *rows = w;
+ *cols = h;
+ }
+}
+
static bool has_input_events(HANDLE h)
{
DWORD num_events;
@@ -123,42 +159,87 @@ static void read_input(HANDLE in)
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);
+ switch (event.EventType)
+ {
+ case KEY_EVENT: {
+ 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);
+ }
+ break;
+ }
+ case MOUSE_EVENT: {
+ MOUSE_EVENT_RECORD *record = &event.Event.MouseEvent;
+ 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;
+
+ switch (record->dwEventFlags) {
+ case MOUSE_MOVED: {
+ int w = 0, h = 0;
+ if (get_font_size(&w, &h)) {
+ mp_input_set_mouse_pos(input_ctx, w * (record->dwMousePosition.X + 0.5),
+ h * (record->dwMousePosition.Y + 0.5));
+ }
+ break;
+ }
+ case MOUSE_HWHEELED: {
+ int button = (int16_t)HIWORD(record->dwButtonState) > 0 ? MP_WHEEL_RIGHT : MP_WHEEL_LEFT;
+ mp_input_put_key(input_ctx, button | mods);
+ break;
+ }
+ case MOUSE_WHEELED: {
+ int button = (int16_t)HIWORD(record->dwButtonState) > 0 ? MP_WHEEL_UP : MP_WHEEL_DOWN;
+ mp_input_put_key(input_ctx, button | mods);
+ break;
+ }
+ default: {
+ int left_button_state = record->dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED ?
+ MP_KEY_STATE_DOWN : MP_KEY_STATE_UP;
+ mp_input_put_key(input_ctx, MP_MBTN_LEFT | mods | left_button_state);
+ int right_button_state = record->dwButtonState & RIGHTMOST_BUTTON_PRESSED ?
+ MP_KEY_STATE_DOWN : MP_KEY_STATE_UP;
+ mp_input_put_key(input_ctx, MP_MBTN_RIGHT | mods | right_button_state);
+ break;
+ }
+ }
+ break;
+ }
}
}
}
-static void *input_thread_fn(void *ptr)
+static MP_THREAD_VOID input_thread_fn(void *ptr)
{
- mpthread_set_name("terminal");
+ mp_thread_set_name("terminal/input");
HANDLE in = ptr;
HANDLE stuff[2] = {in, death};
while (1) {
@@ -167,7 +248,7 @@ static void *input_thread_fn(void *ptr)
break;
read_input(in);
}
- return NULL;
+ MP_THREAD_RETURN();
}
void terminal_setup_getch(struct input_ctx *ictx)
@@ -181,7 +262,7 @@ void terminal_setup_getch(struct input_ctx *ictx)
death = CreateEventW(NULL, TRUE, FALSE, NULL);
if (!death)
return;
- if (pthread_create(&input_thread, NULL, input_thread_fn, in)) {
+ if (mp_thread_create(&input_thread, input_thread_fn, in)) {
CloseHandle(death);
return;
}
@@ -189,14 +270,21 @@ void terminal_setup_getch(struct input_ctx *ictx)
}
}
+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);
- pthread_join(input_thread, NULL);
+ mp_thread_join(input_thread);
input_ctx = NULL;
running = false;
}
+ FlsFree(tmp_buffers_key);
}
bool terminal_in_background(void)
@@ -204,16 +292,53 @@ bool terminal_in_background(void)
return false;
}
-void mp_write_console_ansi(HANDLE wstream, char *buf)
+int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args)
{
- wchar_t *wbuf = mp_from_utf8(NULL, buf);
- wchar_t *pos = wbuf;
+ 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);
+ }
- while (*pos) {
- if (is_native_out_vt(wstream)) {
- WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
- break;
+ 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);
@@ -225,6 +350,10 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
// 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.
@@ -248,18 +377,84 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
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);
+ 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 -= 1;
+ 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
@@ -350,7 +545,13 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
pos = next;
}
- talloc_free(wbuf);
+done:;
+ int ret = buffers->write_console_buf.len;
+
+ if (free_buf)
+ talloc_free(buffers);
+
+ return ret;
}
static bool is_a_console(HANDLE h)
@@ -358,6 +559,17 @@ 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);
@@ -367,7 +579,6 @@ static void reopen_console_handle(DWORD std, int fd, FILE *stream)
} else {
freopen("CONOUT$", "wt", stream);
}
- setvbuf(stream, NULL, _IONBF, 0);
// 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
@@ -410,6 +621,17 @@ bool terminal_try_attach(void)
return true;
}
+void terminal_set_mouse_input(bool enable)
+{
+ DWORD cmode;
+ HANDLE in = hSTDIN;
+ if (GetConsoleMode(in, &cmode)) {
+ cmode = enable ? cmode | ENABLE_MOUSE_INPUT
+ : cmode & (~ENABLE_MOUSE_INPUT);
+ SetConsoleMode(in, cmode);
+ }
+}
+
void terminal_init(void)
{
CONSOLE_SCREEN_BUFFER_INFO cinfo;
@@ -418,6 +640,19 @@ void terminal_init(void)
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);
}