From 13d354e46d27fd0c433880839abcf9096dbcbc2f Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 5 Aug 2020 22:37:47 +0200 Subject: auto_profiles: add this script This is taken from a somewhat older proof-of-concept script. The basic idea, and most of the implementation, is still the same. The way the profiles are actually defined changed. I still feel bad about this being a Lua script, and running user expressions as Lua code in a vaguely defined environment, but I guess as far as balance of effort/maintenance/results goes, this is fine. It's a bit bloated (the Lua scripting state is at least 150KB or so in total), so in order to enable this by default, I decided it should unload itself by default if no auto-profiles are used. (And currently, it does not actually rescan the profile list if a new config file is loaded some time later, so the script would do nothing anyway if no auto profiles were defined.) This still requires defining inverse profiles for "unapplying" a profile. Also this is still somewhat racy. Both will probably be alleviated to some degree in the future. --- options/m_config_frontend.c | 16 ++++++++++++++++ options/m_config_frontend.h | 3 +++ options/options.c | 4 ++++ options/options.h | 1 + options/parse_configfile.c | 3 +++ 5 files changed, 27 insertions(+) (limited to 'options') diff --git a/options/m_config_frontend.c b/options/m_config_frontend.c index 467c13eb8f..94cc9c015a 100644 --- a/options/m_config_frontend.c +++ b/options/m_config_frontend.c @@ -50,8 +50,10 @@ struct m_profile { struct m_profile *next; char *name; char *desc; + char *cond; int num_opts; // Option/value pair array. + // name,value = opts[n*2+0],opts[n*2+1] char **opts; }; @@ -85,6 +87,10 @@ static int show_profile(struct m_config *config, bstr param) MP_INFO(config, "Profile %s: %s\n", p->name, p->desc ? p->desc : ""); config->profile_depth++; + if (p->cond) { + MP_INFO(config, "%*sprofile-cond=%s\n", config->profile_depth, "", + p->cond); + } for (int i = 0; i < p->num_opts; i++) { MP_INFO(config, "%*s%s=%s\n", config->profile_depth, "", p->opts[2 * i], p->opts[2 * i + 1]); @@ -884,6 +890,14 @@ void m_profile_set_desc(struct m_profile *p, bstr desc) p->desc = bstrto0(p, desc); } +void m_profile_set_cond(struct m_profile *p, bstr cond) +{ + TA_FREEP(&p->cond); + cond = bstr_strip(cond); + if (cond.len) + p->cond = bstrto0(p, cond); +} + int m_config_set_profile_option(struct m_config *config, struct m_profile *p, bstr name, bstr val) { @@ -944,6 +958,8 @@ struct mpv_node m_config_get_profiles(struct m_config *config) node_map_add_string(entry, "name", profile->name); if (profile->desc) node_map_add_string(entry, "profile-desc", profile->desc); + if (profile->cond) + node_map_add_string(entry, "profile-cond", profile->cond); struct mpv_node *opts = node_map_add(entry, "options", MPV_FORMAT_NODE_ARRAY); diff --git a/options/m_config_frontend.h b/options/m_config_frontend.h index 19fbcadf25..81bc78b6e1 100644 --- a/options/m_config_frontend.h +++ b/options/m_config_frontend.h @@ -232,6 +232,9 @@ struct m_profile *m_config_add_profile(struct m_config *config, char *name); */ void m_profile_set_desc(struct m_profile *p, bstr desc); +// Set auto profile condition of a profile. +void m_profile_set_cond(struct m_profile *p, bstr cond); + /* Add an option to a profile. * Used by the config file parser when defining a profile. * diff --git a/options/options.c b/options/options.c index 4467c106ba..63c99053a8 100644 --- a/options/options.c +++ b/options/options.c @@ -426,6 +426,9 @@ static const m_option_t mp_opts[] = { .flags = UPDATE_BUILTIN_SCRIPTS}, {"load-osd-console", OPT_FLAG(lua_load_console), .flags = UPDATE_BUILTIN_SCRIPTS}, + {"load-auto-profiles", + OPT_CHOICE(lua_load_auto_profiles, {"no", 0}, {"yes", 1}, {"auto", -1}), + .flags = UPDATE_BUILTIN_SCRIPTS}, #endif // ------------------------- stream options -------------------- @@ -944,6 +947,7 @@ static const struct MPOpts mp_default_opts = { .lua_ytdl_raw_options = NULL, .lua_load_stats = 1, .lua_load_console = 1, + .lua_load_auto_profiles = -1, #endif .auto_load_scripts = 1, .loop_times = 1, diff --git a/options/options.h b/options/options.h index 45d0747358..ea5ece65a1 100644 --- a/options/options.h +++ b/options/options.h @@ -144,6 +144,7 @@ typedef struct MPOpts { char **lua_ytdl_raw_options; int lua_load_stats; int lua_load_console; + int lua_load_auto_profiles; int auto_load_scripts; diff --git a/options/parse_configfile.c b/options/parse_configfile.c index 14b30e87b4..96b607d554 100644 --- a/options/parse_configfile.c +++ b/options/parse_configfile.c @@ -131,6 +131,9 @@ int m_config_parse(m_config_t *config, const char *location, bstr data, if (bstr_equals0(option, "profile-desc")) { m_profile_set_desc(profile, value); res = 0; + } else if (bstr_equals0(option, "profile-cond")) { + m_profile_set_cond(profile, value); + res = 0; } else { res = m_config_set_profile_option(config, profile, option, value); } -- cgit v1.2.3