Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions player/javascript/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,13 @@ mp.options = { read_options: read_options };
/**********************************************************************
* input
*********************************************************************/
var input_handle_counter = 1;

function register_event_handler(t) {
mp.register_script_message("input-event", function (type, args) {
var handler_id = "input-event/" + input_handle_counter;
input_handle_counter += 1;

mp.register_script_message(handler_id, function (type, args) {
if (t[type]) {
args = args ? JSON.parse(args) : [];
var result = t[type].apply(null, args);
Expand All @@ -666,18 +671,19 @@ function register_event_handler(t) {
}

if (type == "closed")
mp.unregister_script_message("input-event");
mp.unregister_script_message(handler_id);
})

return handler_id;
}

mp.input = {
get: function(t) {
t.has_completions = t.complete !== undefined

mp.commandv("script-message-to", "console", "get-input", mp.script_name,
JSON.stringify(t));
t.has_completions = t.complete !== undefined;
var handler_id = register_event_handler(t);

register_event_handler(t)
mp.commandv("script-message-to", "console", "get-input",
mp.script_name, handler_id, JSON.stringify(t));
},
terminate: function () {
mp.commandv("script-message-to", "console", "disable");
Expand Down
24 changes: 15 additions & 9 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ local key_bindings = {}
local dont_bind_up_down = false
local global_margins = { t = 0, b = 0 }
local input_caller
local input_caller_handler
local keep_open = false

local completion_buffer = {}
Expand Down Expand Up @@ -949,7 +950,7 @@ end
local function handle_edit()
if not selectable_items then
handle_cursor_move()
mp.commandv("script-message-to", input_caller, "input-event", "edited",
mp.commandv("script-message-to", input_caller, input_caller_handler, "edited",
utils.format_json({line}))
return
end
Expand Down Expand Up @@ -1070,7 +1071,7 @@ local function submit()

if selectable_items then
if #matches > 0 then
mp.commandv("script-message-to", input_caller, "input-event", "submit",
mp.commandv("script-message-to", input_caller, input_caller_handler, "submit",
utils.format_json({matches[focused_match].index}))
end
else
Expand All @@ -1079,7 +1080,7 @@ local function submit()
cycle_through_completions()
end

mp.commandv("script-message-to", input_caller, "input-event", "submit",
mp.commandv("script-message-to", input_caller, input_caller_handler, "submit",
utils.format_json({line}))

history_add(line)
Expand Down Expand Up @@ -1481,7 +1482,7 @@ end
complete = function ()
completion_old_line = line
completion_old_cursor = cursor
mp.commandv("script-message-to", input_caller, "input-event",
mp.commandv("script-message-to", input_caller, input_caller_handler,
"complete", utils.format_json({line:sub(1, cursor - 1)}))
render()
end
Expand Down Expand Up @@ -1651,7 +1652,7 @@ set_active = function (active)
unbind_mouse()
mp.set_property_bool("user-data/mpv/console/open", false)
mp.set_property_bool("input-ime", ime_active)
mp.commandv("script-message-to", input_caller, "input-event",
mp.commandv("script-message-to", input_caller, input_caller_handler,
"closed", utils.format_json({line, cursor}))
collectgarbage()
end
Expand All @@ -1662,13 +1663,14 @@ mp.register_script_message("disable", function()
set_active(false)
end)

mp.register_script_message("get-input", function (script_name, args)
if open and script_name ~= input_caller then
mp.commandv("script-message-to", input_caller, "input-event",
mp.register_script_message("get-input", function (script_name, handler_id, args)
if open then
mp.commandv("script-message-to", input_caller, input_caller_handler,
"closed", utils.format_json({line, cursor}))
end

input_caller = script_name
input_caller_handler = handler_id
args = utils.parse_json(args)
prompt = args.prompt or ""
line = args.default_text or ""
Expand Down Expand Up @@ -1721,11 +1723,15 @@ mp.register_script_message("get-input", function (script_name, args)

if line ~= "" then
complete()
elseif open then
-- This is needed to update the prompt if a new request is
-- received while another is still active.
render()
end
end

set_active(true)
mp.commandv("script-message-to", input_caller, "input-event", "opened")
mp.commandv("script-message-to", input_caller, input_caller_handler, "opened")
end)

-- Add a line to the log buffer
Expand Down
15 changes: 10 additions & 5 deletions player/lua/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ License along with mpv. If not, see <http://www.gnu.org/licenses/>.

local utils = require "mp.utils"
local input = {}
local handle_counter = 1

local function get_non_callbacks(t)
local non_callbacks = {}
Expand All @@ -31,7 +32,10 @@ local function get_non_callbacks(t)
end

local function register_event_handler(t)
mp.register_script_message("input-event", function (type, args)
local handler_id = "input-event/"..handle_counter
handle_counter = handle_counter + 1

mp.register_script_message(handler_id, function (type, args)
if t[type] then
local completions, completion_pos, completion_append =
t[type](unpack(utils.parse_json(args or "") or {}))
Expand All @@ -44,18 +48,19 @@ local function register_event_handler(t)
end

if type == "closed" then
mp.unregister_script_message("input-event")
mp.unregister_script_message(handler_id)
end
end)

return handler_id
end

function input.get(t)
t.has_completions = t.complete ~= nil

local handler_id = register_event_handler(t)
mp.commandv("script-message-to", "console", "get-input",
mp.get_script_name(), utils.format_json(get_non_callbacks(t)))

register_event_handler(t)
mp.get_script_name(), handler_id, utils.format_json(get_non_callbacks(t)))
end

input.select = input.get
Expand Down