command, lua: change script_message semantics

Change script_message to broadcast the message to all clients. Add a new
script_message_to command, which does what the old script_message
command did.

This is intended as simplification, although it might lead to chaos too.
This commit is contained in:
wm4 2014-03-17 18:26:56 +01:00
parent 422af1b948
commit 637664d95a
7 changed files with 54 additions and 17 deletions

View File

@ -443,13 +443,20 @@ Input Commands that are Possibly Subject to Change
Remove an overlay added with ``overlay_add`` and the same ID. Does nothing
if no overlay with this ID exists.
``script_message "<target>" "<arg1>" "<arg2>" ...``
Send a message to the client named ``<target>``, and pass it the following
list of arguments. The target can be for example a script (Lua scripts
can get their name via ``mp.get_script_name()``). What this message means,
how many arguments it takes, and what the arguments mean is fully up to
the target and the sender. Lua scripts use it to add key bindings via
input.conf.
``script_message "<arg1>" "<arg2>" ...``
Send a message to all clients, and pass it the following list of arguments.
What this message means, how many arguments it takes, and what the arguments
mean is fully up to the receiver and the sender. Every client receives the
message, so be careful about name clashes (or use ``script_message_to``).
``script_message_to "<target>" "<arg1>" "<arg2>" ...``
Same as ``script_message``, but send it only to the client named
``<target>``. Each client (scripts etc.) has a unique name. For example,
Lua scripts can get their name via ``mp.get_script_name()``.
(Scripts use this internally to dispatch key bindings, and this can also
be used in input.conf to reassign such bindings.)
Undocumented commands: ``tv_start_scan``, ``tv_step_channel``, ``tv_step_norm``,
``tv_step_chanlist``, ``tv_set_channel``, ``tv_last_channel``, ``tv_set_freq``,

View File

@ -162,13 +162,13 @@ The ``mp`` module is preloaded, although it can be loaded manually with
The ``name`` argument should be a short symbolic string. It allows the user
to remap the key binding via input.conf using the ``script_message``
command, the script name, and the name of the key binding (see below for
command, and the name of the key binding (see below for
an example). The name should be unique across other bindings in the same
script - if not, the previous binding with the same name will be
overwritten. You can omit the name, in which case a random name is generated
internally.
Internally, key bindings are dispatched via the ``script_message`` input
Internally, key bindings are dispatched via the ``script_message_to`` input
command and ``mp.register_script_command``.
Trying to map multiple commands to a key will essentially prefer a random
@ -187,17 +187,24 @@ The ``mp`` module is preloaded, although it can be loaded manually with
This will print the message ``the key was pressed`` when ``x`` was pressed.
The user can remap these key bindings. Assume the above script was using
the filename ``fooscript.lua``, then the user has to put the following
The user can remap these key bindings. Then the user has to put the following
into his input.conf to remap the command to the ``y`` key:
::
y script_message lua/fooscript something
y script_message something
This will print the message when the key ``y`` is pressed. (``x`` will
still work, unless the user overmaps it.)
You can also explicitly send a message to a named script only. Assume the
above script was using the filename ``fooscript.lua``:
::
y script_message_to lua/fooscript something
``mp.add_forced_key_binding(...)``
This works almost the same as ``mp.add_key_binding``, but registers the
key binding in a way that will overwrite the user's custom bindings in his
@ -292,8 +299,9 @@ The ``mp`` module is preloaded, although it can be loaded manually with
The level is a string, see ``msg.log`` for allowed log levels.
``mp.register_script_command(name, fn)``
This is a helper to dispatch ``script_message`` invocations to Lua
functions. ``fn`` is called if ``script_message`` is called on this script
This is a helper to dispatch ``script_message`` or ``script_message_to``
invocations to Lua functions. ``fn`` is called if ``script_message`` or
``script_message_to`` (with this script as destination) is run
with ``name`` as first parameter. The other parameters are passed to ``fn``.
If a command with the given name is already registered, it's overwritten.

View File

@ -168,7 +168,8 @@ const struct mp_cmd_def mp_cmds[] = {
{ MP_CMD_VO_CMDLINE, "vo_cmdline", { ARG_STRING } },
{ MP_CMD_SCRIPT_DISPATCH, "script_dispatch", { ARG_STRING, ARG_INT } },
{ MP_CMD_SCRIPT_MESSAGE, "script_message", { ARG_STRING, ARG_STRING },
{ MP_CMD_SCRIPT_MESSAGE, "script_message", { ARG_STRING }, .vararg = true },
{ MP_CMD_SCRIPT_MESSAGE_TO, "script_message_to", { ARG_STRING, ARG_STRING },
.vararg = true },
{ MP_CMD_OVERLAY_ADD, "overlay_add",

View File

@ -101,6 +101,7 @@ enum mp_command_type {
/// Internal for Lua scripts
MP_CMD_SCRIPT_DISPATCH,
MP_CMD_SCRIPT_MESSAGE,
MP_CMD_SCRIPT_MESSAGE_TO,
MP_CMD_OVERLAY_ADD,
MP_CMD_OVERLAY_REMOVE,

View File

@ -339,6 +339,7 @@ static void status_reply(struct mpv_handle *ctx, int event,
}
// set ev->data to a new copy of the original data
// (done only for message types that are broadcast)
static void dup_event_data(struct mpv_event *ev)
{
switch (ev->event_id) {
@ -346,6 +347,17 @@ static void dup_event_data(struct mpv_event *ev)
case MPV_EVENT_UNPAUSE:
ev->data = talloc_memdup(NULL, ev->data, sizeof(mpv_event_pause_reason));
break;
case MPV_EVENT_CLIENT_MESSAGE: {
struct mpv_event_client_message *src = ev->data;
struct mpv_event_client_message *msg =
talloc_zero(NULL, struct mpv_event_client_message);
for (int n = 0; n < src->num_args; n++) {
MP_TARRAY_APPEND(msg, msg->args, msg->num_args,
talloc_strdup(msg, src->args[n]));
}
ev->data = msg;
break;
}
default:
// Doesn't use events with memory allocation.
if (ev->data)

View File

@ -3370,7 +3370,7 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
break;
}
case MP_CMD_SCRIPT_MESSAGE: {
case MP_CMD_SCRIPT_MESSAGE_TO: {
mpv_event_client_message *event = talloc_ptrtype(NULL, event);
*event = (mpv_event_client_message){0};
for (int n = 1; n < cmd->nargs; n++) {
@ -3385,6 +3385,14 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
}
break;
}
case MP_CMD_SCRIPT_MESSAGE: {
const char *args[MP_CMD_MAX_ARGS];
mpv_event_client_message event = {.args = args};
for (int n = 0; n < cmd->nargs; n++)
event.args[event.num_args++] = cmd->args[n].v.s;
mp_client_broadcast_event(mpctx, MPV_EVENT_CLIENT_MESSAGE, &event);
break;
}
#if HAVE_SYS_MMAN_H
case MP_CMD_OVERLAY_ADD:

View File

@ -103,7 +103,7 @@ local function update_key_bindings()
local cfg = ""
for k, v in pairs(key_bindings) do
if v.forced ~= def then
cfg = cfg .. v.key .. " script_message " .. mp.script_name
cfg = cfg .. v.key .. " script_message_to " .. mp.script_name
.. " " .. v.name .. "\n"
end
end