summaryrefslogtreecommitdiffstats
path: root/mpvcore/lua
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-09-26 00:41:14 +0200
committerwm4 <wm4@nowhere>2013-09-26 01:28:58 +0200
commit6048f87e3c07d03df8a59da58c81608c5375f6dc (patch)
treebe79bfe51b5268f2c1494f456fa72538c391d54f /mpvcore/lua
parentb0cc3c2cf4e342e33300adaea4a98565ad866e22 (diff)
downloadmpv-6048f87e3c07d03df8a59da58c81608c5375f6dc.tar.bz2
mpv-6048f87e3c07d03df8a59da58c81608c5375f6dc.tar.xz
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.
Diffstat (limited to 'mpvcore/lua')
-rw-r--r--mpvcore/lua/assdraw.lua98
-rw-r--r--mpvcore/lua/defaults.lua82
2 files changed, 180 insertions, 0 deletions
diff --git a/mpvcore/lua/assdraw.lua b/mpvcore/lua/assdraw.lua
new file mode 100644
index 0000000000..fc3b727f57
--- /dev/null
+++ b/mpvcore/lua/assdraw.lua
@@ -0,0 +1,98 @@
+local ass_mt = {}
+ass_mt.__index = ass_mt
+
+local function ass_new()
+ return setmetatable({ scale = 4, text = "" }, ass_mt)
+end
+
+function ass_mt.new_event(ass)
+ -- osd_libass.c adds an event per line
+ if #ass.text > 0 then
+ ass.text = ass.text .. "\n"
+ end
+end
+
+function ass_mt.draw_start(ass)
+ ass.text = string.format("%s{\\p%d}", ass.text, ass.scale)
+end
+
+function ass_mt.draw_stop(ass)
+ ass.text = ass.text .. "{\\p0}"
+end
+
+function ass_mt.coord(ass, x, y)
+ local scale = math.pow(2, ass.scale - 1)
+ local ix = math.ceil(x * scale)
+ local iy = math.ceil(y * scale)
+ ass.text = string.format("%s %d %d", ass.text, ix, iy)
+end
+
+function ass_mt.append(ass, s)
+ ass.text = ass.text .. s
+end
+
+function ass_mt.merge(ass1, ass2)
+ ass1.text = ass1.text .. ass2.text
+end
+
+function ass_mt.pos(ass, x, y)
+ ass:append(string.format("{\\pos(%f,%f)}", x, y))
+end
+
+function ass_mt.an(ass, an)
+ ass:append(string.format("{\\an%d}", an))
+end
+
+function ass_mt.move_to(ass, x, y)
+ ass:append(" m")
+ ass:coord(x, y)
+end
+
+function ass_mt.line_to(ass, x, y)
+ ass:append(" l")
+ ass:coord(x, y)
+end
+
+function ass_mt.bezier_curve(ass, x1, y1, x2, y2, x3, y3)
+ ass:append(" b")
+ ass:coord(x1, y1)
+ ass:coord(x2, y2)
+ ass:coord(x3, y3)
+end
+
+
+function ass_mt.rect_ccw(ass, x0, y0, x1, y1)
+ ass:move_to(x0, y0)
+ ass:line_to(x0, y1)
+ ass:line_to(x1, y1)
+ ass:line_to(x1, y0)
+end
+
+function ass_mt.rect_cw(ass, x0, y0, x1, y1)
+ ass:move_to(x0, y0)
+ ass:line_to(x1, y0)
+ ass:line_to(x1, y1)
+ ass:line_to(x0, y1)
+end
+
+function ass_mt.round_rect_cw(ass, x0, y0, x1, y1, r)
+ ass:move_to(x0 + r, y0)
+ ass:line_to(x1 - r, y0) -- top line
+ if r > 0 then
+ ass:bezier_curve(x1, y0, x1, y0, x1, y0 + r) -- top right corner
+ end
+ ass:line_to(x1, y1 - r) -- right line
+ if r > 0 then
+ ass:bezier_curve(x1, y1, x1, y1, x1 - r, y1) -- bottom right corner
+ end
+ ass:line_to(x0 + r, y1) -- bottom line
+ if r > 0 then
+ ass:bezier_curve(x0, y1, x0, y1, x0, y1 - r) -- bottom left corner
+ end
+ ass:line_to(x0, y0 + r) -- left line
+ if r > 0 then
+ ass:bezier_curve(x0, y0, x0, y0, x0 + r, y0) -- top left corner
+ end
+end
+
+return {ass_new = ass_new}
diff --git a/mpvcore/lua/defaults.lua b/mpvcore/lua/defaults.lua
new file mode 100644
index 0000000000..d24cda9cbe
--- /dev/null
+++ b/mpvcore/lua/defaults.lua
@@ -0,0 +1,82 @@
+
+local callbacks = {}
+-- each script has its own section, so that they don't conflict
+local default_section = "input_" .. mp.script_name
+
+-- 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.)
+function mp.set_key_bindings(list, section)
+ 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
+ mp.input_define_section(section or default_section, cfg)
+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
+
+-- called by C on script_dispatch input command
+function mp_script_dispatch(id, event)
+ local cb = callbacks[id]
+ if cb then
+ if event == "press" and cb.press then
+ cb.press()
+ elseif event == "keyup_follows" and cb.before_press then
+ cb.before_press()
+ end
+ end
+end
+
+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,
+ verbose = function(...) return mp.log("verbose", ...) end,
+ debug = function(...) return mp.log("debug", ...) end,
+}
+
+_G.print = mp.msg.info
+
+package.loaded["mp"] = mp
+package.loaded["mp.msg"] = mp.msg
+
+return {}