mirror of
https://github.com/mpv-player/mpv
synced 2024-12-27 17:42:17 +00:00
lua: batch-update key bindings
Lua scripting implements key bindings by defining an input section with all the bindings in it. Every add_key_binding() call ran a mpv command to update this section. This caused a lot of spam at debug log levels. Reduce the spam and more it efficient by batching updates into a single mpv command when the script becomes inactive. This is pretty simple, because there's already the concept of idle handlers. This requires that the script actually goes to sleep, which might not happen in various extremely bogus corner cases, such as polling the mpv message queue with active waiting. Just don't do that.
This commit is contained in:
parent
346c651de3
commit
96932fe77c
@ -132,8 +132,14 @@ end
|
||||
|
||||
local key_bindings = {}
|
||||
local key_binding_counter = 0
|
||||
local key_bindings_dirty = false
|
||||
|
||||
function mp.flush_keybindings()
|
||||
if not key_bindings_dirty then
|
||||
return
|
||||
end
|
||||
key_bindings_dirty = false
|
||||
|
||||
local function update_key_bindings()
|
||||
for i = 1, 2 do
|
||||
local section, flags
|
||||
local def = i == 1
|
||||
@ -229,7 +235,7 @@ local function add_binding(attrs, key, name, fn, rp)
|
||||
key_binding_counter = key_binding_counter + 1
|
||||
attrs.priority = key_binding_counter
|
||||
key_bindings[name] = attrs
|
||||
update_key_bindings()
|
||||
key_bindings_dirty = true
|
||||
dispatch_key_bindings[name] = key_cb
|
||||
mp.register_script_message(name, msg_cb)
|
||||
end
|
||||
@ -245,7 +251,7 @@ end
|
||||
function mp.remove_key_binding(name)
|
||||
key_bindings[name] = nil
|
||||
dispatch_key_bindings[name] = nil
|
||||
update_key_bindings()
|
||||
key_bindings_dirty = true
|
||||
mp.unregister_script_message(name)
|
||||
end
|
||||
|
||||
@ -517,6 +523,8 @@ function mp.dispatch_events(allow_wait)
|
||||
end
|
||||
end
|
||||
|
||||
mp.register_idle(mp.flush_keybindings)
|
||||
|
||||
-- additional helpers
|
||||
|
||||
function mp.osd_message(text, duration)
|
||||
|
Loading…
Reference in New Issue
Block a user