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
2533``current-ao``2533``current-ao``
2534 Current audio output driver (name as used with ``--ao``).2534 Current audio output driver (name as used with ``--ao``).
2535 2535
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
2536``working-directory``2558``working-directory``
2537 Return the working directory of the mpv process. Can be useful for JSON IPC2559 Return the working directory of the mpv process. Can be useful for JSON IPC
2538 users, because the command line player usually works with relative paths.2560 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
102 struct ao_hotplug *hotplug;102 struct ao_hotplug *hotplug;
103 103
104 struct mp_cmd_ctx *cache_dump_cmd; // in progress cache dumping104 struct mp_cmd_ctx *cache_dump_cmd; // in progress cache dumping
105
106 char **script_props;
107};
108
109static const struct m_option script_props_type = {
110 .type = &m_option_type_keyvalue_list
105};111};
106 112
107struct overlay {113struct overlay {
@@ -3217,6 +3223,27 @@ static int mp_property_bindings(void *ctx, struct m_property *prop, · 3109 unmodified lines
3217 return M_PROPERTY_NOT_IMPLEMENTED;3223 return M_PROPERTY_NOT_IMPLEMENTED;
3218}3224}
3219 3225
3226
3227static 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
3220// Redirect a property name to another3247// Redirect a property name to another
3221#define M_PROPERTY_ALIAS(name, real_property) \3248#define M_PROPERTY_ALIAS(name, real_property) \
3222 {(name), mp_property_alias, .priv = (real_property)}3249 {(name), mp_property_alias, .priv = (real_property)}
@@ -3391,6 +3418,8 @@ static const struct m_property mp_properties_base[] = { · 168 unmodified lines
3391 {"command-list", mp_property_commands},3418 {"command-list", mp_property_commands},
3392 {"input-bindings", mp_property_bindings},3419 {"input-bindings", mp_property_bindings},
3393 3420
3421 {"shared-script-properties", mp_property_script_props},
3422
3394 M_PROPERTY_ALIAS("video", "vid"),3423 M_PROPERTY_ALIAS("video", "vid"),
3395 M_PROPERTY_ALIAS("audio", "aid"),3424 M_PROPERTY_ALIAS("audio", "aid"),
3396 M_PROPERTY_ALIAS("sub", "sid"),3425 M_PROPERTY_ALIAS("sub", "sid"),
@@ -5856,6 +5885,8 @@ void command_uninit(struct MPContext *mpctx) · 2459 unmodified lines
5856 overlay_uninit(mpctx);5885 overlay_uninit(mpctx);
5857 ao_hotplug_destroy(ctx->hotplug);5886 ao_hotplug_destroy(ctx->hotplug);
5858 5887
5888 m_option_free(&script_props_type, &ctx->script_props);
5889
5859 talloc_free(mpctx->command_ctx);5890 talloc_free(mpctx->command_ctx);
5860 mpctx->command_ctx = NULL;5891 mpctx->command_ctx = NULL;
5861}5892}
player/lua/defaults.lua+24−0
@@ -676,4 +676,28 @@ function mp_utils.subprocess_detached(t) · 675 unmodified lines
676 mp.commandv("run", unpack(t.args))676 mp.commandv("run", unpack(t.args))
677end677end
678 678
679function 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
687end
688
689function mp_utils.shared_script_property_get(name)
690 local map = mp.get_property_native("shared-script-properties")
691 return map and map[name]
692end
693
694-- cb(name, value) on change and on init
695function 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)
701end
702
679return {}703return {}

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