Commit Graph

48 Commits

Author SHA1 Message Date
Guido Cella eb4c6be630 console.lua: don't convert integers for mp.input to string
I misunderstood CogentRedTester's review in
https://github.com/mpv-player/mpv/pull/10282#discussion_r1428972352 as
referring to the cursor_position in mp.input's arguments instead of the
one received by the closed callback.

The cursor_position passed to input.get doesn't need to be converted to
a number because it was already JSON, while the cursor_position received
by the closed callback is currently a string, and we need to pass JSON
in input-event script messages to keep it as an integer to work around
mpv converting integer script message arguments to string.

This is more noticeable after implementing mp.input.select(): its submit
argument currently receives the selected index as a string, and this
makes Lua error if you use it as an index of a numerical table, e.g.:

submit = function (id)
    mp.set_property(property, tracks[tonumber(id)].selected and "no"
                              or tracks[tonumber(id)].id)
    ...
end,

This commit avoids having to call tonumber(id).
2024-05-12 23:13:48 +02:00
Guido Cella ae7e7d07b8 js: fix defaults.js
Fixes 2354d876da.
2024-05-07 22:57:53 +02:00
Guido Cella 2354d876da scripting: add mp.input.select()
This allows scripts to make the user choose from a list of entries by
typing part of their text and/or by navigating them with keybindings,
like dmenu or fzf.

Closes #13964.
2024-05-07 22:34:22 +02:00
Avi Halachmi (:avih) d70c77b6b6 js: complex key-binding: send also key_text
This is doumented and has been part of the table in lua, but until
now not in the JS object.

Fixes #13448
2024-02-09 16:54:57 +02:00
Guido Cella dfecc9f083 console.lua: style log lines in the terminal
When running the console in the terminal, style log lines with the same
escape sequences as msg.c.

mp.input can also specify terminal escape sequences, e.g. a script to
select a playlist entry can invert the color of the selection.

Also add a missing newline to help's error message.
2024-01-14 23:26:07 +01:00
Guido Cella 871f7a152a scripting: add mp.input
This lets scripts get textual input from the user using console.lua.
2024-01-13 02:53:08 +00:00
Dudemanguy 86b498ecc0 player: remove shared-script-properties property
This property was never encouraged. The manual even stated that "You
should avoid using it, unless you absolutely have to." Since we now have
user-data which is superior in every single way and replaces this,
delete this property. The manual also has threatened people for years
with the line "It's a makeshift solution which could go away any time
(for example, when a better solution becomes available)." We were nice
and deprecated it in 1d00aee8e1 for a
while to give script authors some time to update. Let's remove it for
good now.
2023-11-22 16:29:43 +00:00
Dudemanguy 0bed2a2263 build: remove outdated generated directory
This only existed as essentially a workaround for meson's behavior and
to maintain compatibility with the waf build. Since waf put everything
in a generated subdirectory, we had to put make a subdirectory called
"generated" in the source for meson so stuff could go to the right
place. Well now we don't need to do that anymore. Move the meson.build
files around so they go in the appropriate place in the subdirectory of
the source tree and change the paths of the headers accordingly. A
couple of important things to note.

1. mpv.com now gets made in build/player/mpv.com (necessary because of
   a meson limitation)
2. The macos icon generation path is shortened to
   TOOLS/osxbundle/icon.icns.inc.
2023-07-31 19:00:06 +00:00
Avi Halachmi (:avih) 86093fcae7 lua/js: remove user-data helpers
This reverts:
  3fb4140c lua/defaults: add user_data helpers
  68a20e7a javascript/defaults: add user_data helpers
  00510379 lua/js: fix user_data_del util function

As well as the lua/js parts of:
  3ec2a098 docs: document new user-data property

user-data and its sub-properties can be set/get/observed/deleted
via the standard properties interface, so there's no need for
additional helpers specific to user-data, which only added maintenance
burden.
2023-01-29 01:52:31 +02:00
rcombs 0051037957 lua/js: fix user_data_del util function 2023-01-28 15:21:15 -06:00
rcombs 68a20e7ae4 javascript/defaults: add user_data helpers 2023-01-28 14:37:24 -06:00
Avi Halachmi (:avih) 3694af6076 js: key bindings: ensure priorities for same-key - again
Commit 7f4841ff sorted the bindings so that if the same key was bound
more than once, then the newest binding takes priority (sorted last).

