summaryrefslogtreecommitdiffstats
path: root/player
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2018-05-10 15:26:27 +0200
committerwm4 <wm4@nowhere>2018-05-24 19:56:34 +0200
commit159379980ec8c79608b1adada4b66b1d8c016e4a (patch)
tree39296d3c1a8b75be081e88e037d38e81c3a56a79 /player
parentd7ed3ba4734654f50d577e653179c2c0d729ae30 (diff)
downloadmpv-159379980ec8c79608b1adada4b66b1d8c016e4a.tar.bz2
mpv-159379980ec8c79608b1adada4b66b1d8c016e4a.tar.xz
lua: expose async commands
Might be useful for some.
Diffstat (limited to 'player')
-rw-r--r--player/lua.c19
-rw-r--r--player/lua/defaults.lua21
2 files changed, 40 insertions, 0 deletions
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)