2009-03-01 13:13:25 +00:00
|
|
|
/* Windows TermIO
|
|
|
|
*
|
|
|
|
* copyright (C) 2003 Sascha Sommer
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2003-03-30 21:20:04 +00:00
|
|
|
|
|
|
|
// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
|
|
|
|
// for additional virtual keycodes
|
|
|
|
|
|
|
|
|
2007-03-04 19:04:08 +00:00
|
|
|
#include "config.h"
|
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-01-19 13:20:02 +00:00
|
|
|
#include "osdep/w32_keyboard.h"
|
2008-04-29 11:52:45 +00:00
|
|
|
|
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)
|
|
|
|
|
2013-12-18 14:03:08 +00:00
|
|
|
static short stdoutAttrs = 0;
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
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;
|
2013-07-14 14:08:27 +00:00
|
|
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cinfo)) {
|
2014-08-21 20:13:10 +00:00
|
|
|
*w = cinfo.dwMaximumWindowSize.X - 1;
|
|
|
|
*h = cinfo.dwMaximumWindowSize.Y;
|
2011-10-22 14:24:16 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 20:15:32 +00:00
|
|
|
static void read_input(void)
|
2007-08-25 04:28:08 +00:00
|
|
|
{
|
2003-03-30 21:20:04 +00:00
|
|
|
DWORD retval;
|
2014-08-21 20:13:10 +00:00
|
|
|
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
|
2003-03-30 21:20:04 +00:00
|
|
|
/*check if there are input events*/
|
2014-08-21 20:13:10 +00:00
|
|
|
if (!GetNumberOfConsoleInputEvents(in, &retval))
|
2014-08-21 20:15:32 +00:00
|
|
|
return;
|
2013-07-14 14:08:27 +00:00
|
|
|
if (retval <= 0)
|
2014-08-21 20:15:32 +00:00
|
|
|
return;
|
2013-07-14 14:08:27 +00:00
|
|
|
|
|
|
|
/*read all events*/
|
2014-08-21 20:13:10 +00:00
|
|
|
INPUT_RECORD eventbuffer[128];
|
|
|
|
if (!ReadConsoleInput(in, eventbuffer, MP_ARRAY_SIZE(eventbuffer), &retval))
|
2014-08-21 20:15:32 +00:00
|
|
|
return;
|
2013-07-14 14:08:27 +00:00
|
|
|
|
|
|
|
/*filter out keyevents*/
|
2014-08-21 20:13:10 +00:00
|
|
|
for (int i = 0; i < retval; i++) {
|
2013-07-14 14:08:27 +00:00
|
|
|
switch (eventbuffer[i].EventType) {
|
2014-01-19 13:20:02 +00:00
|
|
|
case KEY_EVENT: {
|
|
|
|
KEY_EVENT_RECORD *record = &eventbuffer[i].Event.KeyEvent;
|
|
|
|
|
2013-07-14 14:08:27 +00:00
|
|
|
/*only a pressed key is interresting for us*/
|
2014-01-19 13:20:02 +00:00
|
|
|
if (record->bKeyDown) {
|
|
|
|
UINT vkey = record->wVirtualKeyCode;
|
|
|
|
bool ext = record->dwControlKeyState & ENHANCED_KEY;
|
|
|
|
|
|
|
|
int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);
|
2014-08-21 20:15:32 +00:00
|
|
|
if (mpkey) {
|
|
|
|
mp_input_put_key(input_ctx, mpkey);
|
|
|
|
} else {
|
|
|
|
/*only characters should be remaining*/
|
|
|
|
int c = eventbuffer[i].Event.KeyEvent.uChar.UnicodeChar;
|
|
|
|
if (c > 0)
|
|
|
|
mp_input_put_key(input_ctx, c);
|
|
|
|
}
|
2013-07-14 14:08:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-01-19 13:20:02 +00:00
|
|
|
}
|
2013-07-14 14:08:27 +00:00
|
|
|
case MOUSE_EVENT:
|
|
|
|
case WINDOW_BUFFER_SIZE_EVENT:
|
|
|
|
case FOCUS_EVENT:
|
|
|
|
case MENU_EVENT:
|
|
|
|
default:
|
|
|
|
break;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
2014-08-21 20:15:32 +00:00
|
|
|
return;
|
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-08-21 20:15:32 +00:00
|
|
|
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
HANDLE stuff[2] = {in, death};
|
|
|
|
while (1) {
|
|
|
|
DWORD r = WaitForMultipleObjects(2, stuff, FALSE, INFINITE);
|
|
|
|
if (r != WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
read_input();
|
|
|
|
}
|
|
|
|
return NULL;
|
2013-12-21 18:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void terminal_setup_getch(struct input_ctx *ictx)
|
|
|
|
{
|
2014-08-21 20:15:32 +00:00
|
|
|
assert(!running);
|
|
|
|
|
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;
|
|
|
|
death = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!death)
|
|
|
|
return;
|
|
|
|
if (pthread_create(&input_thread, NULL, input_thread_fn, NULL)) {
|
|
|
|
CloseHandle(death);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
running = true;
|
|
|
|
}
|
2013-12-21 18:33:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 14:08:27 +00:00
|
|
|
void getch2_poll(void)
|
|
|
|
{
|
2013-01-23 14:47:49 +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
|
|
|
static void write_console_text(HANDLE wstream, char *buf)
|
2013-12-18 14:03:08 +00:00
|
|
|
{
|
2014-08-21 20:11:38 +00:00
|
|
|
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.
|
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
|
|
|
{
|
|
|
|
while (*buf) {
|
|
|
|
char *next = strchr(buf, '\033');
|
|
|
|
if (!next) {
|
|
|
|
write_console_text(wstream, buf);
|
|
|
|
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 += 2;
|
|
|
|
// ANSI 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[2] = {-1, -1}; // 'm' might be unlimited; ignore that
|
|
|
|
int num_params = 0;
|
|
|
|
while (num_params < 2) {
|
|
|
|
char *end = next;
|
|
|
|
long p = strtol(next, &end, 10);
|
|
|
|
if (end == next)
|
|
|
|
break;
|
|
|
|
next = end;
|
|
|
|
params[num_params++] = p;
|
|
|
|
if (next[0] != ';' || !next[0])
|
|
|
|
break;
|
|
|
|
next += 1;
|
|
|
|
}
|
|
|
|
char 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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'A': { // cursor up
|
|
|
|
info.dwCursorPosition.Y -= 1;
|
|
|
|
SetConsoleCursorPosition(wstream, info.dwCursorPosition);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'm': { // "SGR"
|
|
|
|
for (int n = 0; n < num_params; n++) {
|
|
|
|
int p = params[n];
|
2014-08-24 10:59:35 +00:00
|
|
|
if (p == 0) {
|
|
|
|
info.wAttributes = stdoutAttrs;
|
|
|
|
SetConsoleTextAttribute(wstream, info.wAttributes);
|
|
|
|
} else if (p == 1) {
|
|
|
|
info.wAttributes |= FOREGROUND_INTENSITY;
|
|
|
|
SetConsoleTextAttribute(wstream, info.wAttributes);
|
|
|
|
} else if (p >= 30 && p < 38) {
|
|
|
|
info.wAttributes &= ~FOREGROUND_ALL;
|
|
|
|
info.wAttributes |= ansi2win32[p - 30];
|
|
|
|
SetConsoleTextAttribute(wstream, info.wAttributes);
|
2014-08-21 20:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf = next;
|
2013-12-18 14:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int terminal_init(void)
|
|
|
|
{
|
2014-01-16 10:25:52 +00:00
|
|
|
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
|
|
|
|
// We have been started by something with a console window.
|
|
|
|
// Redirect output streams to that console's low-level handles,
|
|
|
|
// so we can actually use WriteConsole later on.
|
|
|
|
|
|
|
|
int hConHandle;
|
|
|
|
|
|
|
|
hConHandle = _open_osfhandle((intptr_t)hSTDOUT, _O_TEXT);
|
|
|
|
*stdout = *_fdopen(hConHandle, "w");
|
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
|
|
|
|
hConHandle = _open_osfhandle((intptr_t)hSTDERR, _O_TEXT);
|
|
|
|
*stderr = *_fdopen(hConHandle, "w");
|
|
|
|
setvbuf(stderr, NULL, _IONBF, 0);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
SetConsoleMode(hSTDOUT, cmode);
|
|
|
|
SetConsoleMode(hSTDERR, cmode);
|
|
|
|
GetConsoleScreenBufferInfo(hSTDOUT, &cinfo);
|
|
|
|
stdoutAttrs = cinfo.wAttributes;
|
|
|
|
return 0;
|
|
|
|
}
|