However, it got the comparison function wrong, which means the result
of the sort depended on the algorithm and not on the actual data, so
the priority for keys with more than one binding was still arbitraty.

Fix the sort function, and finally ensure that later binding acutally
override earlier bindings of the same key.
2022-06-23 17:16:33 +03:00
Avi Halachmi (:avih) 7849a36beb js: utils.get_user_path: make wrapper of expand-path
When utils.get_user_path was added, the expand-path command didn't
exist. Now it does, so remove the C code, make it a trivial wrapper.

Keep this function for backward compat to not break scripts, but
technically it's not required anymore.
2022-02-04 12:53:16 +02:00
Avi Halachmi (:avih) f6b834a2fc js: ~~/init.js: use mp.find_config_file
The problem with the previous code - which used mp.get_user_path, is
that it returns a path even with --no-config, and even if the file
doesn't exist.

This is why we tested the "config" property, and also used
mp.utils.file_info to check that the file exists.

mp.find_config_file doesn't return a path with no-cofig and doesn't
return a path if the file doesn't exists. It's simpler and better.
2021-11-01 14:31:18 +01:00
Avi Halachmi (:avih) 4703b74e6c js: custom-init: use ~~/init.js instead of ~~/.init.js (dot)
mpv doesn't have other dot files in its config dir, and it also
shouldn't be "invisible".

The new name ~~/init.js now replaces ~~/.init.js

While mpv usually deprecates things before outright removing them,
in this case the old (dot) name is replaced without deprecation:
- It's a bad idea to execute hidden scripts, even if at a config dir,
  and we don't want to do that for the next release cycle too.
- We warn if the old (dot) name exists and the new name doesn't,
  which should be reasonably visible for affected users.
- It's likely niche enough to not cause too much pain.

If for some reason both names are needed, e.g. when using also an old
mpv versions, then the old name could be symlinked to the new one, or
simply have one line: `require("~~/init")` to load the new name, while
a new mpv version will load (only) the new name without warning.
2021-10-19 15:43:39 +03:00
Avi Halachmi (:avih) f386fd79b2 js: custom init: ignore ~~/.init.js with --no-config
The custom init script should be considered a configuration file, and
as such it should be ignored when the user wants vanilla mpv - and now
it is ignored with --no-config.
2021-10-12 14:12:24 +03:00
Avi Halachmi (:avih) 6123e97e31 js: custom init (~~/.init.js): fail loudly on errors
Previously, loading ~~/.init.js was inside a try block, in order to
quietly ignore errors if the file doesn't exist.

However, that also prevented any real errors from showing up even when
the file does exist, and the script continued as if everything is OK,
while in fact the custom init script didn't complete correctly.

Now we first check if the file exists, and then load it without
try/catch, so that any error shows up normally (and abort the script).
2021-09-30 18:28:28 +03:00
Avi Halachmi (:avih) 7f4841ff39 js: key bindings: ensure priorities for same-key bindings
Previously, if a script bound the same key more than once to different
functions (without removing the older bindings first), then pressing
the key triggered any of the bound functions arbitrarily[1].

Now the newest binding for the same key is always the active one.

If the newest binding is then removed - the second-newest will become
active, etc. (same mechanism as before, except that the active key
was not always the newest).

[1] The order was implementation-defined, however `for (name in obj)'
    happens to iterate. With mujs that's currently alhabetic order,
    and name is from mp.add_[forced_]key_binding(key, name...)
    or generated internally if name is not provided. So a binding with
    higher alphabetic `name' value had priority over lower name value.
2021-07-21 17:29:11 +03:00
Avi Halachmi (:avih) 0427fe97e1 js: idle-observers: minor performance improvement
The idle-observers block has at least 3 function calls, and it was
entered every time the script went into idle, however, we can save these
calls by checking first that there are indeed such observers.
2021-06-22 18:17:11 +03:00
Avi Halachmi (:avih) bc9d556f3a js: add mp.utils.append_file
Also, for consistency with other APIs, mp.utils.{write,append}_file
now return true on success (and still throw on any error).
2021-06-13 22:53:37 +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
Avi Halachmi (:avih) d5a02dd934 js: hooks: allow deferred continuation (match d0ab562b)
The callback now gets an object argument with defer/cont functions.

