From 4fa6bcbb902d500ca0a1b9d2feeab5a4e5a98345 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 2 Sep 2016 14:49:34 +0200 Subject: 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. --- audio/filter/af.c | 8 ++++---- audio/out/ao.c | 8 ++++---- options/m_config.c | 22 +++++++++++++++++++--- options/m_config.h | 9 +++------ video/filter/vf.c | 8 ++++---- video/out/vo.c | 7 +++---- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/audio/filter/af.c b/audio/filter/af.c index f380459747..e6f19b3e37 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -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; diff --git a/audio/out/ao.c b/audio/out/ao.c index fcbc44b139..189434cf0b 100644 --- a/audio/out/ao.c +++ b/audio/out/ao.c @@ -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; diff --git a/options/m_config.c b/options/m_config.c index d4c19808b0..7da23dc8d0 100644 --- a/options/m_config.c +++ b/options/m_config.c @@ -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) diff --git a/options/m_config.h b/options/m_config.h index 4ac673859f..c8225706ee 100644 --- a/options/m_config.h +++ b/options/m_config.h @@ -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(). diff --git a/video/filter/vf.c b/video/filter/vf.c index b632314426..93b886e69c 100644 --- a/video/filter/vf.c +++ b/video/filter/vf.c @@ -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); diff --git a/video/out/vo.c b/video/out/vo.c index 53525b4049..e37acb1242 100644 --- a/video/out/vo.c +++ b/video/out/vo.c @@ -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; -- cgit v1.2.3