diff options
author | wm4 <wm4@nowhere> | 2014-02-17 02:38:07 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-02-17 02:52:58 +0100 |
commit | 24fa69dbfa6c5c7a80456218f2c31179864c4d77 (patch) | |
tree | 0312cccc355f78cfa1627155eca77cbc0ae6180a /player/lua | |
parent | 12b7850f11f524ec158b392d7fceabd4ba193ba3 (diff) | |
download | mpv-24fa69dbfa6c5c7a80456218f2c31179864c4d77.tar.bz2 mpv-24fa69dbfa6c5c7a80456218f2c31179864c4d77.tar.xz |
lua: add mechanism for script provided key bindings
There was already an undocumented mechanism provided by
mp.set_key_bindings and other functions, but this was relatively
verbose, and also weird. It was mainly to make the OSC happy (including
being efficient and supporting weird corner cases), while the new
functions try to be a bit simpler.
This also provides a way to let users rebind script-provided commands.
(This mechanism is less efficient, because it's O(n^2) for n added key
bindings, but it shouldn't matter.)
Diffstat (limited to 'player/lua')
-rw-r--r-- | player/lua/defaults.lua | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 037529940f..f6dff96c3d 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -4,7 +4,7 @@ end local callbacks = {} -- each script has its own section, so that they don't conflict -local default_section = "input_" .. mp.script_name +local default_section = "input_dispatch_" .. mp.script_name -- Set the list of key bindings. These will override the user's bindings, so -- you should use this sparingly. @@ -66,6 +66,64 @@ local function script_dispatch(event) end end +-- "Newer" and more convenient API + +local key_bindings = {} +local command_id = 1 + +local function update_key_bindings() + for i = 1, 2 do + local section, flags + local def = i == 1 + if def then + section = "input_" .. mp.script_name + flags = "builtin" + else + section = "input_forced_" .. mp.script_name + flags = "default" + end + local cfg = "" + for k, v in pairs(key_bindings) do + if v.forced ~= def then + cfg = cfg .. v.key .. " script_message " .. mp.script_name + .. " " .. v.name .. "\n" + end + end + mp.input_define_section(section, cfg, flags) + -- TODO: remove the section if the script is stopped + mp.input_enable_section(section) + end +end + +local function add_binding(attrs, key, name, fn) + if (type(name) ~= "string") and (not fn) then + fn = name + name = "command" .. tostring(command_id) + command_id = command_id + 1 + end + attrs.key = key + attrs.name = name + key_bindings[name] = attrs + update_key_bindings() + if fn then + mp.register_script_command(name, fn) + end +end + +function mp.add_key_binding(...) + add_binding({forced=false}, ...) +end + +function mp.add_forced_key_binding(...) + add_binding({forced=true}, ...) +end + +function mp.remove_key_binding(name) + key_bindings[name] = nil + update_key_bindings() + mp.unregister_script_command(name) +end + local timers = {} function mp.add_timeout(seconds, cb) @@ -125,6 +183,23 @@ local function process_timers() end end +local commands = {} + +function mp.register_script_command(name, fn) + commands[name] = fn +end + +function mp.unregister_script_command(name) + commands[name] = nil +end + +local function command_dispatch(ev) + if #ev.args > 0 then + local handler = commands[ev.args[1]] + handler(unpack(ev.args, 2)) + end +end + -- used by default event loop (mp_event_loop()) to decide when to quit mp.keep_running = true @@ -143,6 +218,7 @@ end -- default handlers mp.register_event("shutdown", function() mp.keep_running = false end) mp.register_event("script-input-dispatch", script_dispatch) +mp.register_event("client-message", command_dispatch) mp.msg = { log = mp.log, |