diff options
Diffstat (limited to 'player/lua')
-rw-r--r-- | player/lua/defaults.lua | 47 | ||||
-rw-r--r-- | player/lua/osc.lua | 11 |
2 files changed, 45 insertions, 13 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 {} diff --git a/player/lua/osc.lua b/player/lua/osc.lua index cf6ce3449e..d19bfd7d78 100644 --- a/player/lua/osc.lua +++ b/player/lua/osc.lua @@ -1257,14 +1257,9 @@ function tick() end end -function mp_event(name, arg) - if name == "tick" then - tick() - elseif name == "start" or name == "track-layout" then - request_init() - elseif name == "end" then - end -end +mp.register_event("tick", tick) +mp.register_event("start-file", request_init) +mp.register_event("tracks-changed", request_init) -- mouse show/hide bindings mp.set_key_bindings({ |