From 5b5f77690064a1fb602e5b97e935bec65f60eb0d Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Mon, 11 Mar 2019 23:01:04 +0200 Subject: js: expose async commands (match 159379980e) --- player/javascript.c | 17 +++++++++++++++++ player/javascript/defaults.js | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) (limited to 'player') diff --git a/player/javascript.c b/player/javascript.c index 86dc077257..e637aad7bb 100644 --- a/player/javascript.c +++ b/player/javascript.c @@ -710,6 +710,15 @@ static void script_command_native(js_State *J, void *af) pushnode(J, presult_node); } +// args: async-command-id, native-command +static void script__command_native_async(js_State *J, void *af) +{ + uint64_t id = jsL_checkuint64(J, 1); + struct mpv_node node; + makenode(af, &node, J, 2); + push_status(J, mpv_command_node_async(jclient(J), id, &node)); +} + // args: none, result in millisec static void script_get_time_ms(js_State *J) { @@ -1283,6 +1292,13 @@ static void script_wait_event(js_State *J) js_setproperty(J, -2, "hook_id"); // reply.hook_id (is a number) break; } + + case MPV_EVENT_COMMAND_REPLY: { + mpv_event_command *cmd = event->data; + pushnode(J, &cmd->result); + js_setproperty(J, -2, "result"); // reply.result (mpv node) + break; + } } // switch (event->event_id) assert(top == js_gettop(J) - 1); @@ -1310,6 +1326,7 @@ static const struct fn_entry main_fns[] = { FN_ENTRY(command, 1), FN_ENTRY(commandv, 0), AF_ENTRY(command_native, 2), + AF_ENTRY(_command_native_async, 2), FN_ENTRY(get_property_bool, 2), FN_ENTRY(get_property_number, 2), AF_ENTRY(get_property_native, 2), diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 6e5686ff3d..cd67a7a2e8 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -17,7 +17,7 @@ function new_cache() { } /********************************************************************** - * event handlers, property observers, idle, client messages, hooks + * event handlers, property observers, idle, client messages, hooks, async *********************************************************************/ var ehandlers = new_cache() // items of event-name: array of {maybe cb: fn} @@ -138,6 +138,25 @@ mp.add_hook = function add_hook(name, pri, fn) { return mp._hook_add(name, pri - 50, hooks.length); } +// ----- async commands ----- +var async_callbacks = new_cache(); // items of id: fn +var async_next_id = 1; + +mp.command_native_async = function command_native_async(node, cb) { + var id = async_next_id++; + async_callbacks[id] = cb; + return mp._command_native_async(id, node); +} + +function async_command_handler(ev) { + var cb = async_callbacks[ev.id]; + delete async_callbacks[ev.id]; + if (ev.error) + cb(false, undefined, ev.error); + else + cb(true, ev.result, ""); +} + /********************************************************************** * key bindings *********************************************************************/ @@ -557,6 +576,7 @@ g.exit = function() { mp.keep_running = false }; // user-facing too mp.register_event("shutdown", g.exit); mp.register_event("property-change", notify_observer); mp.register_event("hook", run_hook); +mp.register_event("command-reply", async_command_handler); mp.register_event("client-message", dispatch_message); mp.register_script_message("key-binding", dispatch_key_binding); -- cgit v1.2.3