18 Commits

Author SHA1 Message Date
Guido Cella 850677c157 input.lua: don't shadow the global type() function
For consistency, also rename type to event in defaults.js.
2026-02-27 18:12:52 +01:00
CogentRedTester a96bbf5c17 mp.input: sanitise client inputs
This commit should remove any unexpected behaviour or crashes
that
may be caused by users passing unexpected values to mp.input functions.

String and numerical options are now explicitly converted to strings and
numbers. Values which should be tables are set to empty tables, and
warning messages are printed to inform the user.
2026-02-27 09:51:14 +01:00
CogentRedTester 5a1700a456 mp.input: avoid displaying outdated completions from the same client
Previously, completion responses from the same client would be displayed
even if the user had since changed the input, as long as a newer
complete event had already been sent. This meant that, if clients took a
sufficiently long time to return completions, then
console.lua would display completions for previous versions of the line
which were no longer valid.

This commit has the client send back the version of the line their
completions apply to, ensuring that `console.lua` displays the
correct completions.
2026-02-24 20:19:24 +01:00
CogentRedTester c72f2add25 mp.input: allow async completions
Previously, it was impossible to generate a list of completions for an
`input.get()` request asynchronously, as the values had to be returned
by the callback function.

This commit provides a way to send completions asynchronously by passing
a second argument to the complete callback; a function which can be
passed the return values to respond to the complete event.
2026-02-24 20:19:24 +01:00
Guido Cella 1f686769cd mp.input: deprecate log_error()
This was a mistake. It's too situational and it's not worth complicating
the API and every mp.input client with it. It made some sense before I
implemented input.select() because you could use input.get() to input a
choice and validate it, but in general for input.get() clients it's
better to show an error with input.set_log() instead of adding new lines
to the log everytime. This is what e.g.
https://framagit.org/Midgard/mpv-subber/-/blob/master/subber.lua does.

So this is only marginally useful for REPL scripts that keep adding
lines to the log, and the only such scripts are commands.lua and my
lua-repl.lua. commands.lua never used it and I already removed it from
lua-repl.lua. Cloning all scripts in the wiki reveals that **nobody** is
using log_error(), confirming that it is safe to deprecate.
2026-02-08 11:51:17 +01:00
CogentRedTester 2aa727bc3d mp.input: send buffer ids with logs to avoid race conditions
This commit modifies the log methods in mp.input to always send the id
of the latest `input.get()` request with log entries.

Previously, the log methods applied to whichever input request happened
to be open when the log message was received. Even when scripts used
these methods correctly, there was the risk of sending a log to the
wrong log buffer if the active input request changed while the log
message was in transit; a race condition.

Now the id of the latest `input.get()` request is sent alongside the log
messages, preventing data races between scripts while also preventing
those logs from being discarded.
2026-01-22 00:04:35 +01:00
CogentRedTester 3ed9b798bb mp.input: avoid rare race condition when supplying completions
It was previously possible for completion messages to be received by a
new input request, if one was created while the message was in transit.

Since it is trivial to avoid this by passing the script name and
existing handle_id value, we may as well do so and guarantee that there
will not be any data races.
2026-01-22 00:04:35 +01:00
CogentRedTester 5d469e6bf5 mp.input: remove race conditions when calling input.terminate()
This commit ensures that `input.terminate()` can only close input
requests made by the same script, and prevents any in-transit events
for old input requests from being processed.

Previously, there was no way to guarantee that the input request being
terminated was the one intended; the asynchronous nature of the API
meant that it was always possible (though unlikely) that another client
may have activated its own input request while the termination request
was in transit.
This commit removes the race condition between different scripts calling
`input.terminate()` by sending the script name alongside the termination
message.

In addition, when a script overwrites one of its own input requests,
there may be incoming events still in transit. Some of these events may
have a decent chance of calling `input.terminate()` if they are
processed (e.g., `submit`). This commit avoids this issue by only
processing `closed` requests once a new `input.get()` request is made.
2026-01-22 00:04:35 +01:00
CogentRedTester 17551b2d43 mp.input: use unique event handlers for input.get requests
This makes changes to mp.input and console.lua so that every input.get
request uses a unique script-message to handle input events.

Previously, making new input.get requests shortly after the termination
of a previous request made by the same script could cause a race
condition where the input handler was closed but the new request
was still being drawn in the UI. This was caused by the `closed` event
for the previous request being received only after the new request was
registered, hence closing the event handler for the new request instead.

In addition, this commit makes the behaviour of calling input.get while
another request is active more consistent. When a new request is
received it overwrites the in-progress request, sending a `closed`
event. However, previously, the `closed` event could not be sent if both
requests came from the same script, as the new request would have
overwritten the event handler. Now, the `closed` event is called
regardless of where the new request comes from.
2026-01-22 00:04:35 +01:00
Guido Cella e28c6ceea3 console.lua: default to --osd-font without completions
Currently --osd-font is the default for select, and a monospace font is
the default for free-form text mode. We can also default to the
proportional --osd-font for mp.input.get clients that don't specify
completions and thus don't need alignment, which is most clients.

In builtin scripts, this is the case for filtering stats key bindings.
2025-03-08 19:09:23 +00:00
Guido Cella 3c175f0114 console.lua: let the complete callback return a character to append
Let the complete callback return the character to append to the selected
completion.

I didn't want to document this because it's specifically meant for
properties and unlikely to be needed by other scripts, but silently
interpreting the 3rd return value can screw you over if you do something
like

return candidates, code:find('[%w_]*$')

I was doing this in my lua-repl script, and find() returns the end
position as the 2rd argument, this becomes the 3rd return value and gets
appended to completions. So you have to do

return candidates, code:find('[%w_]*$'), nil

Needed to split running commands out of console.lua.
2025-03-08 19:09:23 +00:00
Guido Cella bd9d5e3184 input.lua/defaults.js: unregister input-even again
Partially revert bede44618b. This was removed to allow nested
input.select() calls, but it is better to use the new keep_open flag for
this, because it avoids the flicker of console quickly closing and
reopening.

Unregistering the script message allows garbage collection of variables
defined in the scope that called mp.input, this is useful e.g. if you
were selecting from 10k history entries.
2025-02-24 16:26:58 +01:00
Guido Cella bede44618b {console,input}.lua: allow subsequent mp.input calls
Currently using mp.input while the console is open doesn't do anything.
We can allow using mp.input multiple times in a row by keeping the
console open, and sending a closed event only when the previous calling
script is different from the current one. If it is the same, sending a
closed event would unregister the new input-event listener, so it is
just never sent in this case as there is no sane way to do it. This is
useful e.g. to try out select script bindings in the console, or to
switch to a different mp.input script binding with a key binding without
having to close the current one.

Calling input.select() again within input.select()'s submit handler
needs to be fixed separately because with select the console is
immediately closed on submit (367a6b561a), so input.lua receives submit,
requests a new selection, and receives closed wich unregisters the
handler of the new selection. Just not calling
mp.unregister_script_message("input-event") fixes it; its only purpose
was to allow garbage collection of variables defined in the scope using
mp.input.

Fixes #15795.
2025-02-17 02:32:43 +00:00
Guido Cella b3f8464fa9 input.lua,defaults.js: don't hardcode mp.input arguments
Pass everything in the tables passed by mp.input callers to console.lua
so new flags can be added without modifying these clients.

In Lua, callbacks have to be excluded from the argument tables to not
make utils.format_json() error, while with JSON.stringify they are
automatically omitted.
2024-10-04 21:13:29 +03:00
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 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
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