From f60826c3a14ba3b49077f17e5364b7347f9b468a Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 23 Mar 2018 16:24:49 +0100 Subject: client API: add a first class hook API, and deprecate old API As it turns out, there are multiple libmpv users who saw a need to use the hook API. The API is kind of shitty and was never meant to be actually public (it was mostly a hack for the ytdl script). Introduce a proper API and deprecate the old one. The old one will probably continue to work for a few releases, but will be removed eventually. There are some slight changes to the old API, but if a user followed the manual properly, it won't break. Mostly untested. Appears to work with ytdl_hook. --- player/lua/defaults.lua | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'player/lua/defaults.lua') diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 32dfed9948..d5bb194c50 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -511,24 +511,21 @@ function mp.osd_message(text, duration) end local hook_table = {} -local hook_registered = false -local function hook_run(id, cont) - local fn = hook_table[tonumber(id)] +mp.register_event("hook", function(ev) + local fn = hook_table[tonumber(ev.id)] if fn then fn() end - mp.commandv("hook-ack", cont) -end + mp.raw_hook_continue(ev.hook_id) +end) function mp.add_hook(name, pri, cb) - if not hook_registered then - mp.register_script_message("hook_run", hook_run) - hook_registered = true - end local id = #hook_table + 1 hook_table[id] = cb - mp.commandv("hook-add", name, id, pri) + -- The C API suggests using 0 for a neutral priority, but lua.rst suggests + -- 50 (?), so whatever. + mp.raw_hook_add(id, name, pri - 50) end local mp_utils = package.loaded["mp.utils"] -- cgit v1.2.3