From 159379980ec8c79608b1adada4b66b1d8c016e4a Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 10 May 2018 15:26:27 +0200 Subject: lua: expose async commands Might be useful for some. --- player/lua.c | 19 +++++++++++++++++++ player/lua/defaults.lua | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'player') diff --git a/player/lua.c b/player/lua.c index 98dcfee5b2..8d4e794a52 100644 --- a/player/lua.c +++ b/player/lua.c @@ -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), diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index d5bb194c50..6f5a9c4b6c 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -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) -- cgit v1.2.3