From 1b9d070786d1b9c9d9410956e4897c2ac6c2ace9 Mon Sep 17 00:00:00 2001 From: nanahi <130121847+na-na-hi@users.noreply.github.com> Date: Wed, 23 Oct 2024 03:17:32 -0400 Subject: [PATCH] command: fix keybind command with sequence keys The command is documented to use the same syntax as input.conf, but it doesn't work with sequence keys because it uses mp_input_get_key_from_name for checking key names, when it should use mp_input_get_keys_from_string instead. Fix this by using the correct function. --- input/input.c | 14 ++++++++++++-- input/input.h | 4 ++-- player/command.c | 12 +++++------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/input/input.c b/input/input.c index 37e6c3e210..203c333c96 100644 --- a/input/input.c +++ b/input/input.c @@ -1643,13 +1643,23 @@ void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd) input_unlock(ictx); } -void mp_input_bind_key(struct input_ctx *ictx, int key, bstr command, +bool mp_input_bind_key(struct input_ctx *ictx, const char *key, bstr command, const char *desc) { + char *name = talloc_strdup(NULL, key); + int keys[MP_MAX_KEY_DOWN]; + int num_keys = 0; + if (!mp_input_get_keys_from_string(name, MP_MAX_KEY_DOWN, &num_keys, keys)) { + talloc_free(name); + return false; + } + talloc_free(name); + input_lock(ictx); - bind_keys(ictx, false, (bstr){0}, &key, 1, command, + bind_keys(ictx, false, (bstr){0}, keys, num_keys, command, "keybind-command", desc); input_unlock(ictx); + return true; } struct mpv_node mp_input_get_bindings(struct input_ctx *ictx) diff --git a/input/input.h b/input/input.h index d6ba12fe83..8c5b85e117 100644 --- a/input/input.h +++ b/input/input.h @@ -215,8 +215,8 @@ 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, +// Binds a command to a key. Returns true if the bind is successful. +bool mp_input_bind_key(struct input_ctx *ictx, const char *key, bstr command, const char *desc); void mp_input_set_repeat_info(struct input_ctx *ictx, int rate, int delay); diff --git a/player/command.c b/player/command.c index 8e24b2f5fb..3a59da0dbd 100644 --- a/player/command.c +++ b/player/command.c @@ -6626,17 +6626,15 @@ 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 *key = cmd->args[0].v.s; const char *target_cmd = cmd->args[1].v.s; const char *comment = cmd->args[2].v.s; if (comment && !*comment) comment = NULL; - mp_input_bind_key(mpctx->input, code, bstr0(target_cmd), comment); + if (!mp_input_bind_key(mpctx->input, key, bstr0(target_cmd), comment)) { + MP_ERR(mpctx, "%s is not a valid input name.\n", key); + cmd->success = false; + } } static void cmd_apply_profile(void *p)