diff options
author | wm4 <wm4@nowhere> | 2014-09-06 17:02:47 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-09-06 17:02:47 +0200 |
commit | 017b3fa9dbbed1bcd5711599e42513daf5009fd0 (patch) | |
tree | 12fcf673bdb3d08038c6d5ad67150238f866d0e4 /player/scripting.c | |
parent | 348dfd93c4178633d3995a955045fafb7e9209d3 (diff) | |
download | mpv-017b3fa9dbbed1bcd5711599e42513daf5009fd0.tar.bz2 mpv-017b3fa9dbbed1bcd5711599e42513daf5009fd0.tar.xz |
lua: synchronously wait until scripts are loaded
This makes the player wait until each script is loaded. Do this to give
the script a chance to setup all its event handlers. It might also be
useful to allow a script to change options that matter for playback.
While waiting for a script to be loaded, the player actually accepts
input. This is needed because the scripts can execute player commands
anyway while they are being "loaded". The player won't react to most
commands though: it can't quit or navigate the playlist in this state.
For deciding whether a script is finally loaded, we use a cheap hack: if
mpv_wait_event() is called, it's considered loaded. Let's hope this is
good enough. I think it's better than introducing explicit API for this.
Although I'm sure this will turn out as too simplistic some time in the
future, the same would probably happen with a more explicit API.
Diffstat (limited to 'player/scripting.c')
-rw-r--r-- | player/scripting.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/player/scripting.c b/player/scripting.c index 3c9829b16d..0b5993c8e5 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -66,6 +66,7 @@ static char *script_name_from_filename(void *talloc_ctx, const char *fname) } struct thread_arg { + struct mp_log *log; const struct mp_scripting *backend; mpv_handle *client; const char *fname; @@ -76,20 +77,25 @@ static void *script_thread(void *p) pthread_detach(pthread_self()); struct thread_arg *arg = p; - struct mp_log *log = mp_client_get_log(arg->client); - - mp_verbose(log, "Loading script...\n"); if (arg->backend->load(arg->client, arg->fname) < 0) - mp_err(log, "Could not load script %s\n", arg->fname); + MP_ERR(arg, "Could not load script %s\n", arg->fname); - mp_verbose(log, "Exiting...\n"); + MP_VERBOSE(arg, "Exiting...\n"); mpv_detach_destroy(arg->client); talloc_free(arg); return NULL; } +static void wait_loaded(struct MPContext *mpctx) +{ + while (!mp_clients_all_initialized(mpctx)) { + mp_wait_events(mpctx, 1e9); + mp_process_input(mpctx); + } +} + static void mp_load_script(struct MPContext *mpctx, const char *fname) { char *ext = mp_splitext(fname, NULL); @@ -121,11 +127,17 @@ static void mp_load_script(struct MPContext *mpctx, const char *fname) talloc_free(arg); return; } + arg->log = mp_client_get_log(arg->client); + + MP_VERBOSE(arg, "Loading script %s...\n", fname); pthread_t thread; if (pthread_create(&thread, NULL, script_thread, arg)) talloc_free(arg); + wait_loaded(mpctx); + MP_VERBOSE(mpctx, "Done loading %s.\n", fname); + return; } |