Skip to content

Commit 103df92

Browse files
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.
1 parent 9483d6e commit 103df92

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

player/javascript/defaults.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,13 @@ mp.options = { read_options: read_options };
653653
/**********************************************************************
654654
* input
655655
*********************************************************************/
656+
var input_handle_counter = 1;
657+
656658
function register_event_handler(t) {
657-
mp.register_script_message("input-event", function (type, args) {
659+
var handler_id = "input-event/" + input_handle_counter;
660+
input_handle_counter += 1;
661+
662+
mp.register_script_message(handler_id, function (type, args) {
658663
if (t[type]) {
659664
args = args ? JSON.parse(args) : [];
660665
var result = t[type].apply(null, args);
@@ -666,18 +671,19 @@ function register_event_handler(t) {
666671
}
667672

668673
if (type == "closed")
669-
mp.unregister_script_message("input-event");
674+
mp.unregister_script_message(handler_id);
670675
})
676+
677+
return handler_id;
671678
}
672679

673680
mp.input = {
674681
get: function(t) {
675-
t.has_completions = t.complete !== undefined
676-
677-
mp.commandv("script-message-to", "console", "get-input", mp.script_name,
678-
JSON.stringify(t));
682+
t.has_completions = t.complete !== undefined;
683+
var handler_id = register_event_handler(t);
679684

680-
register_event_handler(t)
685+
mp.commandv("script-message-to", "console", "get-input",
686+
mp.script_name, handler_id, JSON.stringify(t));
681687
},
682688
terminate: function () {
683689
mp.commandv("script-message-to", "console", "disable");

player/lua/console.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ local key_bindings = {}
9696
local dont_bind_up_down = false
9797
local global_margins = { t = 0, b = 0 }
9898
local input_caller
99+
local input_caller_handler
99100
local keep_open = false
100101

101102
local completion_buffer = {}
@@ -949,7 +950,7 @@ end
949950
local function handle_edit()
950951
if not selectable_items then
951952
handle_cursor_move()
952-
mp.commandv("script-message-to", input_caller, "input-event", "edited",
953+
mp.commandv("script-message-to", input_caller, input_caller_handler, "edited",
953954
utils.format_json({line}))
954955
return
955956
end
@@ -1070,7 +1071,7 @@ local function submit()
10701071

10711072
if selectable_items then
10721073
if #matches > 0 then
1073-
mp.commandv("script-message-to", input_caller, "input-event", "submit",
1074+
mp.commandv("script-message-to", input_caller, input_caller_handler, "submit",
10741075
utils.format_json({matches[focused_match].index}))
10751076
end
10761077
else
@@ -1079,7 +1080,7 @@ local function submit()
10791080
cycle_through_completions()
10801081
end
10811082

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

10851086
history_add(line)
@@ -1481,7 +1482,7 @@ end
14811482
complete = function ()
14821483
completion_old_line = line
14831484
completion_old_cursor = cursor
1484-
mp.commandv("script-message-to", input_caller, "input-event",
1485+
mp.commandv("script-message-to", input_caller, input_caller_handler,
14851486
"complete", utils.format_json({line:sub(1, cursor - 1)}))
14861487
render()
14871488
end
@@ -1651,7 +1652,7 @@ set_active = function (active)
16511652
unbind_mouse()
16521653
mp.set_property_bool("user-data/mpv/console/open", false)
16531654
mp.set_property_bool("input-ime", ime_active)
1654-
mp.commandv("script-message-to", input_caller, "input-event",
1655+
mp.commandv("script-message-to", input_caller, input_caller_handler,
16551656
"closed", utils.format_json({line, cursor}))
16561657
collectgarbage()
16571658
end
@@ -1662,13 +1663,14 @@ mp.register_script_message("disable", function()
16621663
set_active(false)
16631664
end)
16641665

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

16711672
input_caller = script_name
1673+
input_caller_handler = handler_id
16721674
args = utils.parse_json(args)
16731675
prompt = args.prompt or ""
16741676
line = args.default_text or ""
@@ -1721,11 +1723,15 @@ mp.register_script_message("get-input", function (script_name, args)
17211723

17221724
if line ~= "" then
17231725
complete()
1726+
elseif open then
1727+
-- This is needed to update the prompt if a new request is
1728+
-- received while another is still active.
1729+
render()
17241730
end
17251731
end
17261732

17271733
set_active(true)
1728-
mp.commandv("script-message-to", input_caller, "input-event", "opened")
1734+
mp.commandv("script-message-to", input_caller, input_caller_handler, "opened")
17291735
end)
17301736

17311737
-- Add a line to the log buffer

player/lua/input.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ License along with mpv. If not, see <http://www.gnu.org/licenses/>.
1717

1818
local utils = require "mp.utils"
1919
local input = {}
20+
local handle_counter = 1
2021

2122
local function get_non_callbacks(t)
2223
local non_callbacks = {}
@@ -31,7 +32,10 @@ local function get_non_callbacks(t)
3132
end
3233

3334
local function register_event_handler(t)
34-
mp.register_script_message("input-event", function (type, args)
35+
local handler_id = "input-event/"..handle_counter
36+
handle_counter = handle_counter + 1
37+
38+
mp.register_script_message(handler_id, function (type, args)
3539
if t[type] then
3640
local completions, completion_pos, completion_append =
3741
t[type](unpack(utils.parse_json(args or "") or {}))
@@ -44,18 +48,19 @@ local function register_event_handler(t)
4448
end
4549

4650
if type == "closed" then
47-
mp.unregister_script_message("input-event")
51+
mp.unregister_script_message(handler_id)
4852
end
4953
end)
54+
55+
return handler_id
5056
end
5157

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

61+
local handler_id = register_event_handler(t)
5562
mp.commandv("script-message-to", "console", "get-input",
56-
mp.get_script_name(), utils.format_json(get_non_callbacks(t)))
57-
58-
register_event_handler(t)
63+
mp.get_script_name(), handler_id, utils.format_json(get_non_callbacks(t)))
5964
end
6065

6166
input.select = input.get

0 commit comments

Comments
 (0)