m_config: add helper function for initializing af/ao/vf/vo suboptions

Normally I'd prefer a bunch of smaller functions with fewer parameters
over a single function with a lot of parameters. But future changes will
require messing with the parameters in a slightly more complex way, so a
combined function will be needed anyway. The now-unused "global"
parameter is required for later as well.
This commit is contained in:
wm4 2016-09-02 14:49:34 +02:00
parent 875aeb0f5c
commit 4fa6bcbb90
6 changed files with 37 additions and 25 deletions

View File

@ -166,10 +166,10 @@ static struct af_instance *af_create(struct af_stream *s, char *name,
.replaygain_data = s->replaygain_data,
.out_pool = mp_audio_pool_create(af),
};
struct m_config *config = m_config_from_obj_desc(af, s->log, &desc);
if (m_config_apply_defaults(config, name, s->opts->af_defs) < 0)
goto error;
if (m_config_set_obj_params(config, args) < 0)
struct m_config *config =
m_config_from_obj_desc_and_args(af, s->log, NULL, &desc,
name, s->opts->af_defs, args);
if (!config)
goto error;
af->priv = config->optstruct;

View File

@ -145,10 +145,10 @@ static struct ao *ao_alloc(bool probing, struct mpv_global *global,
.def_buffer = opts->audio_buffer,
.client_name = talloc_strdup(ao, opts->audio_client_name),
};
struct m_config *config = m_config_from_obj_desc(ao, ao->log, &desc);
if (m_config_apply_defaults(config, name, opts->ao_defs) < 0)
goto error;
if (m_config_set_obj_params(config, args) < 0)
struct m_config *config =
m_config_from_obj_desc_and_args(ao, ao->log, global, &desc,
name, opts->ao_defs, args);
if (!config)
goto error;
ao->priv = config->optstruct;
return ao;

View File

@ -221,7 +221,7 @@ struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx,
return m_config_new(talloc_ctx, log, 0, desc->priv_defaults, desc->options);
}
int m_config_set_obj_params(struct m_config *conf, char **args)
static int m_config_set_obj_params(struct m_config *conf, char **args)
{
for (int n = 0; args && args[n * 2 + 0]; n++) {
int r = m_config_set_option(conf, bstr0(args[n * 2 + 0]),
@ -232,8 +232,8 @@ int m_config_set_obj_params(struct m_config *conf, char **args)
return 0;
}
int m_config_apply_defaults(struct m_config *config, const char *name,
struct m_obj_settings *defaults)
static int m_config_apply_defaults(struct m_config *config, const char *name,
struct m_obj_settings *defaults)
{
int r = 0;
for (int n = 0; defaults && defaults[n].name; n++) {
@ -246,6 +246,22 @@ int m_config_apply_defaults(struct m_config *config, const char *name,
return r;
}
struct m_config *m_config_from_obj_desc_and_args(void *ta_parent,
struct mp_log *log, struct mpv_global *global, struct m_obj_desc *desc,
const char *name, struct m_obj_settings *defaults, char **args)
{
struct m_config *config = m_config_from_obj_desc(ta_parent, log, desc);
if (m_config_apply_defaults(config, name, defaults) < 0)
goto error;
if (m_config_set_obj_params(config, args) < 0)
goto error;
return config;
error:
talloc_free(config);
return NULL;
}
static void ensure_backup(struct m_config *config, struct m_config_option *co)
{
if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)

View File

@ -106,12 +106,9 @@ struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx,
struct mp_log *log,
struct m_obj_desc *desc);
int m_config_set_obj_params(struct m_config *conf, char **args);
// Search for the object with the given name in the defaults list, and apply
// its parameters.
int m_config_apply_defaults(struct m_config *config, const char *name,
struct m_obj_settings *defaults);
struct m_config *m_config_from_obj_desc_and_args(void *ta_parent,
struct mp_log *log, struct mpv_global *global, struct m_obj_desc *desc,
const char *name, struct m_obj_settings *defaults, char **args);
// Make sure the option is backed up. If it's already backed up, do nothing.
// All backed up options can be restored with m_config_restore_backups().

View File

@ -255,10 +255,10 @@ static struct vf_instance *vf_open(struct vf_chain *c, const char *name,
.out_pool = talloc_steal(vf, mp_image_pool_new(16)),
.chain = c,
};
struct m_config *config = m_config_from_obj_desc(vf, vf->log, &desc);
if (m_config_apply_defaults(config, name, c->opts->vf_defs) < 0)
goto error;
if (m_config_set_obj_params(config, args) < 0)
struct m_config *config =
m_config_from_obj_desc_and_args(vf, vf->log, c->global, &desc,
name, c->opts->vf_defs, args);
if (!config)
goto error;
vf->priv = config->optstruct;
int retcode = vf->info->open(vf);

View File

@ -248,10 +248,9 @@ static struct vo *vo_create(bool probing, struct mpv_global *global,
mp_input_set_mouse_transform(vo->input_ctx, NULL, NULL);
if (vo->driver->encode != !!vo->encode_lavc_ctx)
goto error;
vo->config = m_config_from_obj_desc(vo, vo->log, &desc);
if (m_config_apply_defaults(vo->config, name, vo->opts->vo_defs) < 0)
goto error;
if (m_config_set_obj_params(vo->config, args) < 0)
vo->config = m_config_from_obj_desc_and_args(vo, vo->log, global, &desc,
name, vo->opts->vo_defs, args);
if (!vo->config)
goto error;
vo->priv = vo->config->optstruct;