History

scripting: load scripts from directories

The intention is to provide a slightly nicer way to distribute scripts. For example, you could put multiple source files into the directory, and then import them from the actual script file (this is still unimplemented). At first I wanted to require a config file (because you need to know at least which scripting backend it should use). This wouldn't have been too hard (could have reused/abused the mpv config file parsing mechanism, and I already had working code that was just 2 function calls). But probably better to do this without new config files, because it might become a pain in the distant future. So this just probes for "main.lua", "main.js", etc., until an existing file is found. Another important change is that this skips all directory entries whose name starts with ".". This automatically excludes the "." and ".." special directories, and is probably useful to exclude random crap that might be lying around in the directory (such as editor temporary files, or OSX, in its usual hrmful, annoying, and idiotic modus operandi, sharting all over any directories opened by "Finder"). Although the changelog mentions the docs, they're added only in a later commit.

wm4 committed 6 years ago

Commit da38caf1 parent 66a979b7 files · +83 −52Browse files

Unified · Split

7 files
  1. DOCS/interface-changes.rst +4 −0
  2. player/client.c +0 −10
  3. player/client.h +0 −2
  4. player/core.h +9 −1
  5. player/javascript.c +5 −5
  6. player/lua.c +6 −7
  7. player/scripting.c +59 −27
