Commit Graph

82 Commits

Author SHA1 Message Date
CogentRedTester 099ae86717 lua: command_native_async: always callback a-sync
Previously if the raw command_native_async returned an error then the
callback function was run directly. This meant that script writers
potentially had to account for both synchronous and asynchronous logic
in the callback, which can happen even with seemingly 'safe' commands
if the mpv event queue is full.

This was at odds with the Javascript implementation of
the function, which always runs the callback asynchronously.

Now the mp.add_timeout function is used to run the callback
asynchronously on error, replicating the Javascript implementation.

This provides consistency for developers in how the callback is handled
in Lua, and increases consistency between the Lua and Javascript APIs.
2022-06-22 14:44:12 +03:00
sfan5 dfcd561ba9 lua: remove mp.suspend, resume and resume_all
These functions were deprecated in v0.21.0 and no-ops
since v0.23.0. The matching client API functions were
removed in the previous commit.
2021-12-15 12:29:10 +01:00
Avi Halachmi (:avih) d828652f24 lua: fix timers comment (no-op)
process_timers() doesn't return an absolute time. It returned delta>0
or nil before f73814b1 , and since f73814b1 it can also return 0.
2021-07-13 13:11:34 +03:00
Avi Halachmi (:avih) 5b40db385a lua: idle observers: ensure timers are up-to-date
This fixes two issues, both of which resulted from the timers-wait
period not being re-calculated after idle-observers were executed:

- If timers were added from an idle observer then they could fire long
  after they were due (only when/if the next mpv event arrives).

- Idle observers don't execute in zero time, and the wait period for
  the next timer was implicitly extended by the idle observers
  execution time (because it was calculated before the idle observers).

This commit ensures that if idle-observers were executed, then the
timers wait period is re-calculated, which solves both issues.
2021-06-23 23:47:08 +03:00
Avi Halachmi (:avih) f73814b180 lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.

This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.

For instance, this caused process_timers() to never return:

  function render() <longer than 1/60 s on a slow system> end
  mp.add_periodic_timer(1/60, render)

Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.

This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.

This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).

Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-23 23:47:08 +03:00
Avi Halachmi (:avih) 02fbdf8aaf scripting (lua/js): utils.getpid: make wrapper of pid property
We now have at least 3 scripting APIs which are trivial wrappers
around properties: mp.get_mouse_pos, utils.getcwd, utils.getpid.

After some discussion on IRC it was decided that it's easier for us to
maintain them as trivial wrappers than to deprecate them and inflict
pain on users and script authors, so currently no plan to deprecate.
2021-05-01 16:07:05 +03:00
Avi Halachmi (:avih) 0d5055fe93 lua/js: mp.get_mouse_pos: use the mouse-pos property
mp.get_mouse_pos() is undocumented and is no longer required - the
property can be used officially by any client now, however, osc.lua
uses it, and also some user scripts learnt to rely on it, so we keep
it - as a trivial wrapper around the new mouse-pos property.
2020-11-16 20:29:58 +02:00
wm4 d0ab562b1f lua: make hook processing more flexible
This can now opt to not continue a hook after the hook callback returns.
This makes it easier for scripts, and may make it unnecessary to run
reentrant event loops etc. for scripts that want to wait before
continuing while still running the event loop.
2020-08-05 23:19:41 +02:00
wm4 ccbb8b1c9b lua: restore change detection with legacy OSD function
mp.set_osd_ass() (which was undocumented, or in other words, was not
supposed to be used by external scripts) used to do change detection in
the mpv C code. If the resolution or payload did not change, it was not
re-rendered on the lower levels.

