input: rework how input sources are added

Until now, there were two functions to add input sources (stuff like
stdin input, slave mode, lirc, joystick). Unify them to a single
function (mp_input_add_fd()), and make sure the associated callbacks
always have a context parameter.

Change the lirc and joystick code such that they take store their state
in a context struct (probably worthless), and use the new mp_msg
replacements (the point of this refactoring).

Additionally, get rid of the ugly USE_FD0_CMD_SELECT etc. ifdeffery in
the terminal handling code.
This commit is contained in:
wm4
2013-12-21 19:33:45 +01:00
parent dadf3a9a46
commit ed71606e65
13 changed files with 213 additions and 205 deletions

View File

@@ -358,7 +358,7 @@ static void walk_buf(unsigned int count) {
getch2_pos = 0;
}
bool getch2(struct input_ctx *input_ctx)
static bool getch2(struct input_ctx *input_ctx)
{
int retval = read(0, &getch2_buf[getch2_pos], BUF_LEN - getch2_len - getch2_pos);
/* Return false on EOF to stop running select() on the FD, as it'd
@@ -437,6 +437,18 @@ bool getch2(struct input_ctx *input_ctx)
return true;
}
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);
}
static volatile int getch2_active = 0;
static volatile int getch2_enabled = 0;
@@ -593,6 +605,11 @@ void terminal_set_foreground_color(FILE *stream, int c)
}
}
void terminal_setup_stdin_cmd_input(struct input_ctx *ictx)
{
mp_input_add_fd(ictx, 0, 1, input_default_read_cmd, NULL, NULL, NULL);
}
int terminal_init(void)
{
load_termcap(NULL);

View File

@@ -33,6 +33,10 @@
#include "input/input.h"
#include "terminal.h"
int screen_width = 80;
int screen_height = 24;
char *erase_to_end_of_line = NULL;
#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE)
#define hSTDERR GetStdHandle(STD_ERROR_HANDLE)
static short stdoutAttrs = 0;
@@ -47,7 +51,7 @@ static const unsigned char ansi2win32[8] = {
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED,
};
int mp_input_slave_cmd_func(int fd, char *dest, int size)
static int mp_input_slave_cmd_func(void *ctx, int fd, char *dest, int size)
{
DWORD retval;
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
@@ -66,9 +70,10 @@ int mp_input_slave_cmd_func(int fd, char *dest, int size)
return MP_INPUT_NOTHING;
}
int screen_width = 80;
int screen_height = 24;
char *erase_to_end_of_line = NULL;
void terminal_setup_stdin_cmd_input(struct input_ctx *ictx)
{
mp_input_add_fd(ictx, 0, 0, mp_input_slave_cmd_func, NULL, NULL, NULL);
}
void get_screen_size(void)
{
@@ -172,7 +177,7 @@ static int getch2_internal(void)
return -1;
}
bool getch2(struct input_ctx *ctx)
static bool getch2(struct input_ctx *ctx)
{
int r = getch2_internal();
if (r >= 0)
@@ -180,6 +185,18 @@ bool getch2(struct input_ctx *ctx)
return true;
}
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);
}
void getch2_poll(void)
{
}

View File

@@ -27,6 +27,8 @@
#include <stdbool.h>
#include <stdio.h>
struct input_ctx;
/* Screen size. Initialized by load_termcap() and get_screen_size() */
extern int screen_width;
extern int screen_height;
@@ -37,6 +39,12 @@ extern char * erase_to_end_of_line;
/* Global initialization for terminal output. */
int terminal_init(void);
/* Setup ictx to read input commands from stdin (slave mode) */
void terminal_setup_stdin_cmd_input(struct input_ctx *ictx);
/* Setup ictx to read keys from the terminal */
void terminal_setup_getch(struct input_ctx *ictx);
/* Return whether the process has been backgrounded. */
bool terminal_in_background(void);
@@ -54,18 +62,4 @@ void getch2_disable(void);
/* Enable and disable STDIN line-buffering */
void getch2_poll(void);
/* Read a character or a special key code (see keycodes.h) */
struct input_ctx;
bool getch2(struct input_ctx *ictx);
#if defined(__MINGW32__)
// slave cmd function for Windows
int mp_input_slave_cmd_func(int fd,char* dest,int size);
#define USE_FD0_CMD_SELECT 0
#define MP_INPUT_SLAVE_CMD_FUNC mp_input_slave_cmd_func
#else
#define USE_FD0_CMD_SELECT 1
#define MP_INPUT_SLAVE_CMD_FUNC NULL
#endif
#endif /* MPLAYER_GETCH2_H */