diff options
author | wm4 <wm4@nowhere> | 2018-05-01 02:38:59 +0200 |
---|---|---|
committer | Jan Ekström <jeebjp@gmail.com> | 2018-05-03 01:20:01 +0300 |
commit | fb9bbf2a0d7f8dba0d0674595cc2dc0dab68ad94 (patch) | |
tree | b386f221e961ad81e35a3bc23bb66320ba942b80 /input | |
parent | e8b073584d749bb864f495d9e1cd31b102c6283d (diff) | |
download | mpv-fb9bbf2a0d7f8dba0d0674595cc2dc0dab68ad94.tar.bz2 mpv-fb9bbf2a0d7f8dba0d0674595cc2dc0dab68ad94.tar.xz |
command: split big command handler switch into separate functions
This gets rid of run_command() and its big switch statement, which was
an idiotically big function of almost 1000 lines.
The switch is replaced with a callback per command, and each command is
now implemented in its own function. Command IDs are not needed anymore,
so the mp_command_type enum disappears.
There should be no functional changes, but since this refactors 64
commands, regressions are possible.
The handler() parameter is void*, because in theory the input code is
supposed to be independent of the player core code. For example, you
should be able to reuse the command parser code for some other part of
mpv. In practice, the variable containing command list is defined in the
player core anyway, so you could say this doesn't work. But I'm still
trying to hold onto this idea, so I went with void*.
Diffstat (limited to 'input')
-rw-r--r-- | input/cmd_list.h | 92 | ||||
-rw-r--r-- | input/cmd_parse.c | 3 |
2 files changed, 4 insertions, 91 deletions
diff --git a/input/cmd_list.h b/input/cmd_list.h index c6d7c66100..5f44602907 100644 --- a/input/cmd_list.h +++ b/input/cmd_list.h @@ -25,10 +25,13 @@ #define MP_CMD_OPT_ARG 0x1000 +struct mp_cmd_ctx; + struct mp_cmd_def { - int id; // one of MP_CMD_... const char *name; // user-visible name (as used in input.conf) + void (*handler)(void *ctx); const struct m_option args[MP_CMD_DEF_MAX_ARGS]; + const void *priv; // for free use by handler() 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 @@ -40,93 +43,6 @@ struct mp_cmd_def { extern const struct mp_cmd_def mp_cmds[]; -// All command IDs -enum mp_command_type { - MP_CMD_IGNORE, - MP_CMD_SEEK, - MP_CMD_REVERT_SEEK, - MP_CMD_QUIT, - MP_CMD_QUIT_WATCH_LATER, - MP_CMD_PLAYLIST_NEXT, - MP_CMD_PLAYLIST_PREV, - MP_CMD_SCREENSHOT, - MP_CMD_SCREENSHOT_TO_FILE, - MP_CMD_SCREENSHOT_RAW, - MP_CMD_LOADFILE, - MP_CMD_LOADLIST, - MP_CMD_PLAYLIST_CLEAR, - MP_CMD_PLAYLIST_REMOVE, - MP_CMD_PLAYLIST_MOVE, - MP_CMD_PLAYLIST_SHUFFLE, - MP_CMD_SUB_STEP, - MP_CMD_SUB_SEEK, - MP_CMD_TV_LAST_CHANNEL, - MP_CMD_FRAME_STEP, - MP_CMD_FRAME_BACK_STEP, - MP_CMD_RUN, - MP_CMD_SUB_ADD, - MP_CMD_SUB_REMOVE, - MP_CMD_SUB_RELOAD, - MP_CMD_SET, - MP_CMD_CHANGE_LIST, - MP_CMD_PRINT_TEXT, - MP_CMD_SHOW_TEXT, - MP_CMD_EXPAND_TEXT, - MP_CMD_SHOW_PROGRESS, - MP_CMD_ADD, - MP_CMD_CYCLE, - MP_CMD_MULTIPLY, - MP_CMD_CYCLE_VALUES, - MP_CMD_STOP, - MP_CMD_AUDIO_ADD, - MP_CMD_AUDIO_REMOVE, - MP_CMD_AUDIO_RELOAD, - - MP_CMD_ENABLE_INPUT_SECTION, - MP_CMD_DISABLE_INPUT_SECTION, - MP_CMD_DEFINE_INPUT_SECTION, - - MP_CMD_AB_LOOP, - - MP_CMD_DROP_BUFFERS, - - MP_CMD_MOUSE, - MP_CMD_KEYPRESS, - MP_CMD_KEYDOWN, - MP_CMD_KEYUP, - - /// Audio Filter commands - MP_CMD_AF, - MP_CMD_AF_COMMAND, - MP_CMD_AO_RELOAD, - - /// Video filter commands - MP_CMD_VF, - MP_CMD_VF_COMMAND, - - /// Internal for Lua scripts - MP_CMD_SCRIPT_BINDING, - MP_CMD_SCRIPT_MESSAGE, - MP_CMD_SCRIPT_MESSAGE_TO, - - MP_CMD_OVERLAY_ADD, - MP_CMD_OVERLAY_REMOVE, - - MP_CMD_WRITE_WATCH_LATER_CONFIG, - - MP_CMD_HOOK_ADD, - MP_CMD_HOOK_ACK, - - MP_CMD_RESCAN_EXTERNAL_FILES, - - MP_CMD_APPLY_PROFILE, - - MP_CMD_LOAD_SCRIPT, - - // Internal - MP_CMD_COMMAND_LIST, // list of sub-commands in args[0].v.p -}; - // Executing this command will maybe abort playback (play something else, or quit). struct mp_cmd; bool mp_input_is_maybe_abort_cmd(struct mp_cmd *cmd); diff --git a/input/cmd_parse.c b/input/cmd_parse.c index ae518fdf98..f401e5b9c7 100644 --- a/input/cmd_parse.c +++ b/input/cmd_parse.c @@ -29,7 +29,6 @@ #include "libmpv/client.h" const struct mp_cmd_def mp_cmd_list = { - .id = MP_CMD_COMMAND_LIST, .name = "list", }; @@ -87,7 +86,6 @@ static bool find_cmd(struct mp_log *log, struct mp_cmd *cmd, bstr name) if (strcmp(nname, mp_cmds[n].name) == 0) { cmd->def = &mp_cmds[n]; cmd->name = (char *)cmd->def->name; - cmd->id = cmd->def->id; return true; } } @@ -341,7 +339,6 @@ 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_list.id, .name = (char *)mp_cmd_list.name, .def = &mp_cmd_list, .original = bstrdup(list, original), |