2024-01-02 17:58:32 +00:00
|
|
|
--[[
|
|
|
|
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 = {}
|
|
|
|
|
2024-10-01 18:25:36 +00:00
|
|
|
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
|
|
|
|
|
2024-05-04 17:02:18 +00:00
|
|
|
local function register_event_handler(t)
|
2024-05-08 08:22:39 +00:00
|
|
|
mp.register_script_message("input-event", function (type, args)
|
2024-01-02 17:58:32 +00:00
|
|
|
if t[type] then
|
2024-05-08 08:22:39 +00:00
|
|
|
local suggestions, completion_start_position =
|
|
|
|
t[type](unpack(utils.parse_json(args or "") or {}))
|
2024-01-02 17:58:32 +00:00
|
|
|
|
|
|
|
if type == "complete" and suggestions then
|
|
|
|
mp.commandv("script-message-to", "console", "complete",
|
|
|
|
utils.format_json(suggestions), completion_start_position)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if type == "closed" then
|
|
|
|
mp.unregister_script_message("input-event")
|
|
|
|
end
|
|
|
|
end)
|
2024-05-04 17:02:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function input.get(t)
|
|
|
|
mp.commandv("script-message-to", "console", "get-input",
|
2024-10-01 18:25:36 +00:00
|
|
|
mp.get_script_name(), utils.format_json(get_non_callbacks(t)))
|
2024-05-04 17:02:18 +00:00
|
|
|
|
|
|
|
register_event_handler(t)
|
|
|
|
end
|
|
|
|
|
2024-10-01 18:25:36 +00:00
|
|
|
input.select = input.get
|
2024-01-02 17:58:32 +00:00
|
|
|
|
|
|
|
function input.terminate()
|
|
|
|
mp.commandv("script-message-to", "console", "disable")
|
|
|
|
end
|
|
|
|
|
2024-01-13 07:40:16 +00:00
|
|
|
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,
|
|
|
|
}))
|
2024-01-02 17:58:32 +00:00
|
|
|
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
|