2015-03-06 11:20:57 +00:00
|
|
|
-- Compatibility shim for lua 5.2/5.3
|
2024-05-12 00:47:08 +00:00
|
|
|
-- luacheck: globals unpack
|
|
|
|
unpack = unpack or table.unpack -- luacheck: globals table.unpack
|
2014-04-10 21:56:06 +00:00
|
|
|
|
|
|
|
-- these are used internally by lua.c
|
2014-02-26 21:32:57 +00:00
|
|
|
mp.UNKNOWN_TYPE.info = "this value is inserted if the C type is not supported"
|
|
|
|
mp.UNKNOWN_TYPE.type = "UNKNOWN_TYPE"
|
|
|
|
|
|
|
|
mp.ARRAY.info = "native array"
|
|
|
|
mp.ARRAY.type = "ARRAY"
|
|
|
|
|
|
|
|
mp.MAP.info = "native map"
|
|
|
|
mp.MAP.type = "MAP"
|
2014-02-24 19:47:20 +00:00
|
|
|
|
2014-02-10 23:57:40 +00:00
|
|
|
function mp.get_script_name()
|
|
|
|
return mp.script_name
|
|
|
|
end
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
|
2014-02-26 19:58:17 +00:00
|
|
|
function mp.get_opt(key, def)
|
2014-12-14 23:31:30 +00:00
|
|
|
local opts = mp.get_property_native("options/script-opts")
|
2014-02-26 19:58:17 +00:00
|
|
|
local val = opts[key]
|
|
|
|
if val == nil then
|
|
|
|
val = def
|
|
|
|
end
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
2015-08-05 22:14:23 +00:00
|
|
|
function mp.input_define_section(section, contents, flags)
|
|
|
|
if flags == nil or flags == "" then
|
|
|
|
flags = "default"
|
|
|
|
end
|
|
|
|
mp.commandv("define-section", section, contents, flags)
|
|
|
|
end
|
|
|
|
|
2015-08-05 22:31:47 +00:00
|
|
|
function mp.input_enable_section(section, flags)
|
|
|
|
if flags == nil then
|
|
|
|
flags = ""
|
|
|
|
end
|
|
|
|
mp.commandv("enable-section", section, flags)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mp.input_disable_section(section)
|
|
|
|
mp.commandv("disable-section", section)
|
|
|
|
end
|
|
|
|
|
2020-11-16 16:15:18 +00:00
|
|
|
function mp.get_mouse_pos()
|
|
|
|
local m = mp.get_property_native("mouse-pos")
|
|
|
|
return m.x, m.y
|
|
|
|
end
|
|
|
|
|
2016-07-14 18:04:59 +00:00
|
|
|
-- For dispatching script-binding. This is sent as:
|
|
|
|
-- script-message-to $script_name $binding_name $keystate
|
2014-11-23 14:08:49 +00:00
|
|
|
-- The array is indexed by $binding_name, and has functions like this as value:
|
|
|
|
-- fn($binding_name, $keystate)
|
|
|
|
local dispatch_key_bindings = {}
|
|
|
|
|
|
|
|
local message_id = 0
|
|
|
|
local function reserve_binding()
|
|
|
|
message_id = message_id + 1
|
|
|
|
return "__keybinding" .. tostring(message_id)
|
|
|
|
end
|
|
|
|
|
2019-11-21 22:01:56 +00:00
|
|
|
local function dispatch_key_binding(name, state, key_name, key_text)
|
2014-11-23 14:08:49 +00:00
|
|
|
local fn = dispatch_key_bindings[name]
|
|
|
|
if fn then
|
2019-11-21 22:01:56 +00:00
|
|
|
fn(name, state, key_name, key_text)
|
2014-11-23 14:08:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- "Old", deprecated API
|
|
|
|
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
-- each script has its own section, so that they don't conflict
|
2014-02-17 01:38:07 +00:00
|
|
|
local default_section = "input_dispatch_" .. mp.script_name
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
|
|
|
|
-- Set the list of key bindings. These will override the user's bindings, so
|
|
|
|
-- you should use this sparingly.
|
|
|
|
-- A call to this function will remove all bindings previously set with this
|
|
|
|
-- function. For example, set_key_bindings({}) would remove all script defined
|
|
|
|
-- key bindings.
|
|
|
|
-- Note: the bindings are not active by default. Use enable_key_bindings().
|
|
|
|
--
|
|
|
|
-- list is an array of key bindings, where each entry is an array as follow:
|
2014-11-23 14:08:49 +00:00
|
|
|
-- {key, callback_press, callback_down, callback_up}
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
-- key is the key string as used in input.conf, like "ctrl+a"
|
|
|
|
--
|
|
|
|
-- callback can be a string too, in which case the following will be added like
|
|
|
|
-- an input.conf line: key .. " " .. callback
|
|
|
|
-- (And callback_down is ignored.)
|
2014-02-25 23:59:19 +00:00
|
|
|
function mp.set_key_bindings(list, section, flags)
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
local cfg = ""
|
|
|
|
for i = 1, #list do
|
|
|
|
local entry = list[i]
|
|
|
|
local key = entry[1]
|
|
|
|
local cb = entry[2]
|
|
|
|
local cb_down = entry[3]
|
2014-11-23 14:08:49 +00:00
|
|
|
local cb_up = entry[4]
|
|
|
|
if type(cb) ~= "string" then
|
|
|
|
local mangle = reserve_binding()
|
2024-05-12 00:47:08 +00:00
|
|
|
dispatch_key_bindings[mangle] = function(_, state)
|
2014-11-23 14:08:49 +00:00
|
|
|
local event = state:sub(1, 1)
|
|
|
|
local is_mouse = state:sub(2, 2) == "m"
|
|
|
|
local def = (is_mouse and "u") or "d"
|
|
|
|
if event == "r" then
|
2014-11-24 15:47:03 +00:00
|
|
|
return
|
2014-11-23 14:08:49 +00:00
|
|
|
end
|
|
|
|
if event == "p" and cb then
|
|
|
|
cb()
|
|
|
|
elseif event == "d" and cb_down then
|
|
|
|
cb_down()
|
|
|
|
elseif event == "u" and cb_up then
|
|
|
|
cb_up()
|
|
|
|
elseif event == def and cb then
|
|
|
|
cb()
|
|
|
|
end
|
|
|
|
end
|
2015-05-25 19:59:44 +00:00
|
|
|
cfg = cfg .. key .. " script-binding " ..
|
2014-11-23 14:08:49 +00:00
|
|
|
mp.script_name .. "/" .. mangle .. "\n"
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
else
|
|
|
|
cfg = cfg .. key .. " " .. cb .. "\n"
|
|
|
|
end
|
|
|
|
end
|
2014-02-25 23:59:19 +00:00
|
|
|
mp.input_define_section(section or default_section, cfg, flags)
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function mp.enable_key_bindings(section, flags)
|
|
|
|
mp.input_enable_section(section or default_section, flags)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mp.disable_key_bindings(section)
|
|
|
|
mp.input_disable_section(section or default_section)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mp.set_mouse_area(x0, y0, x1, y1, section)
|
|
|
|
mp.input_set_section_mouse_area(section or default_section, x0, y0, x1, y1)
|
|
|
|
end
|
|
|
|
|
2014-02-17 01:38:07 +00:00
|
|
|
-- "Newer" and more convenient API
|
|
|
|
|
|
|
|
local key_bindings = {}
|
2019-12-07 13:53:33 +00:00
|
|
|
local key_binding_counter = 0
|
2019-12-23 10:17:01 +00:00
|
|
|
local key_bindings_dirty = false
|
|
|
|
|
|
|
|
function mp.flush_keybindings()
|
|
|
|
if not key_bindings_dirty then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
key_bindings_dirty = false
|
2014-02-17 01:38:07 +00:00
|
|
|
|
|
|
|
for i = 1, 2 do
|
|
|
|
local section, flags
|
|
|
|
local def = i == 1
|
|
|
|
if def then
|
|
|
|
section = "input_" .. mp.script_name
|
2014-02-28 23:42:53 +00:00
|
|
|
flags = "default"
|
2014-02-17 01:38:07 +00:00
|
|
|
else
|
|
|
|
section = "input_forced_" .. mp.script_name
|
2014-02-28 23:42:53 +00:00
|
|
|
flags = "force"
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
2019-12-07 13:53:33 +00:00
|
|
|
local bindings = {}
|
2024-05-12 00:47:08 +00:00
|
|
|
for _, v in pairs(key_bindings) do
|
2016-03-26 09:44:57 +00:00
|
|
|
if v.bind and v.forced ~= def then
|
2019-12-07 13:53:33 +00:00
|
|
|
bindings[#bindings + 1] = v
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-07 13:53:33 +00:00
|
|
|
table.sort(bindings, function(a, b)
|
|
|
|
return a.priority < b.priority
|
|
|
|
end)
|
|
|
|
local cfg = ""
|
|
|
|
for _, v in ipairs(bindings) do
|
|
|
|
cfg = cfg .. v.bind .. "\n"
|
|
|
|
end
|
2014-02-17 01:38:07 +00:00
|
|
|
mp.input_define_section(section, cfg, flags)
|
|
|
|
-- TODO: remove the section if the script is stopped
|
2015-08-05 22:31:47 +00:00
|
|
|
mp.input_enable_section(section, "allow-hide-cursor+allow-vo-dragging")
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-20 22:59:47 +00:00
|
|
|
local function add_binding(attrs, key, name, fn, rp)
|
2023-10-31 01:23:21 +00:00
|
|
|
if type(name) ~= "string" and name ~= nil then
|
2019-11-23 13:40:00 +00:00
|
|
|
rp = fn
|
2014-02-17 01:38:07 +00:00
|
|
|
fn = name
|
2019-11-23 13:40:00 +00:00
|
|
|
name = nil
|
|
|
|
end
|
2019-11-30 09:56:21 +00:00
|
|
|
rp = rp or ""
|
2019-11-23 13:40:00 +00:00
|
|
|
if name == nil then
|
2014-11-23 14:08:49 +00:00
|
|
|
name = reserve_binding()
|
|
|
|
end
|
2014-11-24 15:47:03 +00:00
|
|
|
local repeatable = rp == "repeatable" or rp["repeatable"]
|
2014-11-23 14:08:49 +00:00
|
|
|
if rp["forced"] then
|
|
|
|
attrs.forced = true
|
|
|
|
end
|
|
|
|
local key_cb, msg_cb
|
|
|
|
if not fn then
|
|
|
|
fn = function() end
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
2014-11-23 14:08:49 +00:00
|
|
|
if rp["complex"] then
|
|
|
|
local key_states = {
|
|
|
|
["u"] = "up",
|
|
|
|
["d"] = "down",
|
|
|
|
["r"] = "repeat",
|
|
|
|
["p"] = "press",
|
|
|
|
}
|
2024-05-12 00:47:08 +00:00
|
|
|
key_cb = function(_, state, key_name, key_text)
|
2019-11-21 22:01:56 +00:00
|
|
|
if key_text == "" then
|
|
|
|
key_text = nil
|
|
|
|
end
|
2014-11-23 14:08:49 +00:00
|
|
|
fn({
|
|
|
|
event = key_states[state:sub(1, 1)] or "unknown",
|
2019-11-19 22:10:41 +00:00
|
|
|
is_mouse = state:sub(2, 2) == "m",
|
2024-06-06 05:30:35 +00:00
|
|
|
canceled = state:sub(3, 3) == "c",
|
2019-11-20 14:21:10 +00:00
|
|
|
key_name = key_name,
|
2019-11-21 22:01:56 +00:00
|
|
|
key_text = key_text,
|
2014-11-23 14:08:49 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
msg_cb = function()
|
|
|
|
fn({event = "press", is_mouse = false})
|
|
|
|
end
|
|
|
|
else
|
2024-05-12 00:47:08 +00:00
|
|
|
key_cb = function(_, state)
|
2014-11-23 14:08:49 +00:00
|
|
|
-- 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.
|
|
|
|
-- Also, key repeat triggers the binding again.
|
|
|
|
local event = state:sub(1, 1)
|
|
|
|
local is_mouse = state:sub(2, 2) == "m"
|
2024-06-06 05:30:35 +00:00
|
|
|
local canceled = state:sub(3, 3) == "c"
|
|
|
|
if canceled or event == "r" and not repeatable then
|
2014-11-24 15:47:03 +00:00
|
|
|
return
|
|
|
|
end
|
2014-12-10 17:35:25 +00:00
|
|
|
if is_mouse and (event == "u" or event == "p") then
|
2014-11-23 14:08:49 +00:00
|
|
|
fn()
|
2023-10-31 01:23:21 +00:00
|
|
|
elseif not is_mouse and (event == "d" or event == "r" or event == "p") then
|
2014-11-23 14:08:49 +00:00
|
|
|
fn()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
msg_cb = fn
|
|
|
|
end
|
2016-03-26 09:44:57 +00:00
|
|
|
if key and #key > 0 then
|
|
|
|
attrs.bind = key .. " script-binding " .. mp.script_name .. "/" .. name
|
|
|
|
end
|
2014-02-17 01:38:07 +00:00
|
|
|
attrs.name = name
|
2019-12-07 13:53:33 +00:00
|
|
|
-- new bindings override old ones (but do not overwrite them)
|
|
|
|
key_binding_counter = key_binding_counter + 1
|
|
|
|
attrs.priority = key_binding_counter
|
2014-02-17 01:38:07 +00:00
|
|
|
key_bindings[name] = attrs
|
2019-12-23 10:17:01 +00:00
|
|
|
key_bindings_dirty = true
|
2014-11-23 14:08:49 +00:00
|
|
|
dispatch_key_bindings[name] = key_cb
|
|
|
|
mp.register_script_message(name, msg_cb)
|
2014-02-17 01:38:07 +00:00
|
|
|
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
|
2014-12-03 21:40:24 +00:00
|
|
|
dispatch_key_bindings[name] = nil
|
2019-12-23 10:17:01 +00:00
|
|
|
key_bindings_dirty = true
|
2014-03-17 17:27:25 +00:00
|
|
|
mp.unregister_script_message(name)
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
|
|
|
|
2014-02-10 20:07:23 +00:00
|
|
|
local timers = {}
|
|
|
|
|
2014-04-02 15:09:45 +00:00
|
|
|
local timer_mt = {}
|
|
|
|
timer_mt.__index = timer_mt
|
|
|
|
|
2023-10-06 22:38:46 +00:00
|
|
|
function mp.add_timeout(seconds, cb, disabled)
|
|
|
|
local t = mp.add_periodic_timer(seconds, cb, disabled)
|
2014-02-10 20:07:23 +00:00
|
|
|
t.oneshot = true
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2023-10-06 22:38:46 +00:00
|
|
|
function mp.add_periodic_timer(seconds, cb, disabled)
|
2014-02-10 20:07:23 +00:00
|
|
|
local t = {
|
|
|
|
timeout = seconds,
|
|
|
|
cb = cb,
|
|
|
|
oneshot = false,
|
|
|
|
}
|
2014-04-02 15:09:45 +00:00
|
|
|
setmetatable(t, timer_mt)
|
2023-10-06 22:38:46 +00:00
|
|
|
if not disabled then
|
|
|
|
t:resume()
|
|
|
|
end
|
2014-02-10 20:07:23 +00:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2014-04-02 15:09:45 +00:00
|
|
|
function timer_mt.stop(t)
|
|
|
|
if timers[t] then
|
2014-02-10 20:07:23 +00:00
|
|
|
timers[t] = nil
|
2014-04-02 15:09:45 +00:00
|
|
|
t.next_deadline = t.next_deadline - mp.get_time()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function timer_mt.kill(t)
|
|
|
|
timers[t] = nil
|
|
|
|
t.next_deadline = nil
|
|
|
|
end
|
|
|
|
mp.cancel_timer = timer_mt.kill
|
|
|
|
|
|
|
|
function timer_mt.resume(t)
|
|
|
|
if not timers[t] then
|
|
|
|
local timeout = t.next_deadline
|
|
|
|
if timeout == nil then
|
|
|
|
timeout = t.timeout
|
|
|
|
end
|
|
|
|
t.next_deadline = mp.get_time() + timeout
|
|
|
|
timers[t] = t
|
2014-02-10 20:07:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-14 17:57:44 +00:00
|
|
|
function timer_mt.is_enabled(t)
|
|
|
|
return timers[t] ~= nil
|
|
|
|
end
|
|
|
|
|
2014-02-10 20:07:23 +00:00
|
|
|
-- Return the timer that expires next.
|
|
|
|
local function get_next_timer()
|
|
|
|
local best = nil
|
|
|
|
for t, _ in pairs(timers) do
|
2023-10-31 01:23:21 +00:00
|
|
|
if best == nil or t.next_deadline < best.next_deadline then
|
2014-02-10 20:07:23 +00:00
|
|
|
best = t
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return best
|
|
|
|
end
|
|
|
|
|
2014-04-12 18:41:12 +00:00
|
|
|
function mp.get_next_timeout()
|
|
|
|
local timer = get_next_timer()
|
|
|
|
if not timer then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local now = mp.get_time()
|
|
|
|
return timer.next_deadline - now
|
|
|
|
end
|
|
|
|
|
2021-07-13 10:11:32 +00:00
|
|
|
-- 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
|
2014-02-10 20:07:23 +00:00
|
|
|
local function process_timers()
|
lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.
This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.
For instance, this caused process_timers() to never return:
function render() <longer than 1/60 s on a slow system> end
mp.add_periodic_timer(1/60, render)
Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.
This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.
This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).
Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-22 12:18:19 +00:00
|
|
|
local t0 = nil
|
2014-02-10 20:07:23 +00:00
|
|
|
while true do
|
|
|
|
local timer = get_next_timer()
|
|
|
|
if not timer then
|
|
|
|
return
|
|
|
|
end
|
2014-04-02 15:09:45 +00:00
|
|
|
local now = mp.get_time()
|
|
|
|
local wait = timer.next_deadline - now
|
2014-02-10 20:07:23 +00:00
|
|
|
if wait > 0 then
|
|
|
|
return wait
|
|
|
|
else
|
lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.
This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.
For instance, this caused process_timers() to never return:
function render() <longer than 1/60 s on a slow system> end
mp.add_periodic_timer(1/60, render)
Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.
This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.
This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).
Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-22 12:18:19 +00:00
|
|
|
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
|
|
|
|
|
2014-02-10 20:07:23 +00:00
|
|
|
if timer.oneshot then
|
2014-04-02 15:09:45 +00:00
|
|
|
timer:kill()
|
|
|
|
else
|
|
|
|
timer.next_deadline = now + timer.timeout
|
2014-02-10 20:07:23 +00:00
|
|
|
end
|
|
|
|
timer.cb()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-17 17:27:25 +00:00
|
|
|
local messages = {}
|
2014-02-17 01:38:07 +00:00
|
|
|
|
2014-03-17 17:27:25 +00:00
|
|
|
function mp.register_script_message(name, fn)
|
|
|
|
messages[name] = fn
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
|
|
|
|
2014-03-17 17:27:25 +00:00
|
|
|
function mp.unregister_script_message(name)
|
|
|
|
messages[name] = nil
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
|
|
|
|
2014-03-17 17:27:25 +00:00
|
|
|
local function message_dispatch(ev)
|
2014-02-17 01:38:07 +00:00
|
|
|
if #ev.args > 0 then
|
2014-03-17 17:27:25 +00:00
|
|
|
local handler = messages[ev.args[1]]
|
2014-02-20 20:09:27 +00:00
|
|
|
if handler then
|
|
|
|
handler(unpack(ev.args, 2))
|
|
|
|
end
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-08 19:10:00 +00:00
|
|
|
local property_id = 0
|
|
|
|
local properties = {}
|
|
|
|
|
|
|
|
function mp.observe_property(name, t, cb)
|
|
|
|
local id = property_id + 1
|
|
|
|
property_id = id
|
|
|
|
properties[id] = cb
|
|
|
|
mp.raw_observe_property(id, name, t)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mp.unobserve_property(cb)
|
|
|
|
for prop_id, prop_cb in pairs(properties) do
|
|
|
|
if cb == prop_cb then
|
|
|
|
properties[prop_id] = nil
|
2019-10-24 14:21:47 +00:00
|
|
|
mp.raw_unobserve_property(prop_id)
|
2014-04-08 19:10:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function property_change(ev)
|
|
|
|
local prop = properties[ev.id]
|
|
|
|
if prop then
|
|
|
|
prop(ev.name, ev.data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-10 20:03:59 +00:00
|
|
|
-- used by default event loop (mp_event_loop()) to decide when to quit
|
|
|
|
mp.keep_running = true
|
|
|
|
|
|
|
|
local event_handlers = {}
|
|
|
|
|
|
|
|
function mp.register_event(name, cb)
|
2014-02-14 12:48:08 +00:00
|
|
|
local list = event_handlers[name]
|
|
|
|
if not list then
|
|
|
|
list = {}
|
|
|
|
event_handlers[name] = list
|
|
|
|
end
|
|
|
|
list[#list + 1] = cb
|
2014-02-10 23:57:40 +00:00
|
|
|
return mp.request_event(name, true)
|
2014-02-10 20:03:59 +00:00
|
|
|
end
|
|
|
|
|
2014-03-31 22:36:04 +00:00
|
|
|
function mp.unregister_event(cb)
|
|
|
|
for name, sub in pairs(event_handlers) do
|
|
|
|
local found = false
|
2024-05-12 00:47:08 +00:00
|
|
|
for _, e in ipairs(sub) do
|
2014-03-31 22:36:04 +00:00
|
|
|
if e == cb then
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if found then
|
|
|
|
-- create a new array, just in case this function was called
|
|
|
|
-- from an event handler
|
|
|
|
local new = {}
|
|
|
|
for i = 1, #sub do
|
|
|
|
if sub[i] ~= cb then
|
|
|
|
new[#new + 1] = sub[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
event_handlers[name] = new
|
|
|
|
if #new == 0 then
|
|
|
|
mp.request_event(name, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-10 20:03:59 +00:00
|
|
|
-- default handlers
|
|
|
|
mp.register_event("shutdown", function() mp.keep_running = false end)
|
2014-03-17 17:27:25 +00:00
|
|
|
mp.register_event("client-message", message_dispatch)
|
2014-04-08 19:10:00 +00:00
|
|
|
mp.register_event("property-change", property_change)
|
2014-02-10 20:03:59 +00:00
|
|
|
|
2016-09-21 12:36:10 +00:00
|
|
|
-- called before the event loop goes back to sleep
|
|
|
|
local idle_handlers = {}
|
|
|
|
|
|
|
|
function mp.register_idle(cb)
|
|
|
|
idle_handlers[#idle_handlers + 1] = cb
|
|
|
|
end
|
|
|
|
|
2017-01-15 15:22:19 +00:00
|
|
|
function mp.unregister_idle(cb)
|
|
|
|
local new = {}
|
|
|
|
for _, handler in ipairs(idle_handlers) do
|
|
|
|
if handler ~= cb then
|
|
|
|
new[#new + 1] = handler
|
|
|
|
end
|
|
|
|
end
|
|
|
|
idle_handlers = new
|
|
|
|
end
|
|
|
|
|
2016-07-14 18:04:59 +00:00
|
|
|
-- sent by "script-binding"
|
2014-11-23 14:08:49 +00:00
|
|
|
mp.register_script_message("key-binding", dispatch_key_binding)
|
|
|
|
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
mp.msg = {
|
|
|
|
log = mp.log,
|
|
|
|
fatal = function(...) return mp.log("fatal", ...) end,
|
|
|
|
error = function(...) return mp.log("error", ...) end,
|
|
|
|
warn = function(...) return mp.log("warn", ...) end,
|
|
|
|
info = function(...) return mp.log("info", ...) end,
|
2014-01-16 20:37:29 +00:00
|
|
|
verbose = function(...) return mp.log("v", ...) end,
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
debug = function(...) return mp.log("debug", ...) end,
|
2017-10-07 20:40:12 +00:00
|
|
|
trace = function(...) return mp.log("trace", ...) end,
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_G.print = mp.msg.info
|
|
|
|
|
|
|
|
package.loaded["mp"] = mp
|
|
|
|
package.loaded["mp.msg"] = mp.msg
|
|
|
|
|
2020-03-22 18:42:59 +00:00
|
|
|
function mp.wait_event(t)
|
|
|
|
local r = mp.raw_wait_event(t)
|
|
|
|
if r and r.file_error and not r.error then
|
|
|
|
-- compat; deprecated
|
|
|
|
r.error = r.file_error
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2014-02-10 20:03:59 +00:00
|
|
|
_G.mp_event_loop = function()
|
2014-04-12 18:41:12 +00:00
|
|
|
mp.dispatch_events(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function call_event_handlers(e)
|
|
|
|
local handlers = event_handlers[e.event]
|
|
|
|
if handlers then
|
|
|
|
for _, handler in ipairs(handlers) do
|
|
|
|
handler(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-18 21:03:04 +00:00
|
|
|
mp.use_suspend = false
|
2014-08-14 17:30:35 +00:00
|
|
|
|
2016-11-22 13:47:50 +00:00
|
|
|
local suspend_warned = false
|
|
|
|
|
2014-04-12 18:41:12 +00:00
|
|
|
function mp.dispatch_events(allow_wait)
|
2014-02-10 20:07:23 +00:00
|
|
|
local more_events = true
|
2014-08-14 17:30:35 +00:00
|
|
|
if mp.use_suspend then
|
2016-11-22 13:47:50 +00:00
|
|
|
if not suspend_warned then
|
|
|
|
mp.msg.error("mp.use_suspend is now ignored.")
|
|
|
|
suspend_warned = true
|
|
|
|
end
|
2014-08-14 17:30:35 +00:00
|
|
|
end
|
2014-02-10 20:03:59 +00:00
|
|
|
while mp.keep_running do
|
2016-09-21 12:32:58 +00:00
|
|
|
local wait = 0
|
|
|
|
if not more_events then
|
2019-01-06 14:01:43 +00:00
|
|
|
wait = process_timers() or 1e20 -- infinity for all practical purposes
|
lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.
This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.
For instance, this caused process_timers() to never return:
function render() <longer than 1/60 s on a slow system> end
mp.add_periodic_timer(1/60, render)
Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.
This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.
This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).
Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-22 12:18:19 +00:00
|
|
|
if wait ~= 0 then
|
2021-06-22 13:06:36 +00:00
|
|
|
local idle_called = nil
|
lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.
This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.
For instance, this caused process_timers() to never return:
function render() <longer than 1/60 s on a slow system> end
mp.add_periodic_timer(1/60, render)
Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.
This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.
This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).
Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-22 12:18:19 +00:00
|
|
|
for _, handler in ipairs(idle_handlers) do
|
2021-06-22 13:06:36 +00:00
|
|
|
idle_called = true
|
lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.
This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.
For instance, this caused process_timers() to never return:
function render() <longer than 1/60 s on a slow system> end
mp.add_periodic_timer(1/60, render)
Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.
This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.
This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).
Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-22 12:18:19 +00:00
|
|
|
handler()
|
|
|
|
end
|
2021-06-22 13:06:36 +00:00
|
|
|
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
|
2016-09-21 12:32:58 +00:00
|
|
|
end
|
2014-04-12 18:41:12 +00:00
|
|
|
if allow_wait ~= true then
|
|
|
|
return
|
|
|
|
end
|
2014-02-10 20:03:59 +00:00
|
|
|
end
|
|
|
|
local e = mp.wait_event(wait)
|
2016-09-21 12:32:58 +00:00
|
|
|
more_events = false
|
|
|
|
if e.event ~= "none" then
|
2014-04-12 18:41:12 +00:00
|
|
|
call_event_handlers(e)
|
2016-09-21 12:32:58 +00:00
|
|
|
more_events = true
|
2014-02-10 20:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-23 10:17:01 +00:00
|
|
|
mp.register_idle(mp.flush_keybindings)
|
|
|
|
|
2014-04-10 21:56:06 +00:00
|
|
|
-- additional helpers
|
|
|
|
|
|
|
|
function mp.osd_message(text, duration)
|
|
|
|
if not duration then
|
|
|
|
duration = "-1"
|
|
|
|
else
|
|
|
|
duration = tostring(math.floor(duration * 1000))
|
|
|
|
end
|
2015-05-25 19:59:44 +00:00
|
|
|
mp.commandv("show-text", text, duration)
|
2014-04-10 21:56:06 +00:00
|
|
|
end
|
|
|
|
|
2014-10-20 22:15:30 +00:00
|
|
|
local hook_table = {}
|
|
|
|
|
2020-08-05 20:58:19 +00:00
|
|
|
local hook_mt = {}
|
|
|
|
hook_mt.__index = hook_mt
|
|
|
|
|
|
|
|
function hook_mt.cont(t)
|
|
|
|
if t._id == nil then
|
|
|
|
mp.msg.error("hook already continued")
|
|
|
|
else
|
|
|
|
mp.raw_hook_continue(t._id)
|
|
|
|
t._id = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function hook_mt.defer(t)
|
|
|
|
t._defer = true
|
|
|
|
end
|
|
|
|
|
2018-03-23 15:24:49 +00:00
|
|
|
mp.register_event("hook", function(ev)
|
|
|
|
local fn = hook_table[tonumber(ev.id)]
|
2020-08-05 20:58:19 +00:00
|
|
|
local hookobj = {
|
|
|
|
_id = ev.hook_id,
|
|
|
|
_defer = false,
|
|
|
|
}
|
|
|
|
setmetatable(hookobj, hook_mt)
|
2014-10-20 22:15:30 +00:00
|
|
|
if fn then
|
2020-08-05 20:58:19 +00:00
|
|
|
fn(hookobj)
|
|
|
|
end
|
2023-10-31 01:23:21 +00:00
|
|
|
if not hookobj._defer and hookobj._id ~= nil then
|
2020-08-05 20:58:19 +00:00
|
|
|
hookobj:cont()
|
2014-10-20 22:15:30 +00:00
|
|
|
end
|
2018-03-23 15:24:49 +00:00
|
|
|
end)
|
2014-10-20 22:15:30 +00:00
|
|
|
|
|
|
|
function mp.add_hook(name, pri, cb)
|
|
|
|
local id = #hook_table + 1
|
|
|
|
hook_table[id] = cb
|
2018-03-23 15:24:49 +00:00
|
|
|
-- The C API suggests using 0 for a neutral priority, but lua.rst suggests
|
|
|
|
-- 50 (?), so whatever.
|
|
|
|
mp.raw_hook_add(id, name, pri - 50)
|
2014-10-20 22:15:30 +00:00
|
|
|
end
|
|
|
|
|
2018-05-10 13:26:27 +00:00
|
|
|
local async_call_table = {}
|
|
|
|
local async_next_id = 1
|
|
|
|
|
|
|
|
function mp.command_native_async(node, cb)
|
|
|
|
local id = async_next_id
|
|
|
|
async_next_id = async_next_id + 1
|
2022-06-22 11:39:49 +00:00
|
|
|
cb = cb or function() end
|
2018-05-12 16:48:35 +00:00
|
|
|
local res, err = mp.raw_command_native_async(id, node)
|
|
|
|
if not res then
|
2022-06-22 11:36:09 +00:00
|
|
|
mp.add_timeout(0, function() cb(false, nil, err) end)
|
2018-05-12 16:48:35 +00:00
|
|
|
return res, err
|
|
|
|
end
|
|
|
|
local t = {cb = cb, id = id}
|
|
|
|
async_call_table[id] = t
|
|
|
|
return t
|
2018-05-10 13:26:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
mp.register_event("command-reply", function(ev)
|
|
|
|
local id = tonumber(ev.id)
|
2018-05-12 16:48:35 +00:00
|
|
|
local t = async_call_table[id]
|
|
|
|
local cb = t.cb
|
|
|
|
t.id = nil
|
2018-05-10 13:26:27 +00:00
|
|
|
async_call_table[id] = nil
|
|
|
|
if ev.error then
|
|
|
|
cb(false, nil, ev.error)
|
|
|
|
else
|
|
|
|
cb(true, ev.result, nil)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-05-12 16:48:35 +00:00
|
|
|
function mp.abort_async_command(t)
|
|
|
|
if t.id ~= nil then
|
|
|
|
mp.raw_abort_async_command(t.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
local overlay_mt = {}
|
|
|
|
overlay_mt.__index = overlay_mt
|
|
|
|
local overlay_new_id = 0
|
|
|
|
|
|
|
|
function mp.create_osd_overlay(format)
|
|
|
|
overlay_new_id = overlay_new_id + 1
|
|
|
|
local overlay = {
|
|
|
|
format = format,
|
|
|
|
id = overlay_new_id,
|
|
|
|
data = "",
|
|
|
|
res_x = 0,
|
|
|
|
res_y = 720,
|
|
|
|
}
|
|
|
|
setmetatable(overlay, overlay_mt)
|
|
|
|
return overlay
|
|
|
|
end
|
|
|
|
|
|
|
|
function overlay_mt.update(ov)
|
|
|
|
local cmd = {}
|
|
|
|
for k, v in pairs(ov) do
|
|
|
|
cmd[k] = v
|
|
|
|
end
|
|
|
|
cmd.name = "osd-overlay"
|
2019-12-23 12:23:10 +00:00
|
|
|
cmd.res_x = math.floor(cmd.res_x)
|
|
|
|
cmd.res_y = math.floor(cmd.res_y)
|
2020-03-06 17:20:11 +00:00
|
|
|
return mp.command_native(cmd)
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function overlay_mt.remove(ov)
|
|
|
|
mp.command_native {
|
|
|
|
name = "osd-overlay",
|
|
|
|
id = ov.id,
|
|
|
|
format = "none",
|
|
|
|
data = "",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- legacy API
|
|
|
|
function mp.set_osd_ass(res_x, res_y, data)
|
|
|
|
if not mp._legacy_overlay then
|
|
|
|
mp._legacy_overlay = mp.create_osd_overlay("ass-events")
|
|
|
|
end
|
2020-04-30 22:59:33 +00:00
|
|
|
if mp._legacy_overlay.res_x ~= res_x or
|
|
|
|
mp._legacy_overlay.res_y ~= res_y or
|
|
|
|
mp._legacy_overlay.data ~= data
|
|
|
|
then
|
|
|
|
mp._legacy_overlay.res_x = res_x
|
|
|
|
mp._legacy_overlay.res_y = res_y
|
|
|
|
mp._legacy_overlay.data = data
|
|
|
|
mp._legacy_overlay:update()
|
|
|
|
end
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function mp.get_osd_size()
|
2020-01-07 23:16:58 +00:00
|
|
|
local prop = mp.get_property_native("osd-dimensions")
|
|
|
|
return prop.w, prop.h, prop.aspect
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
end
|
|
|
|
|
2020-01-07 23:16:58 +00:00
|
|
|
function mp.get_osd_margins()
|
|
|
|
local prop = mp.get_property_native("osd-dimensions")
|
|
|
|
return prop.ml, prop.mt, prop.mr, prop.mb
|
|
|
|
end
|
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
2019-12-23 10:40:27 +00:00
|
|
|
|
2014-11-28 22:18:50 +00:00
|
|
|
local mp_utils = package.loaded["mp.utils"]
|
|
|
|
|
|
|
|
function mp_utils.format_table(t, set)
|
2014-04-10 22:33:38 +00:00
|
|
|
if not set then
|
|
|
|
set = { [t] = true }
|
|
|
|
end
|
|
|
|
local res = "{"
|
|
|
|
-- pretty expensive but simple way to distinguish array and map parts of t
|
|
|
|
local keys = {}
|
|
|
|
local vals = {}
|
|
|
|
local arr = 0
|
|
|
|
for i = 1, #t do
|
|
|
|
if t[i] == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
keys[i] = i
|
|
|
|
vals[i] = t[i]
|
|
|
|
arr = i
|
|
|
|
end
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if not (type(k) == "number" and k >= 1 and k <= arr and keys[k]) then
|
|
|
|
keys[#keys + 1] = k
|
|
|
|
vals[#keys] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i = 1, #keys do
|
|
|
|
if #res > 1 then
|
|
|
|
res = res .. ", "
|
|
|
|
end
|
|
|
|
if i > arr then
|
2014-11-28 22:18:50 +00:00
|
|
|
res = res .. mp_utils.to_string(keys[i], set) .. " = "
|
2014-04-10 22:33:38 +00:00
|
|
|
end
|
2014-11-28 22:18:50 +00:00
|
|
|
res = res .. mp_utils.to_string(vals[i], set)
|
2014-04-10 22:33:38 +00:00
|
|
|
end
|
|
|
|
res = res .. "}"
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2014-11-28 22:18:50 +00:00
|
|
|
function mp_utils.to_string(v, set)
|
|
|
|
if type(v) == "string" then
|
|
|
|
return "\"" .. v .. "\""
|
|
|
|
elseif type(v) == "table" then
|
|
|
|
if set then
|
|
|
|
if set[v] then
|
|
|
|
return "[cycle]"
|
|
|
|
end
|
|
|
|
set[v] = true
|
|
|
|
end
|
|
|
|
return mp_utils.format_table(v, set)
|
|
|
|
else
|
|
|
|
return tostring(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-24 21:04:27 +00:00
|
|
|
function mp_utils.getcwd()
|
|
|
|
return mp.get_property("working-directory")
|
|
|
|
end
|
|
|
|
|
2021-05-01 12:41:13 +00:00
|
|
|
function mp_utils.getpid()
|
|
|
|
return mp.get_property_number("pid")
|
|
|
|
end
|
|
|
|
|
2017-12-26 16:53:22 +00:00
|
|
|
function mp_utils.format_bytes_humanized(b)
|
|
|
|
local d = {"Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"}
|
|
|
|
local i = 1
|
|
|
|
while b >= 1024 do
|
|
|
|
b = b / 1024
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
return string.format("%0.2f %s", b, d[i] and d[i] or "*1024^" .. (i-1))
|
|
|
|
end
|
|
|
|
|
2018-05-12 13:36:43 +00:00
|
|
|
function mp_utils.subprocess(t)
|
|
|
|
local cmd = {}
|
|
|
|
cmd.name = "subprocess"
|
|
|
|
cmd.capture_stdout = true
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if k == "cancellable" then
|
|
|
|
k = "playback_only"
|
|
|
|
elseif k == "max_size" then
|
|
|
|
k = "capture_size"
|
|
|
|
end
|
|
|
|
cmd[k] = v
|
|
|
|
end
|
|
|
|
local res, err = mp.command_native(cmd)
|
|
|
|
if res == nil then
|
|
|
|
-- an error usually happens only if parsing failed (or no args passed)
|
|
|
|
res = {error_string = err, status = -1}
|
|
|
|
end
|
|
|
|
if res.error_string ~= "" then
|
|
|
|
res.error = res.error_string
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2018-05-12 14:03:04 +00:00
|
|
|
function mp_utils.subprocess_detached(t)
|
|
|
|
mp.commandv("run", unpack(t.args))
|
|
|
|
end
|
|
|
|
|
Add initial Lua scripting support
This is preliminary. There are still tons of issues, and any aspect
of scripting may change in the future. I decided to merge this
(preliminary) work now because it makes it easier to develop it, not
because it's done. lua.rst is clear enough about it (plus some
sarcasm).
This requires linking to Lua. Lua has no official pkg-config file, but
there are distribution specific .pc files, all with different names.
Adding a non-pkg-config based configure test was considered, but we'd
rather not.
One major complication is that libquvi links against Lua too, and if
the Lua version is different from mpv's, you will get a crash as soon
as libquvi uses Lua. (libquvi by design always runs when a file is
opened.) I would consider this the problem of distros and whoever
builds mpv, but to make things easier for users, we add a terrible
runtime test to the configure script, which probes whether libquvi
will crash. This is disabled when cross-compiling, but in that case
we hope the user knows what he is doing.
2013-09-25 22:41:14 +00:00
|
|
|
return {}
|