input: add key name to script-binding command response

The "script-binding" command is used by the Lua scripting wrapper to
register key bindings on the fly. It's also the only way to get fine-
grained information about key events (such as separate key up/down
events). This information is sent via a "key-binding" message when the
state of a key changes.

Extend it to send name of the mapped key itself. Previously, it was
assumed that the user just uses an unique identifier for the binding's
name, so it wasn't needed. With this change, a user can map exactly the
same command to multiple keys, which is useful especially with the next
commit.

Part of #2612.
This commit is contained in:
wm4 2015-12-23 19:10:38 +01:00
parent ff520054c8
commit 3e1aed8f40
5 changed files with 20 additions and 13 deletions

View File

@ -647,14 +647,20 @@ Input Commands that are Possibly Subject to Change
For completeness, here is how this command works internally. The details
could change any time. On any matching key event, ``script_message_to``
or ``script_message`` is called (depending on whether the script name is
included), where the first argument is the string ``key-binding``, the
second argument is the name of the binding, and the third argument is the
key state as string. The key state consists of a number of letters. The
first letter is one of ``d`` (key was pressed down), ``u`` (was released),
``r`` (key is still down, and was repeated; only if key repeat is enabled
for this binding), ``p`` (key was pressed; happens if up/down can't be
tracked). The second letter whether the event originates from the mouse,
either ``m`` (mouse button) or ``-`` (something else).
included), with the following arguments:
1. The string ``key-binding``.
2. The name of the binding (as established above).
3. The key state as string (see below).
4. The key name (since mpv 0.15.0).
The key state consists of 2 letters:
1. One of ``d`` (key was pressed down), ``u`` (was released), ``r`` (key
is still down, and was repeated; only if key repeat is enabled for this
binding), ``p`` (key was pressed; happens if up/down can't be tracked).
2. Whether the event originates from the mouse, either ``m`` (mouse button)
or ``-`` (something else).
``ab-loop``
Cycle through A-B loop states. The first command will set the ``A`` point

View File

@ -417,6 +417,7 @@ mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
m_option_copy(ret->args[i].type, &ret->args[i].v, &cmd->args[i].v);
}
ret->original = bstrdup(ret, cmd->original);
ret->key_name = talloc_strdup(ret, ret->key_name);
if (cmd->id == MP_CMD_COMMAND_LIST) {
struct mp_cmd *prev = NULL;

View File

@ -460,11 +460,10 @@ static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, char *force_section,
mp_cmd_t *ret = mp_input_parse_cmd(ictx, bstr0(cmd->cmd), cmd->location);
if (ret) {
ret->input_section = cmd->owner->section;
ret->key_name = talloc_steal(ret, mp_input_get_key_combo_name(&code, 1));
if (mp_msg_test(ictx->log, MSGL_DEBUG)) {
char *keyname = mp_input_get_key_combo_name(&code, 1);
MP_DBG(ictx, "key '%s' -> '%s' in '%s'\n",
keyname, cmd->cmd, ret->input_section);
talloc_free(keyname);
ret->key_name, cmd->cmd, ret->input_section);
}
ret->is_mouse_button = code & MP_KEY_EMIT_ON_UP;
} else {

View File

@ -86,6 +86,7 @@ typedef struct mp_cmd {
double scale; // for scaling numeric arguments
const struct mp_cmd_def *def;
char *sender; // name of the client API user which sent this
char *key_name; // string representation of the key binding
} mp_cmd_t;
struct mp_input_src {

View File

@ -4908,8 +4908,8 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re
char state[3] = {'p', cmd->is_mouse_button ? 'm' : '-'};
if (cmd->is_up_down)
state[0] = cmd->repeated ? 'r' : (cmd->is_up ? 'u' : 'd');
event.num_args = 3;
event.args = (const char*[3]){"key-binding", name, state};
event.num_args = 4;
event.args = (const char*[4]){"key-binding", name, state, cmd->key_name};
if (mp_client_send_event_dup(mpctx, target,
MPV_EVENT_CLIENT_MESSAGE, &event) < 0)
{