Skip to content

Commit

Permalink
defaults.lua: add an exit() function
Browse files Browse the repository at this point in the history
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
bf385e1 (though it was accurate when the JS backend was developed
before upstreaming it).
  • Loading branch information
guidocella authored and avih committed Nov 2, 2024
1 parent e734f5a commit bb0b9f4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ local mp_globals = {
set_osd_ass = {},
}
},
exit = {},
unpack = {},
}

Expand Down
6 changes: 2 additions & 4 deletions DOCS/man/javascript.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ string/boolean/number)

``mp.input.set_log(log)``

``exit()`` (global)

Additional utilities
--------------------

Expand Down Expand Up @@ -256,10 +258,6 @@ text content only.
``mp.get_script_file()``
Returns the file name of the current script.

``exit()`` (global)
Make the script exit at the end of the current event loop iteration.
Note: please remove added key bindings before calling ``exit()``.

``mp.utils.compile_js(fname, content_str)``
Compiles the JS code ``content_str`` as file name ``fname`` (without loading
anything from the filesystem), and returns it as a function. Very similar
Expand Down
14 changes: 14 additions & 0 deletions DOCS/man/lua.rst
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,20 @@ are useful only in special situations.

May return invalid/nonsense values if OSD is not initialized yet.

``exit()`` (global)
Make the script exit at the end of the current event loop iteration. This
does not terminate mpv itself or other scripts.

This can be polyfilled to support mpv versions older than 0.40 with:

::

if not _G.exit then
function exit()
mp.keep_running = false
end
end

mp.msg functions
----------------

Expand Down
6 changes: 5 additions & 1 deletion player/lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit bb0b9f4

Please sign in to comment.