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
227 g->group->opts[opt_index].name);227 g->group->opts[opt_index].name);
228}228}
229 229
230const 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
230const void *m_config_shadow_get_opt_default(struct m_config_shadow *shadow,243const void *m_config_shadow_get_opt_default(struct m_config_shadow *shadow,
231 int32_t id)244 int32_t id)
232{245{
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
175const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow,175const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow,
176 int32_t id, char *buf, size_t buf_size);176 int32_t id, char *buf, size_t buf_size);
177 177
178// Resolve alias mapping from opt.
179const char *m_config_shadow_get_alias_from_opt(struct m_config_shadow *shadow, int32_t id,
180 char *buf, size_t buf_size);
181
178// Pointer to default value, using m_option.type. NULL if option without data.182// Pointer to default value, using m_option.type. NULL if option without data.
179// id must be a valid option ID as returned by m_config_shadow_get_next_opt() or183// id must be a valid option ID as returned by m_config_shadow_get_next_opt() or
180// m_config_cache_get_next_opt().184// 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
302 302
303 const char *prefix = config->is_toplevel ? "--" : "";303 const char *prefix = config->is_toplevel ? "--" : "";
304 if (co->opt->type == &m_option_type_alias) {304 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));
306 if (co->opt->deprecation_message && !co->warning_was_printed) {308 if (co->opt->deprecation_message && !co->warning_was_printed) {
307 if (co->opt->deprecation_message[0]) {309 if (co->opt->deprecation_message[0]) {
308 MP_WARN(config, "Warning: option %s%s was replaced with "310 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
881 MP_INFO(config, " [file]");883 MP_INFO(config, " [file]");
882 if (opt->deprecation_message)884 if (opt->deprecation_message)
883 MP_INFO(config, " [deprecated]");885 MP_INFO(config, " [deprecated]");
884 if (opt->type == &m_option_type_alias)886 if (opt->type == &m_option_type_alias) {
885 MP_INFO(config, " for %s", (char *)opt->priv);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 }
886 if (opt->type == &m_option_type_cli_alias)892 if (opt->type == &m_option_type_cli_alias)
887 MP_INFO(config, " for --%s (CLI/config files only)", (char *)opt->priv);893 MP_INFO(config, " for --%s (CLI/config files only)", (char *)opt->priv);
888 MP_INFO(config, "\n");894 MP_INFO(config, "\n");
options/m_option.h+3−0
@@ -389,6 +389,9 @@ struct m_option { · 388 unmodified lines
389 // Always force an option update even if the written value does not change.389 // Always force an option update even if the written value does not change.
390 bool force_update;390 bool force_update;
391 391
392 // If the option is an alias, use the prefix of sub option.
393 bool alias_use_prefix;
394
392 int offset;395 int offset;
393 396
394 // Most numeric types restrict the range to [min, max] if min<max (this397 // 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
7187 };7187 };
7188 7188
7189 if (co->opt->type == &m_option_type_alias) {7189 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);
7191 7194
7192 prop.call = co->opt->deprecation_message ?7195 prop.call = co->opt->deprecation_message ?
7193 mp_property_deprecated_alias : mp_property_alias;7196 mp_property_deprecated_alias : mp_property_alias;
@@ -7198,8 +7201,9 @@ void command_init(struct MPContext *mpctx) · 4 unmodified lines
7198 // be set as property.7201 // be set as property.
7199 struct m_config_option *co2 = co;7202 struct m_config_option *co2 = co;
7200 while (co2 && co2->opt->type == &m_option_type_alias) {7203 while (co2 && co2->opt->type == &m_option_type_alias) {
7201 const char *alias = (const char *)co2->opt->priv;7204 const char *co2_alias = m_config_shadow_get_alias_from_opt(mpctx->mconfig->shadow, co2->opt_id,
7202 co2 = m_config_get_co_raw(mpctx->mconfig, bstr0(alias));7205 buf, sizeof(buf));
7206 co2 = m_config_get_co_raw(mpctx->mconfig, bstr0(co2_alias));
7203 }7207 }
7204 if (!co2)7208 if (!co2)
7205 continue;7209 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