diff options
Diffstat (limited to 'player/lua/defaults.lua')
-rw-r--r-- | player/lua/defaults.lua | 86 |
1 files changed, 46 insertions, 40 deletions
diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 95f8952f5d..baa3a2461e 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -42,6 +42,11 @@ function mp.input_disable_section(section) mp.commandv("disable-section", section) end +function mp.get_mouse_pos() + local m = mp.get_property_native("mouse-pos") + return m.x, m.y +end + -- For dispatching script-binding. This is sent as: -- script-message-to $script_name $binding_name $keystate -- The array is indexed by $binding_name, and has functions like this as value: @@ -170,7 +175,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 @@ -221,7 +226,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 @@ -260,20 +265,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 @@ -309,7 +316,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 @@ -325,9 +332,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 @@ -338,6 +347,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 @@ -513,12 +530,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 @@ -573,7 +598,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) @@ -592,9 +617,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} @@ -742,6 +768,10 @@ function mp_utils.getcwd() return mp.get_property("working-directory") end +function mp_utils.getpid() + return mp.get_property_number("pid") +end + function mp_utils.format_bytes_humanized(b) local d = {"Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"} local i = 1 @@ -779,28 +809,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 {} |