summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDudemanguy911 <random342@airmail.cc>2019-09-01 21:06:09 -0500
committerDudemanguy911 <random342@airmail.cc>2019-09-21 16:58:14 +0000
commit4614d432a8d21ab135af25a183f57efd5059bb62 (patch)
tree000f2e241600ca79daf515c943c08475925b1543
parentdd547ddcc231fe9e966835bf497ee8458aa6e5f1 (diff)
downloadmpv-4614d432a8d21ab135af25a183f57efd5059bb62.tar.bz2
mpv-4614d432a8d21ab135af25a183f57efd5059bb62.tar.xz
input: add keybind command
-rw-r--r--DOCS/man/input.rst7
-rw-r--r--input/input.c38
-rw-r--r--input/input.h3
-rw-r--r--player/command.c17
4 files changed, 65 insertions, 0 deletions
diff --git a/DOCS/man/input.rst b/DOCS/man/input.rst
index 224032cac3..52be5540df 100644
--- a/DOCS/man/input.rst
+++ b/DOCS/man/input.rst
@@ -576,6 +576,13 @@ Remember to quote string arguments in input.conf (see `Flat command syntax`_).
empty string, ``KEYUP`` will be set on all keys. Otherwise, ``KEYUP`` will
only be set on the key specified by ``name``.
+``keybind <name> <command>``
+ Binds a key to an input command. ``command`` must be a complete command
+ containing all the desired arguments and flags. Both ``name`` and
+ ``command`` use the ``input.conf`` naming scheme. This is primarily
+ useful for the client API. Note that ``keybind`` cannot be bound to
+ another ``keybind`` command.
+
``audio-add <url> [<flags> [<title> [<lang>]]]``
Load the given audio file. See ``sub-add`` command.
diff --git a/input/input.c b/input/input.c
index 9e96da267d..8a14d862b3 100644
--- a/input/input.c
+++ b/input/input.c
@@ -1432,6 +1432,44 @@ void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd)
mp_input_queue_cmd(ictx, mp_input_parse_cmd_strv(ictx->log, cmd));
}
+void mp_input_bind_key(struct input_ctx *ictx, int key, bstr command)
+{
+ struct cmd_bind_section *bs = ictx->cmd_bind_sections;
+ struct cmd_bind *bind = NULL;
+
+ for (int n = 0; n < bs->num_binds; n++) {
+ struct cmd_bind *b = &bs->binds[n];
+ if (bind_matches_key(b, 1, &key) && b->is_builtin == false) {
+ bind = b;
+ break;
+ }
+ }
+
+ if (!bind) {
+ struct cmd_bind empty = {{0}};
+ MP_TARRAY_APPEND(bs, bs->binds, bs->num_binds, empty);
+ bind = &bs->binds[bs->num_binds - 1];
+ }
+
+ bind_dealloc(bind);
+
+ *bind = (struct cmd_bind) {
+ .cmd = bstrdup0(bs->binds, command),
+ .location = talloc_strdup(bs->binds, "keybind-command"),
+ .owner = bs,
+ .is_builtin = false,
+ .num_keys = 1,
+ };
+ memcpy(bind->keys, &key, 1 * sizeof(bind->keys[0]));
+ if (mp_msg_test(ictx->log, MSGL_DEBUG)) {
+ char *s = mp_input_get_key_combo_name(&key, 1);
+ MP_TRACE(ictx, "add:section='%s' key='%s'%s cmd='%s' location='%s'\n",
+ bind->owner->section, s, bind->is_builtin ? " builtin" : "",
+ bind->cmd, bind->location);
+ talloc_free(s);
+ }
+}
+
struct mp_input_src_internal {
pthread_t thread;
bool thread_running;
diff --git a/input/input.h b/input/input.h
index ff194785c9..3dfc053bd0 100644
--- a/input/input.h
+++ b/input/input.h
@@ -203,6 +203,9 @@ bool mp_input_use_media_keys(struct input_ctx *ictx);
// Like mp_input_parse_cmd_strv, but also run the command.
void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd);
+// Binds a command to a key.
+void mp_input_bind_key(struct input_ctx *ictx, int key, bstr command);
+
void mp_input_set_repeat_info(struct input_ctx *ictx, int rate, int delay);
void mp_input_pipe_add(struct input_ctx *ictx, const char *filename);
diff --git a/player/command.c b/player/command.c
index 79b3c1c8b4..6239a3fa95 100644
--- a/player/command.c
+++ b/player/command.c
@@ -5576,6 +5576,21 @@ static void cmd_key(void *p)
}
}
+static void cmd_key_bind(void *p)
+{
+ struct mp_cmd_ctx *cmd = p;
+ struct MPContext *mpctx = cmd->mpctx;
+
+ int code = mp_input_get_key_from_name(cmd->args[0].v.s);
+ if (code < 0) {
+ MP_ERR(mpctx, "%s is not a valid input name.\n", cmd->args[0].v.s);
+ cmd->success = false;
+ return;
+ }
+ const char *target_cmd = cmd->args[1].v.s;
+ mp_input_bind_key(mpctx->input, code, bstr0(target_cmd));
+}
+
static void cmd_apply_profile(void *p)
{
struct mp_cmd_ctx *cmd = p;
@@ -6015,6 +6030,8 @@ const struct mp_cmd_def mp_cmds[] = {
OPT_INT("button", v.i, 0, OPTDEF_INT(-1)),
OPT_CHOICE("mode", v.i, MP_CMD_OPT_ARG,
({"single", 0}, {"double", 1})), }},
+ { "keybind", cmd_key_bind, { OPT_STRING("name", v.s, 0),
+ OPT_STRING("cmd", v.s, 0) }},
{ "keypress", cmd_key, { OPT_STRING("name", v.s, 0) },
.priv = &(const int){0}},
{ "keydown", cmd_key, { OPT_STRING("name", v.s, 0) },