lua: expose mpv_abort_async_command()

Also somewhat cleans up mp.command_native_async() error handling.
This commit is contained in:
wm4 2018-05-12 18:48:35 +02:00
parent 9c530c7ee9
commit dbe831bd02
4 changed files with 47 additions and 3 deletions

View File

@ -134,6 +134,18 @@ The ``mp`` module is preloaded, although it can be loaded manually with
by ``mp.command_native()``). The third parameter is the error string in case
of an error, nil otherwise.
Returns a table with undefined contents, which can be used as argument for
``mp.abort_async_command``.
If starting the command failed for some reason, ``nil, error`` is returned,
and ``fn`` is called indicating failure, using the same error value.
``mp.abort_async_command(t)``
Abort a ``mp.command_native_async`` call. The argument is the return value
of that command (which starts asynchronous execution of the command).
Whether this works and how long it takes depends on the command and the
situation. The abort call itself is asynchronous. Does not return anything.
``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

@ -55,4 +55,13 @@ mp.observe_property("vo-configured", "bool", function(_, v)
function(res, val, err)
print("done subprocess: " .. join(" ", {res, val, err}))
end)
local x = mp.command_native_async({name = "subprocess", args = {"sleep", "inf"}},
function(res, val, err)
print("done sleep inf subprocess: " .. join(" ", {res, val, err}))
end)
mp.add_timeout(15, function()
print("aborting sleep inf subprocess after timeout")
mp.abort_async_command(x)
end)
end)

View File

@ -985,6 +985,14 @@ static int script_raw_command_native_async(lua_State *L)
return check_error(L, res);
}
static int script_raw_abort_async_command(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
uint64_t id = luaL_checknumber(L, 1);
mpv_abort_async_command(ctx->client, id);
return 0;
}
static int script_set_osd_ass(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
@ -1252,6 +1260,7 @@ static const struct fn_entry main_fns[] = {
FN_ENTRY(commandv),
FN_ENTRY(command_native),
FN_ENTRY(raw_command_native_async),
FN_ENTRY(raw_abort_async_command),
FN_ENTRY(get_property_bool),
FN_ENTRY(get_property_number),
FN_ENTRY(get_property_native),

View File

@ -534,13 +534,21 @@ 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)
local res, err = mp.raw_command_native_async(id, node)
if not res then
cb(false, nil, err)
return res, err
end
local t = {cb = cb, id = id}
async_call_table[id] = t
return t
end
mp.register_event("command-reply", function(ev)
local id = tonumber(ev.id)
cb = async_call_table[id]
local t = async_call_table[id]
local cb = t.cb
t.id = nil
async_call_table[id] = nil
if ev.error then
cb(false, nil, ev.error)
@ -549,6 +557,12 @@ mp.register_event("command-reply", function(ev)
end
end)
function mp.abort_async_command(t)
if t.id ~= nil then
mp.raw_abort_async_command(t.id)
end
end
local mp_utils = package.loaded["mp.utils"]
function mp_utils.format_table(t, set)