diff options
Diffstat (limited to 'player/lua/defaults.lua')
-rw-r--r-- | player/lua/defaults.lua | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 0fe90977e6..5f0ced76a5 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -1,5 +1,6 @@ -- Compatibility shim for lua 5.2/5.3 -unpack = unpack or table.unpack +-- luacheck: globals unpack +unpack = unpack or table.unpack -- luacheck: globals table.unpack -- these are used internally by lua.c mp.UNKNOWN_TYPE.info = "this value is inserted if the C type is not supported" @@ -95,7 +96,7 @@ function mp.set_key_bindings(list, section, flags) local cb_up = entry[4] if type(cb) ~= "string" then local mangle = reserve_binding() - dispatch_key_bindings[mangle] = function(name, state) + dispatch_key_bindings[mangle] = function(_, state) local event = state:sub(1, 1) local is_mouse = state:sub(2, 2) == "m" local def = (is_mouse and "u") or "d" @@ -156,7 +157,7 @@ function mp.flush_keybindings() flags = "force" end local bindings = {} - for k, v in pairs(key_bindings) do + for _, v in pairs(key_bindings) do if v.bind and v.forced ~= def then bindings[#bindings + 1] = v end @@ -175,7 +176,7 @@ function mp.flush_keybindings() end local function add_binding(attrs, key, name, fn, rp) - if (type(name) ~= "string") and (name ~= nil) then + if type(name) ~= "string" and name ~= nil then rp = fn fn = name name = nil @@ -199,7 +200,7 @@ local function add_binding(attrs, key, name, fn, rp) ["r"] = "repeat", ["p"] = "press", } - key_cb = function(name, state, key_name, key_text) + key_cb = function(_, state, key_name, key_text) if key_text == "" then key_text = nil end @@ -214,7 +215,7 @@ local function add_binding(attrs, key, name, fn, rp) fn({event = "press", is_mouse = false}) end else - key_cb = function(name, state) + key_cb = function(_, state) -- Emulate the same semantics as input.c uses for most bindings: -- For keyboard, "down" runs the command, "up" does nothing; -- for mouse, "down" does nothing, "up" runs the command. @@ -226,7 +227,7 @@ local function add_binding(attrs, key, name, fn, rp) end if is_mouse and (event == "u" or event == "p") then fn() - elseif (not is_mouse) and (event == "d" or event == "r" or event == "p") then + elseif not is_mouse and (event == "d" or event == "r" or event == "p") then fn() end end @@ -265,20 +266,22 @@ local timers = {} local timer_mt = {} timer_mt.__index = timer_mt -function mp.add_timeout(seconds, cb) - local t = mp.add_periodic_timer(seconds, cb) +function mp.add_timeout(seconds, cb, disabled) + local t = mp.add_periodic_timer(seconds, cb, disabled) t.oneshot = true return t end -function mp.add_periodic_timer(seconds, cb) +function mp.add_periodic_timer(seconds, cb, disabled) local t = { timeout = seconds, cb = cb, oneshot = false, } setmetatable(t, timer_mt) - t:resume() + if not disabled then + t:resume() + end return t end @@ -314,7 +317,7 @@ end local function get_next_timer() local best = nil for t, _ in pairs(timers) do - if (best == nil) or (t.next_deadline < best.next_deadline) then + if best == nil or t.next_deadline < best.next_deadline then best = t end end @@ -330,9 +333,11 @@ function mp.get_next_timeout() return timer.next_deadline - now end --- Run timers that have met their deadline. --- Return: next absolute time a timer expires as number, or nil if no timers +-- Run timers that have met their deadline at the time of invocation. +-- Return: time>0 in seconds till the next due timer, 0 if there are due timers +-- (aborted to avoid infinite loop), or nil if no timers local function process_timers() + local t0 = nil while true do local timer = get_next_timer() if not timer then @@ -343,6 +348,14 @@ local function process_timers() if wait > 0 then return wait else + if not t0 then + t0 = now -- first due callback: always executes, remember t0 + elseif timer.next_deadline > t0 then + -- don't block forever with slow callbacks and endless timers. + -- we'll continue right after checking mpv events. + return 0 + end + if timer.oneshot then timer:kill() else @@ -416,7 +429,7 @@ end function mp.unregister_event(cb) for name, sub in pairs(event_handlers) do local found = false - for i, e in ipairs(sub) do + for _, e in ipairs(sub) do if e == cb then found = true break @@ -518,12 +531,20 @@ function mp.dispatch_events(allow_wait) local wait = 0 if not more_events then wait = process_timers() or 1e20 -- infinity for all practical purposes - for _, handler in ipairs(idle_handlers) do - handler() + if wait ~= 0 then + local idle_called = nil + for _, handler in ipairs(idle_handlers) do + idle_called = true + handler() + end + if idle_called then + -- handlers don't complete in 0 time, and may modify timers + wait = mp.get_next_timeout() or 1e20 + if wait < 0 then + wait = 0 + end + end end - -- Resume playloop - important especially if an error happened while - -- suspended, and the error was handled, but no resume was done. - mp.resume_all() if allow_wait ~= true then return end @@ -578,7 +599,7 @@ mp.register_event("hook", function(ev) if fn then fn(hookobj) end - if (not hookobj._defer) and hookobj._id ~= nil then + if not hookobj._defer and hookobj._id ~= nil then hookobj:cont() end end) @@ -597,9 +618,10 @@ local async_next_id = 1 function mp.command_native_async(node, cb) local id = async_next_id async_next_id = async_next_id + 1 + cb = cb or function() end local res, err = mp.raw_command_native_async(id, node) if not res then - cb(false, nil, err) + mp.add_timeout(0, function() cb(false, nil, err) end) return res, err end local t = {cb = cb, id = id} @@ -788,28 +810,4 @@ function mp_utils.subprocess_detached(t) mp.commandv("run", unpack(t.args)) end -function mp_utils.shared_script_property_set(name, value) - if value ~= nil then - -- no such thing as change-list with mpv_node, so build a string value - mp.commandv("change-list", "shared-script-properties", "append", - name .. "=" .. value) - else - mp.commandv("change-list", "shared-script-properties", "remove", name) - end -end - -function mp_utils.shared_script_property_get(name) - local map = mp.get_property_native("shared-script-properties") - return map and map[name] -end - --- cb(name, value) on change and on init -function mp_utils.shared_script_property_observe(name, cb) - -- it's _very_ wasteful to observe the mpv core "super" property for every - -- shared sub-property, but then again you shouldn't use this - mp.observe_property("shared-script-properties", "native", function(_, val) - cb(name, val and val[name]) - end) -end - return {} |