History

command, lua: add a way to share data between scripts

Very primitive and dumb, but fulfils its purpose for the next commits. I chose this specific implementation because it has the lowest footprint in command.c, without resorting to crazy hacks such as sending messages between scripts (which would be hard to coordinate especially on startup).

wm4 committed 6 years ago

Commit 7e4819e1 parent 6ab013c3 files · +77 −0Browse files

Unified · Split

3 files
  1. DOCS/man/input.rst +22 −0
  2. player/command.c +31 −0
  3. player/lua/defaults.lua +24 −0
DOCS/man/input.rst+22−0
@@ -2533,6 +2533,28 @@ Property list · 2532 unmodified lines
25332533 ``current-ao``
25342534 Current audio output driver (name as used with ``--ao``).
25352535
2536+``shared-script-properties`` (RW)
2537+ This is a key/value map of arbitrary strings shared between scripts for
2538+ general use. The player itself does not use any data in it (although some
2539+ builtin scripts may). The property is not preserved across player restarts.
2540+
2541+ This is very primitive, inefficient, and annoying to use. It's a makeshift
2542+ solution which could go away any time (for example, when a better solution
2543+ becomes available). This is also why this property has an annoying name. You
2544+ should avoid using it, unless you absolutely have to.
2545+
2546+ Lua scripting has helpers starting with ``utils.shared_script_property_``.
2547+ They are undocumented because you should not use this property. If you still
2548+ think you must, you should use the helpers instead of the property directly.
2549+
2550+ You are supposed to use the ``change-list`` command to modify the contents.
2551+ Reading, modifying, and writing the property manually could data loss if two
2552+ scripts update different keys at the same time due to lack of
2553+ synchronization. The Lua helpers take care of this.
2554+
2555+ (There is no way to ensure synchronization if two scripts try to update the
2556+ same key at the same time.)
2557+
25362558 ``working-directory``
25372559 Return the working directory of the mpv process. Can be useful for JSON IPC
25382560 users, because the command line player usually works with relative paths.
player/command.c+31−0
@@ -102,6 +102,12 @@ struct command_ctx { · 101 unmodified lines
102102 struct ao_hotplug *hotplug;
103103
104104 struct mp_cmd_ctx *cache_dump_cmd; // in progress cache dumping
105+
106+ char **script_props;
107+};
108+
109+static const struct m_option script_props_type = {
110+ .type = &m_option_type_keyvalue_list
105111 };
106112
107113 struct overlay {
@@ -3217,6 +3223,27 @@ static int mp_property_bindings(void *ctx, struct m_property *prop, · 3109 unmodified lines
32173223 return M_PROPERTY_NOT_IMPLEMENTED;
32183224 }
32193225
3226+
3227+static int mp_property_script_props(void *ctx, struct m_property *prop,
3228+ int action, void *arg)
3229+{
3230+ MPContext *mpctx = ctx;
3231+ struct command_ctx *cmd = mpctx->command_ctx;
3232+ switch (action) {
3233+ case M_PROPERTY_GET_TYPE:
3234+ *(struct m_option *)arg = script_props_type;
3235+ return M_PROPERTY_OK;
3236+ case M_PROPERTY_GET:
3237+ m_option_copy(&script_props_type, arg, &cmd->script_props);
3238+ return M_PROPERTY_OK;
3239+ case M_PROPERTY_SET:
3240+ m_option_copy(&script_props_type, &cmd->script_props, arg);
3241+ mp_notify_property(mpctx, prop->name);
3242+ return M_PROPERTY_OK;
3243+ }
3244+ return M_PROPERTY_NOT_IMPLEMENTED;
3245+}
3246+
32203247 // Redirect a property name to another
32213248 #define M_PROPERTY_ALIAS(name, real_property) \
32223249 {(name), mp_property_alias, .priv = (real_property)}
@@ -3391,6 +3418,8 @@ static const struct m_property mp_properties_base[] = { · 168 unmodified lines
33913418 {"command-list", mp_property_commands},
33923419 {"input-bindings", mp_property_bindings},
33933420
3421+ {"shared-script-properties", mp_property_script_props},
3422+
33943423 M_PROPERTY_ALIAS("video", "vid"),
33953424 M_PROPERTY_ALIAS("audio", "aid"),
33963425 M_PROPERTY_ALIAS("sub", "sid"),
@@ -5856,6 +5885,8 @@ void command_uninit(struct MPContext *mpctx) · 2459 unmodified lines
58565885 overlay_uninit(mpctx);
58575886 ao_hotplug_destroy(ctx->hotplug);
58585887
5888+ m_option_free(&script_props_type, &ctx->script_props);
5889+
58595890 talloc_free(mpctx->command_ctx);
58605891 mpctx->command_ctx = NULL;
58615892 }
player/lua/defaults.lua+24−0
@@ -676,4 +676,28 @@ function mp_utils.subprocess_detached(t) · 675 unmodified lines
676676 mp.commandv("run", unpack(t.args))
677677 end
678678
679+function mp_utils.shared_script_property_set(name, value)
680+ if value ~= nil then
681+ -- no such thing as change-list with mpv_node, so build a string value
682+ mp.commandv("change-list", "shared-script-properties", "append",
683+ name .. "=" .. value)
684+ else
685+ mp.commandv("change-list", "shared-script-properties", "remove", name)
686+ end
687+end
688+
689+function mp_utils.shared_script_property_get(name)
690+ local map = mp.get_property_native("shared-script-properties")
691+ return map and map[name]
692+end
693+
694+-- cb(name, value) on change and on init
695+function mp_utils.shared_script_property_observe(name, cb)
696+ -- it's _very_ wasteful to observe the mpv core "super" property for every
697+ -- shared sub-property, but then again you shouldn't use this
698+ mp.observe_property("shared-script-properties", "native", function(_, val)
699+ cb(name, val and val[name])
700+ end)
701+end
702+
679703 return {}

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