Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
LUA SCRIPTING
|
|
|
|
=============
|
|
|
|
|
2020-02-01 17:10:07 +00:00
|
|
|
mpv can load Lua scripts. (See `Script location`_.)
|
2014-09-24 18:17:49 +00:00
|
|
|
|
2014-12-25 18:45:17 +00:00
|
|
|
mpv provides the built-in module ``mp``, which contains functions to send
|
2014-09-24 18:17:49 +00:00
|
|
|
commands to the mpv core and to retrieve information about playback state, user
|
|
|
|
settings, file information, and so on.
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
|
2014-02-17 19:24:30 +00:00
|
|
|
These scripts can be used to control mpv in a similar way to slave mode.
|
|
|
|
Technically, the Lua code uses the client API internally.
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
|
2014-02-17 19:24:30 +00:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
A script which leaves fullscreen mode when the player is paused:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2014-11-29 19:27:47 +00:00
|
|
|
function on_pause_change(name, value)
|
|
|
|
if value == true then
|
2014-11-29 18:11:07 +00:00
|
|
|
mp.set_property("fullscreen", "no")
|
|
|
|
end
|
2014-02-17 19:24:30 +00:00
|
|
|
end
|
2014-11-29 18:11:07 +00:00
|
|
|
mp.observe_property("pause", "bool", on_pause_change)
|
2014-02-17 19:24:30 +00:00
|
|
|
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
|
2020-02-01 17:10:07 +00:00
|
|
|
Script location
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Scripts can be passed to the ``--script`` option, and are automatically loaded
|
|
|
|
from the ``scripts`` subdirectory of the mpv configuration directory (usually
|
|
|
|
``~/.config/mpv/scripts/``).
|
|
|
|
|
|
|
|
A script can be a single file. The file extension is used to select the
|
|
|
|
scripting backend to use for it. For Lua, it is ``.lua``. If the extension is
|
|
|
|
not recognized, an error is printed. (If an error happens, the extension is
|
|
|
|
either mistyped, or the backend was not compiled into your mpv binary.)
|
|
|
|
|
|
|
|
Entries with ``.disable`` extension are always ignored.
|
|
|
|
|
|
|
|
If a script is a directory (either if a directory is passed to ``--script``,
|
|
|
|
or any sub-directories in the script directory, such as for example
|
|
|
|
``~/.config/mpv/scripts/something/``), then the directory represents a single
|
|
|
|
script. The player will try to load a file named ``main.x``, where ``x`` is
|
|
|
|
replaced with the file extension. For example, if ``main.lua`` exists, it is
|
|
|
|
loaded with the Lua scripting backend.
|
|
|
|
|
|
|
|
You must not put any other files or directories that start with ``main.`` into
|
|
|
|
the script's top level directory. If the script directory contains for example
|
|
|
|
both ``main.lua`` and ``main.js``, only one of them will be loaded (and which
|
|
|
|
one depends on mpv internals that may change any time). Likewise, if there is
|
|
|
|
for example ``main.foo``, your script will break as soon as mpv adds a backend
|
|
|
|
that uses the ``.foo`` file extension.
|
|
|
|
|
|
|
|
mpv also appends the top level directory of the script to the start of Lua's
|
|
|
|
package path so you can import scripts from there too. Be aware that this will
|
2020-02-04 19:40:16 +00:00
|
|
|
shadow Lua libraries that use the same package path. (Single file scripts do not
|
|
|
|
include mpv specific directory the Lua package path. This was silently changed
|
|
|
|
in mpv 0.32.0.)
|
2020-02-01 17:10:07 +00:00
|
|
|
|
2020-02-04 19:40:16 +00:00
|
|
|
Using a script directory is the recommended way to package a script that
|
|
|
|
consists of multiple source files, or requires other files (you can use
|
|
|
|
``mp.get_script_directory()`` to get the location and e.g. load data files).
|
|
|
|
|
|
|
|
Making a script a git repository, basically a repository which contains a
|
|
|
|
``main.lua``` file in the root directory, makes scripts easily updateable
|
|
|
|
(without the dangers of auto-updates). Another suggestion is to use git
|
|
|
|
submodules to share common files or libraries.
|
2020-02-01 17:10:07 +00:00
|
|
|
|
2014-12-25 18:45:17 +00:00
|
|
|
Details on the script initialization and lifecycle
|
|
|
|
--------------------------------------------------
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-12-14 23:31:30 +00:00
|
|
|
Your script will be loaded by the player at program start from the ``scripts``
|
2014-12-25 18:45:17 +00:00
|
|
|
configuration subdirectory, or from a path specified with the ``--script``
|
|
|
|
option. Some scripts are loaded internally (like ``--osc``). Each script runs in
|
|
|
|
its own thread. Your script is first run "as is", and once that is done, the event loop
|
2014-10-03 09:18:48 +00:00
|
|
|
is entered. This event loop will dispatch events received by mpv and call your
|
|
|
|
own event handlers which you have registered with ``mp.register_event``, or
|
2017-12-06 06:07:20 +00:00
|
|
|
timers added with ``mp.add_timeout`` or similar. Note that since the
|
|
|
|
script starts execution concurrently with player initialization, some properties
|
|
|
|
may not be populated with meaningful values until the relevant subsystems have
|
|
|
|
initialized.
|
2014-02-11 00:25:15 +00:00
|
|
|
|
2014-02-11 16:41:14 +00:00
|
|
|
When the player quits, all scripts will be asked to terminate. This happens via
|
|
|
|
a ``shutdown`` event, which by default will make the event loop return. If your
|
2016-11-22 13:47:50 +00:00
|
|
|
script got into an endless loop, mpv will probably behave fine during playback,
|
|
|
|
but it won't terminate when quitting, because it's waiting on your script.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
Internally, the C code will call the Lua function ``mp_event_loop`` after
|
|
|
|
loading a Lua script. This function is normally defined by the default prelude
|
|
|
|
loaded before your script (see ``player/lua/defaults.lua`` in the mpv sources).
|
2014-02-12 19:15:58 +00:00
|
|
|
The event loop will wait for events and dispatch events registered with
|
|
|
|
``mp.register_event``. It will also handle timers added with ``mp.add_timeout``
|
|
|
|
and similar (by waiting with a timeout).
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-09-06 15:02:47 +00:00
|
|
|
Since mpv 0.6.0, the player will wait until the script is fully loaded before
|
|
|
|
continuing normal operation. The player considers a script as fully loaded as
|
|
|
|
soon as it starts waiting for mpv events (or it exits). In practice this means
|
|
|
|
the player will more or less hang until the script returns from the main chunk
|
|
|
|
(and ``mp_event_loop`` is called), or the script calls ``mp_event_loop`` or
|
|
|
|
``mp.dispatch_events`` directly. This is done to make it possible for a script
|
|
|
|
to fully setup event handlers etc. before playback actually starts. In older
|
scripting: change when/how player waits for scripts being loaded
Fundamentally, scripts are loaded asynchronously, but as a feature,
there was code to wait until a script is loaded (for a certain arbitrary
definition of "loaded"). This was done in scripting.c with the
wait_loaded() function.
This called mp_idle(), and since there are commands to load/unload
scripts, it meant the player core loop could be entered recursively. I
think this is a major complication and has some problems. For example,
if you had a script that does 'os.execute("sleep inf")', then every time
you ran a command to load an instance of the script would add a new
stack frame of mp_idle(). This would lead to some sort of reentrancy
horror that is hard to debug. Also misc/dispatch.c contains a somewhat
tricky mess to support such recursive invocations. There were also some
bugs due to this and due to unforeseen interactions with other messes.
This scripting stuff was the only thing making use of that reentrancy,
and future commands that have "logical" waiting for something should be
implemented differently. So get rid of it.
Change the code to wait only in the player initialization phase: the
only place where it really has to wait is before playback is started,
because scripts might want to set options or hooks that interact with
playback initialization. Unloading of builtin scripts (can happen with
e.g. "set osc no") is left asynchronous; the unloading wasn't too robust
anyway, and this change won't make a difference if someone is trying to
break it intentionally. Note that this is not in mp_initialize(),
because mpv_initialize() uses this by locking the core, which would have
the same problem.
In the future, commands which logically wait should use different
mechanisms. Originally I thought the current approach (that is removed
with this commit) should be used, but it's too much of a mess and can't
even be used in some cases. Examples are:
- "loadfile" should be made blocking (needs to run the normal player
code and manually unblock the thread issuing the command)
- "add-sub" should not freeze the player until the URL is opened (needs
to run opening on a separate thread)
Possibly the current scripting behavior could be restored once new
mechanisms exist, and if it turns out that anyone needs it.
With this commit there should be no further instances of recursive
playloop invocations (other than the case in the following commit),
since all mp_idle()/mp_wait_events() calls are done strictly from the
main thread (and not commands/properties or libmpv client API that
"lock" the main thread).
2018-04-15 08:14:00 +00:00
|
|
|
mpv versions, this happened asynchronously. With mpv 0.29.0, this changes
|
|
|
|
slightly, and it merely waits for scripts to be loaded in this manner before
|
|
|
|
starting playback as part of the player initialization phase. Scripts run though
|
|
|
|
initialization in parallel. This might change again.
|
2014-09-06 15:02:47 +00:00
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
mp functions
|
|
|
|
------------
|
|
|
|
|
|
|
|
The ``mp`` module is preloaded, although it can be loaded manually with
|
|
|
|
``require 'mp'``. It provides the core client API.
|
|
|
|
|
|
|
|
``mp.command(string)``
|
2014-02-12 18:24:30 +00:00
|
|
|
Run the given command. This is similar to the commands used in input.conf.
|
|
|
|
See `List of Input Commands`_.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2015-05-16 10:32:34 +00:00
|
|
|
By default, this will show something on the OSD (depending on the command),
|
|
|
|
as if it was used in ``input.conf``. See `Input Command Prefixes`_ how
|
|
|
|
to influence OSD usage per command.
|
|
|
|
|
2014-05-11 13:42:12 +00:00
|
|
|
Returns ``true`` on success, or ``nil, error`` on error.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-02-11 00:25:26 +00:00
|
|
|
``mp.commandv(arg1, arg2, ...)``
|
|
|
|
Similar to ``mp.command``, but pass each command argument as separate
|
|
|
|
parameter. This has the advantage that you don't have to care about
|
|
|
|
quoting and escaping in some cases.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2014-02-11 14:21:41 +00:00
|
|
|
mp.command("loadfile " .. filename .. " append")
|
2014-02-11 00:25:26 +00:00
|
|
|
mp.commandv("loadfile", filename, "append")
|
|
|
|
|
|
|
|
These two commands are equivalent, except that the first version breaks
|
|
|
|
if the filename contains spaces or certain special characters.
|
|
|
|
|
2014-08-25 00:55:48 +00:00
|
|
|
Note that properties are *not* expanded. You can use either ``mp.command``,
|
|
|
|
the ``expand-properties`` prefix, or the ``mp.get_property`` family of
|
|
|
|
functions.
|
|
|
|
|
2015-05-16 10:32:34 +00:00
|
|
|
Unlike ``mp.command``, this will not use OSD by default either (except
|
2015-06-17 17:37:25 +00:00
|
|
|
for some OSD-specific commands).
|
2015-05-16 10:32:34 +00:00
|
|
|
|
2014-10-10 22:25:07 +00:00
|
|
|
``mp.command_native(table [,def])``
|
|
|
|
Similar to ``mp.commandv``, but pass the argument list as table. This has
|
|
|
|
the advantage that in at least some cases, arguments can be passed as
|
2018-05-12 12:50:07 +00:00
|
|
|
native types. It also allows you to use named argument.
|
|
|
|
|
|
|
|
If the table is an array, each array item is like an argument in
|
|
|
|
``mp.commandv()`` (but can be a native type instead of a string).
|
|
|
|
|
|
|
|
If the table contains string keys, it's interpreted as command with named
|
|
|
|
arguments. This requires at least an entry with the key ``name`` to be
|
|
|
|
present, which must be a string, and contains the command name. The special
|
|
|
|
entry ``_flags`` is optional, and if present, must be an array of
|
|
|
|
`Input Command Prefixes`_ to apply. All other entries are interpreted as
|
|
|
|
arguments.
|
2014-10-10 22:25:07 +00:00
|
|
|
|
|
|
|
Returns a result table on success (usually empty), or ``def, error`` on
|
|
|
|
error. ``def`` is the second parameter provided to the function, and is
|
|
|
|
nil if it's missing.
|
|
|
|
|
2018-05-10 13:26:27 +00:00
|
|
|
``mp.command_native_async(table [,fn])``
|
|
|
|
Like ``mp.command_native()``, but the command is ran asynchronously (as far
|
2020-02-16 22:03:14 +00:00
|
|
|
as possible), and upon completion, fn is called. fn has three arguments:
|
|
|
|
``fn(success, result, error)``:
|
|
|
|
|
|
|
|
``success``
|
|
|
|
Always a Boolean and is true if the command was successful,
|
|
|
|
otherwise false.
|
|
|
|
|
|
|
|
``result``
|
|
|
|
The result value (can be nil) in case of success, nil otherwise (as
|
|
|
|
returned by ``mp.command_native()``).
|
|
|
|
|
|
|
|
``error``
|
|
|
|
The error string in case of an error, nil otherwise.
|
2018-05-10 13:26:27 +00:00
|
|
|
|
2018-05-12 16:48:35 +00:00
|
|
|
Returns a table with undefined contents, which can be used as argument for
|
|
|
|
``mp.abort_async_command``.
|
|
|
|
|
|
|
|
If starting the command failed for some reason, ``nil, error`` is returned,
|
|
|
|
and ``fn`` is called indicating failure, using the same error value.
|
|
|
|
|
|
|
|
``mp.abort_async_command(t)``
|
|
|
|
Abort a ``mp.command_native_async`` call. The argument is the return value
|
|
|
|
of that command (which starts asynchronous execution of the command).
|
|
|
|
Whether this works and how long it takes depends on the command and the
|
|
|
|
situation. The abort call itself is asynchronous. Does not return anything.
|
|
|
|
|
2014-02-16 23:19:30 +00:00
|
|
|
``mp.get_property(name [,def])``
|
2014-02-11 00:05:05 +00:00
|
|
|
Return the value of the given property as string. These are the same
|
|
|
|
properties as used in input.conf. See `Properties`_ for a list of
|
|
|
|
properties. The returned string is formatted similar to ``${=name}``
|
|
|
|
(see `Property Expansion`_).
|
|
|
|
|
2014-02-16 23:19:30 +00:00
|
|
|
Returns the string on success, or ``def, error`` on error. ``def`` is the
|
|
|
|
second parameter provided to the function, and is nil if it's missing.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-02-16 23:19:30 +00:00
|
|
|
``mp.get_property_osd(name [,def])``
|
2014-02-11 00:05:05 +00:00
|
|
|
Similar to ``mp.get_property``, but return the property value formatted for
|
|
|
|
OSD. This is the same string as printed with ``${name}`` when used in
|
|
|
|
input.conf.
|
|
|
|
|
2014-02-16 23:19:30 +00:00
|
|
|
Returns the string on success, or ``def, error`` on error. ``def`` is the
|
|
|
|
second parameter provided to the function, and is an empty string if it's
|
|
|
|
missing. Unlike ``get_property()``, assigning the return value to a variable
|
|
|
|
will always result in a string.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-02-24 19:47:20 +00:00
|
|
|
``mp.get_property_bool(name [,def])``
|
2014-09-01 02:25:57 +00:00
|
|
|
Similar to ``mp.get_property``, but return the property value as Boolean.
|
2014-02-24 19:47:20 +00:00
|
|
|
|
2014-09-01 02:25:57 +00:00
|
|
|
Returns a Boolean on success, or ``def, error`` on error.
|
2014-02-24 19:47:20 +00:00
|
|
|
|
|
|
|
``mp.get_property_number(name [,def])``
|
|
|
|
Similar to ``mp.get_property``, but return the property value as number.
|
|
|
|
|
|
|
|
Note that while Lua does not distinguish between integers and floats,
|
|
|
|
mpv internals do. This function simply request a double float from mpv,
|
|
|
|
and mpv will usually convert integer property values to float.
|
|
|
|
|
|
|
|
Returns a number on success, or ``def, error`` on error.
|
|
|
|
|
|
|
|
``mp.get_property_native(name [,def])``
|
|
|
|
Similar to ``mp.get_property``, but return the property value using the best
|
2014-09-01 02:25:57 +00:00
|
|
|
Lua type for the property. Most time, this will return a string, Boolean,
|
2014-02-24 19:47:20 +00:00
|
|
|
or number. Some properties (for example ``chapter-list``) are returned as
|
|
|
|
tables.
|
|
|
|
|
|
|
|
Returns a value on success, or ``def, error`` on error. Note that ``nil``
|
|
|
|
might be a possible, valid value too in some corner cases.
|
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
``mp.set_property(name, value)``
|
2014-02-24 19:47:20 +00:00
|
|
|
Set the given property to the given string value. See ``mp.get_property``
|
|
|
|
and `Properties`_ for more information about properties.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
Returns true on success, or ``nil, error`` on error.
|
|
|
|
|
2014-02-24 19:47:20 +00:00
|
|
|
``mp.set_property_bool(name, value)``
|
|
|
|
Similar to ``mp.set_property``, but set the given property to the given
|
2014-09-01 02:25:57 +00:00
|
|
|
Boolean value.
|
2014-02-24 19:47:20 +00:00
|
|
|
|
|
|
|
``mp.set_property_number(name, value)``
|
|
|
|
Similar to ``mp.set_property``, but set the given property to the given
|
|
|
|
numeric value.
|
|
|
|
|
|
|
|
Note that while Lua does not distinguish between integers and floats,
|
|
|
|
mpv internals do. This function will test whether the number can be
|
|
|
|
represented as integer, and if so, it will pass an integer value to mpv,
|
|
|
|
otherwise a double float.
|
|
|
|
|
2014-02-26 21:33:23 +00:00
|
|
|
``mp.set_property_native(name, value)``
|
|
|
|
Similar to ``mp.set_property``, but set the given property using its native
|
|
|
|
type.
|
|
|
|
|
2016-07-09 13:48:27 +00:00
|
|
|
Since there are several data types which cannot represented natively in
|
2014-02-26 21:33:23 +00:00
|
|
|
Lua, this might not always work as expected. For example, while the Lua
|
|
|
|
wrapper can do some guesswork to decide whether a Lua table is an array
|
|
|
|
or a map, this would fail with empty tables. Also, there are not many
|
|
|
|
properties for which it makes sense to use this, instead of
|
|
|
|
``set_property``, ``set_property_bool``, ``set_property_number``.
|
2014-05-11 13:42:12 +00:00
|
|
|
For these reasons, this function should probably be avoided for now, except
|
|
|
|
for properties that use tables natively.
|
2014-02-26 21:33:23 +00:00
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
``mp.get_time()``
|
|
|
|
Return the current mpv internal time in seconds as a number. This is
|
|
|
|
basically the system time, with an arbitrary offset.
|
|
|
|
|
2014-11-20 22:59:47 +00:00
|
|
|
``mp.add_key_binding(key, name|fn [,fn [,flags]])``
|
2014-02-17 19:25:11 +00:00
|
|
|
Register callback to be run on a key binding. The binding will be mapped to
|
|
|
|
the given ``key``, which is a string describing the physical key. This uses
|
|
|
|
the same key names as in input.conf, and also allows combinations
|
2016-03-26 09:44:57 +00:00
|
|
|
(e.g. ``ctrl+a``). If the key is empty or ``nil``, no physical key is
|
|
|
|
registered, but the user still can create own bindings (see below).
|
2014-02-17 19:25:11 +00:00
|
|
|
|
|
|
|
After calling this function, key presses will cause the function ``fn`` to
|
2014-09-01 02:25:57 +00:00
|
|
|
be called (unless the user remapped the key with another binding).
|
2014-02-17 19:25:11 +00:00
|
|
|
|
|
|
|
The ``name`` argument should be a short symbolic string. It allows the user
|
2015-12-29 00:36:43 +00:00
|
|
|
to remap the key binding via input.conf using the ``script-message``
|
2014-03-17 17:26:56 +00:00
|
|
|
command, and the name of the key binding (see below for
|
2014-02-17 19:25:11 +00:00
|
|
|
an example). The name should be unique across other bindings in the same
|
|
|
|
script - if not, the previous binding with the same name will be
|
|
|
|
overwritten. You can omit the name, in which case a random name is generated
|
2019-11-23 13:40:00 +00:00
|
|
|
internally. (Omitting works as follows: either pass ``nil`` for ``name``,
|
|
|
|
or pass the ``fn`` argument in place of the name. The latter is not
|
|
|
|
recommended and is handled for compatibility only.)
|
2014-02-17 19:25:11 +00:00
|
|
|
|
2014-11-23 14:08:49 +00:00
|
|
|
The last argument is used for optional flags. This is a table, which can
|
|
|
|
have the following entries:
|
2014-11-20 22:59:47 +00:00
|
|
|
|
2014-11-23 14:08:49 +00:00
|
|
|
``repeatable``
|
|
|
|
If set to ``true``, enables key repeat for this specific binding.
|
|
|
|
|
|
|
|
``complex``
|
|
|
|
If set to ``true``, then ``fn`` is called on both key up and down
|
|
|
|
events (as well as key repeat, if enabled), with the first
|
2019-11-21 21:30:45 +00:00
|
|
|
argument being a table. This table has the following entries (and
|
|
|
|
may contain undocumented ones):
|
|
|
|
|
2020-02-15 19:01:20 +00:00
|
|
|
``event``
|
|
|
|
Set to one of the strings ``down``, ``repeat``, ``up`` or
|
|
|
|
``press`` (the latter if key up/down can't be tracked).
|
|
|
|
|
|
|
|
``is_mouse``
|
|
|
|
Boolean Whether the event was caused by a mouse button.
|
|
|
|
|
|
|
|
``key_name``
|
|
|
|
The name of they key that triggered this, or ``nil`` if
|
|
|
|
invoked artificially. If the key name is unknown, it's an
|
|
|
|
empty string.
|
|
|
|
|
|
|
|
``key_text``
|
|
|
|
Text if triggered by a text key, otherwise ``nil``. See
|
|
|
|
description of ``script-binding`` command for details (this
|
|
|
|
field is equivalent to the 5th argument).
|
2019-11-21 22:01:56 +00:00
|
|
|
|
2015-12-29 00:36:43 +00:00
|
|
|
Internally, key bindings are dispatched via the ``script-message-to`` or
|
|
|
|
``script-binding`` input commands and ``mp.register_script_message``.
|
2014-02-17 19:25:11 +00:00
|
|
|
|
|
|
|
Trying to map multiple commands to a key will essentially prefer a random
|
|
|
|
binding, while the other bindings are not called. It is guaranteed that
|
|
|
|
user defined bindings in the central input.conf are preferred over bindings
|
|
|
|
added with this function (but see ``mp.add_forced_key_binding``).
|
2014-02-17 01:38:07 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
function something_handler()
|
|
|
|
print("the key was pressed")
|
|
|
|
end
|
|
|
|
mp.add_key_binding("x", "something", something_handler)
|
|
|
|
|
|
|
|
This will print the message ``the key was pressed`` when ``x`` was pressed.
|
|
|
|
|
2014-05-23 11:24:27 +00:00
|
|
|
The user can remap these key bindings. Then the user has to put the
|
2017-04-19 06:13:37 +00:00
|
|
|
following into their input.conf to remap the command to the ``y`` key:
|
2014-02-17 01:38:07 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-12-29 00:36:43 +00:00
|
|
|
y script-binding something
|
2014-03-17 17:26:56 +00:00
|
|
|
|
2014-02-17 01:38:07 +00:00
|
|
|
|
|
|
|
This will print the message when the key ``y`` is pressed. (``x`` will
|
2014-09-01 02:25:57 +00:00
|
|
|
still work, unless the user remaps it.)
|
2014-02-17 01:38:07 +00:00
|
|
|
|
2014-03-17 17:26:56 +00:00
|
|
|
You can also explicitly send a message to a named script only. Assume the
|
|
|
|
above script was using the filename ``fooscript.lua``:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2016-03-19 06:49:52 +00:00
|
|
|
y script-binding fooscript/something
|
2014-03-17 17:26:56 +00:00
|
|
|
|
2014-02-17 01:38:07 +00:00
|
|
|
``mp.add_forced_key_binding(...)``
|
|
|
|
This works almost the same as ``mp.add_key_binding``, but registers the
|
2017-04-19 06:13:37 +00:00
|
|
|
key binding in a way that will overwrite the user's custom bindings in their
|
2014-02-17 01:38:07 +00:00
|
|
|
input.conf. (``mp.add_key_binding`` overwrites default key bindings only,
|
|
|
|
but not those by the user's input.conf.)
|
|
|
|
|
|
|
|
``mp.remove_key_binding(name)``
|
|
|
|
Remove a key binding added with ``mp.add_key_binding`` or
|
|
|
|
``mp.add_forced_key_binding``. Use the same name as you used when adding
|
|
|
|
the bindings. It's not possible to remove bindings for which you omitted
|
|
|
|
the name.
|
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
``mp.register_event(name, fn)``
|
|
|
|
Call a specific function when an event happens. The event name is a string,
|
2014-02-12 19:15:58 +00:00
|
|
|
and the function fn is a Lua function value.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-02-12 19:15:58 +00:00
|
|
|
Some events have associated data. This is put into a Lua table and passed
|
|
|
|
as argument to fn. The Lua table by default contains a ``name`` field,
|
|
|
|
which is a string containing the event name. If the event has an error
|
|
|
|
associated, the ``error`` field is set to a string describing the error,
|
|
|
|
on success it's not set.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-02-14 12:48:08 +00:00
|
|
|
If multiple functions are registered for the same event, they are run in
|
|
|
|
registration order, which the first registered function running before all
|
|
|
|
the other ones.
|
|
|
|
|
2014-02-12 19:15:58 +00:00
|
|
|
Returns true if such an event exists, false otherwise.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-02-12 19:15:58 +00:00
|
|
|
See `Events`_ and `List of events`_ for details.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-03-31 22:36:04 +00:00
|
|
|
``mp.unregister_event(fn)``
|
|
|
|
Undo ``mp.register_event(..., fn)``. This removes all event handlers that
|
|
|
|
are equal to the ``fn`` parameter. This uses normal Lua ``==`` comparison,
|
|
|
|
so be careful when dealing with closures.
|
|
|
|
|
2014-04-08 19:10:00 +00:00
|
|
|
``mp.observe_property(name, type, fn)``
|
|
|
|
Watch a property for changes. If the property ``name`` is changed, then
|
|
|
|
the function ``fn(name)`` will be called. ``type`` can be ``nil``, or be
|
|
|
|
set to one of ``none``, ``native``, ``bool``, ``string``, or ``number``.
|
|
|
|
``none`` is the same as ``nil``. For all other values, the new value of
|
|
|
|
the property will be passed as second argument to ``fn``, using
|
|
|
|
``mp.get_property_<type>`` to retrieve it. This means if ``type`` is for
|
|
|
|
example ``string``, ``fn`` is roughly called as in
|
|
|
|
``fn(name, mp.get_property_string(name))``.
|
|
|
|
|
2014-04-08 20:06:39 +00:00
|
|
|
If possible, change events are coalesced. If a property is changed a bunch
|
|
|
|
of times in a row, only the last change triggers the change function. (The
|
2014-04-08 19:10:00 +00:00
|
|
|
exact behavior depends on timing and other things.)
|
|
|
|
|
2019-12-19 10:19:15 +00:00
|
|
|
If a property is unavailable, or on error, the value argument to ``fn`` is
|
|
|
|
``nil``. (The ``observe_property()`` call always succeeds, even if a
|
|
|
|
property does not exist.)
|
|
|
|
|
2014-04-08 20:06:39 +00:00
|
|
|
In some cases the function is not called even if the property changes.
|
2019-10-08 19:11:55 +00:00
|
|
|
This depends on the property, and it's a valid feature request to ask for
|
|
|
|
better update handling of a specific property.
|
2014-04-08 20:06:39 +00:00
|
|
|
|
|
|
|
If the ``type`` is ``none`` or ``nil``, sporadic property change events are
|
|
|
|
possible. This means the change function ``fn`` can be called even if the
|
|
|
|
property doesn't actually change.
|
|
|
|
|
2019-10-08 19:11:55 +00:00
|
|
|
You always get an initial change notification. This is meant to initialize
|
|
|
|
the user's state to the current value of the property.
|
|
|
|
|
2014-04-08 19:10:00 +00:00
|
|
|
``mp.unobserve_property(fn)``
|
|
|
|
Undo ``mp.observe_property(..., fn)``. This removes all property handlers
|
|
|
|
that are equal to the ``fn`` parameter. This uses normal Lua ``==``
|
|
|
|
comparison, so be careful when dealing with closures.
|
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
``mp.add_timeout(seconds, fn)``
|
|
|
|
Call the given function fn when the given number of seconds has elapsed.
|
2014-04-28 05:03:50 +00:00
|
|
|
Note that the number of seconds can be fractional. For now, the timer's
|
|
|
|
resolution may be as low as 50 ms, although this will be improved in the
|
|
|
|
future.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
This is a one-shot timer: it will be removed when it's fired.
|
|
|
|
|
2014-04-02 15:09:45 +00:00
|
|
|
Returns a timer object. See ``mp.add_periodic_timer`` for details.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
``mp.add_periodic_timer(seconds, fn)``
|
|
|
|
Call the given function periodically. This is like ``mp.add_timeout``, but
|
|
|
|
the timer is re-added after the function fn is run.
|
|
|
|
|
2014-04-02 15:09:45 +00:00
|
|
|
Returns a timer object. The timer object provides the following methods:
|
|
|
|
``stop()``
|
|
|
|
Disable the timer. Does nothing if the timer is already disabled.
|
|
|
|
This will remember the current elapsed time when stopping, so that
|
|
|
|
``resume()`` essentially unpauses the timer.
|
|
|
|
|
|
|
|
``kill()``
|
2014-05-11 13:42:12 +00:00
|
|
|
Disable the timer. Resets the elapsed time. ``resume()`` will
|
|
|
|
restart the timer.
|
2014-04-02 15:09:45 +00:00
|
|
|
|
|
|
|
``resume()``
|
|
|
|
Restart the timer. If the timer was disabled with ``stop()``, this
|
|
|
|
will resume at the time it was stopped. If the timer was disabled
|
|
|
|
with ``kill()``, or if it's a previously fired one-shot timer (added
|
|
|
|
with ``add_timeout()``), this starts the timer from the beginning,
|
|
|
|
using the initially configured timeout.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2016-05-14 17:57:44 +00:00
|
|
|
``is_enabled()``
|
|
|
|
Whether the timer is currently enabled or was previously disabled
|
|
|
|
(e.g. by ``stop()`` or ``kill()``).
|
|
|
|
|
2014-07-28 22:22:21 +00:00
|
|
|
``timeout`` (RW)
|
|
|
|
This field contains the current timeout period. This value is not
|
|
|
|
updated as time progresses. It's only used to calculate when the
|
|
|
|
timer should fire next when the timer expires.
|
|
|
|
|
|
|
|
If you write this, you can call ``t:kill() ; t:resume()`` to reset
|
|
|
|
the current timeout to the new one. (``t:stop()`` won't use the
|
|
|
|
new timeout.)
|
|
|
|
|
|
|
|
``oneshot`` (RW)
|
|
|
|
Whether the timer is periodic (``false``) or fires just once
|
|
|
|
(``true``). This value is used when the timer expires (but before
|
2014-09-01 02:25:57 +00:00
|
|
|
the timer callback function fn is run).
|
2014-07-28 22:22:21 +00:00
|
|
|
|
2019-06-22 22:31:11 +00:00
|
|
|
Note that these are methods, and you have to call them using ``:`` instead
|
2015-04-08 19:21:44 +00:00
|
|
|
of ``.`` (Refer to http://www.lua.org/manual/5.2/manual.html#3.4.9 .)
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
seconds = 0
|
|
|
|
timer = mp.add_periodic_timer(1, function()
|
|
|
|
print("called every second")
|
|
|
|
# stop it after 10 seconds
|
|
|
|
seconds = seconds + 1
|
|
|
|
if seconds >= 10 then
|
|
|
|
timer:kill()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
``mp.get_opt(key)``
|
2014-12-14 23:31:30 +00:00
|
|
|
Return a setting from the ``--script-opts`` option. It's up to the user and
|
2014-02-11 00:05:05 +00:00
|
|
|
the script how this mechanism is used. Currently, all scripts can access
|
|
|
|
this equally, so you should be careful about collisions.
|
|
|
|
|
|
|
|
``mp.get_script_name()``
|
2014-02-17 01:38:07 +00:00
|
|
|
Return the name of the current script. The name is usually made of the
|
2014-05-01 23:25:58 +00:00
|
|
|
filename of the script, with directory and file extension removed. If
|
2015-12-19 08:26:27 +00:00
|
|
|
there are several scripts which would have the same name, it's made unique
|
2014-05-01 23:25:58 +00:00
|
|
|
by appending a number.
|
2014-02-17 01:38:07 +00:00
|
|
|
|
|
|
|
.. admonition:: Example
|
|
|
|
|
2014-05-01 23:25:58 +00:00
|
|
|
The script ``/path/to/fooscript.lua`` becomes ``fooscript``.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2020-02-04 19:40:16 +00:00
|
|
|
``mp.get_script_directory()``
|
|
|
|
Return the directory if this is a script packaged as directory (see
|
|
|
|
`Script location`_ for a description). Return nothing if this is a single
|
|
|
|
file script.
|
|
|
|
|
2014-04-12 18:44:13 +00:00
|
|
|
``mp.osd_message(text [,duration])``
|
|
|
|
Show an OSD message on the screen. ``duration`` is in seconds, and is
|
|
|
|
optional (uses ``--osd-duration`` by default).
|
|
|
|
|
|
|
|
Advanced mp functions
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
These also live in the ``mp`` module, but are documented separately as they
|
|
|
|
are useful only in special situations.
|
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
``mp.suspend()``
|
2016-11-22 13:47:50 +00:00
|
|
|
This function has been deprecated in mpv 0.21.0 and does nothing starting
|
|
|
|
with mpv 0.23.0 (no replacement).
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
``mp.resume()``
|
2016-11-22 13:47:50 +00:00
|
|
|
This function has been deprecated in mpv 0.21.0 and does nothing starting
|
|
|
|
with mpv 0.23.0 (no replacement).
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
``mp.resume_all()``
|
2016-11-22 13:47:50 +00:00
|
|
|
This function has been deprecated in mpv 0.21.0 and does nothing starting
|
|
|
|
with mpv 0.23.0 (no replacement).
|
2014-02-11 00:05:05 +00:00
|
|
|
|
2014-04-12 18:13:53 +00:00
|
|
|
``mp.get_wakeup_pipe()``
|
|
|
|
Calls ``mpv_get_wakeup_pipe()`` and returns the read end of the wakeup
|
2018-03-23 15:24:17 +00:00
|
|
|
pipe. This is deprecated, but still works. (See ``client.h`` for details.)
|
2014-04-12 18:13:53 +00:00
|
|
|
|
2014-04-12 18:41:12 +00:00
|
|
|
``mp.get_next_timeout()``
|
|
|
|
Return the relative time in seconds when the next timer (``mp.add_timeout``
|
|
|
|
and similar) expires. If there is no timer, return ``nil``.
|
|
|
|
|
|
|
|
``mp.dispatch_events([allow_wait])``
|
|
|
|
This can be used to run custom event loops. If you want to have direct
|
|
|
|
control what the Lua script does (instead of being called by the default
|
|
|
|
event loop), you can set the global variable ``mp_event_loop`` to your
|
|
|
|
own function running the event loop. From your event loop, you should call
|
2014-09-01 02:25:57 +00:00
|
|
|
``mp.dispatch_events()`` to dequeue and dispatch mpv events.
|
2014-04-12 18:41:12 +00:00
|
|
|
|
|
|
|
If the ``allow_wait`` parameter is set to ``true``, the function will block
|
|
|
|
until the next event is received or the next timer expires. Otherwise (and
|
|
|
|
this is the default behavior), it returns as soon as the event loop is
|
|
|
|
emptied. It's strongly recommended to use ``mp.get_next_timeout()`` and
|
|
|
|
``mp.get_wakeup_pipe()`` if you're interested in properly working
|
|
|
|
notification of new events and working timers.
|
|
|
|
|
2016-09-21 12:36:10 +00:00
|
|
|
``mp.register_idle(fn)``
|
|
|
|
Register an event loop idle handler. Idle handlers are called before the
|
|
|
|
script goes to sleep after handling all new events. This can be used for
|
|
|
|
example to delay processing of property change events: if you're observing
|
|
|
|
multiple properties at once, you might not want to act on each property
|
|
|
|
change, but only when all change notifications have been received.
|
|
|
|
|
2017-01-15 15:22:19 +00:00
|
|
|
``mp.unregister_idle(fn)``
|
|
|
|
Undo ``mp.register_idle(fn)``. This removes all idle handlers that
|
|
|
|
are equal to the ``fn`` parameter. This uses normal Lua ``==`` comparison,
|
|
|
|
so be careful when dealing with closures.
|
|
|
|
|
2014-02-12 19:34:36 +00:00
|
|
|
``mp.enable_messages(level)``
|
|
|
|
Set the minimum log level of which mpv message output to receive. These
|
|
|
|
messages are normally printed to the terminal. By calling this function,
|
|
|
|
you can set the minimum log level of messages which should be received with
|
|
|
|
the ``log-message`` event. See the description of this event for details.
|
|
|
|
The level is a string, see ``msg.log`` for allowed log levels.
|
|
|
|
|
2014-03-17 17:27:25 +00:00
|
|
|
``mp.register_script_message(name, fn)``
|
2016-05-09 18:39:33 +00:00
|
|
|
This is a helper to dispatch ``script-message`` or ``script-message-to``
|
|
|
|
invocations to Lua functions. ``fn`` is called if ``script-message`` or
|
|
|
|
``script-message-to`` (with this script as destination) is run
|
2014-02-17 19:29:27 +00:00
|
|
|
with ``name`` as first parameter. The other parameters are passed to ``fn``.
|
2014-03-17 17:27:25 +00:00
|
|
|
If a message with the given name is already registered, it's overwritten.
|
2014-02-17 19:29:27 +00:00
|
|
|
|
|
|
|
Used by ``mp.add_key_binding``, so be careful about name collisions.
|
|
|
|
|
2014-03-17 17:27:25 +00:00
|
|
|
``mp.unregister_script_message(name)``
|
|
|
|
Undo a previous registration with ``mp.register_script_message``. Does
|
2014-02-17 19:29:27 +00:00
|
|
|
nothing if the ``name`` wasn't registered.
|
|
|
|
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
``mp.create_osd_overlay(format)``
|
|
|
|
Create an OSD overlay. This is a very thin wrapper around the ``osd-overlay``
|
|
|
|
command. The function returns a table, which mostly contains fields that
|
|
|
|
will be passed to ``osd-overlay``. The ``format`` parameter is used to
|
|
|
|
initialize the ``format`` field. The ``data`` field contains the text to
|
|
|
|
be used as overlay. For details, see the ``osd-overlay`` command.
|
|
|
|
|
|
|
|
In addition, it provides the following methods:
|
|
|
|
|
|
|
|
``update()``
|
|
|
|
Commit the OSD overlay to the screen, or in other words, run the
|
|
|
|
``osd-overlay`` command with the current fields of the overlay table.
|
2020-03-06 17:20:11 +00:00
|
|
|
Returns the result of the ``osd-overlay`` command itself.
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
|
|
|
|
``remove()``
|
|
|
|
Remove the overlay from the screen. A ``update()`` call will add it
|
|
|
|
again.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
ov = mp.create_osd_overlay("ass-events")
|
|
|
|
ov.data = "{\\an5}{\\b1}hello world!"
|
|
|
|
ov:update()
|
|
|
|
|
|
|
|
The advantage of using this wrapper (as opposed to running ``osd-overlay``
|
|
|
|
directly) is that the ``id`` field is allocated automatically.
|
|
|
|
|
|
|
|
``mp.get_osd_size()``
|
|
|
|
Returns a tuple of ``osd_width, osd_height, osd_par``. The first two give
|
|
|
|
the size of the OSD in pixels (for video ouputs like ``--vo=xv``, this may
|
|
|
|
be "scaled" pixels). The third is the display pixel aspect ratio.
|
|
|
|
|
|
|
|
May return invalid/nonsense values if OSD is not initialized yet.
|
|
|
|
|
2014-02-11 00:05:05 +00:00
|
|
|
mp.msg functions
|
|
|
|
----------------
|
|
|
|
|
|
|
|
This module allows outputting messages to the terminal, and can be loaded
|
|
|
|
with ``require 'mp.msg'``.
|
|
|
|
|
|
|
|
``msg.log(level, ...)``
|
|
|
|
The level parameter is the message priority. It's a string and one of
|
2017-12-16 08:18:20 +00:00
|
|
|
``fatal``, ``error``, ``warn``, ``info``, ``v``, ``debug``, ``trace``. The
|
|
|
|
user's settings will determine which of these messages will be
|
|
|
|
visible. Normally, all messages are visible, except ``v``, ``debug`` and
|
|
|
|
``trace``.
|
2014-02-11 00:05:05 +00:00
|
|
|
|
|
|
|
The parameters after that are all converted to strings. Spaces are inserted
|
|
|
|
to separate multiple parameters.
|
|
|
|
|
|
|
|
You don't need to add newlines.
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
|
2017-12-16 08:18:20 +00:00
|
|
|
``msg.fatal(...)``, ``msg.error(...)``, ``msg.warn(...)``, ``msg.info(...)``, ``msg.verbose(...)``, ``msg.debug(...)``, ``msg.trace(...)``
|
2014-02-11 00:05:05 +00:00
|
|
|
All of these are shortcuts and equivalent to the corresponding
|
|
|
|
``msg.log(level, ...)`` call.
|
2014-02-12 19:15:58 +00:00
|
|
|
|
2014-05-25 17:43:25 +00:00
|
|
|
mp.options functions
|
|
|
|
--------------------
|
2014-05-23 11:24:27 +00:00
|
|
|
|
|
|
|
mpv comes with a built-in module to manage options from config-files and the
|
|
|
|
command-line. All you have to do is to supply a table with default options to
|
|
|
|
the read_options function. The function will overwrite the default values
|
|
|
|
with values found in the config-file and the command-line (in that order).
|
|
|
|
|
2019-12-20 13:21:16 +00:00
|
|
|
``options.read_options(table [, identifier [, on_update]])``
|
2014-05-23 11:24:27 +00:00
|
|
|
A ``table`` with key-value pairs. The type of the default values is
|
|
|
|
important for converting the values read from the config file or
|
|
|
|
command-line back. Do not use ``nil`` as a default value!
|
|
|
|
|
|
|
|
The ``identifier`` is used to identify the config-file and the command-line
|
|
|
|
options. These needs to unique to avoid collisions with other scripts.
|
2019-12-20 13:21:16 +00:00
|
|
|
Defaults to ``mp.get_script_name()`` if the parameter is ``nil`` or missing.
|
|
|
|
|
|
|
|
The ``on_update`` parameter enables run-time updates of all matching option
|
|
|
|
values via the ``script-opts`` option/property. If any of the matching
|
|
|
|
options changes, the values in the ``table`` (which was originally passed to
|
|
|
|
the function) are changed, and ``on_update(list)`` is called. ``list`` is
|
|
|
|
a table where each updated option has a ``list[option_name] = true`` entry.
|
|
|
|
There is no initial ``on_update()`` call. This never re-reads the config file.
|
2019-12-22 11:30:53 +00:00
|
|
|
``script-opts`` is always applied on the original config file, ignoring
|
|
|
|
previous ``script-opts`` values (for example, if an option is removed from
|
|
|
|
``script-opts`` at runtime, the option will have the value in the config
|
|
|
|
file). ``table`` entries are only written for option values whose values
|
|
|
|
effectively change (this is important if the script changes ``table``
|
|
|
|
entries independently).
|
2014-05-23 11:24:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
Example implementation::
|
|
|
|
|
2014-05-25 17:43:25 +00:00
|
|
|
require 'mp.options'
|
2014-05-23 11:24:27 +00:00
|
|
|
local options = {
|
|
|
|
optionA = "defaultvalueA",
|
|
|
|
optionB = -0.5,
|
|
|
|
optionC = true,
|
|
|
|
}
|
2015-03-05 18:05:58 +00:00
|
|
|
read_options(options, "myscript")
|
2015-05-29 13:50:09 +00:00
|
|
|
print(options.optionA)
|
2014-05-23 11:24:27 +00:00
|
|
|
|
|
|
|
|
2017-12-13 17:37:42 +00:00
|
|
|
The config file will be stored in ``script-opts/identifier.conf`` in mpv's user
|
2014-05-23 11:24:27 +00:00
|
|
|
folder. Comment lines can be started with # and stray spaces are not removed.
|
|
|
|
Boolean values will be represented with yes/no.
|
|
|
|
|
|
|
|
Example config::
|
|
|
|
|
|
|
|
# comment
|
|
|
|
optionA=Hello World
|
|
|
|
optionB=9999
|
|
|
|
optionC=no
|
|
|
|
|
|
|
|
|
2014-12-14 23:31:30 +00:00
|
|
|
Command-line options are read from the ``--script-opts`` parameter. To avoid
|
2014-05-23 11:24:27 +00:00
|
|
|
collisions, all keys have to be prefixed with ``identifier-``.
|
|
|
|
|
|
|
|
Example command-line::
|
|
|
|
|
2014-12-25 18:45:17 +00:00
|
|
|
--script-opts=myscript-optionA=TEST,myscript-optionB=0,myscript-optionC=yes
|
2014-05-23 11:24:27 +00:00
|
|
|
|
|
|
|
|
2016-07-10 12:30:02 +00:00
|
|
|
mp.utils functions
|
|
|
|
------------------
|
2014-05-25 17:51:11 +00:00
|
|
|
|
|
|
|
This built-in module provides generic helper functions for Lua, and have
|
|
|
|
strictly speaking nothing to do with mpv or video/audio playback. They are
|
|
|
|
provided for convenience. Most compensate for Lua's scarce standard library.
|
|
|
|
|
lua: add an utility function for starting processes
Because 1) Lua is terrible, and 2) popen() is terrible. Unfortunately,
since Unix is also terrible, this turned out more complicated than I
hoped. As a consequence and to avoid that this code has to be maintained
forever, add a disclaimer that any function in Lua's utils module can
disappear any time. The complexity seems a bit ridiculous, especially
for a feature so far removed from actual video playback, so if it turns
out that we don't really need this function, it will be dropped again.
The motivation for this commit is the same as with 8e4fa5fc.
Note that there is an "#ifndef __GLIBC__". The GNU people are very
special people and thought it'd be convenient to actually declare
"environ", even though the POSIX people, which are also very special
people, state that no header declares this and that the user has to
declare this manually. Since the GNU people overtook the Unix world with
their very clever "embrace, extend, extinguish" strategy, but not 100%,
and trying to build without _GNU_SOURCE is hopeless; but since there
might be Unix environments which support _GNU_SOURCE features partially,
this means that in practice "environ" will be randomly declared or not
declared by system headers. Also, gcc was written by very clever people
too, and prints a warning if an external variable is declared twice (I
didn't check, but I suppose redeclaring is legal C, and not even the gcc
people are clever enough to only warn against a definitely not legal C
construct, although sometimes they do this), ...and since we at mpv hate
compiler warnings, we seek to silence them all. Adding a configure test
just for a warning seems too radical, so we special-case this against
__GLIBC__, which is hopefully not defined on other libcs, especially not
libcs which don't implement all aspects of _GNU_SOURCE, and redefine
"environ" on systems even if the headers define it already (because they
support _GNU_SOURCE - as I mentioned before, the clever GNU people wrote
software THAT portable that other libcs just gave up and implemented
parts of _GNU_SOURCE, although probably not all), which means that
compiling mpv will print a warning about "environ" being redefined, but
at least this won't happen on my system, so all is fine. However, should
someone complain about this warning, I will force whoever complained
about this warning to read this ENTIRE commit message, and if possible,
will also force them to eat a printed-out copy of the GNU Manifesto, and
if that is not enough, maybe this person could even be forced to
convince the very clever POSIX people of not doing crap like this:
having the user to manually declare somewhat central symbols - but I
doubt it's possible, because the POSIX people are too far gone and only
care about maintaining compatibility with old versions of AIX and HP-UX.
Oh, also, this code contains some subtle and obvious issues, but writing
about this is not fun.
2014-10-18 23:42:28 +00:00
|
|
|
Be warned that any of these functions might disappear any time. They are not
|
|
|
|
strictly part of the guaranteed API.
|
|
|
|
|
2014-08-30 22:23:03 +00:00
|
|
|
``utils.getcwd()``
|
|
|
|
Returns the directory that mpv was launched from. On error, ``nil, error``
|
|
|
|
is returned.
|
|
|
|
|
2014-05-25 17:51:11 +00:00
|
|
|
``utils.readdir(path [, filter])``
|
|
|
|
Enumerate all entries at the given path on the filesystem, and return them
|
|
|
|
as array. Each entry is a directory entry (without the path).
|
|
|
|
The list is unsorted (in whatever order the operating system returns it).
|
|
|
|
|
|
|
|
If the ``filter`` argument is given, it must be one of the following
|
|
|
|
strings:
|
|
|
|
|
|
|
|
``files``
|
|
|
|
List regular files only. This excludes directories, special files
|
|
|
|
(like UNIX device files or FIFOs), and dead symlinks. It includes
|
|
|
|
UNIX symlinks to regular files.
|
|
|
|
|
|
|
|
``dirs``
|
|
|
|
List directories only, or symlinks to directories. ``.`` and ``..``
|
|
|
|
are not included.
|
|
|
|
|
|
|
|
``normal``
|
|
|
|
Include the results of both ``files`` and ``dirs``. (This is the
|
|
|
|
default.)
|
|
|
|
|
|
|
|
``all``
|
|
|
|
List all entries, even device files, dead symlinks, FIFOs, and the
|
|
|
|
``.`` and ``..`` entries.
|
|
|
|
|
|
|
|
On error, ``nil, error`` is returned.
|
|
|
|
|
2017-12-11 21:04:51 +00:00
|
|
|
``utils.file_info(path)``
|
|
|
|
Stats the given path for information and returns a table with the
|
|
|
|
following entries:
|
|
|
|
|
|
|
|
``mode``
|
|
|
|
protection bits (on Windows, always 755 (octal) for directories
|
|
|
|
and 644 (octal) for files)
|
|
|
|
``size``
|
|
|
|
size in bytes
|
|
|
|
``atime``
|
|
|
|
time of last access
|
|
|
|
``mtime``
|
|
|
|
time of last modification
|
|
|
|
``ctime``
|
|
|
|
time of last metadata change (Linux) / time of creation (Windows)
|
|
|
|
``is_file``
|
|
|
|
Whether ``path`` is a regular file (boolean)
|
|
|
|
``is_dir``
|
|
|
|
Whether ``path`` is a directory (boolean)
|
|
|
|
|
|
|
|
``mode`` and ``size`` are integers.
|
|
|
|
Timestamps (``atime``, ``mtime`` and ``ctime``) are integer seconds since
|
|
|
|
the Unix epoch (Unix time).
|
|
|
|
The booleans ``is_file`` and ``is_dir`` are provided as a convenience;
|
|
|
|
they can be and are derived from ``mode``.
|
|
|
|
|
|
|
|
On error (eg. path does not exist), ``nil, error`` is returned.
|
|
|
|
|
2014-05-25 17:51:11 +00:00
|
|
|
``utils.split_path(path)``
|
|
|
|
Split a path into directory component and filename component, and return
|
|
|
|
them. The first return value is always the directory. The second return
|
|
|
|
value is the trailing part of the path, the directory entry.
|
|
|
|
|
|
|
|
``utils.join_path(p1, p2)``
|
|
|
|
Return the concatenation of the 2 paths. Tries to be clever. For example,
|
2020-05-01 00:18:58 +00:00
|
|
|
if ``p2`` is an absolute path, ``p2`` is returned without change.
|
2014-05-23 11:24:27 +00:00
|
|
|
|
lua: add an utility function for starting processes
Because 1) Lua is terrible, and 2) popen() is terrible. Unfortunately,
since Unix is also terrible, this turned out more complicated than I
hoped. As a consequence and to avoid that this code has to be maintained
forever, add a disclaimer that any function in Lua's utils module can
disappear any time. The complexity seems a bit ridiculous, especially
for a feature so far removed from actual video playback, so if it turns
out that we don't really need this function, it will be dropped again.
The motivation for this commit is the same as with 8e4fa5fc.
Note that there is an "#ifndef __GLIBC__". The GNU people are very
special people and thought it'd be convenient to actually declare
"environ", even though the POSIX people, which are also very special
people, state that no header declares this and that the user has to
declare this manually. Since the GNU people overtook the Unix world with
their very clever "embrace, extend, extinguish" strategy, but not 100%,
and trying to build without _GNU_SOURCE is hopeless; but since there
might be Unix environments which support _GNU_SOURCE features partially,
this means that in practice "environ" will be randomly declared or not
declared by system headers. Also, gcc was written by very clever people
too, and prints a warning if an external variable is declared twice (I
didn't check, but I suppose redeclaring is legal C, and not even the gcc
people are clever enough to only warn against a definitely not legal C
construct, although sometimes they do this), ...and since we at mpv hate
compiler warnings, we seek to silence them all. Adding a configure test
just for a warning seems too radical, so we special-case this against
__GLIBC__, which is hopefully not defined on other libcs, especially not
libcs which don't implement all aspects of _GNU_SOURCE, and redefine
"environ" on systems even if the headers define it already (because they
support _GNU_SOURCE - as I mentioned before, the clever GNU people wrote
software THAT portable that other libcs just gave up and implemented
parts of _GNU_SOURCE, although probably not all), which means that
compiling mpv will print a warning about "environ" being redefined, but
at least this won't happen on my system, so all is fine. However, should
someone complain about this warning, I will force whoever complained
about this warning to read this ENTIRE commit message, and if possible,
will also force them to eat a printed-out copy of the GNU Manifesto, and
if that is not enough, maybe this person could even be forced to
convince the very clever POSIX people of not doing crap like this:
having the user to manually declare somewhat central symbols - but I
doubt it's possible, because the POSIX people are too far gone and only
care about maintaining compatibility with old versions of AIX and HP-UX.
Oh, also, this code contains some subtle and obvious issues, but writing
about this is not fun.
2014-10-18 23:42:28 +00:00
|
|
|
``utils.subprocess(t)``
|
|
|
|
Runs an external process and waits until it exits. Returns process status
|
2018-05-12 13:36:43 +00:00
|
|
|
and the captured output. This is a legacy wrapper around calling the
|
|
|
|
``subprocess`` command with ``mp.command_native``. It does the following
|
|
|
|
things:
|
|
|
|
|
|
|
|
- copy the table ``t``
|
|
|
|
- rename ``cancellable`` field to ``playback_only``
|
|
|
|
- rename ``max_size`` to ``capture_size``
|
|
|
|
- set ``capture_stdout`` field to ``true`` if unset
|
|
|
|
- set ``name`` field to ``subprocess``
|
|
|
|
- call ``mp.command_native(copied_t)``
|
|
|
|
- if the command failed, create a dummy result table
|
|
|
|
- copy ``error_string`` to ``error`` field if the string is non-empty
|
|
|
|
- return the result table
|
|
|
|
|
|
|
|
It is recommended to use ``mp.command_native`` or ``mp.command_native_async``
|
|
|
|
directly, instead of calling this legacy wrapper. It is for compatibility
|
|
|
|
only.
|
2015-06-27 19:08:55 +00:00
|
|
|
|
2019-10-04 14:18:10 +00:00
|
|
|
See the ``subprocess`` documentation for semantics and further parameters.
|
|
|
|
|
2016-09-20 18:32:16 +00:00
|
|
|
``utils.subprocess_detached(t)``
|
|
|
|
Runs an external process and detaches it from mpv's control.
|
|
|
|
|
|
|
|
The parameter ``t`` is a table. The function reads the following entries:
|
|
|
|
|
|
|
|
``args``
|
|
|
|
Array of strings of the same semantics as the ``args`` used in the
|
|
|
|
``subprocess`` function.
|
|
|
|
|
|
|
|
The function returns ``nil``.
|
|
|
|
|
2018-05-12 14:03:04 +00:00
|
|
|
This is a legacy wrapper around calling the ``run`` command with
|
|
|
|
``mp.commandv`` and other functions.
|
|
|
|
|
2017-12-17 00:25:23 +00:00
|
|
|
``utils.getpid()``
|
|
|
|
Returns the process ID of the running mpv process. This can be used to identify
|
|
|
|
the calling mpv when launching (detached) subprocesses.
|
|
|
|
|
2020-07-16 23:36:33 +00:00
|
|
|
``utils.get_env_list()``
|
|
|
|
Returns the C environment as a list of strings. (Do not confuse this with
|
|
|
|
the Lua "environment", which is an unrelated concept.)
|
|
|
|
|
2014-10-19 03:27:35 +00:00
|
|
|
``utils.parse_json(str [, trail])``
|
|
|
|
Parses the given string argument as JSON, and returns it as a Lua table. On
|
|
|
|
error, returns ``nil, error``. (Currently, ``error`` is just a string
|
|
|
|
reading ``error``, because there is no fine-grained error reporting of any
|
|
|
|
kind.)
|
|
|
|
|
|
|
|
The returned value uses similar conventions as ``mp.get_property_native()``
|
|
|
|
to distinguish empty objects and arrays.
|
|
|
|
|
|
|
|
If the ``trail`` parameter is ``true`` (or any value equal to ``true``),
|
|
|
|
then trailing non-whitespace text is tolerated by the function, and the
|
|
|
|
trailing text is returned as 3rd return value. (The 3rd return value is
|
|
|
|
always there, but with ``trail`` set, no error is raised.)
|
|
|
|
|
2015-04-22 18:55:05 +00:00
|
|
|
``utils.format_json(v)``
|
|
|
|
Format the given Lua table (or value) as a JSON string and return it. On
|
|
|
|
error, returns ``nil, error``. (Errors usually only happen on value types
|
|
|
|
incompatible with JSON.)
|
|
|
|
|
|
|
|
The argument value uses similar conventions as ``mp.set_property_native()``
|
|
|
|
to distinguish empty objects and arrays.
|
|
|
|
|
2014-11-28 22:18:50 +00:00
|
|
|
``utils.to_string(v)``
|
|
|
|
Turn the given value into a string. Formats tables and their contents. This
|
|
|
|
doesn't do anything special; it is only needed because Lua is terrible.
|
|
|
|
|
2014-02-12 19:15:58 +00:00
|
|
|
Events
|
|
|
|
------
|
|
|
|
|
|
|
|
Events are notifications from player core to scripts. You can register an
|
|
|
|
event handler with ``mp.register_event``.
|
|
|
|
|
|
|
|
Note that all scripts (and other parts of the player) receive events equally,
|
|
|
|
and there's no such thing as blocking other scripts from receiving events.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2014-02-12 19:34:36 +00:00
|
|
|
function my_fn(event)
|
2014-02-12 19:15:58 +00:00
|
|
|
print("start of playback!")
|
|
|
|
end
|
|
|
|
|
2014-12-25 18:45:17 +00:00
|
|
|
mp.register_event("file-loaded", my_fn)
|
2014-02-12 19:15:58 +00:00
|
|
|
|
2020-03-21 18:31:58 +00:00
|
|
|
For the existing event types, see `List of events`_.
|
2014-10-20 22:15:30 +00:00
|
|
|
|
|
|
|
Extras
|
|
|
|
------
|
|
|
|
|
2014-12-25 18:45:17 +00:00
|
|
|
This documents experimental features, or features that are "too special" to
|
|
|
|
guarantee a stable interface.
|
2014-10-20 22:15:30 +00:00
|
|
|
|
|
|
|
``mp.add_hook(type, priority, fn)``
|
|
|
|
Add a hook callback for ``type`` (a string identifying a certain kind of
|
|
|
|
hook). These hooks allow the player to call script functions and wait for
|
|
|
|
their result (normally, the Lua scripting interface is asynchronous from
|
|
|
|
the point of view of the player core). ``priority`` is an arbitrary integer
|
|
|
|
that allows ordering among hooks of the same kind. Using the value 50 is
|
2020-08-05 20:58:19 +00:00
|
|
|
recommended as neutral default value.
|
|
|
|
|
|
|
|
``fn(hook)`` is the function that will be called during execution of the
|
|
|
|
hook. The parameter passed to it (``hook``) is a Lua object that can control
|
|
|
|
further aspects about the currently invoked hook. It provides the following
|
|
|
|
methods:
|
|
|
|
|
|
|
|
``defer()``
|
|
|
|
Returning from the hook function should not automatically continue
|
|
|
|
the hook. Instead, the API user wants to call ``hook:cont()`` on its
|
|
|
|
own at a later point in time (before or after the function has
|
|
|
|
returned).
|
|
|
|
|
|
|
|
``cont()``
|
|
|
|
Continue the hook. Doesn't need to be called unless ``defer()`` was
|
|
|
|
called.
|
2014-10-20 22:15:30 +00:00
|
|
|
|
2015-02-04 18:20:17 +00:00
|
|
|
See `Hooks`_ for currently existing hooks and what they do - only the hook
|
|
|
|
list is interesting; handling hook execution is done by the Lua script
|
|
|
|
function automatically.
|