History

m_option: add a way for aliases to use sub option prefix

Previously, using m_option_type_alias required that the alias have the full name of the option value. This is limited if you are reusing the same opts struct in different sub options. Fix this by add an additional field to m_option to signal whether or not prefixes should be taken into account for the alias option. Instead of blindly using whatever is stored in opt->priv as the name, we can construct the desired option name for either case (using the prefix or not).

Dudemanguy committed 2 years ago

Commit 26029cf1 parent 1225bcb5 files · +36 −6Browse files

Unified · Split

5 files
  1. options/m_config_core.c +13 −0
  2. options/m_config_core.h +4 −0
  3. options/m_config_frontend.c +9 −3
  4. options/m_option.h +3 −0
  5. player/command.c +7 −3
options/m_config_core.c+13−0
@@ -227,6 +227,19 @@ const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow, · 226 unmodified lines
227227 g->group->opts[opt_index].name);
228228 }
229229
230+const char *m_config_shadow_get_alias_from_opt(struct m_config_shadow *shadow, int32_t id,
231+ char *buf, size_t buf_size)
232+{
233+ int group_index, opt_index;
234+ get_opt_from_id(shadow, id, &group_index, &opt_index);
235+
236+ struct m_config_group *g = &shadow->groups[group_index];
237+ const struct m_option *opt = &shadow->groups[group_index].group->opts[opt_index];
238+ if (opt->alias_use_prefix)
239+ return concat_name_buf(buf, buf_size, g->prefix, (const char *)opt->priv);
240+ return (const char *)opt->priv;
241+}
242+
230243 const void *m_config_shadow_get_opt_default(struct m_config_shadow *shadow,
231244 int32_t id)
232245 {
options/m_config_core.h+4−0
@@ -175,6 +175,10 @@ const struct m_option *m_config_shadow_get_opt(struct m_config_shadow *shadow, · 174 unmodified lines
175175 const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow,
176176 int32_t id, char *buf, size_t buf_size);
177177
178+// Resolve alias mapping from opt.
179+const char *m_config_shadow_get_alias_from_opt(struct m_config_shadow *shadow, int32_t id,
180+ char *buf, size_t buf_size);
181+
178182 // Pointer to default value, using m_option.type. NULL if option without data.
179183 // id must be a valid option ID as returned by m_config_shadow_get_next_opt() or
180184 // m_config_cache_get_next_opt().
options/m_config_frontend.c+9−3
@@ -302,7 +302,9 @@ static struct m_config_option *m_config_get_co_any(const struct m_config *config · 301 unmodified lines
302302
303303 const char *prefix = config->is_toplevel ? "--" : "";
304304 if (co->opt->type == &m_option_type_alias) {
305− const char *alias = (const char *)co->opt->priv;
305+ char buf[M_CONFIG_MAX_OPT_NAME_LEN];
306+ const char *alias = m_config_shadow_get_alias_from_opt(config->shadow, co->opt_id,
307+ buf, sizeof(buf));
306308 if (co->opt->deprecation_message && !co->warning_was_printed) {
307309 if (co->opt->deprecation_message[0]) {
308310 MP_WARN(config, "Warning: option %s%s was replaced with "
@@ -881,8 +883,12 @@ void m_config_print_option_list(const struct m_config *config, const char *name) · 572 unmodified lines
881883 MP_INFO(config, " [file]");
882884 if (opt->deprecation_message)
883885 MP_INFO(config, " [deprecated]");
884− if (opt->type == &m_option_type_alias)
885− MP_INFO(config, " for %s", (char *)opt->priv);
886+ if (opt->type == &m_option_type_alias) {
887+ char buf[M_CONFIG_MAX_OPT_NAME_LEN];
888+ const char *alias = m_config_shadow_get_alias_from_opt(config->shadow, co->opt_id,
889+ buf, sizeof(buf));
890+ MP_INFO(config, " for %s", alias);
891+ }
886892 if (opt->type == &m_option_type_cli_alias)
887893 MP_INFO(config, " for --%s (CLI/config files only)", (char *)opt->priv);
888894 MP_INFO(config, "\n");
options/m_option.h+3−0
@@ -389,6 +389,9 @@ struct m_option { · 388 unmodified lines
389389 // Always force an option update even if the written value does not change.
390390 bool force_update;
391391
392+ // If the option is an alias, use the prefix of sub option.
393+ bool alias_use_prefix;
394+
392395 int offset;
393396
394397 // Most numeric types restrict the range to [min, max] if min<max (this
player/command.c+7−3
@@ -7187,7 +7187,10 @@ void command_init(struct MPContext *mpctx) · 7186 unmodified lines
71877187 };
71887188
71897189 if (co->opt->type == &m_option_type_alias) {
7190− prop.priv = co->opt->priv;
7190+ char buf[M_CONFIG_MAX_OPT_NAME_LEN];
7191+ const char *alias = m_config_shadow_get_alias_from_opt(mpctx->mconfig->shadow, co->opt_id,
7192+ buf, sizeof(buf));
7193+ prop.priv = talloc_strdup(ctx, alias);
71917194
71927195 prop.call = co->opt->deprecation_message ?
71937196 mp_property_deprecated_alias : mp_property_alias;
@@ -7198,8 +7201,9 @@ void command_init(struct MPContext *mpctx) · 4 unmodified lines
71987201 // be set as property.
71997202 struct m_config_option *co2 = co;
72007203 while (co2 && co2->opt->type == &m_option_type_alias) {
7201− const char *alias = (const char *)co2->opt->priv;
7202− co2 = m_config_get_co_raw(mpctx->mconfig, bstr0(alias));
7204+ const char *co2_alias = m_config_shadow_get_alias_from_opt(mpctx->mconfig->shadow, co2->opt_id,
7205+ buf, sizeof(buf));
7206+ co2 = m_config_get_co_raw(mpctx->mconfig, bstr0(co2_alias));
72037207 }
72047208 if (!co2)
72057209 continue;

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