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>
|
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-01-19 13:20:02 +00:00
|
|
|
#include "osdep/w32_keyboard.h"
|
2008-04-29 11:52:45 +00:00
|
|
|
|
player: redo terminal OSD and status line handling
The terminal OSD code includes the handling of the terminal status line,
showing player OSD messages on the terminal, and showing subtitles on
terminal (the latter two only if there is no video window, or if
terminal OSD is forced).
This didn't handle some corner cases correctly. For example, showing an
OSD message on the terminal always cleared the previous line, even if
the line was an important message (or even just the command prompt, if
most other messages were silenced).
Attempt to handle this correctly by keeping track of how many lines the
terminal OSD currently consists of. Since there could be race conditions
with other messages being printed, implement this in msg.c. Now msg.c
expects that MSGL_STATUS messages rewrite the status line, so the caller
is forced to use a single mp_msg() call to set the status line.
Instead of littering print_status() all over the place, update the
status only once per playloop iteration in update_osd_msg(). In audio-
only mode, the status line might now be a little bit off, but it's
perhaps ok.
Print the status line only if it has changed, or if another message was
printed. This might help with extremely slow terminals, although in
audio+video mode, it'll still be updated very often (A-V sync display
changes on every frame).
Instead of hardcoding the terminal sequences, use
terminfo/termcap to get the sequences. Remove the --term-osd-esc option,
which allowed to override the hardcoded escapes - it's useless now.
The fallback for terminals with no escape sequences for moving the
cursor and clearing a line is removed. This somewhat breaks status line
display on these terminals, including the MS Windows console: instead of
querying the terminal size and clearing the line manually by padding the
output with spaces, the line is simply not cleared. I don't expect this
to be a problem on UNIX, and on MS Windows we could emulate escape
sequences. Note that terminal OSD (other than the status line) was
broken anyway on these terminals.
In osd.c, the function get_term_width() is not used anymore, so remove
it. To remind us that the MS Windows console apparently adds a line
break when writint the last column, adjust screen_width in terminal-
win.c accordingly.
2014-01-13 19:05:41 +00:00
|
|
|
int screen_width = 79;
|
2013-12-21 18:33:45 +00:00
|
|
|
int screen_height = 24;
|
player: redo terminal OSD and status line handling
The terminal OSD code includes the handling of the terminal status line,
showing player OSD messages on the terminal, and showing subtitles on
terminal (the latter two only if there is no video window, or if
terminal OSD is forced).
This didn't handle some corner cases correctly. For example, showing an
OSD message on the terminal always cleared the previous line, even if
the line was an important message (or even just the command prompt, if
most other messages were silenced).
Attempt to handle this correctly by keeping track of how many lines the
terminal OSD currently consists of. Since there could be race conditions
with other messages being printed, implement this in msg.c. Now msg.c
expects that MSGL_STATUS messages rewrite the status line, so the caller
is forced to use a single mp_msg() call to set the status line.
Instead of littering print_status() all over the place, update the
status only once per playloop iteration in update_osd_msg(). In audio-
only mode, the status line might now be a little bit off, but it's
perhaps ok.
Print the status line only if it has changed, or if another message was
printed. This might help with extremely slow terminals, although in
audio+video mode, it'll still be updated very often (A-V sync display
changes on every frame).
Instead of hardcoding the terminal sequences, use
terminfo/termcap to get the sequences. Remove the --term-osd-esc option,
which allowed to override the hardcoded escapes - it's useless now.
The fallback for terminals with no escape sequences for moving the
cursor and clearing a line is removed. This somewhat breaks status line
display on these terminals, including the MS Windows console: instead of
querying the terminal size and clearing the line manually by padding the
output with spaces, the line is simply not cleared. I don't expect this
to be a problem on UNIX, and on MS Windows we could emulate escape
sequences. Note that terminal OSD (other than the status line) was
broken anyway on these terminals.
In osd.c, the function get_term_width() is not used anymore, so remove
it. To remind us that the MS Windows console apparently adds a line
break when writint the last column, adjust screen_width in terminal-
win.c accordingly.
2014-01-13 19:05:41 +00:00
|
|
|
char *terminal_erase_to_end_of_line = "";
|
|
|
|
char *terminal_cursor_up = "";
|
2013-12-21 18:33:45 +00:00
|
|
|
|
2013-12-18 14:03:08 +00:00
|
|
|
#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE)
|
|
|
|
#define hSTDERR GetStdHandle(STD_ERROR_HANDLE)
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2013-07-14 14:08:27 +00:00
|
|
|
void get_screen_size(void)
|
|
|
|
{
|
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)) {
|
2011-10-22 14:24:16 +00:00
|
|
|
screen_width = cinfo.dwMaximumWindowSize.X;
|
|
|
|
screen_height = cinfo.dwMaximumWindowSize.Y;
|
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 21:50:59 +00:00
|
|
|
static HANDLE in;
|
2013-07-14 14:08:27 +00:00
|
|
|
static int getch2_status = 0;
|
2003-03-30 21:20:04 +00:00
|
|
|
|
2007-08-25 04:28:08 +00:00
|
|
|
static int getch2_internal(void)
|
|
|
|
{
|
2013-07-14 14:08:27 +00:00
|
|
|
INPUT_RECORD eventbuffer[128];
|
2003-03-30 21:20:04 +00:00
|
|
|
DWORD retval;
|
2013-07-14 14:08:27 +00:00
|
|
|
int i = 0;
|
|
|
|
if (!getch2_status) {
|
|
|
|
// supports e.g. MinGW xterm, unfortunately keys are only received after
|
|
|
|
// enter was pressed.
|
|
|
|
uint8_t c;
|
|
|
|
if (!PeekNamedPipe(in, NULL, 1, &retval, NULL, NULL) || !retval)
|
|
|
|
return -1;
|
|
|
|
ReadFile(in, &c, 1, &retval, NULL);
|
|
|
|
return retval == 1 ? c : -1;
|
2010-02-28 00:24:01 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
/*check if there are input events*/
|
2013-07-14 14:08:27 +00:00
|
|
|
if (!GetNumberOfConsoleInputEvents(in, &retval)) {
|
|
|
|
printf("getch2: can't get number of input events: %i\n",
|
|
|
|
(int)GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (retval <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*read all events*/
|
|
|
|
if (!ReadConsoleInput(in, eventbuffer, 128, &retval)) {
|
|
|
|
printf("getch: can't read input events\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*filter out keyevents*/
|
|
|
|
for (i = 0; i < retval; i++) {
|
|
|
|
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);
|
|
|
|
if (mpkey)
|
|
|
|
return mpkey;
|
2013-07-14 14:08:27 +00:00
|
|
|
|
|
|
|
/*only characters should be remaining*/
|
|
|
|
//printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
|
2014-01-19 13:20:02 +00:00
|
|
|
return eventbuffer[i].Event.KeyEvent.uChar.UnicodeChar;
|
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:
|
|
|
|
//printf("getch2: unsupported event type");
|
|
|
|
break;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
2013-07-14 14:08:27 +00:00
|
|
|
return -1;
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
static bool getch2(struct input_ctx *ctx)
|
2007-08-25 04:28:08 +00:00
|
|
|
{
|
|
|
|
int r = getch2_internal();
|
|
|
|
if (r >= 0)
|
2013-07-14 14:08:27 +00:00
|
|
|
mp_input_put_key(ctx, r);
|
2012-04-06 11:24:26 +00:00
|
|
|
return true;
|
2007-08-25 04:28:08 +00:00
|
|
|
}
|
2003-03-30 21:20:04 +00:00
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
static int read_keys(void *ctx, int fd)
|
|
|
|
{
|
|
|
|
if (getch2(ctx))
|
|
|
|
return MP_INPUT_NOTHING;
|
|
|
|
return MP_INPUT_DEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void terminal_setup_getch(struct input_ctx *ictx)
|
|
|
|
{
|
|
|
|
mp_input_add_fd(ictx, 0, 1, NULL, read_keys, NULL, ictx);
|
2014-08-01 20:57:56 +00:00
|
|
|
getch2_enable();
|
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
|
|
|
}
|
|
|
|
|
2009-01-05 14:48:03 +00:00
|
|
|
void getch2_enable(void)
|
|
|
|
{
|
2013-07-14 14:08:27 +00:00
|
|
|
DWORD retval;
|
2010-02-27 21:50:59 +00:00
|
|
|
in = GetStdHandle(STD_INPUT_HANDLE);
|
2013-07-14 14:08:27 +00:00
|
|
|
if (!GetNumberOfConsoleInputEvents(in, &retval)) {
|
|
|
|
printf("getch2: %i can't get number of input events "
|
|
|
|
"[disabling console input]\n", (int)GetLastError());
|
|
|
|
getch2_status = 0;
|
|
|
|
} else
|
|
|
|
getch2_status = 1;
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2009-01-05 14:48:03 +00:00
|
|
|
void getch2_disable(void)
|
|
|
|
{
|
2013-07-14 14:08:27 +00:00
|
|
|
if (!getch2_status)
|
|
|
|
return; // already disabled / never enabled
|
|
|
|
getch2_status = 0;
|
2003-03-30 21:20:04 +00:00
|
|
|
}
|
2013-12-18 14:03:08 +00:00
|
|
|
|
|
|
|
bool terminal_in_background(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void terminal_set_foreground_color(FILE *stream, int c)
|
|
|
|
{
|
|
|
|
HANDLE *wstream = stream == stderr ? hSTDERR : hSTDOUT;
|
|
|
|
if (c < 0 || c >= 8) { // reset or invalid
|
|
|
|
SetConsoleTextAttribute(wstream, stdoutAttrs);
|
|
|
|
} else {
|
|
|
|
SetConsoleTextAttribute(wstream, ansi2win32[c] | FOREGROUND_INTENSITY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|