summaryrefslogtreecommitdiffstats
path: root/mpvcore/command.c
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/command.c
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/command.c')
-rw-r--r--mpvcore/command.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/mpvcore/command.c b/mpvcore/command.c
index b30a5299e0..35e72cb3ad 100644
--- a/mpvcore/command.c
+++ b/mpvcore/command.c
@@ -68,6 +68,8 @@
#include "mpvcore/mp_core.h"
+#include "mp_lua.h"
+
static int edit_filters(struct MPContext *mpctx, enum stream_type mediatype,
const char *cmd, const char *arg);
static int set_filters(struct MPContext *mpctx, enum stream_type mediatype,
@@ -1913,6 +1915,11 @@ static const m_option_t mp_properties[] = {
{0},
};
+const struct m_option *mp_get_property_list(void)
+{
+ return mp_properties;
+}
+
int mp_property_do(const char *name, int action, void *val,
struct MPContext *ctx)
{
@@ -2729,6 +2736,15 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
cmd->args[1].v.s, msg_osd);
break;
+ case MP_CMD_SCRIPT_DISPATCH:
+ if (mpctx->lua_ctx) {
+#ifdef CONFIG_LUA
+ mp_lua_script_dispatch(mpctx, cmd->args[0].v.s, cmd->args[1].v.i,
+ cmd->key_up_follows ? "keyup_follows" : "press");
+#endif
+ }
+ break;
+
case MP_CMD_COMMAND_LIST: {
for (struct mp_cmd *sub = cmd->args[0].v.p; sub; sub = sub->queue_next)
run_command(mpctx, sub);
@@ -2755,3 +2771,59 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
break;
}
}
+
+struct command_ctx {
+ int events;
+};
+
+void command_init(struct MPContext *mpctx)
+{
+ mpctx->command_ctx = talloc_zero(mpctx, struct command_ctx);
+}
+
+// Notify that a property might have changed.
+void mp_notify_property(struct MPContext *mpctx, const char *property)
+{
+ mp_notify(mpctx, MP_EVENT_PROPERTY, (void *)property);
+}
+
+void mp_notify(struct MPContext *mpctx, enum mp_event event, void *arg)
+{
+ struct command_ctx *ctx = mpctx->command_ctx;
+ ctx->events |= 1u << event;
+}
+
+static void handle_script_event(struct MPContext *mpctx, const char *name,
+ const char *arg)
+{
+#ifdef CONFIG_LUA
+ mp_lua_event(mpctx, name, arg);
+#endif
+}
+
+void mp_flush_events(struct MPContext *mpctx)
+{
+ struct command_ctx *ctx = mpctx->command_ctx;
+
+ ctx->events |= (1u << MP_EVENT_TICK);
+
+ for (int n = 0; n < 16; n++) {
+ enum mp_event event = n;
+ unsigned mask = 1 << event;
+ if (ctx->events & mask) {
+ // The event handler could set event flags again; in this case let
+ // the next mp_flush_events() call handle it to avoid infinite loops.
+ ctx->events &= ~mask;
+ const char *name = NULL;
+ switch (event) {
+ case MP_EVENT_TICK: name = "tick"; break;
+ case MP_EVENT_TRACKS_CHANGED: name = "track-layout"; break;
+ case MP_EVENT_START_FILE: name = "start"; break;
+ case MP_EVENT_END_FILE: name = "end"; break;
+ default: ;
+ }
+ if (name)
+ handle_script_event(mpctx, name, "");
+ }
+ }
+}