Skip to content

Commit 4de6117

Browse files
mp.input: allow specifying which input request to terminate
This commit adds a new argument to the `input.terminate()` function which allows specifying the specific input request to cancel. Previosuly, 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 provides a backwards compatible way to terminate specific requests. Now `input.get` and `input.select` return an opaque id object which can be optionally passed into `input.terminate()` to specify which input request should be terminated. One advantage of this approach is that it is backwards compatible; a script passing id values will work without issue on older versions of mpv as `input.terminate()` previously ignored all of its arguments. The documentation has been updated to show an example of using these id values and to promote safe handling of async terminations. This approach is based on the behaviour of `mp.command_native_async` and `mp.abort_async_command`.
1 parent 103df92 commit 4de6117

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

DOCS/man/javascript.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ meta-paths like ``~~/foo`` (other JS file functions do expand meta paths).
196196
``mp.options.read_options(obj [, identifier [, on_update]])`` (types:
197197
string/boolean/number)
198198

199-
``mp.input.get(obj)``
199+
``id = mp.input.get(obj)``
200200

201-
``mp.input.select(obj)``
201+
``id = mp.input.select(obj)``
202202

203-
``mp.input.terminate()``
203+
``mp.input.terminate([id])``
204204

205205
``mp.input.log(message, style)``
206206

DOCS/man/lua.rst

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,35 @@ REPL.
975975
among the ones stored for ``input.get()`` calls. Defaults to the calling
976976
script name with ``prompt`` appended.
977977

978-
``input.terminate()``
979-
Close the console.
978+
Returns a table with undefined contents, which can be used as argument for
979+
``input.terminate()``.
980+
981+
``input.terminate([t])``
982+
Close the console. If the return value of an ``input.get`` or ``input.select``
983+
request is passed in as the first argument then the console will only be
984+
closed if it is presenting that specific request.
985+
986+
Example:
987+
988+
::
989+
990+
local id, timer
991+
id = input.get({
992+
prompt = "Open File:\n",
993+
history_path = "~~state/open-file-history",
994+
opened = function()
995+
timer = mp.add_timeout(20, function()
996+
input.terminate(id)
997+
end)
998+
end,
999+
edited = function()
1000+
timer:kill()
1001+
timer:resume()
1002+
end,
1003+
submit = function(path)
1004+
mp.commandv("loadfile", path)
1005+
end
1006+
})
9801007

9811008
``input.log(message, style, terminal_style)``
9821009
Add a line to the log buffer. ``style`` can contain additional ASS tags to
@@ -1027,6 +1054,9 @@ REPL.
10271054
``keep_open``
10281055
Whether to keep the console open on submit. Defaults to ``false``.
10291056

1057+
Returns a table with undefined contents, which can be used as argument for
1058+
``input.terminate()``.
1059+
10301060
Example:
10311061

10321062
::

player/javascript/defaults.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,16 @@ mp.input = {
684684

685685
mp.commandv("script-message-to", "console", "get-input",
686686
mp.script_name, handler_id, JSON.stringify(t));
687+
688+
return {
689+
handler_id: handler_id
690+
}
687691
},
688-
terminate: function () {
689-
mp.commandv("script-message-to", "console", "disable");
692+
terminate: function (t) {
693+
mp.commandv("script-message-to", "console", "terminate-input", JSON.stringify({
694+
script_name: mp.script_name,
695+
handler_id: (t ? t.handler_id : '') || '',
696+
}));
690697
},
691698
log: function (message, style, terminal_style) {
692699
mp.commandv("script-message-to", "console", "log", JSON.stringify({

player/lua/console.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,15 @@ mp.register_script_message("disable", function()
16631663
set_active(false)
16641664
end)
16651665

1666+
mp.register_script_message("terminate-input", function(message)
1667+
message = utils.parse_json(message)
1668+
if not message.handler_id or message.handler_id == '' then
1669+
set_active(false)
1670+
elseif message.script_name == input_caller and message.handler_id == input_caller_handler then
1671+
set_active(false)
1672+
end
1673+
end)
1674+
16661675
mp.register_script_message("get-input", function (script_name, handler_id, args)
16671676
if open then
16681677
mp.commandv("script-message-to", input_caller, input_caller_handler,

player/lua/input.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ function input.get(t)
6161
local handler_id = register_event_handler(t)
6262
mp.commandv("script-message-to", "console", "get-input",
6363
mp.get_script_name(), handler_id, utils.format_json(get_non_callbacks(t)))
64+
65+
return {
66+
handler_id = handler_id
67+
}
6468
end
6569

6670
input.select = input.get
6771

68-
function input.terminate()
69-
mp.commandv("script-message-to", "console", "disable")
72+
function input.terminate(t)
73+
mp.commandv("script-message-to", "console", "terminate-input", utils.format_json({
74+
script_name = mp.get_script_name(),
75+
handler_id = t and t.handler_id or ''
76+
}))
7077
end
7178

7279
function input.log(message, style, terminal_style)

0 commit comments

Comments
 (0)