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)
|
|
|
|
local opts = mp.get_property_native("options/lua-opts")
|
|
|
|
local val = opts[key]
|
|
|
|
if val == nil then
|
|
|
|
val = def
|
|
|
|
end
|
|
|
|
return val
|
|
|
|
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
|
|
|
local callbacks = {}
|
|
|
|
-- 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:
|
|
|
|
-- {key, callback}
|
|
|
|
-- {key, callback, callback_down}
|
|
|
|
-- key is the key string as used in input.conf, like "ctrl+a"
|
|
|
|
-- callback is a Lua function that is called when the key binding is used.
|
|
|
|
-- callback_down can be given too, and is called when a mouse button is pressed
|
|
|
|
-- if the key is a mouse button. (The normal callback will be for mouse button
|
|
|
|
-- down.)
|
|
|
|
--
|
|
|
|
-- 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]
|
|
|
|
if type(cb) == "function" then
|
|
|
|
callbacks[#callbacks + 1] = {press=cb, before_press=cb_down}
|
|
|
|
cfg = cfg .. key .. " script_dispatch " .. mp.script_name
|
|
|
|
.. " " .. #callbacks .. "\n"
|
|
|
|
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-10 20:03:59 +00:00
|
|
|
local function script_dispatch(event)
|
|
|
|
local cb = callbacks[event.arg0]
|
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
|
|
|
if cb then
|
2014-02-10 20:03:59 +00:00
|
|
|
if event.type == "press" and cb.press then
|
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
|
|
|
cb.press()
|
2014-02-10 20:03:59 +00:00
|
|
|
elseif event.type == "keyup_follows" and cb.before_press then
|
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
|
|
|
cb.before_press()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-17 01:38:07 +00:00
|
|
|
-- "Newer" and more convenient API
|
|
|
|
|
|
|
|
local key_bindings = {}
|
2014-03-17 17:27:25 +00:00
|
|
|
local message_id = 1
|
2014-02-17 01:38:07 +00:00
|
|
|
|
|
|
|
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
|
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
|
|
|
|
local cfg = ""
|
|
|
|
for k, v in pairs(key_bindings) do
|
|
|
|
if v.forced ~= def then
|
2014-11-20 22:59:47 +00:00
|
|
|
local flags = (v.repeatable and " repeatable") or ""
|
|
|
|
cfg = cfg .. v.key .. " " .. flags .. " script_message_to "
|
|
|
|
.. mp.script_name .. " " .. v.name .. "\n"
|
2014-02-17 01:38:07 +00:00
|
|
|
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
|
|
|
|
|
2014-11-20 22:59:47 +00:00
|
|
|
local function add_binding(attrs, key, name, fn, rp)
|
2014-02-17 01:38:07 +00:00
|
|
|
if (type(name) ~= "string") and (not fn) then
|
|
|
|
fn = name
|
2014-03-17 17:27:25 +00:00
|
|
|
name = "message" .. tostring(message_id)
|
|
|
|
message_id = message_id + 1
|
2014-02-17 01:38:07 +00:00
|
|
|
end
|
2014-11-20 22:59:47 +00:00
|
|
|
attrs.repeatable = rp == "repeatable"
|
2014-02-17 01:38:07 +00:00
|
|
|
attrs.key = key
|
|
|
|
attrs.name = name
|
|
|
|
key_bindings[name] = attrs
|
|
|
|
update_key_bindings()
|
|
|
|
if fn then
|
2014-03-17 17:27:25 +00:00
|
|
|
mp.register_script_message(name, fn)
|
2014-02-17 01:38:07 +00:00
|
|
|
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()
|
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
|
|
|
|
|
2014-02-10 20:07:23 +00:00
|
|
|
function mp.add_timeout(seconds, cb)
|
|
|
|
local t = mp.add_periodic_timer(seconds, cb)
|
|
|
|
t.oneshot = true
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function mp.add_periodic_timer(seconds, cb)
|
|
|
|
local t = {
|
|
|
|
timeout = seconds,
|
|
|
|
cb = cb,
|
|
|
|
oneshot = false,
|
|
|
|
}
|
2014-04-02 15:09:45 +00:00
|
|
|
setmetatable(t, timer_mt)
|
|
|
|
t:resume()
|
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
|
|
|
|
|
|
|
|
-- Return the timer that expires next.
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2014-02-10 20:07:23 +00:00
|
|
|
-- Run timers that have met their deadline.
|
|
|
|
-- Return: next absolute time a timer expires as number, or nil if no timers
|
|
|
|
local function process_timers()
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
for i, e in ipairs(sub) do
|
|
|
|
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)
|
|
|
|
mp.register_event("script-input-dispatch", script_dispatch)
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
_G.print = mp.msg.info
|
|
|
|
|
|
|
|
package.loaded["mp"] = mp
|
|
|
|
package.loaded["mp.msg"] = mp.msg
|
|
|
|
|
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
|
|
|
|
|
2014-08-14 17:30:35 +00:00
|
|
|
mp.use_suspend = true
|
|
|
|
|
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
|
|
|
|
mp.suspend()
|
|
|
|
end
|
2014-02-10 20:03:59 +00:00
|
|
|
while mp.keep_running do
|
2014-02-10 20:07:23 +00:00
|
|
|
local wait = process_timers()
|
|
|
|
if wait == nil then
|
|
|
|
wait = 1e20 -- infinity for all practical purposes
|
|
|
|
end
|
2014-06-07 08:44:21 +00:00
|
|
|
if more_events or wait < 0 then
|
2014-02-10 20:07:23 +00:00
|
|
|
wait = 0
|
|
|
|
end
|
|
|
|
-- Resume playloop - important especially if an error happened while
|
|
|
|
-- suspended, and the error was handled, but no resume was done.
|
2014-02-10 20:03:59 +00:00
|
|
|
if wait > 0 then
|
2014-02-10 23:57:40 +00:00
|
|
|
mp.resume_all()
|
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)
|
2014-02-10 20:07:23 +00:00
|
|
|
-- Empty the event queue while suspended; otherwise, each
|
|
|
|
-- event will keep us waiting until the core suspends again.
|
2014-08-14 17:30:35 +00:00
|
|
|
if mp.use_suspend then
|
|
|
|
mp.suspend()
|
|
|
|
end
|
2014-02-10 20:07:23 +00:00
|
|
|
more_events = (e.event ~= "none")
|
|
|
|
if more_events then
|
2014-04-12 18:41:12 +00:00
|
|
|
call_event_handlers(e)
|
2014-02-10 20:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
mp.commandv("show_text", text, duration)
|
|
|
|
end
|
|
|
|
|
2014-10-20 22:15:30 +00:00
|
|
|
local hook_table = {}
|
|
|
|
local hook_registered = false
|
|
|
|
|
|
|
|
local function hook_run(id, cont)
|
|
|
|
local fn = hook_table[tonumber(id)]
|
|
|
|
if fn then
|
|
|
|
fn()
|
|
|
|
end
|
|
|
|
mp.commandv("hook_ack", cont)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mp.add_hook(name, pri, cb)
|
|
|
|
if not hook_registered then
|
|
|
|
mp.register_script_message("hook_run", hook_run)
|
|
|
|
hook_registered = true
|
|
|
|
end
|
|
|
|
local id = #hook_table + 1
|
|
|
|
hook_table[id] = cb
|
|
|
|
mp.commandv("hook_add", name, id, pri)
|
|
|
|
end
|
|
|
|
|
2014-04-10 22:33:38 +00:00
|
|
|
function mp.format_table(t, set)
|
|
|
|
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
|
|
|
|
local function fmtval(v)
|
|
|
|
if type(v) == "string" then
|
|
|
|
return "\"" .. v .. "\""
|
|
|
|
elseif type(v) == "table" then
|
|
|
|
if set[v] then
|
|
|
|
return "[cycle]"
|
|
|
|
end
|
|
|
|
set[v] = true
|
|
|
|
return mp.format_table(v, set)
|
|
|
|
else
|
|
|
|
return tostring(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i = 1, #keys do
|
|
|
|
if #res > 1 then
|
|
|
|
res = res .. ", "
|
|
|
|
end
|
|
|
|
if i > arr then
|
|
|
|
res = res .. fmtval(keys[i]) .. " = "
|
|
|
|
end
|
|
|
|
res = res .. fmtval(vals[i])
|
|
|
|
end
|
|
|
|
res = res .. "}"
|
|
|
|
return res
|
|
|
|
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 {}
|