terminal: some cleanups

In particular, remove all the stupid debug printfs from the win code.
This commit is contained in:
wm4 2014-08-21 22:13:10 +02:00
parent 070c54dff4
commit 5890e59dbc
5 changed files with 44 additions and 74 deletions

View File

@ -52,9 +52,6 @@ static volatile struct termios tio_orig;
static volatile int tio_orig_set;
#endif
int screen_width = 80;
int screen_height = 24;
typedef struct {
char *cap;
int len;
@ -274,13 +271,6 @@ static int load_termcap(char *termtype){
// http://linux.die.net/man/5/termcap
// http://unixhelp.ed.ac.uk/CGI/man-cgi?terminfo+5
screen_width = tgetnum("co");
screen_height = tgetnum("li");
if (screen_width < 1 || screen_width > 255)
screen_width = 80;
if (screen_height < 1 || screen_height > 255)
screen_height = 24;
term_smkx = tgetstr("ks", &buf_ptr);
term_rmkx = tgetstr("ke", &buf_ptr);
@ -334,13 +324,14 @@ static int load_termcap(char *termtype){
return getch2_keys.len;
}
void get_screen_size(void) {
void terminal_get_size(int *w, int *h)
{
struct winsize ws;
if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col)
return;
screen_width = ws.ws_col;
screen_height = ws.ws_row;
*w = ws.ws_col;
*h = ws.ws_row;
}
#define BUF_LEN 256
@ -451,15 +442,16 @@ static int read_keys(void *ctx, int fd)
return MP_INPUT_DEAD;
}
void terminal_setup_getch(struct input_ctx *ictx)
{
mp_input_add_fd(ictx, 0, 1, NULL, read_keys, NULL, ictx);
getch2_enable();
}
static volatile int getch2_active = 0;
static volatile int getch2_enabled = 0;
void terminal_setup_getch(struct input_ctx *ictx)
{
if (!getch2_enabled)
return;
mp_input_add_fd(ictx, 0, 1, NULL, read_keys, NULL, ictx);
}
static void do_activate_getch2(void)
{
if (getch2_active || !isatty(1))
@ -563,7 +555,8 @@ static void quit_request_sighandler(int signum)
async_quit_request = 1;
}
void getch2_enable(void){
static void getch2_enable(void)
{
if (getch2_enabled)
return;
@ -581,7 +574,8 @@ void getch2_enable(void){
getch2_enabled = 1;
}
void getch2_disable(void){
void terminal_uninit(void)
{
if (!getch2_enabled)
return;
@ -608,5 +602,6 @@ int terminal_init(void)
{
if (isatty(1))
load_termcap(NULL);
getch2_enable();
return 0;
}

View File

@ -30,15 +30,13 @@
#include <string.h>
#include <windows.h>
#include <io.h>
#include "common/common.h"
#include "input/keycodes.h"
#include "input/input.h"
#include "terminal.h"
#include "osdep/io.h"
#include "osdep/w32_keyboard.h"
int screen_width = 79;
int screen_height = 24;
#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE)
#define hSTDERR GetStdHandle(STD_ERROR_HANDLE)
static short stdoutAttrs = 0;
@ -53,40 +51,35 @@ static const unsigned char ansi2win32[8] = {
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED,
};
void get_screen_size(void)
void terminal_get_size(int *w, int *h)
{
CONSOLE_SCREEN_BUFFER_INFO cinfo;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cinfo)) {
screen_width = cinfo.dwMaximumWindowSize.X - 1;
screen_height = cinfo.dwMaximumWindowSize.Y;
*w = cinfo.dwMaximumWindowSize.X - 1;
*h = cinfo.dwMaximumWindowSize.Y;
}
}
static HANDLE in;
static int getch2_status = 0;
static int getch2_internal(void)
{
INPUT_RECORD eventbuffer[128];
DWORD retval;
int i = 0;
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
/*check if there are input events*/
if (!GetNumberOfConsoleInputEvents(in, &retval)) {
printf("getch2: can't get number of input events: %i\n",
(int)GetLastError());
if (!GetNumberOfConsoleInputEvents(in, &retval))
return -1;
}
if (retval <= 0)
return -1;
/*read all events*/
if (!ReadConsoleInput(in, eventbuffer, 128, &retval)) {
printf("getch: can't read input events\n");
INPUT_RECORD eventbuffer[128];
if (!ReadConsoleInput(in, eventbuffer, MP_ARRAY_SIZE(eventbuffer), &retval))
return -1;
}
/*filter out keyevents*/
for (i = 0; i < retval; i++) {
for (int i = 0; i < retval; i++) {
switch (eventbuffer[i].EventType) {
case KEY_EVENT: {
KEY_EVENT_RECORD *record = &eventbuffer[i].Event.KeyEvent;
@ -101,7 +94,6 @@ static int getch2_internal(void)
return mpkey;
/*only characters should be remaining*/
//printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
return eventbuffer[i].Event.KeyEvent.uChar.UnicodeChar;
}
break;
@ -111,7 +103,6 @@ static int getch2_internal(void)
case FOCUS_EVENT:
case MENU_EVENT:
default:
//printf("getch2: unsupported event type");
break;
}
}
@ -136,29 +127,16 @@ static int read_keys(void *ctx, int fd)
void terminal_setup_getch(struct input_ctx *ictx)
{
mp_input_add_fd(ictx, 0, 1, NULL, read_keys, NULL, ictx);
getch2_enable();
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
getch2_status = !!GetNumberOfConsoleInputEvents(in, &(DWORD){0});
}
void getch2_poll(void)
{
}
void getch2_enable(void)
void terminal_uninit(void)
{
DWORD retval;
in = GetStdHandle(STD_INPUT_HANDLE);
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;
}
void getch2_disable(void)
{
if (!getch2_status)
return; // already disabled / never enabled
getch2_status = 0;
}

View File

@ -29,25 +29,20 @@
struct input_ctx;
/* Screen size. Initialized by load_termcap() and get_screen_size() */
extern int screen_width;
extern int screen_height;
/* Global initialization for terminal output. */
int terminal_init(void);
/* Setup ictx to read keys from the terminal */
void terminal_setup_getch(struct input_ctx *ictx);
/* Undo terminal_init(), and also terminal_setup_getch() */
void terminal_uninit(void);
/* Return whether the process has been backgrounded. */
bool terminal_in_background(void);
/* Get screen-size using IOCTL call. */
void get_screen_size(void);
/* Initialize getch2 */
void getch2_enable(void);
void getch2_disable(void);
/* Get terminal-size in columns/rows. */
void terminal_get_size(int *w, int *h);
/* Enable and disable STDIN line-buffering */
void getch2_poll(void);

View File

@ -132,9 +132,6 @@ void mp_destroy(struct MPContext *mpctx)
command_uninit(mpctx);
mp_dispatch_set_wakeup_fn(mpctx->dispatch, NULL, NULL);
mp_input_uninit(mpctx->input);
osd_free(mpctx->osd);
#if HAVE_LIBASS
@ -142,10 +139,14 @@ void mp_destroy(struct MPContext *mpctx)
ass_library_done(mpctx->ass_library);
#endif
if (mpctx->opts->use_terminal) {
getch2_disable();
if (mpctx->opts->use_terminal && terminal_initialized) {
terminal_uninit();
terminal_initialized = false;
}
mp_dispatch_set_wakeup_fn(mpctx->dispatch, NULL, NULL);
mp_input_uninit(mpctx->input);
uninit_libav(mpctx->global);
if (mpctx->autodetach)
@ -403,7 +404,7 @@ int mp_initialize(struct MPContext *mpctx)
}
#endif
if (opts->use_terminal && opts->consolecontrols)
if (opts->use_terminal && opts->consolecontrols && terminal_initialized)
terminal_setup_getch(mpctx->input);
#if HAVE_LIBASS

View File

@ -238,8 +238,9 @@ static void print_status(struct MPContext *mpctx)
if (opts->term_osd_bar) {
saddf(&line, "\n");
get_screen_size();
add_term_osd_bar(mpctx, &line, screen_width);
int w = 80, h = 24;
terminal_get_size(&w, &h);
add_term_osd_bar(mpctx, &line, w);
}
// end