defaults.lua: add an exit() function

Scripts can terminate execution by setting mp.keep_running = false. Add
an exit() function to wrap setting mp.keep_running and properly expose
this feature. It can be used e.g. by a thumbnail script to spawn workers
with load-script and then let them quit.

It is not added to the mp namespace as mp.exit because that would make
it look like it terminates mpv.

This mirrors the exit() function which already exists in js.

The note in javascript.rst about having to remove key bindings before
exit is not kept because they are actually removed automatically since
bf385e1140 (though it was accurate when the JS backend was developed
before upstreaming it).
This commit is contained in:
Guido Cella
2024-11-01 16:15:36 +01:00
committed by Avi Halachmi
parent e734f5ae33
commit bb0b9f4cc8
4 changed files with 22 additions and 5 deletions

View File

@@ -416,6 +416,10 @@ end
-- used by default event loop (mp_event_loop()) to decide when to quit
mp.keep_running = true
function _G.exit()
mp.keep_running = false
end
local event_handlers = {}
function mp.register_event(name, cb)
@@ -455,7 +459,7 @@ function mp.unregister_event(cb)
end
-- default handlers
mp.register_event("shutdown", function() mp.keep_running = false end)
mp.register_event("shutdown", exit)
mp.register_event("client-message", message_dispatch)
mp.register_event("property-change", property_change)