Skip to content

Commit

Permalink
command: add custom arg for script-binding
Browse files Browse the repository at this point in the history
This allows passing arbitrary information in the script-binding
command. The content is available as a new message argument.
  • Loading branch information
na-na-hi committed Nov 18, 2024
1 parent 3604497 commit 66a8a2c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 5 additions & 2 deletions DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1273,11 +1273,12 @@ Scripting Commands
This command has a variable number of arguments, and cannot be used with
named arguments.

``script-binding <name>``
``script-binding <name> [<arg>]``
Invoke a script-provided key binding. This can be used to remap key
bindings provided by external Lua scripts.

The argument is the name of the binding.
``<name>`` is the name of the binding. ``<arg>`` is a user-provided
arbitrary string which can be used to provide extra information.

It can optionally be prefixed with the name of the script, using ``/`` as
separator, e.g. ``script-binding scriptname/bindingname``. Note that script
Expand All @@ -1295,6 +1296,8 @@ Scripting Commands
5. The text the key would produce, or empty string if not applicable.
6. The scale of the key, such as the ones produced by ``WHEEL_*`` keys.
The scale is 1 if the key is nonscalable.
7. The user-provided string ``<arg>``, or empty string if the argument is
not used.

The 5th argument is only set if no modifiers are present (using the shift
key with a letter is normally not emitted as having a modifier, and results
Expand Down
13 changes: 9 additions & 4 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -6439,11 +6439,11 @@ static void cmd_script_binding(void *p)
char *scale_s = mp_format_double(NULL, scale, 6, false, false, false);

for (int i = 0; i < scale_units; i++) {
event.num_args = 6;
event.args = (const char*[6]){"key-binding", name, state,
event.num_args = 7;
event.args = (const char*[7]){"key-binding", name, state,
incmd->key_name ? incmd->key_name : "",
incmd->key_text ? incmd->key_text : "",
scale_s};
scale_s, cmd->args[1].v.s};
if (mp_client_send_event_dup(mpctx, target,
MPV_EVENT_CLIENT_MESSAGE, &event) < 0)
{
Expand Down Expand Up @@ -7156,7 +7156,12 @@ const struct mp_cmd_def mp_cmds[] = {

{ "ao-reload", cmd_ao_reload },

{ "script-binding", cmd_script_binding, { {"name", OPT_STRING(v.s)} },
{ "script-binding", cmd_script_binding,
{
{"name", OPT_STRING(v.s)},
{"arg", OPT_STRING(v.s), OPTDEF_STR(""),
.flags = MP_CMD_OPT_ARG},
},
.allow_auto_repeat = true, .on_updown = true, .scalable = true },

{ "script-message", cmd_script_message, { {"args", OPT_STRING(v.s)} },
Expand Down

0 comments on commit 66a8a2c

Please sign in to comment.