lua: expose async commands

Might be useful for some.
This commit is contained in:
wm4 2018-05-10 15:26:27 +02:00
parent d7ed3ba473
commit 159379980e
3 changed files with 49 additions and 0 deletions

View File

@ -115,6 +115,15 @@ The ``mp`` module is preloaded, although it can be loaded manually with
error. ``def`` is the second parameter provided to the function, and is
nil if it's missing.
``mp.command_native_async(table [,fn])``
Like ``mp.command_native()``, but the command is ran asynchronously (as far
as possible), and upon completion, fn is called. fn has two arguments:
``fn(success, result, error)``. ``success`` is always a Boolean and is true
if the command was successful, otherwise false. The second parameter is
the result value (can be nil) in case of success, nil otherwise (as returned
by ``mp.command_native()``). The third parameter is the error string in case
of an error, nil otherwise.
``mp.get_property(name [,def])``
Return the value of the given property as string. These are the same
properties as used in input.conf. See `Properties`_ for a list of

View File

@ -562,6 +562,12 @@ static int script_wait_event(lua_State *L)
lua_setfield(L, -2, "hook_id");
break;
}
case MPV_EVENT_COMMAND_REPLY: {
mpv_event_command *cmd = event->data;
pushnode(L, &cmd->result);
lua_setfield(L, -2, "result");
break;
}
default: ;
}
@ -967,6 +973,18 @@ static int script_command_native(lua_State *L)
return 2;
}
static int script_raw_command_native_async(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
uint64_t id = luaL_checknumber(L, 1);
struct mpv_node node;
void *tmp = mp_lua_PITA(L);
makenode(tmp, &node, L, 2);
int res = mpv_command_node_async(ctx->client, id, &node);
talloc_free_children(tmp);
return check_error(L, res);
}
static int script_set_osd_ass(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
@ -1339,6 +1357,7 @@ static const struct fn_entry main_fns[] = {
FN_ENTRY(command),
FN_ENTRY(commandv),
FN_ENTRY(command_native),
FN_ENTRY(raw_command_native_async),
FN_ENTRY(get_property_bool),
FN_ENTRY(get_property_number),
FN_ENTRY(get_property_native),

View File

@ -528,6 +528,27 @@ function mp.add_hook(name, pri, cb)
mp.raw_hook_add(id, name, pri - 50)
end
local async_call_table = {}
local async_next_id = 1
function mp.command_native_async(node, cb)
local id = async_next_id
async_next_id = async_next_id + 1
async_call_table[id] = cb
mp.raw_command_native_async(id, node)
end
mp.register_event("command-reply", function(ev)
local id = tonumber(ev.id)
cb = async_call_table[id]
async_call_table[id] = nil
if ev.error then
cb(false, nil, ev.error)
else
cb(true, ev.result, nil)
end
end)
local mp_utils = package.loaded["mp.utils"]
function mp_utils.format_table(t, set)