DOCS/interface-changes.rst+4−0
@@ -24,6 +24,10 @@ Interface changes · 23 unmodified lines
2424
2525 ::
2626
27+ --- mpv 0.33.0 ---
28+ - directories in ~/.mpv/scripts/ (or equivalent) now have special semantics
29+ (see mpv Lua scripting docs)
30+ - names starting with "." in ~/.mpv/scripts/ (or equivalent) are now ignored
2731 --- mpv 0.32.0 ---
2832 - change behavior when using legacy option syntax with options that start
2933 with two dashes (``--`` instead of a ``-``). Now, using the recommended
player/client.c+0−10
@@ -316,16 +316,6 @@ struct mpv_global *mp_client_get_global(struct mpv_handle *ctx) · 315 unmodified lines
316316 return ctx->mpctx->global;
317317 }
318318
319−struct MPContext *mp_client_get_core(struct mpv_handle *ctx)
320−{
321− return ctx->mpctx;
322−}
323−
324−struct MPContext *mp_client_api_get_core(struct mp_client_api *api)
325−{
326− return api->mpctx;
327−}
328−
329319 static void wakeup_client(struct mpv_handle *ctx)
330320 {
331321 pthread_mutex_lock(&ctx->wakeup_lock);
player/client.h+0−2
@@ -36,8 +36,6 @@ struct mpv_handle *mp_new_client(struct mp_client_api *clients, const char *name · 35 unmodified lines
3636 void mp_client_set_weak(struct mpv_handle *ctx);
3737 struct mp_log *mp_client_get_log(struct mpv_handle *ctx);
3838 struct mpv_global *mp_client_get_global(struct mpv_handle *ctx);
39−struct MPContext *mp_client_get_core(struct mpv_handle *ctx);
40−struct MPContext *mp_client_api_get_core(struct mp_client_api *api);
4139
4240 void mp_client_broadcast_event_external(struct mp_client_api *api, int event,
4341 void *data);
player/core.h+9−1
@@ -619,10 +619,18 @@ void update_screensaver_state(struct MPContext *mpctx); · 618 unmodified lines
619619 void update_ab_loop_clip(struct MPContext *mpctx);
620620
621621 // scripting.c
622+struct mp_script_args {
623+ const struct mp_scripting *backend;
624+ struct MPContext *mpctx;
625+ struct mp_log *log;
626+ struct mpv_handle *client;
627+ const char *filename;
628+ const char *path;
629+};
622630 struct mp_scripting {
623631 const char *name; // e.g. "lua script"
624632 const char *file_ext; // e.g. "lua"
625− int (*load)(struct mpv_handle *client, const char *filename);
633+ int (*load)(struct mp_script_args *args);
626634 };
627635 bool mp_load_scripts(struct MPContext *mpctx);
628636 void mp_load_builtin_scripts(struct MPContext *mpctx);
player/javascript.c+5−5
@@ -468,15 +468,15 @@ static int s_init_js(js_State *J, struct script_ctx *ctx) · 467 unmodified lines
468468 //
469469 // Note: init functions don't need autofree. They can use ctx as a talloc
470470 // context and free normally. If they throw - ctx is freed right afterwards.
471−static int s_load_javascript(struct mpv_handle *client, const char *fname)
471+static int s_load_javascript(struct mp_script_args *args)
472472 {
473473 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);
474474 *ctx = (struct script_ctx) {
475− .client = client,
476− .mpctx = mp_client_get_core(client),
477− .log = mp_client_get_log(client),
475+ .client = args->client,
476+ .mpctx = args->mpctx,
477+ .log = args->log,
478478 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),
479− .filename = fname,
479+ .filename = args->filename,
480480 };
481481
482482 int r = -1;
player/lua.c+6−7
@@ -337,18 +337,17 @@ static int run_lua(lua_State *L) · 336 unmodified lines
337337 return 0;
338338 }
339339
340−static int load_lua(struct mpv_handle *client, const char *fname)
340+static int load_lua(struct mp_script_args *args)
341341 {
342− struct MPContext *mpctx = mp_client_get_core(client);
343342 int r = -1;
344343
345344 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);
346345 *ctx = (struct script_ctx) {
347− .mpctx = mpctx,
348− .client = client,
349− .name = mpv_client_name(client),
350− .log = mp_client_get_log(client),
351− .filename = fname,
346+ .mpctx = args->mpctx,
347+ .client = args->client,
348+ .name = mpv_client_name(args->client),
349+ .log = args->log,
350+ .filename = args->filename,
352351 };
353352
354353 if (LUA_VERSION_NUM != 501 && LUA_VERSION_NUM != 502) {
player/scripting.c+59−27
@@ -30,6 +30,8 @@ · 29 unmodified lines
3030
3131 #include "common/common.h"
3232 #include "common/msg.h"
33+#include "options/m_config.h"
34+#include "options/parse_configfile.h"
3335 #include "options/path.h"
3436 #include "misc/bstr.h"
3537 #include "core.h"
@@ -74,26 +76,19 @@ static char *script_name_from_filename(void *talloc_ctx, const char *fname) · 38 unmodified lines
7476 return talloc_asprintf(talloc_ctx, "%s", name);
7577 }
7678
77−struct thread_arg {
78− struct mp_log *log;
79− const struct mp_scripting *backend;
80− mpv_handle *client;
81− const char *fname;
82−};
83−
8479 static void *script_thread(void *p)
8580 {
8681 pthread_detach(pthread_self());
8782
88− struct thread_arg *arg = p;
83+ struct mp_script_args *arg = p;
8984
9085 char name[90];
9186 snprintf(name, sizeof(name), "%s (%s)", arg->backend->name,
9287 mpv_client_name(arg->client));
9388 mpthread_set_name(name);
9489
95− if (arg->backend->load(arg->client, arg->fname) < 0)
96− MP_ERR(arg, "Could not load %s %s\n", arg->backend->name, arg->fname);
90+ if (arg->backend->load(arg) < 0)
91+ MP_ERR(arg, "Could not load %s %s\n", arg->backend->name, arg->filename);
9792
9893 mpv_destroy(arg->client);
9994 talloc_free(arg);
@@ -106,34 +101,70 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) · 6 unmodified lines
106101 if (ext && strcasecmp(ext, "disable") == 0)
107102 return 0;
108103
104+ void *tmp = talloc_new(NULL);
105+
106+ const char *path = NULL;
109107 const struct mp_scripting *backend = NULL;
110− for (int n = 0; scripting_backends[n]; n++) {
111− const struct mp_scripting *b = scripting_backends[n];
112− if (ext && strcasecmp(ext, b->file_ext) == 0) {
113− backend = b;
114− break;
108+
109+ struct stat s;
110+ if (!stat(fname, &s) && S_ISDIR(s.st_mode)) {
111+ path = fname;
112+ fname = NULL;
113+
114+ for (int n = 0; scripting_backends[n]; n++) {
115+ const struct mp_scripting *b = scripting_backends[n];
116+ char *filename = mp_tprintf(80, "main.%s", b->file_ext);
117+ fname = mp_path_join(tmp, path, filename);
118+ if (!stat(fname, &s) && S_ISREG(s.st_mode)) {
119+ backend = b;
120+ break;
121+ }
122+ talloc_free((void *)fname);
123+ fname = NULL;
124+ }
125+
126+ if (!fname) {
127+ MP_ERR(mpctx, "Cannot find main.* for any supported scripting "
128+ "backend in: %s\n", path);
129+ talloc_free(tmp);
130+ return -1;
131+ }
132+ } else {
133+ for (int n = 0; scripting_backends[n]; n++) {
134+ const struct mp_scripting *b = scripting_backends[n];
135+ if (ext && strcasecmp(ext, b->file_ext) == 0) {
136+ backend = b;
137+ break;
138+ }
115139 }
116140 }
117141
118142 if (!backend) {
119143 MP_ERR(mpctx, "Can't load unknown script: %s\n", fname);
144+ talloc_free(tmp);
120145 return -1;
121146 }
122147
123− struct thread_arg *arg = talloc_ptrtype(NULL, arg);
148+ struct mp_script_args *arg = talloc_ptrtype(NULL, arg);
124149 char *name = script_name_from_filename(arg, fname);
125− *arg = (struct thread_arg){
126− .fname = talloc_strdup(arg, fname),
150+ *arg = (struct mp_script_args){
151+ .mpctx = mpctx,
152+ .filename = talloc_strdup(arg, fname),
153+ .path = talloc_strdup(arg, path),
127154 .backend = backend,
128155 // Create the client before creating the thread; otherwise a race
129156 // condition could happen, where MPContext is destroyed while the
130157 // thread tries to create the client.
131158 .client = mp_new_client(mpctx->clients, name),
132159 };
160+
161+ talloc_free(tmp);
162+
133163 if (!arg->client) {
134164 talloc_free(arg);
135165 return -1;
136166 }
167+
137168 mp_client_set_weak(arg->client);
138169 arg->log = mp_client_get_log(arg->client);
139170
@@ -173,10 +204,12 @@ static char **list_script_files(void *talloc_ctx, char *path) · 33 unmodified lines
173204 return NULL;
174205 struct dirent *ep;
175206 while ((ep = readdir(dp))) {
176− char *fname = mp_path_join(talloc_ctx, path, ep->d_name);
177− struct stat s;
178− if (!stat(fname, &s) && S_ISREG(s.st_mode))
179− MP_TARRAY_APPEND(talloc_ctx, files, count, fname);
207+ if (ep->d_name[0] != '.') {
208+ char *fname = mp_path_join(talloc_ctx, path, ep->d_name);
209+ struct stat s;
210+ if (!stat(fname, &s) && (S_ISREG(s.st_mode) || S_ISDIR(s.st_mode)))
211+ MP_TARRAY_APPEND(talloc_ctx, files, count, fname);
212+ }
180213 }
181214 closedir(dp);
182215 if (files)
@@ -247,10 +280,9 @@ bool mp_load_scripts(struct MPContext *mpctx) · 64 unmodified lines
247280 #define MPV_DLOPEN_FN "mpv_open_cplugin"
248281 typedef int (*mpv_open_cplugin)(mpv_handle *handle);
249282
250−static int load_cplugin(struct mpv_handle *client, const char *fname)
283+static int load_cplugin(struct mp_script_args *args)
251284 {
252− MPContext *ctx = mp_client_get_core(client);
253− void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
285+ void *lib = dlopen(args->filename, RTLD_NOW | RTLD_LOCAL);
254286 if (!lib)
255287 goto error;
256288 // Note: once loaded, we never unload, as unloading the libraries linked to
@@ -258,11 +290,11 @@ static int load_cplugin(struct mpv_handle *client, const char *fname) · 1 unmodified line
258290 mpv_open_cplugin sym = (mpv_open_cplugin)dlsym(lib, MPV_DLOPEN_FN);
259291 if (!sym)
260292 goto error;
261− return sym(client) ? -1 : 0;
293+ return sym(args->client) ? -1 : 0;
262294 error: ;
263295 char *err = dlerror();
264296 if (err)
265− MP_ERR(ctx, "C plugin error: '%s'\n", err);
297+ MP_ERR(args, "C plugin error: '%s'\n", err);
266298 return -1;
267299 }
268300

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t