diff options
Diffstat (limited to 'player/javascript/defaults.js')
-rw-r--r-- | player/javascript/defaults.js | 120 |
1 files changed, 84 insertions, 36 deletions
diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 2435390b87..9f130c9c92 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -177,28 +177,6 @@ mp.abort_async_command = function abort_async_command(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) { @@ -268,17 +246,26 @@ mp.get_osd_margins = function get_osd_margins() { // {cb: fn, forced: bool, maybe input: str, repeatable: bool, complex: bool} var binds = new_cache(); -function dispatch_key_binding(name, state, key_name) { +function dispatch_key_binding(name, state, key_name, key_text) { var cb = binds[name] ? binds[name].cb : false; if (cb) // "script-binding [<script_name>/]<name>" command was invoked - cb(state, key_name); + cb(state, key_name, key_text); } 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) // Array.join() will later skip undefined .input - (binds[n].forced ? forced : def).push(binds[n].input); + 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"); @@ -306,24 +293,26 @@ function add_binding(forced, key, name, fn, opts) { fn = name; name = false; } - if (!name) - name = "__keybinding" + next_bid++; // new unique binding name 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) { + key_data.cb = function key_cb(state, key_name, key_text) { fn({ event: KEY_STATES[state[0]] || "unknown", is_mouse: state[1] == "m", - key_name: key_name || undefined + key_name: key_name || undefined, + key_text: key_text || undefined }); } } else { @@ -655,6 +644,56 @@ function read_options(opts, id, on_update, conf_override) { mp.options = { read_options: read_options }; /********************************************************************** +* input +*********************************************************************/ +mp.input = { + get: function(t) { + mp.commandv("script-message-to", "console", "get-input", mp.script_name, + JSON.stringify({ + prompt: t.prompt, + default_text: t.default_text, + cursor_position: t.cursor_position, + id: t.id, + })); + + mp.register_script_message("input-event", function (type, text, cursor_position) { + if (t[type]) { + var result = t[type](text, cursor_position); + + if (type == "complete" && result) { + mp.commandv("script-message-to", "console", "complete", + JSON.stringify(result[0]), result[1]); + } + } + + if (type == "closed") { + mp.unregister_script_message("input-event"); + } + }) + + return true; + }, + terminate: function () { + mp.commandv("script-message-to", "console", "disable"); + }, + log: function (message, style, terminal_style) { + mp.commandv("script-message-to", "console", "log", JSON.stringify({ + text: message, + style: style, + terminal_style: terminal_style, + })); + }, + log_error: function (message) { + mp.commandv("script-message-to", "console", "log", + JSON.stringify({ text: message, error: true })); + }, + set_log: function (log) { + mp.commandv("script-message-to", "console", "set-log", + JSON.stringify(log)); + } +} + +/********************************************************************** * various *********************************************************************/ g.print = mp.msg.info; // convenient alias @@ -663,6 +702,12 @@ 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; @@ -747,7 +792,7 @@ g.mp_event_loop = function mp_event_loop() { wait = 0; // poll the next one } else { wait = process_timers() / 1000; - if (wait != 0) { + if (wait != 0 && iobservers.length) { notify_idle_observers(); // can add timers -> recalculate wait wait = peek_timers_wait() / 1000; } @@ -755,9 +800,12 @@ g.mp_event_loop = function mp_event_loop() { } while (mp.keep_running); }; -})(this) -try { - // let the user extend us, e.g. for updating mp.module_paths - require("~~/.init"); -} catch(e) {} +// 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) |