Like the lua code, the behavior is that each hook event allows at
most one continue, but nothing enforces the order of continuations
if more hook events arrive before prior ones were continued - which
is possible now with the defer option, but wasn't possible before
(continuation was synchronous from the hook event handler).
2020-08-07 15:12:20 +03:00
Avi Halachmi (:avih) b5284a6829 js: mp.set_osd_ass: ignore identical inputs (match ccbb8b1c) 2020-05-10 21:20:28 +03:00
Avi Halachmi (:avih) a23869379c js: osd-overlay update: return the command result (match 7a76b577)
Currently only useful for the new 'compute_bounds' command key.
2020-03-07 12:37:35 +02:00
Avi Halachmi (:avih) 9cf45d8300 js: osd-overlay update: support arbitrary key names
Until now the 'update' method used mp.command_native with a hardcoded
list of key names.

Change it to use whatever keys the user set to this object, so that
we can remain oblivious to new keys which 'osd-overlay' may support.

This is how the lua code did it from the begining. We didn't, and now
we pay the price.

Note: could be implemented either as we have now (clone `this`
excluding the methods) or by moving the methods up the prototype chain
(i.e. class methods) so they don't get enumerated and use `this` as
the command object itself.

However, in the latter approach we'll have to save the values which we
change (name, res_x, res_y) and restore them after the command, so it's
simpler to just clone `this`.
2020-03-07 12:37:35 +02:00
Avi Halachmi (:avih) 756960bf3c js: require: directory-scripts: first look at <dir>/modules/
Also, add the function mp.get_script_directory() to let scripts know if
they're loaded as a directory and where.
2020-02-07 18:22:12 +02:00
Avi Halachmi (:avih) 68a1b47d4d js: require: don't use ~~/scripts/modules.js/
Directories inside ~~/scripts/ are now loaded as scripts, so don't use
it also for modules. Now there are no default module paths.

To compensate, we now try to run ~~/.init.js right after defaults.js,
so the user may extend the js init procedure via this script, e.g. for
adding default paths to mp.module_paths .
2020-02-07 18:20:33 +02:00
Avi Halachmi (:avih) 5e0875c9e0 js: use osd-dimentions for mp.get_osd_{size,margins}
This matches lua's 11b9315b but with the lagacy field names which the
js code used previously.

Currently the property always returns an object (with dummy/last/null
field values if there are no dimensions), but the code is ready for
a future case where it might return null if there are no dimensions - at
which case it will forward the null, breaking backward compatibility for
a better API.
2020-01-08 11:49:49 +02:00
Avi Halachmi (:avih) 9f2fda7d85 js: support mp.create_osd_overlay (match 07287262)
The legacy mp.set_osd_ass(...) is still supported (but also still
undocumented) as a wrapper for the new mp.create_osd_overlay(...).
2019-12-23 17:52:34 +02:00
Avi Halachmi (:avih) 5a74bf5f95 js: batch key bindings updates (match 96932fe7)
Implemented using one-time idle observer (i.e. setTimeout), to avoid
additional function call(back) every time the event loop enters idle.

The lua mp.flush_key_bindings() is also available, also undocumented.
2019-12-23 17:52:34 +02:00
Avi Halachmi (:avih) b670838b3d js: read_options: on_update: don't re-read the config file
Now that 00af718a made the lua read_options behavior much more similar
to the js behavior, the main difference was that lua does not re-read
the config file at on_update (but it does re-apply its stored content)
while js did re-read it.

Now the js on_update also does not re-read the config file and instead
applies its stored original content.

This is slightly hacky by adding an undocumented optional 4th argument
to read_options which allows overriding the config file content.
2019-12-22 14:51:13 +02:00
Avi Halachmi (:avih) 71ddb22b39 js: read_options: support on_update (match 478a321d)
This is a bit different than the lua code: on script-opts change it
simply re-applies the conf-file and script-opts to the options object,
and if this results in any changed value at options then on_update is
called with the changelist as argument.

