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
24 24
25::25::
26 26
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
27 --- mpv 0.32.0 ---31 --- mpv 0.32.0 ---
28 - change behavior when using legacy option syntax with options that start32 - change behavior when using legacy option syntax with options that start
29 with two dashes (``--`` instead of a ``-``). Now, using the recommended33 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
316 return ctx->mpctx->global;316 return ctx->mpctx->global;
317}317}
318 318
319struct MPContext *mp_client_get_core(struct mpv_handle *ctx)
320{
321 return ctx->mpctx;
322}
323
324struct MPContext *mp_client_api_get_core(struct mp_client_api *api)
325{
326 return api->mpctx;
327}
328
329static void wakeup_client(struct mpv_handle *ctx)319static void wakeup_client(struct mpv_handle *ctx)
330{320{
331 pthread_mutex_lock(&ctx->wakeup_lock);321 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
36void mp_client_set_weak(struct mpv_handle *ctx);36void mp_client_set_weak(struct mpv_handle *ctx);
37struct mp_log *mp_client_get_log(struct mpv_handle *ctx);37struct mp_log *mp_client_get_log(struct mpv_handle *ctx);
38struct mpv_global *mp_client_get_global(struct mpv_handle *ctx);38struct mpv_global *mp_client_get_global(struct mpv_handle *ctx);
39struct MPContext *mp_client_get_core(struct mpv_handle *ctx);
40struct MPContext *mp_client_api_get_core(struct mp_client_api *api);
41 39
42void mp_client_broadcast_event_external(struct mp_client_api *api, int event,40void mp_client_broadcast_event_external(struct mp_client_api *api, int event,
43 void *data);41 void *data);
player/core.h+9−1
@@ -619,10 +619,18 @@ void update_screensaver_state(struct MPContext *mpctx); · 618 unmodified lines
619void update_ab_loop_clip(struct MPContext *mpctx);619void update_ab_loop_clip(struct MPContext *mpctx);
620 620
621// scripting.c621// scripting.c
622struct 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};
622struct mp_scripting {630struct mp_scripting {
623 const char *name; // e.g. "lua script"631 const char *name; // e.g. "lua script"
624 const char *file_ext; // e.g. "lua"632 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);
626};634};
627bool mp_load_scripts(struct MPContext *mpctx);635bool mp_load_scripts(struct MPContext *mpctx);
628void mp_load_builtin_scripts(struct MPContext *mpctx);636void 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
468//468//
469// Note: init functions don't need autofree. They can use ctx as a talloc469// Note: init functions don't need autofree. They can use ctx as a talloc
470// context and free normally. If they throw - ctx is freed right afterwards.470// context and free normally. If they throw - ctx is freed right afterwards.
471static int s_load_javascript(struct mpv_handle *client, const char *fname)471static int s_load_javascript(struct mp_script_args *args)
472{472{
473 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);473 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);
474 *ctx = (struct script_ctx) {474 *ctx = (struct script_ctx) {
475 .client = client,475 .client = args->client,
476 .mpctx = mp_client_get_core(client),476 .mpctx = args->mpctx,
477 .log = mp_client_get_log(client),477 .log = args->log,
478 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),478 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),
479 .filename = fname,479 .filename = args->filename,
480 };480 };
481 481
482 int r = -1;482 int r = -1;
player/lua.c+6−7
@@ -337,18 +337,17 @@ static int run_lua(lua_State *L) · 336 unmodified lines
337 return 0;337 return 0;
338}338}
339 339
340static int load_lua(struct mpv_handle *client, const char *fname)340static int load_lua(struct mp_script_args *args)
341{341{
342 struct MPContext *mpctx = mp_client_get_core(client);
343 int r = -1;342 int r = -1;
344 343
345 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);344 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);
346 *ctx = (struct script_ctx) {345 *ctx = (struct script_ctx) {
347 .mpctx = mpctx,346 .mpctx = args->mpctx,
348 .client = client,347 .client = args->client,
349 .name = mpv_client_name(client),348 .name = mpv_client_name(args->client),
350 .log = mp_client_get_log(client),349 .log = args->log,
351 .filename = fname,350 .filename = args->filename,
352 };351 };
353 352
354 if (LUA_VERSION_NUM != 501 && LUA_VERSION_NUM != 502) {353 if (LUA_VERSION_NUM != 501 && LUA_VERSION_NUM != 502) {
player/scripting.c+59−27
@@ -30,6 +30,8 @@ · 29 unmodified lines
30 30
31#include "common/common.h"31#include "common/common.h"
32#include "common/msg.h"32#include "common/msg.h"
33#include "options/m_config.h"
34#include "options/parse_configfile.h"
33#include "options/path.h"35#include "options/path.h"
34#include "misc/bstr.h"36#include "misc/bstr.h"
35#include "core.h"37#include "core.h"
@@ -74,26 +76,19 @@ static char *script_name_from_filename(void *talloc_ctx, const char *fname) · 38 unmodified lines
74 return talloc_asprintf(talloc_ctx, "%s", name);76 return talloc_asprintf(talloc_ctx, "%s", name);
75}77}
76 78
77struct thread_arg {
78 struct mp_log *log;
79 const struct mp_scripting *backend;
80 mpv_handle *client;
81 const char *fname;
82};
83
84static void *script_thread(void *p)79static void *script_thread(void *p)
85{80{
86 pthread_detach(pthread_self());81 pthread_detach(pthread_self());
87 82
88 struct thread_arg *arg = p;83 struct mp_script_args *arg = p;
89 84
90 char name[90];85 char name[90];
91 snprintf(name, sizeof(name), "%s (%s)", arg->backend->name,86 snprintf(name, sizeof(name), "%s (%s)", arg->backend->name,
92 mpv_client_name(arg->client));87 mpv_client_name(arg->client));
93 mpthread_set_name(name);88 mpthread_set_name(name);
94 89
95 if (arg->backend->load(arg->client, arg->fname) < 0)90 if (arg->backend->load(arg) < 0)
96 MP_ERR(arg, "Could not load %s %s\n", arg->backend->name, arg->fname);91 MP_ERR(arg, "Could not load %s %s\n", arg->backend->name, arg->filename);
97 92
98 mpv_destroy(arg->client);93 mpv_destroy(arg->client);
99 talloc_free(arg);94 talloc_free(arg);
@@ -106,34 +101,70 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) · 6 unmodified lines
106 if (ext && strcasecmp(ext, "disable") == 0)101 if (ext && strcasecmp(ext, "disable") == 0)
107 return 0;102 return 0;
108 103
104 void *tmp = talloc_new(NULL);
105
106 const char *path = NULL;
109 const struct mp_scripting *backend = NULL;107 const struct mp_scripting *backend = NULL;
110 for (int n = 0; scripting_backends[n]; n++) {108
111 const struct mp_scripting *b = scripting_backends[n];109 struct stat s;
112 if (ext && strcasecmp(ext, b->file_ext) == 0) {110 if (!stat(fname, &s) && S_ISDIR(s.st_mode)) {
113 backend = b;111 path = fname;
114 break;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 }
115 }139 }
116 }140 }
117 141
118 if (!backend) {142 if (!backend) {
119 MP_ERR(mpctx, "Can't load unknown script: %s\n", fname);143 MP_ERR(mpctx, "Can't load unknown script: %s\n", fname);
144 talloc_free(tmp);
120 return -1;145 return -1;
121 }146 }
122 147
123 struct thread_arg *arg = talloc_ptrtype(NULL, arg);148 struct mp_script_args *arg = talloc_ptrtype(NULL, arg);
124 char *name = script_name_from_filename(arg, fname);149 char *name = script_name_from_filename(arg, fname);
125 *arg = (struct thread_arg){150 *arg = (struct mp_script_args){
126 .fname = talloc_strdup(arg, fname),151 .mpctx = mpctx,
152 .filename = talloc_strdup(arg, fname),
153 .path = talloc_strdup(arg, path),
127 .backend = backend,154 .backend = backend,
128 // Create the client before creating the thread; otherwise a race155 // Create the client before creating the thread; otherwise a race
129 // condition could happen, where MPContext is destroyed while the156 // condition could happen, where MPContext is destroyed while the
130 // thread tries to create the client.157 // thread tries to create the client.
131 .client = mp_new_client(mpctx->clients, name),158 .client = mp_new_client(mpctx->clients, name),
132 };159 };
160
161 talloc_free(tmp);
162
133 if (!arg->client) {163 if (!arg->client) {
134 talloc_free(arg);164 talloc_free(arg);
135 return -1;165 return -1;
136 }166 }
167
137 mp_client_set_weak(arg->client);168 mp_client_set_weak(arg->client);
138 arg->log = mp_client_get_log(arg->client);169 arg->log = mp_client_get_log(arg->client);
139 170
@@ -173,10 +204,12 @@ static char **list_script_files(void *talloc_ctx, char *path) · 33 unmodified lines
173 return NULL;204 return NULL;
174 struct dirent *ep;205 struct dirent *ep;
175 while ((ep = readdir(dp))) {206 while ((ep = readdir(dp))) {
176 char *fname = mp_path_join(talloc_ctx, path, ep->d_name);207 if (ep->d_name[0] != '.') {
177 struct stat s;208 char *fname = mp_path_join(talloc_ctx, path, ep->d_name);
178 if (!stat(fname, &s) && S_ISREG(s.st_mode))209 struct stat s;
179 MP_TARRAY_APPEND(talloc_ctx, files, count, fname);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 }
180 }213 }
181 closedir(dp);214 closedir(dp);
182 if (files)215 if (files)
@@ -247,10 +280,9 @@ bool mp_load_scripts(struct MPContext *mpctx) · 64 unmodified lines
247#define MPV_DLOPEN_FN "mpv_open_cplugin"280#define MPV_DLOPEN_FN "mpv_open_cplugin"
248typedef int (*mpv_open_cplugin)(mpv_handle *handle);281typedef int (*mpv_open_cplugin)(mpv_handle *handle);
249 282
250static int load_cplugin(struct mpv_handle *client, const char *fname)283static int load_cplugin(struct mp_script_args *args)
251{284{
252 MPContext *ctx = mp_client_get_core(client);285 void *lib = dlopen(args->filename, RTLD_NOW | RTLD_LOCAL);
253 void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
254 if (!lib)286 if (!lib)
255 goto error;287 goto error;
256 // Note: once loaded, we never unload, as unloading the libraries linked to288 // 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
258 mpv_open_cplugin sym = (mpv_open_cplugin)dlsym(lib, MPV_DLOPEN_FN);290 mpv_open_cplugin sym = (mpv_open_cplugin)dlsym(lib, MPV_DLOPEN_FN);
259 if (!sym)291 if (!sym)
260 goto error;292 goto error;
261 return sym(client) ? -1 : 0;293 return sym(args->client) ? -1 : 0;
262error: ;294error: ;
263 char *err = dlerror();295 char *err = dlerror();
264 if (err)296 if (err)
265 MP_ERR(ctx, "C plugin error: '%s'\n", err);297 MP_ERR(args, "C plugin error: '%s'\n", err);
266 return -1;298 return -1;
267}299}
268 300

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