mirror of https://github.com/mpv-player/mpv
input: simplify
This commit is contained in:
parent
0c5fd5a047
commit
d33ae93b89
|
@ -73,7 +73,8 @@ const struct mp_cmd_def mp_cmds[] = {
|
|||
{ MP_CMD_QUIT, "quit", { OARG_INT(0) } },
|
||||
{ MP_CMD_QUIT_WATCH_LATER, "quit_watch_later", { OARG_INT(0) } },
|
||||
{ MP_CMD_STOP, "stop", },
|
||||
{ MP_CMD_FRAME_STEP, "frame_step", .allow_auto_repeat = true },
|
||||
{ MP_CMD_FRAME_STEP, "frame_step", .allow_auto_repeat = true,
|
||||
.on_updown = true },
|
||||
{ MP_CMD_FRAME_BACK_STEP, "frame_back_step", .allow_auto_repeat = true },
|
||||
{ MP_CMD_PLAYLIST_NEXT, "playlist_next", {
|
||||
OARG_CHOICE(0, ({"weak", 0}, {"0", 0},
|
||||
|
@ -167,7 +168,7 @@ const struct mp_cmd_def mp_cmds[] = {
|
|||
{ MP_CMD_VO_CMDLINE, "vo_cmdline", { ARG_STRING } },
|
||||
|
||||
{ MP_CMD_SCRIPT_BINDING, "script_binding", { ARG_STRING },
|
||||
.allow_auto_repeat = true},
|
||||
.allow_auto_repeat = true, .on_updown = true},
|
||||
|
||||
{ MP_CMD_SCRIPT_MESSAGE, "script_message", { ARG_STRING }, .vararg = true },
|
||||
{ MP_CMD_SCRIPT_MESSAGE_TO, "script_message_to", { ARG_STRING, ARG_STRING },
|
||||
|
|
|
@ -31,6 +31,7 @@ struct mp_cmd_def {
|
|||
const char *name; // user-visible name (as used in input.conf)
|
||||
const struct m_option args[MP_CMD_MAX_ARGS];
|
||||
bool allow_auto_repeat; // react to repeated key events
|
||||
bool on_updown; // always emit it on both up and down key events
|
||||
bool vararg; // last argument can be given 0 to multiple times
|
||||
};
|
||||
|
||||
|
|
|
@ -319,6 +319,11 @@ error:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static struct mp_cmd_def list_def = {
|
||||
.id = MP_CMD_COMMAND_LIST,
|
||||
.name = "list",
|
||||
};
|
||||
|
||||
mp_cmd_t *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc)
|
||||
{
|
||||
void *tmp = talloc_new(NULL);
|
||||
|
@ -341,8 +346,9 @@ mp_cmd_t *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc)
|
|||
struct mp_cmd *list = talloc_ptrtype(NULL, list);
|
||||
talloc_set_destructor(list, destroy_cmd);
|
||||
*list = (struct mp_cmd) {
|
||||
.id = MP_CMD_COMMAND_LIST,
|
||||
.name = "list",
|
||||
.id = list_def.id,
|
||||
.name = (char *)list_def.name,
|
||||
.def = &list_def,
|
||||
.original = bstrdup(list, original),
|
||||
};
|
||||
talloc_steal(list, cmd);
|
||||
|
|
|
@ -116,7 +116,6 @@ struct input_ctx {
|
|||
// key code of the last key that triggered MP_KEY_STATE_DOWN
|
||||
int last_key_down;
|
||||
int64_t last_key_down_time;
|
||||
bool current_down_cmd_need_release;
|
||||
struct mp_cmd *current_down_cmd;
|
||||
|
||||
int last_doubleclick_key_down;
|
||||
|
@ -512,38 +511,22 @@ static void update_mouse_section(struct input_ctx *ictx)
|
|||
// thinking a key is still held down.
|
||||
static void release_down_cmd(struct input_ctx *ictx, bool drop_current)
|
||||
{
|
||||
if (ictx->current_down_cmd_need_release)
|
||||
drop_current = false;
|
||||
if (!drop_current && ictx->current_down_cmd &&
|
||||
ictx->current_down_cmd->key_up_follows)
|
||||
if (ictx->current_down_cmd && ictx->current_down_cmd->emit_on_up &&
|
||||
(!drop_current || ictx->current_down_cmd->def->on_updown))
|
||||
{
|
||||
memset(ictx->key_history, 0, sizeof(ictx->key_history));
|
||||
ictx->current_down_cmd->key_up_follows = false;
|
||||
ictx->current_down_cmd->is_up = true;
|
||||
mp_input_queue_cmd(ictx, ictx->current_down_cmd);
|
||||
} else {
|
||||
talloc_free(ictx->current_down_cmd);
|
||||
}
|
||||
ictx->current_down_cmd = NULL;
|
||||
ictx->current_down_cmd_need_release = false;
|
||||
ictx->last_key_down = 0;
|
||||
ictx->last_key_down_time = 0;
|
||||
ictx->ar_state = -1;
|
||||
update_mouse_section(ictx);
|
||||
}
|
||||
|
||||
// Whether a command shall be sent on both key down and key up events.
|
||||
static bool key_updown_ok(enum mp_command_type cmd)
|
||||
{
|
||||
switch (cmd) {
|
||||
case MP_CMD_SCRIPT_BINDING:
|
||||
case MP_CMD_FRAME_STEP:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// We don't want the append to the command queue indefinitely, because that
|
||||
// could lead to situations where recovery would take too long. On the other
|
||||
// hand, don't drop commands that will abort playback.
|
||||
|
@ -594,14 +577,12 @@ static void interpret_key(struct input_ctx *ictx, int code, double scale)
|
|||
cmd = resolve_key(ictx, code);
|
||||
if (cmd) {
|
||||
cmd->is_up_down = true;
|
||||
cmd->key_up_follows = (code & MP_KEY_EMIT_ON_UP) |
|
||||
key_updown_ok(cmd->id);
|
||||
cmd->emit_on_up = (code & MP_KEY_EMIT_ON_UP) || cmd->def->on_updown;
|
||||
ictx->current_down_cmd = mp_cmd_clone(cmd);
|
||||
}
|
||||
ictx->last_key_down = code;
|
||||
ictx->last_key_down_time = mp_time_us();
|
||||
ictx->ar_state = 0;
|
||||
ictx->current_down_cmd = mp_cmd_clone(cmd);
|
||||
ictx->current_down_cmd_need_release = false;
|
||||
mp_input_wakeup(ictx); // possibly start timer for autorepeat
|
||||
} else if (state == MP_KEY_STATE_UP) {
|
||||
// Most VOs send RELEASE_ALL anyway
|
||||
|
@ -621,7 +602,7 @@ static void interpret_key(struct input_ctx *ictx, int code, double scale)
|
|||
// Don't emit a command on key-down if the key is designed to emit commands
|
||||
// on key-up (like mouse buttons). Also, if the command specifically should
|
||||
// be sent both on key down and key up, still emit the command.
|
||||
if (cmd->key_up_follows && !key_updown_ok(cmd->id)) {
|
||||
if (cmd->emit_on_up && !cmd->def->on_updown) {
|
||||
talloc_free(cmd);
|
||||
return;
|
||||
}
|
||||
|
@ -629,9 +610,6 @@ static void interpret_key(struct input_ctx *ictx, int code, double scale)
|
|||
memset(ictx->key_history, 0, sizeof(ictx->key_history));
|
||||
|
||||
cmd->scale = scale;
|
||||
|
||||
if (cmd->key_up_follows)
|
||||
ictx->current_down_cmd_need_release = true;
|
||||
mp_input_queue_cmd(ictx, cmd);
|
||||
}
|
||||
|
||||
|
@ -835,6 +813,7 @@ static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
|
|||
(ictx->last_key_down & MP_NO_REPEAT_KEY) ||
|
||||
!mp_input_is_repeatable_cmd(ictx->current_down_cmd))
|
||||
ictx->ar_state = -1; // disable
|
||||
|
||||
if (ictx->ar_state >= 0) {
|
||||
int64_t t = mp_time_us();
|
||||
if (ictx->last_ar + 2000000 < t)
|
||||
|
@ -845,13 +824,16 @@ static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
|
|||
{
|
||||
ictx->ar_state = 1;
|
||||
ictx->last_ar = ictx->last_key_down_time + opts->ar_delay * 1000;
|
||||
return mp_cmd_clone(ictx->current_down_cmd);
|
||||
// Then send rate / sec event
|
||||
} else if (ictx->ar_state == 1
|
||||
&& (t - ictx->last_ar) >= 1000000 / opts->ar_rate) {
|
||||
ictx->last_ar += 1000000 / opts->ar_rate;
|
||||
return mp_cmd_clone(ictx->current_down_cmd);
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
struct mp_cmd *ret = mp_cmd_clone(ictx->current_down_cmd);
|
||||
ret->repeated = true;
|
||||
return ret;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -889,11 +871,8 @@ mp_cmd_t *mp_input_read_cmd(struct input_ctx *ictx)
|
|||
{
|
||||
input_lock(ictx);
|
||||
struct mp_cmd *ret = queue_remove_head(&ictx->cmd_queue);
|
||||
if (!ret) {
|
||||
if (!ret)
|
||||
ret = check_autorepeat(ictx);
|
||||
if (ret)
|
||||
ret->repeated = true;
|
||||
}
|
||||
if (ret && ret->mouse_move) {
|
||||
ictx->mouse_x = ret->mouse_x;
|
||||
ictx->mouse_y = ret->mouse_y;
|
||||
|
|
|
@ -78,8 +78,8 @@ typedef struct mp_cmd {
|
|||
char *input_section;
|
||||
bool is_up_down : 1;
|
||||
bool is_up : 1;
|
||||
bool emit_on_up : 1;
|
||||
bool is_mouse_button : 1;
|
||||
bool key_up_follows : 1;
|
||||
bool repeated : 1;
|
||||
bool mouse_move : 1;
|
||||
int mouse_x, mouse_y;
|
||||
|
|
Loading…
Reference in New Issue