command: make script-binding command scalable

script-binding command is currently not scalable, so script
registered key bindings also cannot be scalable, unlink input.conf
bindings.

This makes script-binding command scalable so that it's possible to
define scalable key bindings in scripts. It now calls script-message
command with an extra argument with the scale of the key.
This commit is contained in:
nanahi
2024-11-14 16:34:11 -05:00
committed by Kacper Michajłow
parent b97e3b9e4b
commit 34571e7882
3 changed files with 29 additions and 12 deletions

View File

@@ -0,0 +1 @@
make `script-binding` command scalable; add `nonscalable` command prefix to restore the old behavior

View File

@@ -1274,13 +1274,15 @@ Scripting Commands
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), with the following arguments:
included), with the following arguments in string format:
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).
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.
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

View File

@@ -6561,17 +6561,31 @@ static void cmd_script_binding(void *p)
incmd->canceled ? 'c' : '-'};
if (incmd->is_up_down)
state[0] = incmd->repeated ? 'r' : (incmd->is_up ? 'u' : 'd');
event.num_args = 5;
event.args = (const char*[5]){"key-binding", name, state,
incmd->key_name ? incmd->key_name : "",
incmd->key_text ? incmd->key_text : ""};
if (mp_client_send_event_dup(mpctx, target,
MPV_EVENT_CLIENT_MESSAGE, &event) < 0)
{
MP_VERBOSE(mpctx, "Can't find script '%s' when handling input.\n",
target ? target : "-");
cmd->success = false;
double scale = 1;
int scale_units = incmd->scale_units;
if (mp_input_is_scalable_cmd(incmd)) {
scale = incmd->scale;
scale_units = 1;
}
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,
incmd->key_name ? incmd->key_name : "",
incmd->key_text ? incmd->key_text : "",
scale_s};
if (mp_client_send_event_dup(mpctx, target,
MPV_EVENT_CLIENT_MESSAGE, &event) < 0)
{
MP_VERBOSE(mpctx, "Can't find script '%s' when handling input.\n",
target ? target : "-");
cmd->success = false;
break;
}
}
talloc_free(scale_s);
}
static void cmd_script_message_to(void *p)
@@ -7283,7 +7297,7 @@ const struct mp_cmd_def mp_cmds[] = {
{ "ao-reload", cmd_ao_reload },
{ "script-binding", cmd_script_binding, { {"name", OPT_STRING(v.s)} },
.allow_auto_repeat = true, .on_updown = true},
.allow_auto_repeat = true, .on_updown = true, .scalable = true },
{ "script-message", cmd_script_message, { {"args", OPT_STRING(v.s)} },
.vararg = true },