mirror of
https://github.com/mpv-player/mpv.git
synced 2026-10-18 07:30:39 +08:00
bede44618b
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.
78 lines
2.3 KiB
Lua
78 lines
2.3 KiB
Lua
--[[
|
|
This file is part of mpv.
|
|
|
|
mpv is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
mpv is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
]]
|
|
|
|
local utils = require "mp.utils"
|
|
local input = {}
|
|
|
|
local function get_non_callbacks(t)
|
|
local non_callbacks = {}
|
|
|
|
for key, value in pairs(t) do
|
|
if type(value) ~= "function" then
|
|
non_callbacks[key] = value
|
|
end
|
|
end
|
|
|
|
return non_callbacks
|
|
end
|
|
|
|
local function register_event_handler(t)
|
|
mp.register_script_message("input-event", function (type, args)
|
|
if t[type] then
|
|
local suggestions, completion_start_position =
|
|
t[type](unpack(utils.parse_json(args or "") or {}))
|
|
|
|
if type == "complete" and suggestions then
|
|
mp.commandv("script-message-to", "console", "complete",
|
|
utils.format_json(suggestions), completion_start_position)
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
|
|
function input.get(t)
|
|
mp.commandv("script-message-to", "console", "get-input",
|
|
mp.get_script_name(), utils.format_json(get_non_callbacks(t)))
|
|
|
|
register_event_handler(t)
|
|
end
|
|
|
|
input.select = input.get
|
|
|
|
function input.terminate()
|
|
mp.commandv("script-message-to", "console", "disable")
|
|
end
|
|
|
|
function input.log(message, style, terminal_style)
|
|
mp.commandv("script-message-to", "console", "log", utils.format_json({
|
|
text = message,
|
|
style = style,
|
|
terminal_style = terminal_style,
|
|
}))
|
|
end
|
|
|
|
function input.log_error(message)
|
|
mp.commandv("script-message-to", "console", "log",
|
|
utils.format_json({ text = message, error = true }))
|
|
end
|
|
|
|
function input.set_log(log)
|
|
mp.commandv("script-message-to", "console", "set-log", utils.format_json(log))
|
|
end
|
|
|
|
return input
|