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/defaults.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'player/lua') 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 From 548ef07864f3e1a40f731b2643f037435ceae46d Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 12 May 2018 15:36:43 +0200 Subject: lua: reimplement mp.subprocess() by invoking the new subprocess command We keep mp.subprocess() with roughly the same semantics for compatibility with scripts (including the internal ytdl script). Seems to work with rhe ytdl wrapper. Not tested further. --- player/lua/defaults.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'player/lua') diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 6f5a9c4b6c..feb400111a 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -617,4 +617,27 @@ function mp_utils.format_bytes_humanized(b) return string.format("%0.2f %s", b, d[i] and d[i] or "*1024^" .. (i-1)) end +function mp_utils.subprocess(t) + local cmd = {} + cmd.name = "subprocess" + cmd.capture_stdout = true + for k, v in pairs(t) do + if k == "cancellable" then + k = "playback_only" + elseif k == "max_size" then + k = "capture_size" + end + cmd[k] = v + end + local res, err = mp.command_native(cmd) + if res == nil then + -- an error usually happens only if parsing failed (or no args passed) + res = {error_string = err, status = -1} + end + if res.error_string ~= "" then + res.error = res.error_string + end + return res +end + return {} -- cgit v1.2.3 From 7f91e2684e8600c45512e36f03aadff0b825a1b0 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 12 May 2018 16:03:04 +0200 Subject: lua: reimplement mp.subprocess_detached() by invoking the "run" command The "run" command is old. I'm not sure why the separate Lua implementation was added. But maybe it as because the "run" command used to be limited to a small number of arguments. This limit has been removed a while ago. In any case, the old implementation is not needed anymore. --- player/lua/defaults.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'player/lua') diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index feb400111a..a00a563478 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -640,4 +640,8 @@ function mp_utils.subprocess(t) return res end +function mp_utils.subprocess_detached(t) + mp.commandv("run", unpack(t.args)) +end + return {} -- cgit v1.2.3 From dbe831bd025d34930b97c493d9ef61278408cf46 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 12 May 2018 18:48:35 +0200 Subject: lua: expose mpv_abort_async_command() Also somewhat cleans up mp.command_native_async() error handling. --- player/lua/defaults.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'player/lua') diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index a00a563478..1da6420182 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -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) -- cgit v1.2.3