diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:36:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:36:56 +0000 |
commit | 51de1d8436100f725f3576aefa24a2bd2057bc28 (patch) | |
tree | c6d1d5264b6d40a8d7ca34129f36b7d61e188af3 /player/javascript | |
parent | Initial commit. (diff) | |
download | mpv-51de1d8436100f725f3576aefa24a2bd2057bc28.tar.xz mpv-51de1d8436100f725f3576aefa24a2bd2057bc28.zip |
Adding upstream version 0.37.0.upstream/0.37.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | player/javascript.c | 1262 | ||||
-rw-r--r-- | player/javascript/defaults.js | 782 | ||||
-rw-r--r-- | player/javascript/meson.build | 6 |
3 files changed, 2050 insertions, 0 deletions
diff --git a/player/javascript.c b/player/javascript.c new file mode 100644 index 0000000..5be7277 --- /dev/null +++ b/player/javascript.c @@ -0,0 +1,1262 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <string.h> +#include <strings.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +#include <math.h> +#include <stdint.h> + +#include <mujs.h> + +#include "osdep/io.h" +#include "mpv_talloc.h" +#include "common/common.h" +#include "options/m_property.h" +#include "common/msg.h" +#include "common/msg_control.h" +#include "common/stats.h" +#include "options/m_option.h" +#include "input/input.h" +#include "options/path.h" +#include "misc/bstr.h" +#include "osdep/timer.h" +#include "osdep/threads.h" +#include "stream/stream.h" +#include "sub/osd.h" +#include "core.h" +#include "command.h" +#include "client.h" +#include "libmpv/client.h" + +// List of builtin modules and their contents as strings. +// All these are generated from player/javascript/*.js +static const char *const builtin_files[][3] = { + {"@/defaults.js", +# include "player/javascript/defaults.js.inc" + }, + {0} +}; + +// Represents a loaded script. Each has its own js state. +struct script_ctx { + const char *filename; + const char *path; // NULL if single file + struct mpv_handle *client; + struct MPContext *mpctx; + struct mp_log *log; + char *last_error_str; + size_t js_malloc_size; + struct stats_ctx *stats; +}; + +static struct script_ctx *jctx(js_State *J) +{ + return (struct script_ctx *)js_getcontext(J); +} + +static mpv_handle *jclient(js_State *J) +{ + return jctx(J)->client; +} + +static void pushnode(js_State *J, mpv_node *node); +static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx); +static int jsL_checkint(js_State *J, int idx); +static uint64_t jsL_checkuint64(js_State *J, int idx); + +/********************************************************************** + * conventions, MuJS notes and vm errors + *********************************************************************/ +// - push_foo functions are called from C and push a value to the vm stack. +// +// - JavaScript C functions are code which the vm can call as a js function. +// By convention, script_bar and script__baz are js C functions. The former +// is exposed to end users as bar, and _baz is for internal use. +// +// - js C functions get a fresh vm stack with their arguments, and may +// manipulate their stack as they see fit. On exit, the vm considers the +// top value of their stack as their return value, and GC the rest. +// +// - js C function's stack[0] is "this", and the rest (1, 2, ...) are the args. +// On entry the stack has at least the number of args defined for the func, +// padded with undefined if called with less, or bigger if called with more. +// +// - Almost all vm APIs (js_*) may throw an error - a longjmp to the last +// recovery/catch point, which could skip releasing resources. This includes +// js_try itself(!), except at the outer-most [1] js_try which is always +// entering the try part (and the catch part if the try part throws). +// The assumption should be that anything can throw and needs careful setup. +// One such automated setup is the autofree mechanism. Details later. +// +// - Unless named s_foo, all the functions at this file (inc. init) which +// touch the vm may throw, but either cleanup resources regardless (mostly +// autofree) or leave allocated resources on caller-provided talloc context +// which the caller should release, typically with autofree (e.g. makenode). +// +// - Functions named s_foo (safe foo) never throw if called at the outer-most +// try-levels, or, inside JS C functions - never throw after allocating. +// If they didn't throw then they return 0 on success, 1 on js-errors. +// +// [1] In practice the N outer-most (nested) tries are guaranteed to enter the +// try/carch code, where N is the mujs try-stack size (64 with mujs 1.1.3). +// But because we can't track try-level at (called-back) JS C functions, +// it's only guaranteed when we know we're near the outer-most try level. + +/********************************************************************** + * mpv scripting API error handling + *********************************************************************/ +// - Errors may be thrown on some cases - the reason is at the exception. +// +// - Some APIs also set last error which can be fetched with mp.last_error(), +// where empty string (false-y) is success, or an error string otherwise. +// +// - The rest of the APIs are guaranteed to return undefined on error or a +// true-thy value on success and may or may not set last error. +// +// - push_success, push_failure, push_status and pushed_error set last error. + +// iserr as true indicates an error, and if so, str may indicate a reason. +// Internally ctx->last_error_str is never NULL, and empty indicates success. +static void set_last_error(struct script_ctx *ctx, bool iserr, const char *str) +{ + ctx->last_error_str[0] = 0; + if (!iserr) + return; + if (!str || !str[0]) + str = "Error"; + ctx->last_error_str = talloc_strdup_append(ctx->last_error_str, str); +} + +// For use only by wrappers at defaults.js. +// arg: error string. Use empty string to indicate success. +static void script__set_last_error(js_State *J) +{ + const char *e = js_tostring(J, 1); + set_last_error(jctx(J), e[0], e); +} + +// mp.last_error() . args: none. return the last error without modifying it. +static void script_last_error(js_State *J) +{ + js_pushstring(J, jctx(J)->last_error_str); +} + +// Generic success for APIs which don't return an actual value. +static void push_success(js_State *J) +{ + set_last_error(jctx(J), 0, NULL); + js_pushboolean(J, true); +} + +// Doesn't (intentionally) throw. Just sets last_error and pushes undefined +static void push_failure(js_State *J, const char *str) +{ + set_last_error(jctx(J), 1, str); + js_pushundefined(J); +} + +// Most of the scripting APIs are either sending some values and getting status +// code in return, or requesting some value while providing a default in case an +// error happened. These simplify the C code for that and always set last_error. + +static void push_status(js_State *J, int err) +{ + if (err >= 0) { + push_success(J); + } else { + push_failure(J, mpv_error_string(err)); + } +} + + // If err is success then return 0, else push the item at def and return 1 +static bool pushed_error(js_State *J, int err, int def) +{ + bool iserr = err < 0; + set_last_error(jctx(J), iserr, iserr ? mpv_error_string(err) : NULL); + if (!iserr) + return false; + + js_copy(J, def); + return true; +} + +/********************************************************************** + * Autofree - care-free resource deallocation on vm errors, and otherwise + *********************************************************************/ +// - Autofree (af) functions are called with a talloc context argument which is +// freed after the function exits - either normally or because it threw an +// error, on the latter case it then re-throws the error after the cleanup. +// +// Autofree js C functions should have an additional void* talloc arg and +// inserted into the vm using af_newcfunction, but otherwise used normally. +// +// To wrap an autofree function af_TARGET in C: +// 1. Create a wrapper s_TARGET which does this: +// if (js_try(J)) +// return 1; +// *af = talloc_new(NULL); +// af_TARGET(J, ..., *af); +// js_endtry(J); +// return 0; +// 2. Use s_TARGET like so (frees if allocated, throws if {s,af}_TARGET threw): +// void *af = NULL; +// int r = s_TARGET(J, ..., &af); // use J, af where the callee expects. +// talloc_free(af); +// if (r) +// js_throw(J); +// +// The reason that the allocation happens inside try/catch is that js_try +// itself can throw (if it runs out of try-stack) and therefore the code +// inside the try part is not reached - but neither is the catch part(!), +// and instead it throws to the next outer catch - but before we've allocated +// anything, hence no leaks on such case. If js_try did get entered, then the +// allocation happened, and then if af_TARGET threw then s_TARGET will catch +// it (and return 1) and we'll free if afterwards. + +// add_af_file, add_af_dir, add_af_mpv_alloc take a valid FILE*/DIR*/char* value +// respectively, and fclose/closedir/mpv_free it when the parent is freed. + +static void destruct_af_file(void *p) +{ + fclose(*(FILE**)p); +} + +static void add_af_file(void *parent, FILE *f) +{ + FILE **pf = talloc(parent, FILE*); + *pf = f; + talloc_set_destructor(pf, destruct_af_file); +} + +static void destruct_af_dir(void *p) +{ + closedir(*(DIR**)p); +} + +static void add_af_dir(void *parent, DIR *d) +{ + DIR **pd = talloc(parent, DIR*); + *pd = d; + talloc_set_destructor(pd, destruct_af_dir); +} + +static void destruct_af_mpv_alloc(void *p) +{ + mpv_free(*(char**)p); +} + +static void add_af_mpv_alloc(void *parent, char *ma) +{ + char **p = talloc(parent, char*); + *p = ma; + talloc_set_destructor(p, destruct_af_mpv_alloc); +} + +static void destruct_af_mpv_node(void *p) +{ + mpv_free_node_contents((mpv_node*)p); // does nothing for MPV_FORMAT_NONE +} + +// returns a new zeroed allocated struct mpv_node, and free it and its content +// when the parent is freed. +static mpv_node *new_af_mpv_node(void *parent) +{ + mpv_node *p = talloc_zero(parent, mpv_node); // .format == MPV_FORMAT_NONE + talloc_set_destructor(p, destruct_af_mpv_node); + return p; +} + +// Prototype for autofree functions which can be called from inside the vm. +typedef void (*af_CFunction)(js_State*, void*); + +// safely run autofree js c function directly +static int s_run_af_jsc(js_State *J, af_CFunction fn, void **af) +{ + if (js_try(J)) + return 1; + *af = talloc_new(NULL); + fn(J, *af); + js_endtry(J); + return 0; +} + +// The trampoline function through which all autofree functions are called from +// inside the vm. Obtains the target function address and autofree-call it. +static void script__autofree(js_State *J) +{ + // The target function is at the "af_" property of this function instance. + js_currentfunction(J); + js_getproperty(J, -1, "af_"); + af_CFunction fn = (af_CFunction)js_touserdata(J, -1, "af_fn"); + js_pop(J, 2); + + void *af = NULL; + int r = s_run_af_jsc(J, fn, &af); + talloc_free(af); + if (r) + js_throw(J); +} + +// Identical to js_newcfunction, but the function is inserted with an autofree +// wrapper, and its prototype should have the additional af argument. +static void af_newcfunction(js_State *J, af_CFunction fn, const char *name, + int length) +{ + js_newcfunction(J, script__autofree, name, length); + js_pushnull(J); // a prototype for the userdata object + js_newuserdata(J, "af_fn", fn, NULL); // uses a "af_fn" verification tag + js_defproperty(J, -2, "af_", JS_READONLY | JS_DONTENUM | JS_DONTCONF); +} + +/********************************************************************** + * Initialization and file loading + *********************************************************************/ + +static const char *get_builtin_file(const char *name) +{ + for (int n = 0; builtin_files[n][0]; n++) { + if (strcmp(builtin_files[n][0], name) == 0) + return builtin_files[n][1]; + } + return NULL; +} + +// Push up to limit bytes of file fname: from builtin_files, else from the OS. +static void af_push_file(js_State *J, const char *fname, int limit, void *af) +{ + char *filename = mp_get_user_path(af, jctx(J)->mpctx->global, fname); + MP_VERBOSE(jctx(J), "Reading file '%s'\n", filename); + if (limit < 0) + limit = INT_MAX - 1; + + const char *builtin = get_builtin_file(filename); + if (builtin) { + js_pushlstring(J, builtin, MPMIN(limit, strlen(builtin))); + return; + } + + FILE *f = fopen(filename, "rb"); + if (!f) + js_error(J, "cannot open file: '%s'", filename); + add_af_file(af, f); + + int len = MPMIN(limit, 32 * 1024); // initial allocation, size*2 strategy + int got = 0; + char *s = NULL; + while ((s = talloc_realloc(af, s, char, len))) { + int want = len - got; + int r = fread(s + got, 1, want, f); + + if (feof(f) || (len == limit && r == want)) { + js_pushlstring(J, s, got + r); + return; + } + if (r != want) + js_error(J, "cannot read data from file: '%s'", filename); + + got = got + r; + len = MPMIN(limit, len * 2); + } + + js_error(J, "cannot allocate %d bytes for file: '%s'", len, filename); +} + +// Safely run af_push_file. +static int s_push_file(js_State *J, const char *fname, int limit, void **af) +{ + if (js_try(J)) + return 1; + *af = talloc_new(NULL); + af_push_file(J, fname, limit, *af); + js_endtry(J); + return 0; +} + +// Called directly, push up to limit bytes of file fname (from builtin/os). +static void push_file_content(js_State *J, const char *fname, int limit) +{ + void *af = NULL; + int r = s_push_file(J, fname, limit, &af); + talloc_free(af); + if (r) + js_throw(J); +} + +// utils.read_file(..). args: fname [,max]. returns [up to max] bytes as string. +static void script_read_file(js_State *J) +{ + int limit = js_isundefined(J, 2) ? -1 : jsL_checkint(J, 2); + push_file_content(J, js_tostring(J, 1), limit); +} + +// Runs a file with the caller's this, leaves the stack as is. +static void run_file(js_State *J, const char *fname) +{ + MP_VERBOSE(jctx(J), "Loading file %s\n", fname); + push_file_content(J, fname, -1); + js_loadstring(J, fname, js_tostring(J, -1)); + js_copy(J, 0); // use the caller's this + js_call(J, 0); + js_pop(J, 2); // result, file content +} + +// The spec defines .name and .message for Error objects. Most engines also set +// a very convenient .stack = name + message + trace, but MuJS instead sets +// .stackTrace = trace only. Normalize by adding such .stack if required. +// Run this before anything such that we can get traces on any following errors. +static const char *norm_err_proto_js = "\ + if (Error().stackTrace && !Error().stack) {\ + Object.defineProperty(Error.prototype, 'stack', {\ + get: function() {\ + return this.name + ': ' + this.message + this.stackTrace;\ + }\ + });\ + }\ +"; + +static void add_functions(js_State*, struct script_ctx*); + +// args: none. called as script, setup and run the main script +static void script__run_script(js_State *J) +{ + js_loadstring(J, "@/norm_err.js", norm_err_proto_js); + js_copy(J, 0); + js_pcall(J, 0); + + struct script_ctx *ctx = jctx(J); + add_functions(J, ctx); + run_file(J, "@/defaults.js"); + run_file(J, ctx->filename); // the main file to run + + if (!js_hasproperty(J, 0, "mp_event_loop") || !js_iscallable(J, -1)) + js_error(J, "no event loop function"); + js_copy(J, 0); + js_call(J, 0); // mp_event_loop +} + +// Safely set last error from stack top: stack trace or toString or generic. +// May leave items on stack - the caller should detect and pop if it cares. +static void s_top_to_last_error(struct script_ctx *ctx, js_State *J) +{ + set_last_error(ctx, 1, "unknown error"); + if (js_try(J)) + return; + if (js_isobject(J, -1)) + js_hasproperty(J, -1, "stack"); // fetches it if exists + set_last_error(ctx, 1, js_tostring(J, -1)); + js_endtry(J); +} + +// MuJS can report warnings through this. +static void report_handler(js_State *J, const char *msg) +{ + MP_WARN(jctx(J), "[JS] %s\n", msg); +} + +// Safely setup the js vm for calling run_script. +static int s_init_js(js_State *J, struct script_ctx *ctx) +{ + if (js_try(J)) + return 1; + js_setcontext(J, ctx); + js_setreport(J, report_handler); + js_newcfunction(J, script__run_script, "run_script", 0); + js_pushglobal(J); // 'this' for script__run_script + js_endtry(J); + return 0; +} + +static void *mp_js_alloc(void *actx, void *ptr, int size_) +{ + if (size_ < 0) + return NULL; + + struct script_ctx* ctx = actx; + size_t size = size_, osize = 0; + if (ptr) // free/realloc + osize = ta_get_size(ptr); + + void *ret = talloc_realloc_size(actx, ptr, size); + + if (!size || ret) { // free / successful realloc/malloc + ctx->js_malloc_size = ctx->js_malloc_size - osize + size; + stats_size_value(ctx->stats, "mem", ctx->js_malloc_size); + } + return ret; +} + +/********************************************************************** + * Initialization - booting the script + *********************************************************************/ +// s_load_javascript: (entry point) creates the js vm, runs the script, returns +// on script exit or uncaught js errors. Never throws. +// script__run_script: - loads the built in functions and vars into the vm +// - runs the default file[s] and the main script file +// - calls mp_event_loop, returns on script-exit or throws. +// +// Note: init functions don't need autofree. They can use ctx as a talloc +// context and free normally. If they throw - ctx is freed right afterwards. +static int s_load_javascript(struct mp_script_args *args) +{ + struct script_ctx *ctx = talloc_ptrtype(NULL, ctx); + *ctx = (struct script_ctx) { + .client = args->client, + .mpctx = args->mpctx, + .log = args->log, + .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"), + .filename = args->filename, + .path = args->path, + .js_malloc_size = 0, + .stats = stats_ctx_create(ctx, args->mpctx->global, + mp_tprintf(80, "script/%s", mpv_client_name(args->client))), + }; + + stats_register_thread_cputime(ctx->stats, "cpu"); + + js_Alloc alloc_fn = NULL; + void *actx = NULL; + + if (args->mpctx->opts->js_memory_report) { + alloc_fn = mp_js_alloc; + actx = ctx; + } + + int r = -1; + js_State *J = js_newstate(alloc_fn, actx, 0); + if (!J || s_init_js(J, ctx)) + goto error_out; + + set_last_error(ctx, 0, NULL); + if (js_pcall(J, 0)) { // script__run_script + s_top_to_last_error(ctx, J); + goto error_out; + } + + r = 0; + +error_out: + if (r) + MP_FATAL(ctx, "%s\n", ctx->last_error_str); + if (J) + js_freestate(J); + + talloc_free(ctx); + return r; +} + +/********************************************************************** + * Main mp.* scripting APIs and helpers + *********************************************************************/ +// Return the index in opts of stack[idx] (or of def if undefined), else throws. +static int checkopt(js_State *J, int idx, const char *def, const char *opts[], + const char *desc) +{ + const char *opt = js_isundefined(J, idx) ? def : js_tostring(J, idx); + for (int i = 0; opts[i]; i++) { + if (strcmp(opt, opts[i]) == 0) + return i; + } + js_error(J, "Invalid %s '%s'", desc, opt); +} + +// args: level as string and a variable numbers of args to print. adds final \n +static void script_log(js_State *J) +{ + const char *level = js_tostring(J, 1); + int msgl = mp_msg_find_level(level); + if (msgl < 0) + js_error(J, "Invalid log level '%s'", level); + + struct mp_log *log = jctx(J)->log; + for (int top = js_gettop(J), i = 2; i < top; i++) + mp_msg(log, msgl, (i == 2 ? "%s" : " %s"), js_tostring(J, i)); + mp_msg(log, msgl, "\n"); + push_success(J); +} + +static void script_find_config_file(js_State *J, void *af) +{ + const char *fname = js_tostring(J, 1); + char *path = mp_find_config_file(af, jctx(J)->mpctx->global, fname); + if (path) { + js_pushstring(J, path); + } else { + push_failure(J, "not found"); + } +} + +static void script__request_event(js_State *J) +{ + const char *event = js_tostring(J, 1); + bool enable = js_toboolean(J, 2); + + for (int n = 0; n < 256; n++) { + // some n's may be missing ("holes"), returning NULL + const char *name = mpv_event_name(n); + if (name && strcmp(name, event) == 0) { + push_status(J, mpv_request_event(jclient(J), n, enable)); + return; + } + } + push_failure(J, "Unknown event name"); +} + +static void script_enable_messages(js_State *J) +{ + const char *level = js_tostring(J, 1); + int e = mpv_request_log_messages(jclient(J), level); + if (e == MPV_ERROR_INVALID_PARAMETER) + js_error(J, "Invalid log level '%s'", level); + push_status(J, e); +} + +// args - command [with arguments] as string +static void script_command(js_State *J) +{ + push_status(J, mpv_command_string(jclient(J), js_tostring(J, 1))); +} + +// args: strings of command and then variable number of arguments +static void script_commandv(js_State *J) +{ + const char *argv[MP_CMD_MAX_ARGS + 1]; + int length = js_gettop(J) - 1; + if (length >= MP_ARRAY_SIZE(argv)) + js_error(J, "Too many arguments"); + + for (int i = 0; i < length; i++) + argv[i] = js_tostring(J, 1 + i); + argv[length] = NULL; + push_status(J, mpv_command(jclient(J), argv)); +} + +// args: name, string value +static void script_set_property(js_State *J) +{ + int e = mpv_set_property_string(jclient(J), js_tostring(J, 1), + js_tostring(J, 2)); + push_status(J, e); +} + +// args: name, boolean +static void script_set_property_bool(js_State *J) +{ + int v = js_toboolean(J, 2); + int e = mpv_set_property(jclient(J), js_tostring(J, 1), MPV_FORMAT_FLAG, &v); + push_status(J, e); +} + +// args: name [,def] +static void script_get_property_number(js_State *J) +{ + double result; + const char *name = js_tostring(J, 1); + int e = mpv_get_property(jclient(J), name, MPV_FORMAT_DOUBLE, &result); + if (!pushed_error(J, e, 2)) + js_pushnumber(J, result); +} + +// args: name, native value +static void script_set_property_native(js_State *J, void *af) +{ + mpv_node node; + makenode(af, &node, J, 2); + mpv_handle *h = jclient(J); + int e = mpv_set_property(h, js_tostring(J, 1), MPV_FORMAT_NODE, &node); + push_status(J, e); +} + +// args: name [,def] +static void script_get_property(js_State *J, void *af) +{ + mpv_handle *h = jclient(J); + char *res = NULL; + int e = mpv_get_property(h, js_tostring(J, 1), MPV_FORMAT_STRING, &res); + if (e >= 0) + add_af_mpv_alloc(af, res); + if (!pushed_error(J, e, 2)) + js_pushstring(J, res); +} + +// args: name +static void script_del_property(js_State *J) +{ + int e = mpv_del_property(jclient(J), js_tostring(J, 1)); + push_status(J, e); +} + +// args: name [,def] +static void script_get_property_bool(js_State *J) +{ + int result; + mpv_handle *h = jclient(J); + int e = mpv_get_property(h, js_tostring(J, 1), MPV_FORMAT_FLAG, &result); + if (!pushed_error(J, e, 2)) + js_pushboolean(J, result); +} + +// args: name, number +static void script_set_property_number(js_State *J) +{ + double v = js_tonumber(J, 2); + mpv_handle *h = jclient(J); + int e = mpv_set_property(h, js_tostring(J, 1), MPV_FORMAT_DOUBLE, &v); + push_status(J, e); +} + +// args: name [,def] +static void script_get_property_native(js_State *J, void *af) +{ + const char *name = js_tostring(J, 1); + mpv_handle *h = jclient(J); + mpv_node *presult_node = new_af_mpv_node(af); + int e = mpv_get_property(h, name, MPV_FORMAT_NODE, presult_node); + if (!pushed_error(J, e, 2)) + pushnode(J, presult_node); +} + +// args: name [,def] +static void script_get_property_osd(js_State *J, void *af) +{ + const char *name = js_tostring(J, 1); + mpv_handle *h = jclient(J); + char *res = NULL; + int e = mpv_get_property(h, name, MPV_FORMAT_OSD_STRING, &res); + if (e >= 0) + add_af_mpv_alloc(af, res); + if (!pushed_error(J, e, 2)) + js_pushstring(J, res); +} + +// args: id, name, type +static void script__observe_property(js_State *J) +{ + const char *fmts[] = {"none", "native", "bool", "string", "number", NULL}; + const mpv_format mf[] = {MPV_FORMAT_NONE, MPV_FORMAT_NODE, MPV_FORMAT_FLAG, + MPV_FORMAT_STRING, MPV_FORMAT_DOUBLE}; + + mpv_format f = mf[checkopt(J, 3, "none", fmts, "observe type")]; + int e = mpv_observe_property(jclient(J), jsL_checkuint64(J, 1), + js_tostring(J, 2), + f); + push_status(J, e); +} + +// args: id +static void script__unobserve_property(js_State *J) +{ + int e = mpv_unobserve_property(jclient(J), jsL_checkuint64(J, 1)); + push_status(J, e); +} + +// args: native (array of command and args, similar to commandv) [,def] +static void script_command_native(js_State *J, void *af) +{ + mpv_node cmd; + makenode(af, &cmd, J, 1); + mpv_node *presult_node = new_af_mpv_node(af); + int e = mpv_command_node(jclient(J), &cmd, presult_node); + if (!pushed_error(J, e, 2)) + pushnode(J, presult_node); +} + +// args: async-command-id, native-command +static void script__command_native_async(js_State *J, void *af) +{ + uint64_t id = jsL_checkuint64(J, 1); + struct mpv_node node; + makenode(af, &node, J, 2); + push_status(J, mpv_command_node_async(jclient(J), id, &node)); +} + +// args: async-command-id +static void script__abort_async_command(js_State *J) +{ + mpv_abort_async_command(jclient(J), jsL_checkuint64(J, 1)); + push_success(J); +} + +// args: none, result in millisec +static void script_get_time_ms(js_State *J) +{ + js_pushnumber(J, mpv_get_time_us(jclient(J)) / (double)(1000)); +} + +// push object with properties names (NULL terminated) with respective vals +static void push_nums_obj(js_State *J, const char * const names[], + const double vals[]) +{ + js_newobject(J); + for (int i = 0; names[i]; i++) { + js_pushnumber(J, vals[i]); + js_setproperty(J, -2, names[i]); + } +} + +// args: input-section-name, x0, y0, x1, y1 +static void script_input_set_section_mouse_area(js_State *J) +{ + char *section = (char *)js_tostring(J, 1); + mp_input_set_section_mouse_area(jctx(J)->mpctx->input, section, + jsL_checkint(J, 2), jsL_checkint(J, 3), // x0, y0 + jsL_checkint(J, 4), jsL_checkint(J, 5)); // x1, y1 + push_success(J); +} + +// args: time-in-ms [,format-string] +static void script_format_time(js_State *J, void *af) +{ + double t = js_tonumber(J, 1); + const char *fmt = js_isundefined(J, 2) ? "%H:%M:%S" : js_tostring(J, 2); + char *r = talloc_steal(af, mp_format_time_fmt(fmt, t)); + if (!r) + js_error(J, "Invalid time format string '%s'", fmt); + js_pushstring(J, r); +} + +// TODO: untested +static void script_get_wakeup_pipe(js_State *J) +{ + js_pushnumber(J, mpv_get_wakeup_pipe(jclient(J))); +} + +// args: name (str), priority (int), id (uint) +static void script__hook_add(js_State *J) +{ + const char *name = js_tostring(J, 1); + int pri = jsL_checkint(J, 2); + uint64_t id = jsL_checkuint64(J, 3); + push_status(J, mpv_hook_add(jclient(J), id, name, pri)); +} + +// args: id (uint) +static void script__hook_continue(js_State *J) +{ + push_status(J, mpv_hook_continue(jclient(J), jsL_checkuint64(J, 1))); +} + +/********************************************************************** + * mp.utils + *********************************************************************/ + +// args: [path [,filter]] +static void script_readdir(js_State *J, void *af) +{ + // 0 1 2 3 + const char *filters[] = {"all", "files", "dirs", "normal", NULL}; + const char *path = js_isundefined(J, 1) ? "." : js_tostring(J, 1); + int t = checkopt(J, 2, "normal", filters, "listing filter"); + + DIR *dir = opendir(path); + if (!dir) { + push_failure(J, "Cannot open dir"); + return; + } + add_af_dir(af, dir); + set_last_error(jctx(J), 0, NULL); + js_newarray(J); // the return value + char *fullpath = talloc_strdup(af, ""); + struct dirent *e; + int n = 0; + while ((e = readdir(dir))) { + char *name = e->d_name; + if (t) { + if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) + continue; + if (fullpath) + fullpath[0] = '\0'; + fullpath = talloc_asprintf_append(fullpath, "%s/%s", path, name); + struct stat st; + if (stat(fullpath, &st)) + continue; + if (!(((t & 1) && S_ISREG(st.st_mode)) || + ((t & 2) && S_ISDIR(st.st_mode)))) + { + continue; + } + } + js_pushstring(J, name); + js_setindex(J, -2, n++); + } +} + +static void script_file_info(js_State *J) +{ + const char *path = js_tostring(J, 1); + + struct stat statbuf; + if (stat(path, &statbuf) != 0) { + push_failure(J, "Cannot stat path"); + return; + } + // Clear last error + set_last_error(jctx(J), 0, NULL); + + const char * stat_names[] = { + "mode", "size", + "atime", "mtime", "ctime", NULL + }; + const double stat_values[] = { + statbuf.st_mode, + statbuf.st_size, + statbuf.st_atime, + statbuf.st_mtime, + statbuf.st_ctime + }; + // Create an object and add all fields + push_nums_obj(J, stat_names, stat_values); + + // Convenience booleans + js_pushboolean(J, S_ISREG(statbuf.st_mode)); + js_setproperty(J, -2, "is_file"); + + js_pushboolean(J, S_ISDIR(statbuf.st_mode)); + js_setproperty(J, -2, "is_dir"); +} + + +static void script_split_path(js_State *J) +{ + const char *p = js_tostring(J, 1); + bstr fname = mp_dirname(p); + js_newarray(J); + js_pushlstring(J, fname.start, fname.len); + js_setindex(J, -2, 0); + js_pushstring(J, mp_basename(p)); + js_setindex(J, -2, 1); +} + +static void script_join_path(js_State *J, void *af) +{ + js_pushstring(J, mp_path_join(af, js_tostring(J, 1), js_tostring(J, 2))); +} + +// args: is_append, prefixed file name, data (c-str) +static void script__write_file(js_State *J, void *af) +{ + static const char *prefix = "file://"; + bool append = js_toboolean(J, 1); + const char *fname = js_tostring(J, 2); + const char *data = js_tostring(J, 3); + const char *opstr = append ? "append" : "write"; + + if (strstr(fname, prefix) != fname) // simple protection for incorrect use + js_error(J, "File name must be prefixed with '%s'", prefix); + fname += strlen(prefix); + fname = mp_get_user_path(af, jctx(J)->mpctx->global, fname); + MP_VERBOSE(jctx(J), "%s file '%s'\n", opstr, fname); + + FILE *f = fopen(fname, append ? "ab" : "wb"); + if (!f) + js_error(J, "Cannot open (%s) file: '%s'", opstr, fname); + add_af_file(af, f); + + int len = strlen(data); // limited by terminating null + int wrote = fwrite(data, 1, len, f); + if (len != wrote) + js_error(J, "Cannot %s to file: '%s'", opstr, fname); + js_pushboolean(J, 1); // success. doesn't touch last_error +} + +// args: env var name +static void script_getenv(js_State *J) +{ + const char *v = getenv(js_tostring(J, 1)); + if (v) { + js_pushstring(J, v); + } else { + js_pushundefined(J); + } +} + +// args: none +static void script_get_env_list(js_State *J) +{ + js_newarray(J); + for (int n = 0; environ && environ[n]; n++) { + js_pushstring(J, environ[n]); + js_setindex(J, -2, n); + } +} + +// args: as-filename, content-string, returns the compiled result as a function +static void script_compile_js(js_State *J) +{ + js_loadstring(J, js_tostring(J, 1), js_tostring(J, 2)); +} + +// args: true = print info (with the warning report function - no info report) +static void script__gc(js_State *J) +{ + js_gc(J, js_toboolean(J, 1) ? 1 : 0); + push_success(J); +} + +/********************************************************************** + * Core functions: pushnode, makenode and the event loop backend + *********************************************************************/ + +// pushes a js value/array/object from an mpv_node +static void pushnode(js_State *J, mpv_node *node) +{ + int len; + switch (node->format) { + case MPV_FORMAT_NONE: js_pushnull(J); break; + case MPV_FORMAT_STRING: js_pushstring(J, node->u.string); break; + case MPV_FORMAT_INT64: js_pushnumber(J, node->u.int64); break; + case MPV_FORMAT_DOUBLE: js_pushnumber(J, node->u.double_); break; + case MPV_FORMAT_FLAG: js_pushboolean(J, node->u.flag); break; + case MPV_FORMAT_BYTE_ARRAY: + js_pushlstring(J, node->u.ba->data, node->u.ba->size); + break; + case MPV_FORMAT_NODE_ARRAY: + js_newarray(J); + len = node->u.list->num; + for (int n = 0; n < len; n++) { + pushnode(J, &node->u.list->values[n]); + js_setindex(J, -2, n); + } + break; + case MPV_FORMAT_NODE_MAP: + js_newobject(J); + len = node->u.list->num; + for (int n = 0; n < len; n++) { + pushnode(J, &node->u.list->values[n]); + js_setproperty(J, -2, node->u.list->keys[n]); + } + break; + default: + js_pushstring(J, "[UNSUPPORTED_MPV_FORMAT]"); + break; + } +} + +// For the object at stack index idx, extract the (own) property names into +// keys array (and allocate it to accommodate) and return the number of keys. +static int get_obj_properties(void *ta_ctx, char ***keys, js_State *J, int idx) +{ + int length = 0; + js_pushiterator(J, idx, 1); + + *keys = talloc_new(ta_ctx); + const char *name; + while ((name = js_nextiterator(J, -1))) + MP_TARRAY_APPEND(ta_ctx, *keys, length, talloc_strdup(ta_ctx, name)); + + js_pop(J, 1); // the iterator + return length; +} + +// true if we don't lose (too much) precision when casting to int64 +static bool same_as_int64(double d) +{ + // The range checks also validly filter inf and nan, so behavior is defined + return d >= INT64_MIN && d <= (double) INT64_MAX && d == (int64_t)d; +} + +static int jsL_checkint(js_State *J, int idx) +{ + double d = js_tonumber(J, idx); + if (!(d >= INT_MIN && d <= INT_MAX)) + js_error(J, "int out of range at index %d", idx); + return d; +} + +static uint64_t jsL_checkuint64(js_State *J, int idx) +{ + double d = js_tonumber(J, idx); + if (!(d >= 0 && d <= (double) UINT64_MAX)) + js_error(J, "uint64 out of range at index %d", idx); + return d; +} + +// From the js stack value/array/object at index idx +static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx) +{ + if (js_isundefined(J, idx) || js_isnull(J, idx)) { + dst->format = MPV_FORMAT_NONE; + + } else if (js_isboolean(J, idx)) { + dst->format = MPV_FORMAT_FLAG; + dst->u.flag = js_toboolean(J, idx); + + } else if (js_isnumber(J, idx)) { + double val = js_tonumber(J, idx); + if (same_as_int64(val)) { // use int, because we can + dst->format = MPV_FORMAT_INT64; + dst->u.int64 = val; + } else { + dst->format = MPV_FORMAT_DOUBLE; + dst->u.double_ = val; + } + + } else if (js_isarray(J, idx)) { + dst->format = MPV_FORMAT_NODE_ARRAY; + dst->u.list = talloc(ta_ctx, struct mpv_node_list); + dst->u.list->keys = NULL; + + int length = js_getlength(J, idx); + dst->u.list->num = length; + dst->u.list->values = talloc_array(ta_ctx, mpv_node, length); + for (int n = 0; n < length; n++) { + js_getindex(J, idx, n); + makenode(ta_ctx, &dst->u.list->values[n], J, -1); + js_pop(J, 1); + } + + } else if (js_isobject(J, idx)) { + dst->format = MPV_FORMAT_NODE_MAP; + dst->u.list = talloc(ta_ctx, struct mpv_node_list); + + int length = get_obj_properties(ta_ctx, &dst->u.list->keys, J, idx); + dst->u.list->num = length; + dst->u.list->values = talloc_array(ta_ctx, mpv_node, length); + for (int n = 0; n < length; n++) { + js_getproperty(J, idx, dst->u.list->keys[n]); + makenode(ta_ctx, &dst->u.list->values[n], J, -1); + js_pop(J, 1); + } + + } else { // string, or anything else as string + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(ta_ctx, js_tostring(J, idx)); + } +} + +// args: wait in secs (infinite if negative) if mpv doesn't send events earlier. +static void script_wait_event(js_State *J, void *af) +{ + double timeout = js_isnumber(J, 1) ? js_tonumber(J, 1) : -1; + mpv_event *event = mpv_wait_event(jclient(J), timeout); + + mpv_node *rn = new_af_mpv_node(af); + mpv_event_to_node(rn, event); + pushnode(J, rn); +} + +/********************************************************************** + * Script functions setup + *********************************************************************/ +#define FN_ENTRY(name, length) {#name, length, script_ ## name, NULL} +#define AF_ENTRY(name, length) {#name, length, NULL, script_ ## name} +struct fn_entry { + const char *name; + int length; + js_CFunction jsc_fn; + af_CFunction afc_fn; +}; + +// Names starting with underscore are wrapped at @defaults.js +// FN_ENTRY is a normal js C function, AF_ENTRY is an autofree js C function. +static const struct fn_entry main_fns[] = { + FN_ENTRY(log, 1), + AF_ENTRY(wait_event, 1), + FN_ENTRY(_request_event, 2), + AF_ENTRY(find_config_file, 1), + FN_ENTRY(command, 1), + FN_ENTRY(commandv, 0), + AF_ENTRY(command_native, 2), + AF_ENTRY(_command_native_async, 2), + FN_ENTRY(_abort_async_command, 1), + FN_ENTRY(del_property, 1), + FN_ENTRY(get_property_bool, 2), + FN_ENTRY(get_property_number, 2), + AF_ENTRY(get_property_native, 2), + AF_ENTRY(get_property, 2), + AF_ENTRY(get_property_osd, 2), + FN_ENTRY(set_property, 2), + FN_ENTRY(set_property_bool, 2), + FN_ENTRY(set_property_number, 2), + AF_ENTRY(set_property_native, 2), + FN_ENTRY(_observe_property, 3), + FN_ENTRY(_unobserve_property, 1), + FN_ENTRY(get_time_ms, 0), + AF_ENTRY(format_time, 2), + FN_ENTRY(enable_messages, 1), + FN_ENTRY(get_wakeup_pipe, 0), + FN_ENTRY(_hook_add, 3), + FN_ENTRY(_hook_continue, 1), + FN_ENTRY(input_set_section_mouse_area, 5), + FN_ENTRY(last_error, 0), + FN_ENTRY(_set_last_error, 1), + {0} +}; + +static const struct fn_entry utils_fns[] = { + AF_ENTRY(readdir, 2), + FN_ENTRY(file_info, 1), + FN_ENTRY(split_path, 1), + AF_ENTRY(join_path, 2), + FN_ENTRY(get_env_list, 0), + + FN_ENTRY(read_file, 2), + AF_ENTRY(_write_file, 3), + FN_ENTRY(getenv, 1), + FN_ENTRY(compile_js, 2), + FN_ENTRY(_gc, 1), + {0} +}; + +// Adds an object <module> with the functions at e to the top object +static void add_package_fns(js_State *J, const char *module, + const struct fn_entry *e) +{ + js_newobject(J); + for (int n = 0; e[n].name; n++) { + if (e[n].jsc_fn) { + js_newcfunction(J, e[n].jsc_fn, e[n].name, e[n].length); + } else { + af_newcfunction(J, e[n].afc_fn, e[n].name, e[n].length); + } + js_setproperty(J, -2, e[n].name); + } + js_setproperty(J, -2, module); +} + +// Called directly, adds functions/vars to the caller's this. +static void add_functions(js_State *J, struct script_ctx *ctx) +{ + js_copy(J, 0); + add_package_fns(J, "mp", main_fns); + js_getproperty(J, 0, "mp"); // + this mp + add_package_fns(J, "utils", utils_fns); + + js_pushstring(J, mpv_client_name(ctx->client)); + js_setproperty(J, -2, "script_name"); + + js_pushstring(J, ctx->filename); + js_setproperty(J, -2, "script_file"); + + if (ctx->path) { + js_pushstring(J, ctx->path); + js_setproperty(J, -2, "script_path"); + } + + js_pop(J, 2); // leave the stack as we got it +} + +// main export of this file, used by cplayer to load js scripts +const struct mp_scripting mp_scripting_js = { + .name = "js", + .file_ext = "js", + .load = s_load_javascript, +}; diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js new file mode 100644 index 0000000..d906ec2 --- /dev/null +++ b/player/javascript/defaults.js @@ -0,0 +1,782 @@ +"use strict"; +(function main_default_js(g) { +// - g is the global object. +// - User callbacks called without 'this', global only if callee is non-strict. +// - The names of function expressions are not required, but are used in stack +// traces. We name them where useful to show up (fname:#line always shows). + +mp.msg = { log: mp.log }; +mp.msg.verbose = mp.log.bind(null, "v"); +var levels = ["fatal", "error", "warn", "info", "debug", "trace"]; +levels.forEach(function(l) { mp.msg[l] = mp.log.bind(null, l) }); + +// same as {} but without inherited stuff, e.g. o["toString"] doesn't exist. +// used where we try to fetch items by keys which we don't absolutely trust. +function new_cache() { + return Object.create(null, {}); +} + +/********************************************************************** + * event handlers, property observers, idle, client messages, hooks, async + *********************************************************************/ +var ehandlers = new_cache() // items of event-name: array of {maybe cb: fn} + +mp.register_event = function(name, fn) { + if (!ehandlers[name]) + ehandlers[name] = []; + ehandlers[name] = ehandlers[name].concat([{cb: fn}]); // replaces the arr + return mp._request_event(name, true); +} + +mp.unregister_event = function(fn) { + for (var name in ehandlers) { + ehandlers[name] = ehandlers[name].filter(function(h) { + if (h.cb != fn) + return true; + delete h.cb; // dispatch could have a ref to h + }); // replacing, not mutating the array + if (!ehandlers[name].length) { + delete ehandlers[name]; + mp._request_event(name, false); + } + } +} + +// call only pre-registered handlers, but not ones which got unregistered +function dispatch_event(e) { + var handlers = ehandlers[e.event]; + if (handlers) { + for (var len = handlers.length, i = 0; i < len; i++) { + var cb = handlers[i].cb; // 'handlers' won't mutate, but unregister + if (cb) // could remove cb from some items + cb(e); + } + } +} + +// ----- idle observers ----- +var iobservers = [], // array of callbacks + ideleted = false; + +mp.register_idle = function(fn) { + iobservers.push(fn); +} + +mp.unregister_idle = function(fn) { + iobservers.forEach(function(f, i) { + if (f == fn) + delete iobservers[i]; // -> same length but [more] sparse + }); + ideleted = true; +} + +function notify_idle_observers() { + // forEach and filter skip deleted items and newly added items + iobservers.forEach(function(f) { f() }); + if (ideleted) { + iobservers = iobservers.filter(function() { return true }); + ideleted = false; + } +} + +// ----- property observers ----- +var next_oid = 1, + observers = new_cache(); // items of id: fn + +mp.observe_property = function(name, format, fn) { + var id = next_oid++; + observers[id] = fn; + return mp._observe_property(id, name, format || undefined); // allow null +} + +mp.unobserve_property = function(fn) { + for (var id in observers) { + if (observers[id] == fn) { + delete observers[id]; + mp._unobserve_property(id); + } + } +} + +function notify_observer(e) { + var cb = observers[e.id]; + if (cb) + cb(e.name, e.data); +} + +// ----- Client messages ----- +var messages = new_cache(); // items of name: fn + +// overrides name. no libmpv API to reg/unreg specific messages. +mp.register_script_message = function(name, fn) { + messages[name] = fn; +} + +mp.unregister_script_message = function(name) { + delete messages[name]; +} + +function dispatch_message(ev) { + var cb = ev.args.length ? messages[ev.args[0]] : false; + if (cb) + cb.apply(null, ev.args.slice(1)); +} + +// ----- hooks ----- +var hooks = []; // array of callbacks, id is index+1 + +function run_hook(ev) { + var state = 0; // 0:initial, 1:deferred, 2:continued + function do_cont() { return state = 2, mp._hook_continue(ev.hook_id) } + + function err() { return mp.msg.error("hook already continued"), undefined } + function usr_defer() { return state == 2 ? err() : (state = 1, true) } + function usr_cont() { return state == 2 ? err() : do_cont() } + + var cb = ev.id > 0 && hooks[ev.id - 1]; + if (cb) + cb({ defer: usr_defer, cont: usr_cont }); + return state == 0 ? do_cont() : true; +} + +mp.add_hook = function add_hook(name, pri, fn) { + hooks.push(fn); + // 50 (scripting docs default priority) maps to 0 (default in C API docs) + return mp._hook_add(name, pri - 50, hooks.length); +} + +// ----- async commands ----- +var async_callbacks = new_cache(); // items of id: fn +var async_next_id = 1; + +mp.command_native_async = function command_native_async(node, cb) { + var id = async_next_id++; + cb = cb || function dummy() {}; + if (!mp._command_native_async(id, node)) { + var le = mp.last_error(); + setTimeout(cb, 0, false, undefined, le); /* callback async */ + mp._set_last_error(le); + return undefined; + } + async_callbacks[id] = cb; + return id; +} + +function async_command_handler(ev) { + var cb = async_callbacks[ev.id]; + delete async_callbacks[ev.id]; + if (ev.error) + cb(false, undefined, ev.error); + else + cb(true, ev.result, ""); +} + +mp.abort_async_command = function abort_async_command(id) { + // cb will be invoked regardless, possibly with the abort result + if (async_callbacks[id]) + mp._abort_async_command(id); +} + +// shared-script-properties - always an object, even if without properties +function shared_script_property_set(name, val) { + if (arguments.length > 1) + return mp.commandv("change-list", "shared-script-properties", "append", "" + name + "=" + val); + else + return mp.commandv("change-list", "shared-script-properties", "remove", name); +} + +function shared_script_property_get(name) { + return mp.get_property_native("shared-script-properties")[name]; +} + +function shared_script_property_observe(name, cb) { + return mp.observe_property("shared-script-properties", "native", + function shared_props_cb(_name, val) { cb(name, val[name]) } + ); +} + +mp.utils.shared_script_property_set = shared_script_property_set; +mp.utils.shared_script_property_get = shared_script_property_get; +mp.utils.shared_script_property_observe = shared_script_property_observe; + +// osd-ass +var next_assid = 1; +mp.create_osd_overlay = function create_osd_overlay(format) { + return { + format: format || "ass-events", + id: next_assid++, + data: "", + res_x: 0, + res_y: 720, + z: 0, + + update: function ass_update() { + var cmd = {}; // shallow clone of `this', excluding methods + for (var k in this) { + if (typeof this[k] != "function") + cmd[k] = this[k]; + } + + cmd.name = "osd-overlay"; + cmd.res_x = Math.round(this.res_x); + cmd.res_y = Math.round(this.res_y); + + return mp.command_native(cmd); + }, + + remove: function ass_remove() { + mp.command_native({ + name: "osd-overlay", + id: this.id, + format: "none", + data: "", + }); + return mp.last_error() ? undefined : true; + }, + }; +} + +// osd-ass legacy API +mp.set_osd_ass = function set_osd_ass(res_x, res_y, data) { + if (!mp._legacy_overlay) + mp._legacy_overlay = mp.create_osd_overlay("ass-events"); + + var lo = mp._legacy_overlay; + if (lo.res_x == res_x && lo.res_y == res_y && lo.data == data) + return true; + + mp._legacy_overlay.res_x = res_x; + mp._legacy_overlay.res_y = res_y; + mp._legacy_overlay.data = data; + return mp._legacy_overlay.update(); +} + +// the following return undefined on error, null passthrough, or legacy object +mp.get_osd_size = function get_osd_size() { + var d = mp.get_property_native("osd-dimensions"); + return d && {width: d.w, height: d.h, aspect: d.aspect}; +} +mp.get_osd_margins = function get_osd_margins() { + var d = mp.get_property_native("osd-dimensions"); + return d && {left: d.ml, right: d.mr, top: d.mt, bottom: d.mb}; +} + +/********************************************************************** + * key bindings + *********************************************************************/ +// binds: items of (binding) name which are objects of: +// {cb: fn, forced: bool, maybe input: str, repeatable: bool, complex: bool} +var binds = new_cache(); + +function dispatch_key_binding(name, state, key_name) { + var cb = binds[name] ? binds[name].cb : false; + if (cb) // "script-binding [<script_name>/]<name>" command was invoked + cb(state, key_name); +} + +var binds_tid = 0; // flush timer id. actual id's are always true-thy +mp.flush_key_bindings = function flush_key_bindings() { + function prioritized_inputs(arr) { + return arr.sort(function(a, b) { return a.id - b.id }) + .map(function(bind) { return bind.input }); + } + + var def = [], forced = []; + for (var n in binds) + if (binds[n].input) + (binds[n].forced ? forced : def).push(binds[n]); + // newer bindings for the same key override/hide older ones + def = prioritized_inputs(def); + forced = prioritized_inputs(forced); + + var sect = "input_" + mp.script_name; + mp.commandv("define-section", sect, def.join("\n"), "default"); + mp.commandv("enable-section", sect, "allow-hide-cursor+allow-vo-dragging"); + + sect = "input_forced_" + mp.script_name; + mp.commandv("define-section", sect, forced.join("\n"), "force"); + mp.commandv("enable-section", sect, "allow-hide-cursor+allow-vo-dragging"); + + clearTimeout(binds_tid); // cancel future flush if called directly + binds_tid = 0; +} + +function sched_bindings_flush() { + if (!binds_tid) + binds_tid = setTimeout(mp.flush_key_bindings, 0); // fires on idle +} + +// name/opts maybe omitted. opts: object with optional bool members: repeatable, +// complex, forced, or a string str which is evaluated as object {str: true}. +var next_bid = 1; +function add_binding(forced, key, name, fn, opts) { + if (typeof name == "function") { // as if "name" is not part of the args + opts = fn; + fn = name; + name = false; + } + var key_data = {forced: forced}; + switch (typeof opts) { // merge opts into key_data + case "string": key_data[opts] = true; break; + case "object": for (var o in opts) key_data[o] = opts[o]; + } + key_data.id = next_bid++; + if (!name) + name = "__keybinding" + key_data.id; // new unique binding name + + if (key_data.complex) { + mp.register_script_message(name, function msg_cb() { + fn({event: "press", is_mouse: false}); + }); + var KEY_STATES = { u: "up", d: "down", r: "repeat", p: "press" }; + key_data.cb = function key_cb(state, key_name) { + fn({ + event: KEY_STATES[state[0]] || "unknown", + is_mouse: state[1] == "m", + key_name: key_name || undefined + }); + } + } else { + mp.register_script_message(name, fn); + key_data.cb = function key_cb(state) { + // Emulate the semantics at input.c: mouse emits on up, kb on down. + // Also, key repeat triggers the binding again. + var e = state[0], + emit = (state[1] == "m") ? (e == "u") : (e == "d"); + if (emit || e == "p" || e == "r" && key_data.repeatable) + fn(); + } + } + + if (key) + key_data.input = key + " script-binding " + mp.script_name + "/" + name; + binds[name] = key_data; // used by user and/or our (key) script-binding + sched_bindings_flush(); +} + +mp.add_key_binding = add_binding.bind(null, false); +mp.add_forced_key_binding = add_binding.bind(null, true); + +mp.remove_key_binding = function(name) { + mp.unregister_script_message(name); + delete binds[name]; + sched_bindings_flush(); +} + +/********************************************************************** + Timers: compatible HTML5 WindowTimers - set/clear Timeout/Interval + - Spec: https://www.w3.org/TR/html5/webappapis.html#timers + - Guaranteed to callback a-sync to [re-]insertion (event-loop wise). + - Guaranteed to callback by expiration order, or, if equal, by insertion order. + - Not guaranteed schedule accuracy, though intervals should have good average. + *********************************************************************/ + +// pending 'timers' ordered by expiration: latest at index 0 (top fires first). +// Earlier timers are quicker to handle - just push/pop or fewer items to shift. +var next_tid = 1, + timers = [], // while in process_timers, just insertion-ordered (push) + tset_is_push = false, // signal set_timer that we're in process_timers + tcanceled = false, // or object of items timer-id: true + now = mp.get_time_ms; // just an alias + +function insert_sorted(arr, t) { + for (var i = arr.length - 1; i >= 0 && t.when >= arr[i].when; i--) + arr[i + 1] = arr[i]; // move up timers which fire earlier than t + arr[i + 1] = t; // i is -1 or fires later than t +} + +// args (is "arguments"): fn_or_str [,duration [,user_arg1 [, user_arg2 ...]]] +function set_timer(repeat, args) { + var fos = args[0], + duration = Math.max(0, (args[1] || 0)), // minimum and default are 0 + t = { + id: next_tid++, + when: now() + duration, + interval: repeat ? duration : -1, + callback: (typeof fos == "function") ? fos : Function(fos), + args: (args.length < 3) ? false : [].slice.call(args, 2), + }; + + if (tset_is_push) { + timers.push(t); + } else { + insert_sorted(timers, t); + } + return t.id; +} + +g.setTimeout = function setTimeout() { return set_timer(false, arguments) }; +g.setInterval = function setInterval() { return set_timer(true, arguments) }; + +g.clearTimeout = g.clearInterval = function(id) { + if (id < next_tid) { // must ignore if not active timer id. + if (!tcanceled) + tcanceled = {}; + tcanceled[id] = true; + } +} + +// arr: ordered timers array. ret: -1: no timers, 0: due, positive: ms to wait +function peek_wait(arr) { + return arr.length ? Math.max(0, arr[arr.length - 1].when - now()) : -1; +} + +function peek_timers_wait() { + return peek_wait(timers); // must not be called while in process_timers +} + +// Callback all due non-canceled timers which were inserted before calling us. +// Returns wait in ms till the next timer (possibly 0), or -1 if nothing pends. +function process_timers() { + var wait = peek_wait(timers); + if (wait != 0) + return wait; + + var actives = timers; // only process those already inserted by now + timers = []; // we'll handle added new timers at the end of processing. + tset_is_push = true; // signal set_timer to just push-insert + + do { + var t = actives.pop(); + if (tcanceled && tcanceled[t.id]) + continue; + + if (t.args) { + t.callback.apply(null, t.args); + } else { + (0, t.callback)(); // faster, nicer stack trace than t.cb.call() + } + + if (t.interval >= 0) { + // allow 20 ms delay/clock-resolution/gc before we skip and reset + t.when = Math.max(now() - 20, t.when + t.interval); + timers.push(t); // insertion order only + } + } while (peek_wait(actives) == 0); + + // new 'timers' are insertion-ordered. remains of actives are fully ordered + timers.forEach(function(t) { insert_sorted(actives, t) }); + timers = actives; // now we're fully ordered again, and with all timers + tset_is_push = false; + if (tcanceled) { + timers = timers.filter(function(t) { return !tcanceled[t.id] }); + tcanceled = false; + } + return peek_wait(timers); +} + +/********************************************************************** + CommonJS module/require + + Spec: http://wiki.commonjs.org/wiki/Modules/1.1.1 + - All the mandatory requirements are implemented, all the unit tests pass. + - The implementation makes the following exception: + - Allows the chars [~@:\\] in module id for meta-dir/builtin/dos-drive/UNC. + + Implementation choices beyond the specification: + - A module may assign to module.exports (rather than only to exports). + - A module's 'this' is the global object, also if it sets strict mode. + - No 'global'/'self'. Users can do "this.global = this;" before require(..) + - A module has "privacy of its top scope", runs in its own function context. + - No id identity with symlinks - a valid choice which others make too. + - require("X") always maps to "X.js" -> require("foo.js") is file "foo.js.js". + - Global modules search paths are 'scripts/modules.js/' in mpv config dirs. + - A main script could e.g. require("./abc") to load a non-global module. + - Module id supports mpv path enhancements, e.g. ~/foo, ~~/bar, ~~desktop/baz + *********************************************************************/ + +mp.module_paths = []; // global modules search paths +if (mp.script_path !== undefined) // loaded as a directory + mp.module_paths.push(mp.utils.join_path(mp.script_path, "modules")); + +// Internal meta top-dirs. Users should not rely on these names. +var MODULES_META = "~~modules", + SCRIPTDIR_META = "~~scriptdir", // relative script path -> meta absolute id + main_script = mp.utils.split_path(mp.script_file); // -> [ path, file ] + +function resolve_module_file(id) { + var sep = id.indexOf("/"), + base = id.substring(0, sep), + rest = id.substring(sep + 1) + ".js"; + + if (base == SCRIPTDIR_META) + return mp.utils.join_path(main_script[0], rest); + + if (base == MODULES_META) { + for (var i = 0; i < mp.module_paths.length; i++) { + try { + var f = mp.utils.join_path(mp.module_paths[i], rest); + mp.utils.read_file(f, 1); // throws on any error + return f; + } catch (e) {} + } + throw(Error("Cannot find module file '" + rest + "'")); + } + + return id + ".js"; +} + +// Delimiter '/', remove redundancies, prefix with modules meta-root if needed. +// E.g. c:\x -> c:/x, or ./x//y/../z -> ./x/z, or utils/x -> ~~modules/utils/x . +function canonicalize(id) { + var path = id.replace(/\\/g,"/").split("/"), + t = path[0], + base = []; + + // if not strictly relative then must be top-level. figure out base/rest + if (t != "." && t != "..") { + // global module if it's not fs-root/home/dos-drive/builtin/meta-dir + if (!(t == "" || t == "~" || t[1] == ":" || t == "@" || t.match(/^~~/))) + path.unshift(MODULES_META); // add an explicit modules meta-root + + if (id.match(/^\\\\/)) // simple UNC handling, preserve leading \\srv + path = ["\\\\" + path[2]].concat(path.slice(3)); // [ \\srv, shr..] + + if (t[1] == ":" && t.length > 2) { // path: [ "c:relative", "path" ] + path[0] = t.substring(2); + path.unshift(t[0] + ":."); // -> [ "c:.", "relative", "path" ] + } + base = [path.shift()]; + } + + // path is now logically relative. base, if not empty, is its [meta] root. + // normalize the relative part - always id-based (spec Module Id, 1.3.6). + var cr = []; // canonicalized relative + for (var i = 0; i < path.length; i++) { + if (path[i] == "." || path[i] == "") + continue; + if (path[i] == ".." && cr.length && cr[cr.length - 1] != "..") { + cr.pop(); + continue; + } + cr.push(path[i]); + } + + if (!base.length && cr[0] != "..") + base = ["."]; // relative and not ../<stuff> so must start with ./ + return base.concat(cr).join("/"); +} + +function resolve_module_id(base_id, new_id) { + new_id = canonicalize(new_id); + if (!new_id.match(/^\.\/|^\.\.\//)) // doesn't start with ./ or ../ + return new_id; // not relative, we don't care about base_id + + var combined = mp.utils.join_path(mp.utils.split_path(base_id)[0], new_id); + return canonicalize(combined); +} + +var req_cache = new_cache(); // global for all instances of require + +// ret: a require function instance which uses base_id to resolve relative id's +function new_require(base_id) { + return function require(id) { + id = resolve_module_id(base_id, id); // id is now top-level + if (req_cache[id]) + return req_cache[id].exports; + + var new_module = {id: id, exports: {}}; + req_cache[id] = new_module; + try { + var filename = resolve_module_file(id); + // we need dedicated free vars + filename in traces + allow strict + var str = "mp._req = function(require, exports, module) {" + + mp.utils.read_file(filename) + + "\n;}"; + mp.utils.compile_js(filename, str)(); // only runs the assignment + var tmp = mp._req; // we have mp._req, or else we'd have thrown + delete mp._req; + tmp.call(g, new_require(id), new_module.exports, new_module); + } catch (e) { + delete req_cache[id]; + throw(e); + } + + return new_module.exports; + }; +} + +g.require = new_require(SCRIPTDIR_META + "/" + main_script[1]); + +/********************************************************************** + * mp.options + *********************************************************************/ +function read_options(opts, id, on_update, conf_override) { + id = String(id ? id : mp.get_script_name()); + mp.msg.debug("reading options for " + id); + + var conf, fname = "~~/script-opts/" + id + ".conf"; + try { + conf = arguments.length > 3 ? conf_override : mp.utils.read_file(fname); + } catch (e) { + mp.msg.verbose(fname + " not found."); + } + + // data as config file lines array, or empty array + var data = conf ? conf.replace(/\r\n/g, "\n").split("\n") : [], + conf_len = data.length; // before we append script-opts below + + // Append relevant script-opts as <key-sans-id>=<value> to data + var sopts = mp.get_property_native("options/script-opts"), + prefix = id + "-"; + for (var key in sopts) { + if (key.indexOf(prefix) == 0) + data.push(key.substring(prefix.length) + "=" + sopts[key]); + } + + // Update opts from data + data.forEach(function(line, i) { + if (line[0] == "#" || line.trim() == "") + return; + + var key = line.substring(0, line.indexOf("=")), + val = line.substring(line.indexOf("=") + 1), + type = typeof opts[key], + info = i < conf_len ? fname + ":" + (i + 1) // 1-based line number + : "script-opts:" + prefix + key; + + if (!opts.hasOwnProperty(key)) + mp.msg.warn(info, "Ignoring unknown key '" + key + "'"); + else if (type == "string") + opts[key] = val; + else if (type == "boolean" && (val == "yes" || val == "no")) + opts[key] = (val == "yes"); + else if (type == "number" && val.trim() != "" && !isNaN(val)) + opts[key] = Number(val); + else + mp.msg.error(info, "Error: can't convert '" + val + "' to " + type); + }); + + if (on_update) { + mp.observe_property("options/script-opts", "native", function(_n, _v) { + var saved = JSON.parse(JSON.stringify(opts)); // clone + var changelist = {}, changed = false; + read_options(opts, id, 0, conf); // re-apply orig-file + script-opts + for (var key in opts) { + if (opts[key] != saved[key]) // type always stays the same + changelist[key] = changed = true; + } + if (changed) + on_update(changelist); + }); + } +} + +mp.options = { read_options: read_options }; + +/********************************************************************** + * various + *********************************************************************/ +g.print = mp.msg.info; // convenient alias +mp.get_script_name = function() { return mp.script_name }; +mp.get_script_file = function() { return mp.script_file }; +mp.get_script_directory = function() { return mp.script_path }; +mp.get_time = function() { return mp.get_time_ms() / 1000 }; +mp.utils.getcwd = function() { return mp.get_property("working-directory") }; +mp.utils.getpid = function() { return mp.get_property_number("pid") } +mp.utils.get_user_path = + function(p) { return mp.command_native(["expand-path", String(p)]) }; +mp.get_mouse_pos = function() { return mp.get_property_native("mouse-pos") }; +mp.utils.write_file = mp.utils._write_file.bind(null, false); +mp.utils.append_file = mp.utils._write_file.bind(null, true); +mp.dispatch_event = dispatch_event; +mp.process_timers = process_timers; +mp.notify_idle_observers = notify_idle_observers; +mp.peek_timers_wait = peek_timers_wait; + +mp.get_opt = function(key, def) { + var v = mp.get_property_native("options/script-opts")[key]; + return (typeof v != "undefined") ? v : def; +} + +mp.osd_message = function osd_message(text, duration) { + mp.commandv("show_text", text, Math.round(1000 * (duration || -1))); +} + +mp.utils.subprocess = function subprocess(t) { + var cmd = { name: "subprocess", capture_stdout: true }; + var new_names = { cancellable: "playback_only", max_size: "capture_size" }; + for (var k in t) + cmd[new_names[k] || k] = t[k]; + + var rv = mp.command_native(cmd); + if (mp.last_error()) /* typically on missing/incorrect args */ + rv = { error_string: mp.last_error(), status: -1 }; + if (rv.error_string) + rv.error = rv.error_string; + return rv; +} + +mp.utils.subprocess_detached = function subprocess_detached(t) { + return mp.commandv.apply(null, ["run"].concat(t.args)); +} + + +// ----- dump: like print, but expands objects/arrays recursively ----- +function replacer(k, v) { + var t = typeof v; + if (t == "function" || t == "undefined") + return "<" + t + ">"; + if (Array.isArray(this) && t == "object" && v !== null) { // "safe" mode + if (this.indexOf(v) >= 0) + return "<VISITED>"; + this.push(v); + } + return v; +} + +function obj2str(v) { + try { // can process objects more than once, but throws on cycles + return JSON.stringify(v, replacer.bind(null), 2); + } catch (e) { // simple safe: exclude visited objects, even if not cyclic + return JSON.stringify(v, replacer.bind([]), 2); + } +} + +g.dump = function dump() { + var toprint = []; + for (var i = 0; i < arguments.length; i++) { + var v = arguments[i]; + toprint.push((typeof v == "object") ? obj2str(v) : replacer(0, v)); + } + print.apply(null, toprint); +} + +/********************************************************************** + * main listeners and event loop + *********************************************************************/ +mp.keep_running = true; +g.exit = function() { mp.keep_running = false }; // user-facing too +mp.register_event("shutdown", g.exit); +mp.register_event("property-change", notify_observer); +mp.register_event("hook", run_hook); +mp.register_event("command-reply", async_command_handler); +mp.register_event("client-message", dispatch_message); +mp.register_script_message("key-binding", dispatch_key_binding); + +g.mp_event_loop = function mp_event_loop() { + var wait = 0; // seconds + do { // distapch events as long as they arrive, then do the timers/idle + var e = mp.wait_event(wait); + if (e.event != "none") { + dispatch_event(e); + wait = 0; // poll the next one + } else { + wait = process_timers() / 1000; + if (wait != 0 && iobservers.length) { + notify_idle_observers(); // can add timers -> recalculate wait + wait = peek_timers_wait() / 1000; + } + } + } while (mp.keep_running); +}; + + +// let the user extend us, e.g. by adding items to mp.module_paths +var initjs = mp.find_config_file("init.js"); // ~~/init.js +if (initjs) + require(initjs.slice(0, -3)); // remove ".js" +else if ((initjs = mp.find_config_file(".init.js"))) + mp.msg.warn("Use init.js instead of .init.js (ignoring " + initjs + ")"); + +})(this) diff --git a/player/javascript/meson.build b/player/javascript/meson.build new file mode 100644 index 0000000..bfff4b4 --- /dev/null +++ b/player/javascript/meson.build @@ -0,0 +1,6 @@ +defaults_js = custom_target('defaults.js', + input: join_paths(source_root, 'player', 'javascript', 'defaults.js'), + output: 'defaults.js.inc', + command: [file2string, '@INPUT@', '@OUTPUT@'], +) +sources += defaults_js |