summaryrefslogtreecommitdiffstats
path: root/player/lua/defaults.lua
diff options
context:
space:
mode:
Diffstat (limited to 'player/lua/defaults.lua')
-rw-r--r--player/lua/defaults.lua47
1 files changed, 42 insertions, 5 deletions
diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua
index 4b5e5e6126..7dca478252 100644
--- a/player/lua/defaults.lua
+++ b/player/lua/defaults.lua
@@ -52,18 +52,31 @@ 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]
+local function script_dispatch(event)
+ local cb = callbacks[event.arg0]
if cb then
- if event == "press" and cb.press then
+ if event.type == "press" and cb.press then
cb.press()
- elseif event == "keyup_follows" and cb.before_press then
+ elseif event.type == "keyup_follows" and cb.before_press then
cb.before_press()
end
end
end
+-- 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)
+ event_handlers[name] = cb
+ mp.request_event(name, true)
+end
+
+-- default handlers
+mp.register_event("shutdown", function() mp.keep_running = false end)
+mp.register_event("script-input-dispatch", script_dispatch)
+
mp.msg = {
log = mp.log,
fatal = function(...) return mp.log("fatal", ...) end,
@@ -79,4 +92,28 @@ _G.print = mp.msg.info
package.loaded["mp"] = mp
package.loaded["mp.msg"] = mp.msg
+_G.mp_event_loop = function()
+ wait = 0
+ while mp.keep_running do
+ -- Drop all locks - important especially if an error happened while
+ -- locked, and the error was handled, but the lock not dropped.
+ if wait > 0 then
+ mp.resume("all")
+ end
+ local e = mp.wait_event(wait)
+ if e.event == "none" then
+ wait = 1e20
+ else
+ -- Empty the event queue while suspended; otherwise, each
+ -- event will keep us waiting until the core suspends again.
+ mp.suspend()
+ wait = 0
+ local handler = event_handlers[e.event]
+ if handler then
+ handler(e)
+ end
+ end
+ end
+end
+
return {}