From e8b073584d749bb864f495d9e1cd31b102c6283d Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 30 Apr 2018 20:33:05 +0200 Subject: input: remove some explicit uses of command IDs The plan is to remove the command ID enum. This will happen by replacing the big switch statement in command.c with dispatching to per-command callbacks. As preparation, remove uses of the command IDs outside of the actual dispatching mechanism. Also remove some instances of checking cmd->def for NULL. We now require this always to be set. --- input/cmd_list.c | 15 +++++---------- input/cmd_list.h | 3 +++ input/cmd_parse.c | 18 +++++++++--------- input/cmd_parse.h | 2 ++ input/input.c | 2 +- player/command.c | 28 +++++++++++++++++----------- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/input/cmd_list.c b/input/cmd_list.c index 936cef9125..23a5da774e 100644 --- a/input/cmd_list.c +++ b/input/cmd_list.c @@ -30,15 +30,11 @@ // 0: no, 1: maybe, 2: sure static int is_abort_cmd(struct mp_cmd *cmd) { - switch (cmd->id) { - case MP_CMD_QUIT: - case MP_CMD_QUIT_WATCH_LATER: - case MP_CMD_STOP: + if (cmd->def->is_abort) return 2; - case MP_CMD_PLAYLIST_NEXT: - case MP_CMD_PLAYLIST_PREV: + if (cmd->def->is_soft_abort) return 1; - case MP_CMD_COMMAND_LIST:; + if (cmd->def == &mp_cmd_list) { int r = 0; for (struct mp_cmd *sub = cmd->args[0].v.p; sub; sub = sub->queue_next) { int x = is_abort_cmd(sub); @@ -61,14 +57,13 @@ bool mp_input_is_abort_cmd(struct mp_cmd *cmd) bool mp_input_is_repeatable_cmd(struct mp_cmd *cmd) { - return (cmd->def && cmd->def->allow_auto_repeat) || - cmd->id == MP_CMD_COMMAND_LIST || + return (cmd->def->allow_auto_repeat) || cmd->def == &mp_cmd_list || (cmd->flags & MP_ALLOW_REPEAT); } bool mp_input_is_scalable_cmd(struct mp_cmd *cmd) { - return cmd->def && cmd->def->scalable; + return cmd->def->scalable; } void mp_print_cmd_list(struct mp_log *out) diff --git a/input/cmd_list.h b/input/cmd_list.h index 441410a570..c6d7c66100 100644 --- a/input/cmd_list.h +++ b/input/cmd_list.h @@ -33,6 +33,9 @@ struct mp_cmd_def { bool on_updown; // always emit it on both up and down key events bool vararg; // last argument can be given 0 to multiple times bool scalable; + bool is_abort; + bool is_soft_abort; + bool is_ignore; }; extern const struct mp_cmd_def mp_cmds[]; diff --git a/input/cmd_parse.c b/input/cmd_parse.c index da595d4963..ae518fdf98 100644 --- a/input/cmd_parse.c +++ b/input/cmd_parse.c @@ -28,6 +28,11 @@ #include "libmpv/client.h" +const struct mp_cmd_def mp_cmd_list = { + .id = MP_CMD_COMMAND_LIST, + .name = "list", +}; + static void destroy_cmd(void *ptr) { struct mp_cmd *cmd = ptr; @@ -314,11 +319,6 @@ 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,9 +341,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 = list_def.id, - .name = (char *)list_def.name, - .def = &list_def, + .id = mp_cmd_list.id, + .name = (char *)mp_cmd_list.name, + .def = &mp_cmd_list, .original = bstrdup(list, original), }; talloc_steal(list, cmd); @@ -407,7 +407,7 @@ mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd) ret->original = bstrdup(ret, cmd->original); ret->key_name = talloc_strdup(ret, ret->key_name); - if (cmd->id == MP_CMD_COMMAND_LIST) { + if (cmd->def == &mp_cmd_list) { struct mp_cmd *prev = NULL; for (struct mp_cmd *sub = cmd->args[0].v.p; sub; sub = sub->queue_next) { sub = mp_cmd_clone(sub); diff --git a/input/cmd_parse.h b/input/cmd_parse.h index 295aa3b2e7..13a055ff13 100644 --- a/input/cmd_parse.h +++ b/input/cmd_parse.h @@ -24,6 +24,8 @@ struct mp_log; struct mp_cmd; struct mpv_node; +extern const struct mp_cmd_def mp_cmd_list; + // Parse text and return corresponding struct mp_cmd. // The location parameter is for error messages. struct mp_cmd *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc); diff --git a/input/input.c b/input/input.c index 616ce3450c..03771dc2bb 100644 --- a/input/input.c +++ b/input/input.c @@ -547,7 +547,7 @@ static struct mp_cmd *resolve_key(struct input_ctx *ictx, int code) update_mouse_section(ictx); struct mp_cmd *cmd = get_cmd_from_keys(ictx, NULL, code); key_buf_add(ictx->key_history, code); - if (cmd && cmd->id != MP_CMD_IGNORE && !should_drop_cmd(ictx, cmd)) + if (cmd && !cmd->def->is_ignore && !should_drop_cmd(ictx, cmd)) return cmd; talloc_free(cmd); return NULL; diff --git a/player/command.c b/player/command.c index c35cbec06a..c820a1ceb5 100644 --- a/player/command.c +++ b/player/command.c @@ -4830,7 +4830,7 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re int osdl = msg_osd ? 1 : OSD_LEVEL_INVISIBLE; bool async = cmd->flags & MP_ASYNC_CMD; - mp_cmd_dump(mpctx->log, cmd->id == MP_CMD_IGNORE ? MSGL_TRACE : MSGL_DEBUG, + mp_cmd_dump(mpctx->log, cmd->def->is_ignore ? MSGL_TRACE : MSGL_DEBUG, "Run command:", cmd); if (cmd->flags & MP_EXPAND_PROPERTIES) { @@ -5645,7 +5645,7 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re #define OARG_CYCLEDIR(def) OPT_CYCLEDIR(ARG(d), 0, OPTDEF_DOUBLE(def)) const struct mp_cmd_def mp_cmds[] = { - { MP_CMD_IGNORE, "ignore", }, + { MP_CMD_IGNORE, "ignore", .is_ignore = true }, { MP_CMD_SEEK, "seek", { ARG_TIME, @@ -5666,20 +5666,26 @@ const struct mp_cmd_def mp_cmds[] = { { MP_CMD_REVERT_SEEK, "revert-seek", { OARG_FLAGS(0, ({"mark", 1})), }}, - { MP_CMD_QUIT, "quit", { OARG_INT(0) } }, - { MP_CMD_QUIT_WATCH_LATER, "quit-watch-later", { OARG_INT(0) } }, - { MP_CMD_STOP, "stop", }, + { MP_CMD_QUIT, "quit", { OARG_INT(0) }, + .is_abort = true }, + { MP_CMD_QUIT_WATCH_LATER, "quit-watch-later", { OARG_INT(0) }, + .is_abort = true }, + { MP_CMD_STOP, "stop", .is_abort = 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}, - {"force", 1})), - }}, + OARG_CHOICE(0, ({"weak", 0}, + {"force", 1})), + }, + .is_soft_abort = true, + }, { MP_CMD_PLAYLIST_PREV, "playlist-prev", { - OARG_CHOICE(0, ({"weak", 0}, - {"force", 1})), - }}, + OARG_CHOICE(0, ({"weak", 0}, + {"force", 1})), + }, + .is_soft_abort = true, + }, { MP_CMD_PLAYLIST_SHUFFLE, "playlist-shuffle", }, { MP_CMD_SUB_STEP, "sub-step", { ARG_INT }, .allow_auto_repeat = true }, { MP_CMD_SUB_SEEK, "sub-seek", { ARG_INT }, .allow_auto_repeat = true }, -- cgit v1.2.3