summaryrefslogtreecommitdiffstats
path: root/input
diff options
context:
space:
mode:
Diffstat (limited to 'input')
-rw-r--r--input/cmd_list.c281
-rw-r--r--input/cmd_list.h118
-rw-r--r--input/cmd_parse.c410
-rw-r--r--input/cmd_parse.h52
-rw-r--r--input/input.c981
-rw-r--r--input/input.h102
-rw-r--r--input/keycodes.c321
-rw-r--r--input/keycodes.h18
8 files changed, 1234 insertions, 1049 deletions
diff --git a/input/cmd_list.c b/input/cmd_list.c
new file mode 100644
index 0000000000..05dcb72d25
--- /dev/null
+++ b/input/cmd_list.c
@@ -0,0 +1,281 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <limits.h>
+
+#include "common/common.h"
+#include "common/msg.h"
+#include "options/m_option.h"
+
+#include "input.h"
+#include "cmd_list.h"
+#include "cmd_parse.h"
+
+// This does not specify the real destination of the command parameter values,
+// it just provides a dummy for the OPT_ macros.
+#define OPT_BASE_STRUCT struct mp_cmd_arg
+#define ARG(t) "", v. t
+
+/* This array defines all known commands.
+ * The first field is an id used to recognize the command.
+ * The second is the command name used in slave mode and input.conf.
+ * Then comes the definition of each argument, first mandatory arguments
+ * (ARG_INT, ARG_FLOAT, ARG_STRING) if any, then optional arguments
+ * (OARG_INT(default), etc) if any. The command will be given the default
+ * argument value if the user didn't give enough arguments to specify it.
+ * A command can take a maximum of MP_CMD_MAX_ARGS arguments.
+ */
+
+#define ARG_INT OPT_INT(ARG(i), 0)
+#define ARG_FLOAT OPT_FLOAT(ARG(f), 0)
+#define ARG_DOUBLE OPT_DOUBLE(ARG(d), 0)
+#define ARG_STRING OPT_STRING(ARG(s), 0)
+#define ARG_CHOICE(c) OPT_CHOICE(ARG(i), 0, c)
+#define ARG_CHOICE_OR_INT(...) OPT_CHOICE_OR_INT(ARG(i), 0, __VA_ARGS__)
+#define ARG_TIME OPT_TIME(ARG(d), 0)
+#define OARG_DOUBLE(def) OPT_DOUBLE(ARG(d), 0, OPTDEF_DOUBLE(def))
+#define OARG_INT(def) OPT_INT(ARG(i), 0, OPTDEF_INT(def))
+#define OARG_CHOICE(def, c) OPT_CHOICE(ARG(i), 0, c, OPTDEF_INT(def))
+
+#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_SEEK, "seek", {
+ ARG_TIME,
+ OARG_CHOICE(0, ({"relative", 0}, {"0", 0}, {"-", 0},
+ {"absolute-percent", 1}, {"1", 1},
+ {"absolute", 2}, {"2", 2})),
+ OARG_CHOICE(0, ({"default-precise", 0}, {"0", 0},
+ {"exact", 1}, {"1", 1},
+ {"keyframes", -1}, {"-1", -1})),
+ },
+ .allow_auto_repeat = true,
+ },
+ { MP_CMD_REVERT_SEEK, "revert_seek", },
+ { MP_CMD_QUIT, "quit", { OARG_INT(0) } },
+ { MP_CMD_QUIT_WATCH_LATER, "quit_watch_later", },
+ { MP_CMD_STOP, "stop", },
+ { MP_CMD_FRAME_STEP, "frame_step", .allow_auto_repeat = 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},
+ {"force", 1}, {"1", 1})),
+ }},
+ { MP_CMD_PLAYLIST_PREV, "playlist_prev", {
+ OARG_CHOICE(0, ({"weak", 0}, {"0", 0},
+ {"force", 1}, {"1", 1})),
+ }},
+ { MP_CMD_SUB_STEP, "sub_step", { ARG_INT }, .allow_auto_repeat = true },
+ { MP_CMD_SUB_SEEK, "sub_seek", { ARG_INT }, .allow_auto_repeat = true },
+ { MP_CMD_OSD, "osd", { OARG_INT(-1) } },
+ { MP_CMD_PRINT_TEXT, "print_text", { ARG_STRING }, .allow_auto_repeat = true },
+ { MP_CMD_SHOW_TEXT, "show_text", { ARG_STRING, OARG_INT(-1), OARG_INT(0) },
+ .allow_auto_repeat = true},
+ { MP_CMD_SHOW_PROGRESS, "show_progress", .allow_auto_repeat = true},
+ { MP_CMD_SUB_ADD, "sub_add", { ARG_STRING } },
+ { MP_CMD_SUB_REMOVE, "sub_remove", { OARG_INT(-1) } },
+ { MP_CMD_SUB_RELOAD, "sub_reload", { OARG_INT(-1) } },
+
+ { MP_CMD_TV_START_SCAN, "tv_start_scan", },
+ { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", { ARG_INT } },
+ { MP_CMD_TV_STEP_NORM, "tv_step_norm", },
+ { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", },
+ { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", { ARG_STRING } },
+ { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", },
+ { MP_CMD_TV_SET_FREQ, "tv_set_freq", { ARG_FLOAT } },
+ { MP_CMD_TV_STEP_FREQ, "tv_step_freq", { ARG_FLOAT } },
+ { MP_CMD_TV_SET_NORM, "tv_set_norm", { ARG_STRING } },
+
+ { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", { ARG_INT, ARG_INT } },
+
+ { MP_CMD_SCREENSHOT, "screenshot", {
+ OARG_CHOICE(2, ({"video", 0},
+ {"window", 1},
+ {"subtitles", 2}, {"-", 2})),
+ OARG_CHOICE(0, ({"single", 0},
+ {"each-frame", 1})),
+ }},
+ { MP_CMD_SCREENSHOT_TO_FILE, "screenshot_to_file", {
+ ARG_STRING,
+ OARG_CHOICE(2, ({"video", 0},
+ {"window", 1},
+ {"subtitles", 2})),
+ }},
+ { MP_CMD_LOADFILE, "loadfile", {
+ ARG_STRING,
+ OARG_CHOICE(0, ({"replace", 0}, {"0", 0},
+ {"append", 1}, {"1", 1})),
+ }},
+ { MP_CMD_LOADLIST, "loadlist", {
+ ARG_STRING,
+ OARG_CHOICE(0, ({"replace", 0}, {"0", 0},
+ {"append", 1}, {"1", 1})),
+ }},
+ { MP_CMD_PLAYLIST_CLEAR, "playlist_clear", },
+ { MP_CMD_PLAYLIST_REMOVE, "playlist_remove", {
+ ARG_CHOICE_OR_INT(0, INT_MAX, ({"current", -1})),
+ }},
+ { MP_CMD_PLAYLIST_MOVE, "playlist_move", { ARG_INT, ARG_INT } },
+ { MP_CMD_RUN, "run", { ARG_STRING, ARG_STRING }, .vararg = true },
+
+ { MP_CMD_SET, "set", { ARG_STRING, ARG_STRING } },
+ { MP_CMD_GET_PROPERTY, "get_property", { ARG_STRING } },
+ { MP_CMD_ADD, "add", { ARG_STRING, OARG_DOUBLE(0) },
+ .allow_auto_repeat = true},
+ { MP_CMD_CYCLE, "cycle", {
+ ARG_STRING,
+ OARG_CYCLEDIR(1),
+ },
+ .allow_auto_repeat = true
+ },
+ { MP_CMD_MULTIPLY, "multiply", { ARG_STRING, ARG_DOUBLE },
+ .allow_auto_repeat = true},
+
+ { MP_CMD_CYCLE_VALUES, "cycle_values", { ARG_STRING, ARG_STRING, ARG_STRING },
+ .vararg = true, .allow_auto_repeat = true},
+
+ { MP_CMD_ENABLE_INPUT_SECTION, "enable_section", {
+ ARG_STRING,
+ OARG_CHOICE(0, ({"default", 0},
+ {"exclusive", 1})),
+ }},
+ { MP_CMD_DISABLE_INPUT_SECTION, "disable_section", { ARG_STRING } },
+
+ { MP_CMD_DVDNAV, "dvdnav", { ARG_STRING } },
+
+ { MP_CMD_AF, "af", { ARG_STRING, ARG_STRING } },
+
+ { MP_CMD_VF, "vf", { ARG_STRING, ARG_STRING } },
+
+ { MP_CMD_VO_CMDLINE, "vo_cmdline", { ARG_STRING } },
+
+ { MP_CMD_SCRIPT_DISPATCH, "script_dispatch", { ARG_STRING, ARG_INT } },
+
+ { MP_CMD_OVERLAY_ADD, "overlay_add",
+ { ARG_INT, ARG_INT, ARG_INT, ARG_STRING, ARG_INT, ARG_STRING, ARG_INT,
+ ARG_INT, ARG_INT }},
+ { MP_CMD_OVERLAY_REMOVE, "overlay_remove", { ARG_INT } },
+
+ {0}
+};
+
+#undef OPT_BASE_STRUCT
+#undef ARG
+
+// Map legacy commands to proper commands
+struct legacy_cmd {
+ const char *old, *new;
+};
+static const struct legacy_cmd legacy_cmds[] = {
+ {"loop", "cycle loop"},
+ {"seek_chapter", "add chapter"},
+ {"switch_angle", "cycle angle"},
+ {"pause", "cycle pause"},
+ {"volume", "add volume"},
+ {"mute", "cycle mute"},
+ {"audio_delay", "add audio-delay"},
+ {"switch_audio", "cycle audio"},
+ {"balance", "add balance"},
+ {"vo_fullscreen", "cycle fullscreen"},
+ {"panscan", "add panscan"},
+ {"vo_ontop", "cycle ontop"},
+ {"vo_border", "cycle border"},
+ {"frame_drop", "cycle framedrop"},
+ {"gamma", "add gamma"},
+ {"brightness", "add brightness"},
+ {"contrast", "add contrast"},
+ {"saturation", "add saturation"},
+ {"hue", "add hue"},
+ {"switch_vsync", "cycle vsync"},
+ {"sub_load", "sub_add"},
+ {"sub_select", "cycle sub"},
+ {"sub_pos", "add sub-pos"},
+ {"sub_delay", "add sub-delay"},
+ {"sub_visibility", "cycle sub-visibility"},
+ {"forced_subs_only", "cycle sub-forced-only"},
+ {"sub_scale", "add sub-scale"},
+ {"ass_use_margins", "cycle ass-use-margins"},
+ {"tv_set_brightness", "add tv-brightness"},
+ {"tv_set_hue", "add tv-hue"},
+ {"tv_set_saturation", "add tv-saturation"},
+ {"tv_set_contrast", "add tv-contrast"},
+ {"step_property_osd", "cycle"},
+ {"step_property", "no-osd cycle"},
+ {"set_property", "no-osd set"},
+ {"set_property_osd", "set"},
+ {"speed_set", "set speed"},
+ {"osd_show_text", "show_text"},
+ {"osd_show_property_text", "show_text"},
+ {"osd_show_progression", "show_progress"},
+ {"show_chapters_osd", "show_text ${chapter-list}"},
+ {"show_chapters", "show_text ${chapter-list}"},
+ {"show_tracks_osd", "show_text ${track-list}"},
+ {"show_tracks", "show_text ${track-list}"},
+ {"show_playlist", "show_text ${playlist}"},
+ {"speed_mult", "multiply speed"},
+
+ // Approximate (can fail if user added additional whitespace)
+ {"pt_step 1", "playlist_next"},
+ {"pt_step -1", "playlist_prev"},
+ // Switch_ratio without argument resets aspect ratio
+ {"switch_ratio ", "set aspect "},
+ {"switch_ratio", "set aspect 0"},
+ {0}
+};
+
+bool mp_replace_legacy_cmd(void *t, bstr *s)
+{
+ for (const struct legacy_cmd *entry = legacy_cmds; entry->old; entry++) {
+ bstr old = bstr0(entry->old);
+ if (bstrcasecmp(bstr_splice(*s, 0, old.len), old) == 0) {
+ bstr rest = bstr_cut(*s, old.len);
+ *s = bstr0(talloc_asprintf(t, "%s%.*s", entry->new, BSTR_P(rest)));
+ return true;
+ }
+ }
+ return false;
+}
+
+bool mp_input_is_abort_cmd(int cmd_id)
+{
+ switch (cmd_id) {
+ case MP_CMD_QUIT:
+ case MP_CMD_PLAYLIST_NEXT:
+ case MP_CMD_PLAYLIST_PREV:
+ return true;
+ }
+ return false;
+}
+
+void mp_print_cmd_list(struct mp_log *out)
+{
+ for (int i = 0; mp_cmds[i].name; i++) {
+ const struct mp_cmd_def *def = &mp_cmds[i];
+ mp_info(out, "%-20.20s", def->name);
+ for (int j = 0; j < MP_CMD_MAX_ARGS && def->args[j].type; j++) {
+ const char *type = def->args[j].type->name;
+ if (def->args[j].defval)
+ mp_info(out, " [%s]", type);
+ else
+ mp_info(out, " %s", type);
+ }
+ mp_info(out, "\n");
+ }
+}
diff --git a/input/cmd_list.h b/input/cmd_list.h
new file mode 100644
index 0000000000..211df2b0b0
--- /dev/null
+++ b/input/cmd_list.h
@@ -0,0 +1,118 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MP_COMMAND_LIST_H
+#define MP_COMMAND_LIST_H
+
+#include <stdbool.h>
+#include "options/m_option.h"
+
+#define MP_CMD_MAX_ARGS 10
+
+struct mp_cmd_def {
+ int id; // one of MP_CMD_...
+ 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 vararg; // last argument can be given 0 to multiple times
+};
+
+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_OSD,
+ MP_CMD_TV_STEP_CHANNEL,
+ MP_CMD_TV_STEP_NORM,
+ MP_CMD_TV_STEP_CHANNEL_LIST,
+ MP_CMD_SCREENSHOT,
+ MP_CMD_SCREENSHOT_TO_FILE,
+ MP_CMD_LOADFILE,
+ MP_CMD_LOADLIST,
+ MP_CMD_PLAYLIST_CLEAR,
+ MP_CMD_PLAYLIST_REMOVE,
+ MP_CMD_PLAYLIST_MOVE,
+ MP_CMD_SUB_STEP,
+ MP_CMD_SUB_SEEK,
+ MP_CMD_TV_SET_CHANNEL,
+ MP_CMD_TV_LAST_CHANNEL,
+ MP_CMD_TV_SET_FREQ,
+ MP_CMD_TV_SET_NORM,
+ 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_GET_PROPERTY,
+ MP_CMD_PRINT_TEXT,
+ MP_CMD_SHOW_TEXT,
+ MP_CMD_SHOW_PROGRESS,
+ MP_CMD_ADD,
+ MP_CMD_CYCLE,
+ MP_CMD_MULTIPLY,
+ MP_CMD_CYCLE_VALUES,
+ MP_CMD_TV_STEP_FREQ,
+ MP_CMD_TV_START_SCAN,
+ MP_CMD_STOP,
+
+ MP_CMD_ENABLE_INPUT_SECTION,
+ MP_CMD_DISABLE_INPUT_SECTION,
+
+ MP_CMD_DVDNAV,
+
+ /// DVB commands
+ MP_CMD_DVB_SET_CHANNEL,
+
+ /// Audio Filter commands
+ MP_CMD_AF,
+
+ /// Video filter commands
+ MP_CMD_VF,
+
+ /// Video output commands
+ MP_CMD_VO_CMDLINE,
+
+ /// Internal for Lua scripts
+ MP_CMD_SCRIPT_DISPATCH,
+
+ MP_CMD_OVERLAY_ADD,
+ MP_CMD_OVERLAY_REMOVE,
+
+ // Internal
+ MP_CMD_COMMAND_LIST, // list of sub-commands in args[0].v.p
+};
+
+// Executing this command will abort playback (play something else, or quit).
+bool mp_input_is_abort_cmd(int cmd_id);
+
+struct bstr;
+bool mp_replace_legacy_cmd(void *talloc_ctx, struct bstr *s);
+
+struct mp_log;
+void mp_print_cmd_list(struct mp_log *out);
+
+#endif
diff --git a/input/cmd_parse.c b/input/cmd_parse.c
new file mode 100644
index 0000000000..c9a70035fc
--- /dev/null
+++ b/input/cmd_parse.c
@@ -0,0 +1,410 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stddef.h>
+
+#include "bstr/bstr.h"
+#include "common/common.h"
+#include "common/msg.h"
+#include "options/m_option.h"
+
+#include "cmd_parse.h"
+#include "cmd_list.h"
+#include "input.h"
+
+static bool read_token(bstr str, bstr *out_rest, bstr *out_token)
+{
+ bstr t = bstr_lstrip(str);
+ char nextc = t.len > 0 ? t.start[0] : 0;
+ if (nextc == '#' || nextc == ';')
+ return false; // comment or command separator
+ int next = bstrcspn(t, WHITESPACE);
+ if (!next)
+ return false;
+ *out_token = bstr_splice(t, 0, next);
+ *out_rest = bstr_cut(t, next);
+ return true;
+}
+
+static bool read_escaped_string(void *talloc_ctx, bstr *str, bstr *literal)
+{
+ bstr t = *str;
+ char *new = talloc_strdup(talloc_ctx, "");
+ while (t.len) {
+ if (t.start[0] == '"')
+ break;
+ if (t.start[0] == '\\') {
+ t = bstr_cut(t, 1);
+ if (!mp_parse_escape(&t, &new))
+ goto error;
+ } else {
+ new = talloc_strndup_append_buffer(new, t.start, 1);
+ t = bstr_cut(t, 1);
+ }
+ }
+ int len = str->len - t.len;
+ *literal = new ? bstr0(new) : bstr_splice(*str, 0, len);
+ *str = bstr_cut(*str, len);
+ return true;
+error:
+ talloc_free(new);
+ return false;
+}
+
+// Somewhat awkward; the main purpose is supporting both strings and
+// pre-split string arrays as input.
+struct parse_ctx {
+ struct mp_log *log;
+ const char *loc;
+ void *tmp;
+ bool array_input; // false: use start/str, true: use num_strs/strs
+ bstr start, str;
+ bstr *strs;
+ int num_strs;
+};
+
+static int pctx_read_token(struct parse_ctx *ctx, bstr *out)
+{
+ *out = (bstr){0};
+ if (ctx->array_input) {
+ if (!ctx->num_strs)
+ return 0;
+ *out = ctx->strs[0];
+ ctx->strs++;
+ ctx->num_strs--;
+ return 1;
+ } else {
+ ctx->str = bstr_lstrip(ctx->str);
+ bstr start = ctx->str;
+ if (bstr_eatstart0(&ctx->str, "\"")) {
+ if (!read_escaped_string(ctx->tmp, &ctx->str, out)) {
+ MP_ERR(ctx, "Broken string escapes: ...>%.*s<.\n", BSTR_P(start));
+ return -1;
+ }
+ if (!bstr_eatstart0(&ctx->str, "\"")) {
+ MP_ERR(ctx, "Unterminated quotes: ...>%.*s<.\n", BSTR_P(start));
+ return -1;
+ }
+ return 1;
+ }
+ return read_token(ctx->str, &ctx->str, out) ? 1 : 0;
+ }
+}
+
+static bstr pctx_get_trailing(struct parse_ctx *ctx)
+{
+ if (ctx->array_input) {
+ if (ctx->num_strs == 0)
+ return (bstr){0};
+ return ctx->strs[0]; // mentioning the first trailing arg is enough?
+ } else {
+ bstr dummy;
+ if (!read_token(ctx->str, &dummy, &dummy))
+ return (bstr){0};
+ return ctx->str;
+ }
+}
+
+struct flag {
+ const char *name;
+ unsigned int remove, add;
+};
+
+static const struct flag cmd_flags[] = {
+ {"pausing", MP_PAUSING_FLAGS, MP_PAUSING},
+ {"pausing-toggle", MP_PAUSING_FLAGS, MP_PAUSING_TOGGLE},
+ {"no-osd", MP_ON_OSD_FLAGS, MP_ON_OSD_NO},
+ {"osd-bar", MP_ON_OSD_FLAGS, MP_ON_OSD_BAR},
+ {"osd-msg", MP_ON_OSD_FLAGS, MP_ON_OSD_MSG},
+ {"osd-msg-bar", MP_ON_OSD_FLAGS, MP_ON_OSD_MSG | MP_ON_OSD_BAR},
+ {"osd-auto", MP_ON_OSD_FLAGS, MP_ON_OSD_AUTO},
+ {"expand-properties", 0, MP_EXPAND_PROPERTIES},
+ {"raw", MP_EXPAND_PROPERTIES, 0},
+ {0}
+};
+
+static struct mp_cmd *parse_cmd(struct parse_ctx *ctx, int def_flags)
+{
+ struct mp_cmd *cmd = NULL;
+ int r;
+
+ if (!ctx->array_input) {
+ ctx->str = bstr_lstrip(ctx->str);
+ bstr old = ctx->str;
+ if (mp_replace_legacy_cmd(ctx->tmp, &ctx->str)) {
+ MP_WARN(ctx, "Warning: command '%.*s' is deprecated, "
+ "replaced with '%.*s' at %s.\n",
+ BSTR_P(old), BSTR_P(ctx->str), ctx->loc);
+ ctx->start = ctx->str;
+ }
+ }
+
+ bstr cur_token;
+ if (pctx_read_token(ctx, &cur_token) < 0)
+ goto error;
+
+ while (1) {
+ for (int n = 0; cmd_flags[n].name; n++) {
+ if (bstr_equals0(cur_token, cmd_flags[n].name)) {
+ if (pctx_read_token(ctx, &cur_token) < 0)
+ goto error;
+ def_flags &= ~cmd_flags[n].remove;
+ def_flags |= cmd_flags[n].add;
+ goto cont;
+ }
+ }
+ break;
+ cont: ;
+ }
+
+ if (cur_token.len == 0) {
+ MP_ERR(ctx, "Command name missing.\n");
+ goto error;
+ }
+ const struct mp_cmd_def *cmd_def = NULL;
+ for (int n = 0; mp_cmds[n].name; n++) {
+ if (bstr_equals0(cur_token, mp_cmds[n].name)) {
+ cmd_def = &mp_cmds[n];
+ break;
+ }
+ }
+
+ if (!cmd_def) {
+ MP_ERR(ctx, "Command '%.*s' not found.\n", BSTR_P(cur_token));
+ goto error;
+ }
+
+ cmd = talloc_ptrtype(NULL, cmd);
+ *cmd = (struct mp_cmd) {
+ .name = (char *)cmd_def->name,
+ .id = cmd_def->id,
+ .flags = def_flags,
+ .scale = 1,
+ .def = cmd_def,
+ };
+
+ for (int i = 0; i < MP_CMD_MAX_ARGS; i++) {
+ const struct m_option *opt = &cmd_def->args[i];
+ bool is_vararg = cmd_def->vararg &&
+ (i + 1 >= MP_CMD_MAX_ARGS || !cmd_def->args[i + 1].type); // last arg
+ if (!opt->type && is_vararg && cmd->nargs > 0)
+ opt = cmd->args[cmd->nargs - 1].type;
+ if (!opt->type)
+ break;
+
+ r = pctx_read_token(ctx, &cur_token);
+ if (r < 0) {
+ MP_ERR(ctx, "Command %s: error in argument %d.\n", cmd->name, i + 1);
+ goto error;
+ }
+ if (r < 1) {
+ if (is_vararg)
+ continue;
+ // Skip optional arguments
+ if (opt->defval) {
+ struct mp_cmd_arg *cmdarg = &cmd->args[cmd->nargs];
+ cmdarg->type = opt;
+ memcpy(&cmdarg->v, opt->defval, opt->type->size);
+ cmd->nargs++;
+ continue;
+ }
+ MP_ERR(ctx, "Command %s: more than %d arguments required.\n",
+ cmd->name, cmd->nargs);
+ goto error;
+ }
+
+ struct mp_cmd_arg *cmdarg = &cmd->args[cmd->nargs];
+ cmdarg->type = opt;
+ cmd->nargs++;
+ r = m_option_parse(ctx->log, opt, bstr0(cmd->name), cur_token, &cmdarg->v);
+ if (r < 0) {
+ MP_ERR(ctx, "Command %s: argument %d can't be parsed: %s.\n",
+ cmd->name, i + 1, m_option_strerror(r));
+ goto error;
+ }
+ if (opt->type == &m_option_type_string)
+ talloc_steal(cmd, cmdarg->v.s);
+ }
+
+ bstr left = pctx_get_trailing(ctx);
+ if (left.len) {
+ MP_ERR(ctx, "Command %s has trailing unused arguments: '%.*s'.\n",
+ cmd->name, BSTR_P(left));
+ // Better make it fatal to make it clear something is wrong.
+ goto error;
+ }
+
+ if (!ctx->array_input) {
+ bstr orig = {ctx->start.start, ctx->str.start - ctx->start.start};
+ cmd->original = bstrdup(cmd, bstr_strip(orig));
+ }
+
+ return cmd;
+
+error:
+ MP_ERR(ctx, "Command was defined at %s.\n", ctx->loc);
+ talloc_free(cmd);
+ return NULL;
+}
+
+static struct mp_cmd *parse_cmd_str(struct mp_log *log, bstr *str, const char *loc)
+{
+ struct parse_ctx ctx = {
+ .log = log,
+ .loc = loc,
+ .tmp = talloc_new(NULL),
+ .str = *str,
+ .start = *str,
+ };
+ struct mp_cmd *res = parse_cmd(&ctx, MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES);
+ talloc_free(ctx.tmp);
+ *str = ctx.str;
+ return res;
+}
+
+mp_cmd_t *mp_input_parse_cmd_(struct mp_log *log, bstr str, const char *loc)
+{
+ bstr original = str;
+ struct mp_cmd *cmd = parse_cmd_str(log, &str, loc);
+ if (!cmd)
+ return NULL;
+
+ // Handle "multi" commands
+ struct mp_cmd **p_prev = NULL;
+ while (1) {
+ str = bstr_lstrip(str);
+ // read_token just to check whether it's trailing whitespace only
+ bstr u1, u2;
+ if (!bstr_eatstart0(&str, ";") || !read_token(str, &u1, &u2))
+ break;
+ // Multi-command. Since other input.c code uses queue_next for its
+ // own purposes, a pseudo-command is used to wrap the command list.
+ if (!p_prev) {
+ struct mp_cmd *list = talloc_ptrtype(NULL, list);
+ *list = (struct mp_cmd) {
+ .id = MP_CMD_COMMAND_LIST,
+ .name = "list",
+ .original = bstrdup(list, original),
+ };
+ talloc_steal(list, cmd);
+ list->args[0].v.p = cmd;
+ p_prev = &cmd->queue_next;
+ cmd = list;
+ }
+ struct mp_cmd *sub = parse_cmd_str(log, &str, loc);
+ if (!sub) {
+ talloc_free(cmd);
+ return NULL;
+ }
+ talloc_steal(cmd, sub);
+ *p_prev = sub;
+ p_prev = &sub->queue_next;
+ }
+
+ return cmd;
+}
+
+struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, int def_flags,
+ const char **argv, const char *location)
+{
+ bstr args[MP_CMD_MAX_ARGS];
+ int num = 0;
+ for (; argv[num]; num++) {
+ if (num > MP_CMD_MAX_ARGS) {
+ mp_err(log, "%s: too many arguments.\n", location);
+ return NULL;
+ }
+ args[num] = bstr0(argv[num]);
+ }
+ return mp_input_parse_cmd_bstrv(log, def_flags, num, args, location);
+}
+
+struct mp_cmd *mp_input_parse_cmd_bstrv(struct mp_log *log, int def_flags,
+ int argc, bstr *argv,
+ const char *location)
+{
+ struct parse_ctx ctx = {
+ .log = log,
+ .loc = location,
+ .tmp = talloc_new(NULL),
+ .array_input = true,
+ .strs = argv,
+ .num_strs = argc,
+ };
+ struct mp_cmd *res = parse_cmd(&ctx, def_flags);
+ talloc_free(ctx.tmp);
+ return res;
+}
+
+void mp_cmd_free(mp_cmd_t *cmd)
+{
+ talloc_free(cmd);
+}
+
+mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
+{
+ mp_cmd_t *ret;
+ int i;
+
+ if (!cmd)
+ return NULL;
+
+ ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
+ ret->name = talloc_strdup(ret, cmd->name);
+ for (i = 0; i < ret->nargs; i++) {
+ if (cmd->args[i].type->type == &m_option_type_string)
+ ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
+ }
+
+ if (cmd->id == MP_CMD_COMMAND_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);
+ talloc_steal(ret, sub);
+ if (prev) {
+ prev->queue_next = sub;
+ } else {
+ ret->args[0].v.p = sub;
+ }
+ prev = sub;
+ }
+ }
+
+ return ret;
+}
+
+static int parse_cycle_dir(struct mp_log *log, const struct m_option *opt,
+ struct bstr name, struct bstr param, void *dst)
+{
+ double val;
+ if (bstrcmp0(param, "up") == 0) {
+ val = +1;
+ } else if (bstrcmp0(param, "down") == 0) {
+ val = -1;
+ } else {
+ return m_option_type_double.parse(log, opt, name, param, dst);
+ }
+ *(double *)dst = val;
+ return 1;
+}
+
+const struct m_option_type m_option_type_cycle_dir = {
+ .name = "up|down",
+ .parse = parse_cycle_dir,
+ .size = sizeof(double),
+};
diff --git a/input/cmd_parse.h b/input/cmd_parse.h
new file mode 100644
index 0000000000..c8f165b11d
--- /dev/null
+++ b/input/cmd_parse.h
@@ -0,0 +1,52 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef MP_PARSE_COMMAND_H
+#define MP_PARSE_COMMAND_H
+
+struct mp_log;
+struct mp_cmd;
+
+// 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);
+
+// Similar to mp_input_parse_cmd(), but takes a list of strings instead.
+// Also, def_flags contains initial command flags (see mp_cmd_flags; the default
+// as used by mp_input_parse_cmd is MP_ON_OSD_AUTO | MP_EXPAND_PROPERTIES).
+// Keep in mind that these functions (naturally) don't take multiple commands,
+// i.e. a ";" argument does not start a new command.
+// The _strv version is limitted to MP_CMD_MAX_ARGS argv array items.
+struct mp_cmd *mp_input_parse_cmd_strv(struct mp_log *log, int def_flags,
+ const char **argv, const char *location);
+struct mp_cmd *mp_input_parse_cmd_bstrv(struct mp_log *log, int def_flags,
+ int argc, bstr *argv,
+ const char *location);
+
+// After getting a command from mp_input_get_cmd you need to free it using this
+// function
+void mp_cmd_free(struct mp_cmd *cmd);
+
+// This creates a copy of a command (used by the auto repeat stuff).
+struct mp_cmd *mp_cmd_clone(struct mp_cmd *cmd);
+
+extern const struct m_option_type m_option_type_cycle_dir;
+
+#define OPT_CYCLEDIR(...) \
+ OPT_GENERAL(double, __VA_ARGS__, .type = &m_option_type_cycle_dir)
+
+#endif
diff --git a/input/input.c b/input/input.c
index b2e5f9a9f8..dc82683bfb 100644
--- a/input/input.c
+++ b/input/input.c
@@ -39,6 +39,8 @@
#include "input.h"
#include "keycodes.h"
+#include "cmd_list.h"
+#include "cmd_parse.h"
#include "osdep/timer.h"
#include "common/msg.h"
#include "common/global.h"
@@ -74,428 +76,13 @@ struct cmd_bind {
struct cmd_bind_section *owner;
};
-struct key_name {
- int key;
- char *name;
-};
-
-// This does not specify the real destination of the command parameter values,
-// it just provides a dummy for the OPT_ macros.
-#define OPT_BASE_STRUCT struct mp_cmd_arg
-#define ARG(t) "", v. t
-
-/* This array defines all known commands.
- * The first field is an id used to recognize the command.
- * The second is the command name used in slave mode and input.conf.
- * Then comes the definition of each argument, first mandatory arguments
- * (ARG_INT, ARG_FLOAT, ARG_STRING) if any, then optional arguments
- * (OARG_INT(default), etc) if any. The command will be given the default
- * argument value if the user didn't give enough arguments to specify it.
- * A command can take a maximum of MP_CMD_MAX_ARGS arguments.
- */
-
-#define ARG_INT OPT_INT(ARG(i), 0)
-#define ARG_FLOAT OPT_FLOAT(ARG(f), 0)
-#define ARG_DOUBLE OPT_DOUBLE(ARG(d), 0)
-#define ARG_STRING OPT_STRING(ARG(s), 0)
-#define ARG_CHOICE(c) OPT_CHOICE(ARG(i), 0, c)
-#define ARG_CHOICE_OR_INT(...) OPT_CHOICE_OR_INT(ARG(i), 0, __VA_ARGS__)
-#define ARG_TIME OPT_TIME(ARG(d), 0)
-#define OARG_DOUBLE(def) OPT_DOUBLE(ARG(d), 0, OPTDEF_DOUBLE(def))
-#define OARG_INT(def) OPT_INT(ARG(i), 0, OPTDEF_INT(def))
-#define OARG_CHOICE(def, c) OPT_CHOICE(ARG(i), 0, c, OPTDEF_INT(def))
-
-static int parse_cycle_dir(struct mp_log *log, const struct m_option *opt,
- struct bstr name, struct bstr param, void *dst);
-static const struct m_option_type m_option_type_cycle_dir = {
- .name = "up|down",
- .parse = parse_cycle_dir,
- .size = sizeof(double),
-};
-
-#define OPT_CYCLEDIR(...) \
- OPT_GENERAL(double, __VA_ARGS__, .type = &m_option_type_cycle_dir)
-
-#define OARG_CYCLEDIR(def) OPT_CYCLEDIR(ARG(d), 0, OPTDEF_DOUBLE(def))
-
-struct mp_cmd_def {
- int id; // one of MP_CMD_...
- 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 vararg; // last argument can be given 0 to multiple times
-};
-
-static const struct mp_cmd_def mp_cmds[] = {
- { MP_CMD_IGNORE, "ignore", },
-
- { MP_CMD_SEEK, "seek", {
- ARG_TIME,
- OARG_CHOICE(0, ({"relative", 0}, {"0", 0}, {"-", 0},
- {"absolute-percent", 1}, {"1", 1},
- {"absolute", 2}, {"2", 2})),
- OARG_CHOICE(0, ({"default-precise", 0}, {"0", 0},
- {"exact", 1}, {"1", 1},
- {"keyframes", -1}, {"-1", -1})),
- },
- .allow_auto_repeat = true,
- },
- { MP_CMD_REVERT_SEEK, "revert_seek", },
- { MP_CMD_QUIT, "quit", { OARG_INT(0) } },
- { MP_CMD_QUIT_WATCH_LATER, "quit_watch_later", },
- { MP_CMD_STOP, "stop", },
- { MP_CMD_FRAME_STEP, "frame_step", .allow_auto_repeat = true },
- { MP_CMD_FRAME_BACK_STEP, "frame_back_step", .al