Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaults.lua: add an exit() function #15235

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local mp_globals = {
commandv = {},
command_native = {},
command_native_async = {},
add_hook = {},
abort_async_command = {},
del_property = {},
get_property = {},
Expand Down Expand Up @@ -53,7 +54,6 @@ local mp_globals = {
},
-- Not documented
-- TODO: Document or remove them
add_hook = {},
disable_key_bindings = {},
enable_key_bindings = {},
find_config_file = {},
Expand All @@ -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 @@ -646,6 +646,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
3 changes: 1 addition & 2 deletions player/lua/auto_profiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ mp.observe_property("profile-list", "native", function (_, profiles_property)
load_profiles(profiles_property)

if #profiles < 1 and mp.get_property("load-auto-profiles") == "auto" then
-- make it exit immediately
_G.mp_event_loop = function() end
exit()
return
end

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
Loading