This allows a value to revert back to the conf-file value if the
matching script-opts key had a different value and then got deleted.
It also guarantees to call back whenever the options object is
modified, which the lua code doesn't do (e.g. if the caller changed
a value and the observer changed it back - it won't detect a change).
2019-12-21 14:57:10 +02:00
Avi Halachmi (:avih) 3460a7b696 js: support shared script properties (match 7e4819e7) 2019-12-19 16:23:19 +02:00
Avi Halachmi (:avih) 4fc5cd32d0 js: require: allow custom module search paths via mp.module_paths 2019-12-19 16:23:19 +02:00
Avi Halachmi (:avih) b67d124dc0 js: mp.add_key_binding: allow false-y name (match 311cc5b)
This is in addition to the allowed omission of name and/or flags (as
documented).
2019-11-30 12:23:47 +02:00
Avi Halachmi (:avih) 67fef2b152 js: complex key binding: use key_name like the docs say 2019-11-20 16:55:21 +02:00
Avi Halachmi (:avih) 14971424b9 js: report key name for "complex" key bindings - match b08c8f50 2019-11-20 16:28:49 +02:00
Avi Halachmi (:avih) 44f8dccfb6 js: expose mpv_abort_async_command() (match dbe831bd)
With minor difference from lua, as documented.
2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) ad0a525f20 js: reimplement subprocess_detached using the run command (match 7f91e268) 2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) fea39b5a6b js: reimplement subprocess using the subprocess command (match 548ef078)
Semantics changes are the same as at 548ef078 .

Also, the previous C implementation returnd a string for the `stdout`
value, but stdout of the subprocess command is MPV_FORMAT_BYTE_ARRAY
which js previously didn't support, so support it too (at pushnode)
by returning it as a string - the same as the lua code does.
2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) 5b5f776900 js: expose async commands (match 159379980e) 2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) ec625266c8
js: use new hooks API (match f60826c3) 2018-04-07 16:02:20 -07:00
Avi Halachmi (:avih) 84aa9e7146
js: dump(..): fix incorrect <VISITED> with array argument
When dump's argument is an array, it was displaying <VISITED> for all
the array's object elements (objects, arrays, etc), regardless if they're
actually visited or not.

The reason is that we try to stringify twice: once normally which may
throw (on cycles), and a second time while excluding visited items which
is indicated by binding the replacer to an empty array - in which we hold
the visited items, where the replacer tests if its 'this' is an array or
not and acts accordingly.

However, its "this" may also be an array even if we don't bind it to one,
because its "normal" this is the main stringified object, so the test of
Array.isArray(this) is true when the top object is an array, and the object
items are indeed are in it - so the replacer considers them visited.

Fix by binding to null on the first attempt such that "this" is an array
only when we want it to test for visited items and not when the argument
itself is an array.
2018-04-07 16:02:19 -07:00
Avi Halachmi (:avih) 9a47023c44
js: implement mp.register_idle
Due to earlier misinterpretation of the Lua docs as if mp.register_idle
registers a one-shot callback, the JS docs suggested to use setTimeout.

But the behavior and Lua docs are such that it's a repeating callback
which fires just before the script thread goes to sleep.

Implement it for JS too.
2018-04-07 16:02:19 -07:00
Avi Halachmi (:avih) b04f0cad43
js: implement mp.options.read_options 2018-04-07 16:02:19 -07:00
TheAMM 3c4667c862 js: implement mp.msg.trace()
To match the new Lua helper introduced in
1afdeee1ad

Add documentation for both.
2017-12-16 02:25:24 -08:00
Avi Halachmi (:avih) d223a63bc5 js: add javascript scripting support using MuJS
Implements JS with almost identical API to the Lua support.

Key differences from Lua:
- The global mp, mp.msg and mp.utils are always available.
- Instead of returning x, error, return x and expose mp.last_error().
- Timers are JS standard set/clear Timeout/Interval.
- Supports CommonJS modules/require.
- Added at mp.utils: getenv, read_file, write_file and few more.
- Global print and dump (expand objects) functions.
- mp.options currently not supported.

See DOCS/man/javascript.rst for more details.
2017-06-14 12:29:32 +02:00