Apparently this made some people sad, so fix it. (But only after I told
them to fuck off.) (Well I didn't put it this way, but still.)
2020-05-01 00:59:57 +02:00
wm4 37f441d61b lua: restore recent end-file event, and deprecate it
Lua changed behavior for this specific event. I considered the change
minor enough that it would not need to go through deprecation, but
someone hit it immediately and ask on the -dev channel.

It's probably better to restore the behavior. But mark it as deprecated,
since it's problematic (mismatch with the C API). Unfortunately, no
automatic warning is possible. (Or maybe it is, by playing sophisticated
Lua tricks such as setting a metatable and overriding indexing, but
let's not.)
2020-03-22 19:42:59 +01:00
wm4 7a76b577d8 command: extend osd-overlay command with bounds reporting
This is more or less a minimal hack to make _some_ text measurement
functionality available to scripts. Since libass does not support such a
thing, this simply uses the bounding box of the rendered text.

This is far from ideal. Problems include:
- using a bitmap bounding box
- additional memory waste and/or flushing caches
- dependency on window size
- odd small deviations with different window sizes (run osd-test.lua and
  resize the window after each timer update; the bounding boxes aren't
  adjusted in an overly useful way)
- inability to query the size _after_ actual rendering

But I guess it's a start. Since I'm aware that it's crap, add a threat
to the manpage that this may be changed/removed again. For now, I'm
interested whether anyone will have use for it in its current form, as
it's an often requested feature.
2020-03-06 18:20:11 +01:00
wm4 11b9315b3f lua: use new OSD property
See previous commit.

A nice side-effect is that mp.get_osd_margins() is not a special
Lua-only thing anymore. I didn't test whether this function still works
as expected, though.
2020-01-08 00:16:58 +01:00
wm4 091ee9d770 lua: fix guard against division by 0
This was incorrectly converted from the original C code. Change it to
what the original code used.
2019-12-23 16:10:06 +01:00
wm4 d951a7f021 lua: fix passing non-integers to mp.set_osd_ass()
libass uses integers for PlayResX/Y, so the osd-overlay command also
does. Lua (pre-5.3) does not have an integer type, but the command
interface makes a difference anyway. If you pass a Lua number with a
fractional part to an integer parameter (using mp.command_native()), it
will result in an error and complain about incompatible types.

I think that's fine, but since this behavior extends to
mp.set_osd_ass(), this is a compatibility problem.

Fix this by explicitly coercing the resolution parameters to integer
numbers.
2019-12-23 13:23:10 +01:00
wm4 0728726251 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 11:44:24 +01:00
wm4 96932fe77c lua: batch-update key bindings
Lua scripting implements key bindings by defining an input section with
all the bindings in it. Every add_key_binding() call ran a mpv command
to update this section. This caused a lot of spam at debug log levels.

Reduce the spam and more it efficient by batching updates into a single
mpv command when the script becomes inactive. This is pretty simple,
because there's already the concept of idle handlers.

This requires that the script actually goes to sleep, which might not
happen in various extremely bogus corner cases, such as polling the mpv
message queue with active waiting. Just don't do that.
2019-12-23 11:17:01 +01:00
wm4 7e4819e705 command, lua: add a way to share data between scripts
Very primitive and dumb, but fulfils its purpose for the next commits.

I chose this specific implementation because it has the lowest footprint
in command.c, without resorting to crazy hacks such as sending messages
between scripts (which would be hard to coordinate especially on
startup).
2019-12-18 08:58:49 +01:00
wm4 b36e8569a1 lua: make later key bindings always have higher priority
Later calls to mp.add_key_binding() should take priority over previous
calls with the same key. Until now, the order was random (due to using
table pairs() iteration order).

Do this by simply sorting by a counter that is never reset. Since
input.c also gives later bindings priority, this works out.

Calling mp.remove_key_binding() on a newer binding makes an older still
existing binding with the same key active again. New bindings override
older ones, but do not overwrite them. I think these are good semantics
for most use cases.

(Note that the Lua code cannot determine whether two bindings use the
same key. Keys are strings, and two different strings could refer to the
same key. The code does not have access to input.c's key name
normalization, so it cannot compare them.)
2019-12-07 14:53:33 +01:00
Avi Halachmi (:avih) abf6b22abd lua: unbreak mp.add_key_binding(key, fn)
Commit 311cc5b6 added the ability use flags while omitting name, but
broke the case where both name and flags are omitted.

Now omitting either name or flags or both works as documented.
2019-11-30 12:06:22 +02:00
wm4 311cc5b611 lua: make add_key_binding() rotate optional arguments correctly
add_key_binding() makes the name argument optional (in weird Lua
fashion), which did not work if there were additional arguments. So
there is no way to avoid specifying a name while passing a rp argument.
Fix this, declare this way of skipping the argument as deprecated, and
allow passing name=nil as the preferred way to skip the name argument.
2019-11-23 14:40:00 +01:00
wm4 21f2468d67 input: add text produced by key to script key events
Particularly for "any_unicode" mappings, so they don't have to
special-case keys like '#' and ' ', which are normally mapped to
symbolic names for input.conf reasons. (Though admittedly, this is a
pretty minor thing, since API users could map these manually.)
2019-11-22 01:15:08 +01:00
Avi Halachmi (:avih) da13a3b231 lua: complex key binding: use key_name like the docs say 2019-11-20 16:28:49 +02:00
wm4 b08c8f50b5 lua: report key name for "complex" key bindings
This might make certain use cases less of a mess.
2019-11-19 23:11:05 +01:00
wm4 962a9fa981 lua: actually unobserve properties in mp.unobserve_property()
Not doing this looked like a memory leak.

This looks like an oversight in the commit that added it: a94020e25b,
possible brain damage?

Fixes: #6823
2019-10-24 16:27:05 +02:00
Jan Ekström 199aabddcc Merge branch 'master' into pr6360
Manual changes done:
  * Merged the interface-changes under the already master'd changes.
  * Moved the hwdec-related option changes to video/decode/vd_lavc.c.
2019-03-11 01:00:27 +02:00
Olivier Perret 9be68e49b2 lua: execute idle handlers after timers have been processed
Idle handlers used to not be executed when timers were active
Now they are executed:
* After all expired timers have been executed
* After all events have been processed (same as when there are no timers)
2019-01-16 16:58:46 +01:00
wm4 dbe831bd02 lua: expose mpv_abort_async_command()
Also somewhat cleans up mp.command_native_async() error handling.
2018-05-24 19:56:34 +02:00
wm4 7f91e2684e lua: reimplement mp.subprocess_detached() by invoking the "run" command
The "run" command is old. I'm not sure why the separate Lua
implementation was added. But maybe it as because the "run" command used
to be limited to a small number of arguments. This limit has been
removed a while ago. In any case, the old implementation is not needed
anymore.
2018-05-24 19:56:34 +02:00
wm4 548ef07864 lua: reimplement mp.subprocess() by invoking the new subprocess command
We keep mp.subprocess() with roughly the same semantics for
compatibility with scripts (including the internal ytdl script).

Seems to work with rhe ytdl wrapper. Not tested further.
2018-05-24 19:56:34 +02:00
wm4 159379980e lua: expose async commands
Might be useful for some.
2018-05-24 19:56:34 +02:00
wm4 f60826c3a1
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.
2018-03-26 23:02:23 -07:00
Julian a4eda0c984 lua: implement mp_utils.format_bytes_humanized 2017-12-26 18:40:43 +01:00
Niklas Haas 1afdeee1ad lua: implement mp.msg.trace 2017-12-15 22:28:47 -08:00
Olivier Perret 348c610b68 lua: allow unregistration of idle handlers 2017-01-15 17:02:21 +01:00
wm4 f30c5d09f4 client API: turn mpv_suspend() and mpv_resume() into stubs
As threatened by the API changes document.

This commit also removes or stubs equivalent calls in IPC and Lua
scripting.

The stubs are left to maintain ABI compatibility. The semantics of the
API functions have been close enough to doing nothing that this probably
won't even break existing API users. Probably.
2016-11-22 15:54:44 +01:00
wm4 47f3cc7e6b lua: add API for registering idle handlers
This is only a functionality the Lua event dispatcher provides, rather
than the libmpv client API.
2016-09-21 15:47:52 +02:00
wm4 b521f15ae9 lua: run timers only after draining the event queue
Instead of rechecking the timers every time after an event is read, do
it only once the event queue is empty. This is probably slightly more
efficient, and facilitates the next commit.
2016-09-21 15:47:51 +02:00
Timotej Lazar 91a1b17104 Use - as command-name separator everywhere
Old-style commands using _ as separator (e.g. show_progress) were still
used in some places, including documentation and configuration files.
This commit updates all such instances to the new style (show-progress)
so that commands are easier to find in the manual.
2016-07-14 22:37:42 +02:00
Julian 6ae7a555bf lua: add timer:is_enabled() function
Allows to query if some timer is currently running or was
stopped/killed.
2016-05-14 20:49:28 +02:00
wm4 32fde273d3 lua: don't require key for mp.add_key_binding()
Requested. The intention is that scripts can provide mappable actions
for key bindings without setting a default key.
2016-03-26 10:44:57 +01:00
wm4 fe67aa7b18 lua: don't suspend core by default during script execution
This changes behavior somewhat. The old behavior can be restored by
running "mp.use_suspend=true". It was originally introduced for the OSC,
but I can't reproduce whatever misbehavior I was seeing.

(See mp.suspend()/resume() for explanations what the suspend mechanism
does.)
2016-03-18 22:03:04 +01:00
wm4 d6c99bcda2 lua: implement input_enable_section/input_disable_section via commands
Removes some more internal API calls from the Lua scripting backend.
Which is good, because ideally the scripting backend would use libmpv
functions only.

One awkwardness is that mouse sections are still not supported by the
public commands (and probably will never), so flags like allow-hide-
cursor make no sense to an outside user.

Also, the way flags are passed to the Lua function changes. But that's
ok, because they're only undocumented internal functions, and not
supposed to be used by script users. osc.lua only does due to historical
reasons.
2015-08-06 00:31:47 +02:00
wm4 9bcc882659 lua: make mp.input_define_section use the define-section command 2015-08-06 00:17:30 +02:00
wm4 289705daaf input: allow - as separator between commands, instead of _
Wnile it seems quite logical to me that commands use _ as word
separator, while properties use -, I can't really explain the
difference, and it tends to confuse users as well. So always
prefer - as separator for everything.

Using _ still works, and will probably forever. Not doing so would
probably create too much chaos and confusion.
2015-05-25 21:59:44 +02:00
wm4 08df18aea3 lua: replace getcwd() implementation 2015-03-24 22:04:27 +01:00
torque 4ff29f33b0 Lua: add unpack shim for Lua 5.2/5.3 compatibility.
The global unpack function got moved to table.unpack in Lua 5.2, and
it's only available as the global if 5.2 is built with compatibility
enabled (the default). Lua 5.3 does not build with 5.1 compatibility by
default.

Fixes #1648.
2015-03-06 12:28:20 +01:00
Avi Halachmi (:avih) 39e04e9294 options: deprecate 'lua' based options/dirs for 'script'
- --lua and --lua-opts change to --script and --script-opts
- 'lua' default script dirs change to 'scripts'
- DOCS updated

- 'lua-settings' dir was _not_ modified

The old lua-based names/dirs still work, but display a warning.

Signed-off-by: wm4 <wm4@nowhere>
2014-12-15 04:39:56 +01:00
wm4 eb36bd6945 lua: don't ignore key press events for script key bindings
Meh.
2014-12-10 18:36:05 +01:00
wm4 8fc3d7dc39 lua: fully cleanup removed key bindings
This is basically cosmetic; it was leaking the old handler functions.
2014-12-03 23:01:19 +01:00
wm4 3295caca3d lua: add a function that formats Lua values as strings
Yep, Lua is so crappy that the stdlib doesn't provide anything like
this.

Repurposes the undocumented mp.format_table() function and moves it to
mp.utils.
2014-11-29 00:03:46 +01:00