input: add keybind command

This commit is contained in:
Dudemanguy911 2019-09-01 21:06:09 -05:00
parent dd547ddcc2
commit 4614d432a8
4 changed files with 65 additions and 0 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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);

View File

@ -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) },