diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:36:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:36:56 +0000 |
commit | 51de1d8436100f725f3576aefa24a2bd2057bc28 (patch) | |
tree | c6d1d5264b6d40a8d7ca34129f36b7d61e188af3 /options | |
parent | Initial commit. (diff) | |
download | mpv-51de1d8436100f725f3576aefa24a2bd2057bc28.tar.xz mpv-51de1d8436100f725f3576aefa24a2bd2057bc28.zip |
Adding upstream version 0.37.0.upstream/0.37.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'options')
-rw-r--r-- | options/m_config.h | 1 | ||||
-rw-r--r-- | options/m_config_core.c | 876 | ||||
-rw-r--r-- | options/m_config_core.h | 194 | ||||
-rw-r--r-- | options/m_config_frontend.c | 1080 | ||||
-rw-r--r-- | options/m_config_frontend.h | 266 | ||||
-rw-r--r-- | options/m_option.c | 3866 | ||||
-rw-r--r-- | options/m_option.h | 764 | ||||
-rw-r--r-- | options/m_property.c | 630 | ||||
-rw-r--r-- | options/m_property.h | 234 | ||||
-rw-r--r-- | options/options.c | 1097 | ||||
-rw-r--r-- | options/options.h | 406 | ||||
-rw-r--r-- | options/parse_commandline.c | 261 | ||||
-rw-r--r-- | options/parse_commandline.h | 32 | ||||
-rw-r--r-- | options/parse_configfile.c | 178 | ||||
-rw-r--r-- | options/parse_configfile.h | 30 | ||||
-rw-r--r-- | options/path.c | 410 | ||||
-rw-r--r-- | options/path.h | 98 |
17 files changed, 10423 insertions, 0 deletions
diff --git a/options/m_config.h b/options/m_config.h new file mode 100644 index 0000000..d2ce2b4 --- /dev/null +++ b/options/m_config.h @@ -0,0 +1 @@ +#include "m_config_core.h"
\ No newline at end of file diff --git a/options/m_config_core.c b/options/m_config_core.c new file mode 100644 index 0000000..08a76eb --- /dev/null +++ b/options/m_config_core.c @@ -0,0 +1,876 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <errno.h> +#include <stdatomic.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +#include "common/common.h" +#include "common/global.h" +#include "common/msg_control.h" +#include "common/msg.h" +#include "m_config_core.h" +#include "misc/dispatch.h" +#include "options/m_option.h" +#include "osdep/threads.h" + +// For use with m_config_cache. +struct m_config_shadow { + mp_mutex lock; + // Incremented on every option change. + _Atomic uint64_t ts; + // -- immutable after init + // List of m_sub_options instances. + // Index 0 is the top-level and is always present. + // Immutable after init. + // Invariant: a parent is always at a lower index than any of its children. + struct m_config_group *groups; + int num_groups; + // -- protected by lock + struct m_config_data *data; // protected shadow copy of the option data + struct config_cache **listeners; + int num_listeners; +}; + +// Represents a sub-struct (OPT_SUBSTRUCT()). +struct m_config_group { + const struct m_sub_options *group; + int opt_count; // cached opt. count; group->opts[opt_count].name==NULL + int group_count; // 1 + number of all sub groups owned by this (so + // m_config_shadow.groups[idx..idx+group_count] is used + // by the entire tree of sub groups included by this + // group) + int parent_group; // index of parent group into m_config_shadow.groups[], + // or -1 for group 0 + int parent_ptr; // ptr offset in the parent group's data, or -1 if + // none + const char *prefix; // concat_name(_, prefix, opt->name) => full name + // (the parent names are already included in this) +}; + +// A copy of option data. Used for the main option struct, the shadow data, +// and copies for m_config_cache. +struct m_config_data { + struct m_config_shadow *shadow; // option definitions etc., main data copy + int group_index; // start index into m_config.groups[] + struct m_group_data *gdata; // user struct allocation (our copy of data) + int num_gdata; // (group_index+num_gdata = end index) +}; + +struct config_cache { + struct m_config_cache *public; + + struct m_config_data *data; // public data + struct m_config_data *src; // global data (currently ==shadow->data) + struct m_config_shadow *shadow; // global metadata + int group_start, group_end; // derived from data->group_index etc. + uint64_t ts; // timestamp of this data copy + bool in_list; // part of m_config_shadow->listeners[] + int upd_group; // for "incremental" change notification + int upd_opt; + + + // --- Implicitly synchronized by setting/unsetting wakeup_cb. + struct mp_dispatch_queue *wakeup_dispatch_queue; + void (*wakeup_dispatch_cb)(void *ctx); + void *wakeup_dispatch_cb_ctx; + + // --- Protected by shadow->lock + void (*wakeup_cb)(void *ctx); + void *wakeup_cb_ctx; +}; + +// Per m_config_data state for each m_config_group. +struct m_group_data { + char *udata; // pointer to group user option struct + uint64_t ts; // timestamp of the data copy +}; + +static void add_sub_group(struct m_config_shadow *shadow, const char *name_prefix, + int parent_group_index, int parent_ptr, + const struct m_sub_options *subopts); + +static struct m_group_data *m_config_gdata(struct m_config_data *data, + int group_index) +{ + if (group_index < data->group_index || + group_index >= data->group_index + data->num_gdata) + return NULL; + + return &data->gdata[group_index - data->group_index]; +} + +// Like concat_name(), but returns either a, b, or buf. buf/buf_size is used as +// target for snprintf(). (buf_size is recommended to be MAX_OPT_NAME_LEN.) +static const char *concat_name_buf(char *buf, size_t buf_size, + const char *a, const char *b) +{ + assert(a); + assert(b); + if (!a[0]) + return b; + if (!b[0]) + return a; + snprintf(buf, buf_size, "%s-%s", a, b); + return buf; +} + +// Return full option name from prefix (a) and option name (b). Returns either +// a, b, or a talloc'ed string under ta_parent. +static const char *concat_name(void *ta_parent, const char *a, const char *b) +{ + char buf[M_CONFIG_MAX_OPT_NAME_LEN]; + const char *r = concat_name_buf(buf, sizeof(buf), a, b); + return r == buf ? talloc_strdup(ta_parent, r) : r; +} + +static bool iter_next(struct m_config_shadow *shadow, int group_start, + int group_end, int32_t *p_id) +{ + int32_t id = *p_id; + int group_index = id == -1 ? group_start : id >> 16; + int opt_index = id == -1 ? -1 : id & 0xFFFF; + + assert(group_index >= group_start && group_index <= group_end); + + while (1) { + if (group_index >= group_end) + return false; + + struct m_config_group *g = &shadow->groups[group_index]; + const struct m_option *opts = g->group->opts; + + assert(opt_index >= -1 && opt_index < g->opt_count); + + opt_index += 1; + + if (!opts || !opts[opt_index].name) { + group_index += 1; + opt_index = -1; + continue; + } + + if (opts[opt_index].type == &m_option_type_subconfig) + continue; + + *p_id = (group_index << 16) | opt_index; + return true; + } +} + +bool m_config_shadow_get_next_opt(struct m_config_shadow *shadow, int32_t *p_id) +{ + return iter_next(shadow, 0, shadow->num_groups, p_id); +} + +bool m_config_cache_get_next_opt(struct m_config_cache *cache, int32_t *p_id) +{ + return iter_next(cache->shadow, cache->internal->group_start, + cache->internal->group_end, p_id); +} + +static void get_opt_from_id(struct m_config_shadow *shadow, int32_t id, + int *out_group_index, int *out_opt_index) +{ + int group_index = id >> 16; + int opt_index = id & 0xFFFF; + + assert(group_index >= 0 && group_index < shadow->num_groups); + assert(opt_index >= 0 && opt_index < shadow->groups[group_index].opt_count); + + *out_group_index = group_index; + *out_opt_index = opt_index; +} + +const struct m_option *m_config_shadow_get_opt(struct m_config_shadow *shadow, + int32_t id) +{ + int group_index, opt_index; + get_opt_from_id(shadow, id, &group_index, &opt_index); + + return &shadow->groups[group_index].group->opts[opt_index]; +} + +const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow, + int32_t id, char *buf, size_t buf_size) +{ + int group_index, opt_index; + get_opt_from_id(shadow, id, &group_index, &opt_index); + + struct m_config_group *g = &shadow->groups[group_index]; + return concat_name_buf(buf, buf_size, g->prefix, + g->group->opts[opt_index].name); +} + +const void *m_config_shadow_get_opt_default(struct m_config_shadow *shadow, + int32_t id) +{ + int group_index, opt_index; + get_opt_from_id(shadow, id, &group_index, &opt_index); + + const struct m_sub_options *subopt = shadow->groups[group_index].group; + const struct m_option *opt = &subopt->opts[opt_index]; + + if (opt->offset < 0) + return NULL; + + if (opt->defval) + return opt->defval; + + if (subopt->defaults) + return (char *)subopt->defaults + opt->offset; + + return &m_option_value_default; +} + +void *m_config_cache_get_opt_data(struct m_config_cache *cache, int32_t id) +{ + int group_index, opt_index; + get_opt_from_id(cache->shadow, id, &group_index, &opt_index); + + assert(group_index >= cache->internal->group_start && + group_index < cache->internal->group_end); + + struct m_group_data *gd = m_config_gdata(cache->internal->data, group_index); + const struct m_option *opt = + &cache->shadow->groups[group_index].group->opts[opt_index]; + + return gd && opt->offset >= 0 ? gd->udata + opt->offset : NULL; +} + +static uint64_t get_opt_change_mask(struct m_config_shadow *shadow, int group_index, + int group_root, const struct m_option *opt) +{ + uint64_t changed = opt->flags & UPDATE_OPTS_MASK; + while (group_index != group_root) { + struct m_config_group *g = &shadow->groups[group_index]; + changed |= g->group->change_flags; + group_index = g->parent_group; + } + return changed; +} + +uint64_t m_config_cache_get_option_change_mask(struct m_config_cache *cache, + int32_t id) +{ + struct m_config_shadow *shadow = cache->shadow; + int group_index, opt_index; + get_opt_from_id(shadow, id, &group_index, &opt_index); + + assert(group_index >= cache->internal->group_start && + group_index < cache->internal->group_end); + + return get_opt_change_mask(cache->shadow, group_index, + cache->internal->data->group_index, + &shadow->groups[group_index].group->opts[opt_index]); +} + +// The memcpys are supposed to work around the strict aliasing violation, +// that would result if we just dereferenced a void** (where the void** is +// actually casted from struct some_type* ). The dummy struct type is in +// theory needed, because void* and struct pointers could have different +// representations, while pointers to different struct types don't. +static void *substruct_read_ptr(const void *ptr) +{ + struct mp_dummy_ *res; + memcpy(&res, ptr, sizeof(res)); + return res; +} +static void substruct_write_ptr(void *ptr, void *val) +{ + struct mp_dummy_ *src = val; + memcpy(ptr, &src, sizeof(src)); +} + +// Initialize a field with a given value. In case this is dynamic data, it has +// to be allocated and copied. src can alias dst. +static void init_opt_inplace(const struct m_option *opt, void *dst, + const void *src) +{ + // The option will use dynamic memory allocation iff it has a free callback. + if (opt->type->free) { + union m_option_value temp; + memcpy(&temp, src, opt->type->size); + memset(dst, 0, opt->type->size); + m_option_copy(opt, dst, &temp); + } else if (src != dst) { + memcpy(dst, src, opt->type->size); + } +} + +static void alloc_group(struct m_config_data *data, int group_index, + struct m_config_data *copy) +{ + assert(group_index == data->group_index + data->num_gdata); + assert(group_index < data->shadow->num_groups); + struct m_config_group *group = &data->shadow->groups[group_index]; + const struct m_sub_options *opts = group->group; + + MP_TARRAY_GROW(data, data->gdata, data->num_gdata); + struct m_group_data *gdata = &data->gdata[data->num_gdata++]; + + struct m_group_data *copy_gdata = + copy ? m_config_gdata(copy, group_index) : NULL; + + *gdata = (struct m_group_data){ + .udata = talloc_zero_size(data, opts->size), + .ts = copy_gdata ? copy_gdata->ts : 0, + }; + + if (opts->defaults) + memcpy(gdata->udata, opts->defaults, opts->size); + + char *copy_src = copy_gdata ? copy_gdata->udata : NULL; + + for (int n = 0; opts->opts && opts->opts[n].name; n++) { + const struct m_option *opt = &opts->opts[n]; + + if (opt->offset < 0 || opt->type->size == 0) + continue; + + void *dst = gdata->udata + opt->offset; + const void *defptr = opt->defval ? opt->defval : dst; + if (copy_src) + defptr = copy_src + opt->offset; + + init_opt_inplace(opt, dst, defptr); + } + + // If there's a parent, update its pointer to the new struct. + if (group->parent_group >= data->group_index && group->parent_ptr >= 0) { + struct m_group_data *parent_gdata = + m_config_gdata(data, group->parent_group); + assert(parent_gdata); + + substruct_write_ptr(parent_gdata->udata + group->parent_ptr, gdata->udata); + } +} + +static void free_option_data(void *p) +{ + struct m_config_data *data = p; + + for (int i = 0; i < data->num_gdata; i++) { + struct m_group_data *gdata = &data->gdata[i]; + struct m_config_group *group = + &data->shadow->groups[data->group_index + i]; + const struct m_option *opts = group->group->opts; + + for (int n = 0; opts && opts[n].name; n++) { + const struct m_option *opt = &opts[n]; + + if (opt->offset >= 0 && opt->type->size > 0) + m_option_free(opt, gdata->udata + opt->offset); + } + } +} + +// Allocate data using the option description in shadow, starting at group_index +// (index into m_config.groups[]). +// If copy is not NULL, copy all data from there (for groups which are in both +// m_config_data instances), in all other cases init the data with the defaults. +static struct m_config_data *allocate_option_data(void *ta_parent, + struct m_config_shadow *shadow, + int group_index, + struct m_config_data *copy) +{ + assert(group_index >= 0 && group_index < shadow->num_groups); + struct m_config_data *data = talloc_zero(ta_parent, struct m_config_data); + talloc_set_destructor(data, free_option_data); + + data->shadow = shadow; + data->group_index = group_index; + + struct m_config_group *root_group = &shadow->groups[group_index]; + assert(root_group->group_count > 0); + + for (int n = group_index; n < group_index + root_group->group_count; n++) + alloc_group(data, n, copy); + + return data; +} + +static void shadow_destroy(void *p) +{ + struct m_config_shadow *shadow = p; + + // must all have been unregistered + assert(shadow->num_listeners == 0); + + talloc_free(shadow->data); + mp_mutex_destroy(&shadow->lock); +} + +struct m_config_shadow *m_config_shadow_new(const struct m_sub_options *root) +{ + struct m_config_shadow *shadow = talloc_zero(NULL, struct m_config_shadow); + talloc_set_destructor(shadow, shadow_destroy); + mp_mutex_init(&shadow->lock); + + add_sub_group(shadow, NULL, -1, -1, root); + + if (!root->size) + return shadow; + + shadow->data = allocate_option_data(shadow, shadow, 0, NULL); + + return shadow; +} + +static void init_obj_settings_list(struct m_config_shadow *shadow, + int parent_group_index, + const struct m_obj_list *list) +{ + struct m_obj_desc desc; + for (int n = 0; ; n++) { + if (!list->get_desc(&desc, n)) + break; + if (desc.global_opts) { + add_sub_group(shadow, NULL, parent_group_index, -1, + desc.global_opts); + } + if (list->use_global_options && desc.options) { + struct m_sub_options *conf = talloc_ptrtype(shadow, conf); + *conf = (struct m_sub_options){ + .prefix = desc.options_prefix, + .opts = desc.options, + .defaults = desc.priv_defaults, + .size = desc.priv_size, + }; + add_sub_group(shadow, NULL, parent_group_index, -1, conf); + } + } +} + +static void add_sub_group(struct m_config_shadow *shadow, const char *name_prefix, + int parent_group_index, int parent_ptr, + const struct m_sub_options *subopts) +{ + // Can't be used multiple times. + for (int n = 0; n < shadow->num_groups; n++) + assert(shadow->groups[n].group != subopts); + + if (!name_prefix) + name_prefix = ""; + if (subopts->prefix && subopts->prefix[0]) { + assert(!name_prefix[0]); + name_prefix = subopts->prefix; + } + + // You can only use UPDATE_ flags here. + assert(!(subopts->change_flags & ~(unsigned)UPDATE_OPTS_MASK)); + + assert(parent_group_index >= -1 && parent_group_index < shadow->num_groups); + + int group_index = shadow->num_groups++; + MP_TARRAY_GROW(shadow, shadow->groups, group_index); + shadow->groups[group_index] = (struct m_config_group){ + .group = subopts, + .parent_group = parent_group_index, + .parent_ptr = parent_ptr, + .prefix = name_prefix, + }; + + for (int i = 0; subopts->opts && subopts->opts[i].name; i++) { + const struct m_option *opt = &subopts->opts[i]; + + if (opt->type == &m_option_type_subconfig) { + const struct m_sub_options *new_subopts = opt->priv; + + // Providing default structs in-place is not allowed. + if (opt->offset >= 0 && subopts->defaults) { + void *ptr = (char *)subopts->defaults + opt->offset; + assert(!substruct_read_ptr(ptr)); + } + + const char *prefix = concat_name(shadow, name_prefix, opt->name); + add_sub_group(shadow, prefix, group_index, opt->offset, new_subopts); + + } else if (opt->type == &m_option_type_obj_settings_list) { + const struct m_obj_list *objlist = opt->priv; + init_obj_settings_list(shadow, group_index, objlist); + } + + shadow->groups[group_index].opt_count = i + 1; + } + + if (subopts->get_sub_options) { + for (int i = 0; ; i++) { + const struct m_sub_options *sub = NULL; + if (!subopts->get_sub_options(i, &sub)) + break; + if (sub) + add_sub_group(shadow, NULL, group_index, -1, sub); + } + } + + shadow->groups[group_index].group_count = shadow->num_groups - group_index; +} + +static void cache_destroy(void *p) +{ + struct m_config_cache *cache = p; + + // (technically speaking, being able to call them both without anything + // breaking is a feature provided by these functions) + m_config_cache_set_wakeup_cb(cache, NULL, NULL); + m_config_cache_set_dispatch_change_cb(cache, NULL, NULL, NULL); +} + +struct m_config_cache *m_config_cache_from_shadow(void *ta_parent, + struct m_config_shadow *shadow, + const struct m_sub_options *group) +{ + int group_index = -1; + + for (int n = 0; n < shadow->num_groups; n++) { + if (shadow->groups[n].group == group) { + group_index = n; + break; + } + } + + assert(group_index >= 0); // invalid group (or not in option tree) + + struct cache_alloc { + struct m_config_cache a; + struct config_cache b; + }; + struct cache_alloc *alloc = talloc_zero(ta_parent, struct cache_alloc); + assert((void *)&alloc->a == (void *)alloc); + struct m_config_cache *cache = &alloc->a; + talloc_set_destructor(cache, cache_destroy); + cache->internal = &alloc->b; + cache->shadow = shadow; + + struct config_cache *in = cache->internal; + in->shadow = shadow; + in->src = shadow->data; + + mp_mutex_lock(&shadow->lock); + in->data = allocate_option_data(cache, shadow, group_index, in->src); + mp_mutex_unlock(&shadow->lock); + + cache->opts = in->data->gdata[0].udata; + + in->group_start = in->data->group_index; + in->group_end = in->group_start + in->data->num_gdata; + assert(shadow->groups[in->group_start].group_count == in->data->num_gdata); + + in->upd_group = -1; + + return cache; +} + +struct m_config_cache *m_config_cache_alloc(void *ta_parent, + struct mpv_global *global, + const struct m_sub_options *group) +{ + return m_config_cache_from_shadow(ta_parent, global->config, group); +} + +static void update_next_option(struct m_config_cache *cache, void **p_opt) +{ + struct config_cache *in = cache->internal; + struct m_config_data *dst = in->data; + struct m_config_data *src = in->src; + + assert(src->group_index == 0); // must be the option root currently + + *p_opt = NULL; + + while (in->upd_group < dst->group_index + dst->num_gdata) { + struct m_group_data *gsrc = m_config_gdata(src, in->upd_group); + struct m_group_data *gdst = m_config_gdata(dst, in->upd_group); + assert(gsrc && gdst); + + if (gdst->ts < gsrc->ts) { + struct m_config_group *g = &dst->shadow->groups[in->upd_group]; + const struct m_option *opts = g->group->opts; + + while (opts && opts[in->upd_opt].name) { + const struct m_option *opt = &opts[in->upd_opt]; + + if (opt->offset >= 0 && opt->type->size) { + void *dsrc = gsrc->udata + opt->offset; + void *ddst = gdst->udata + opt->offset; + + if (!m_option_equal(opt, ddst, dsrc)) { + uint64_t ch = get_opt_change_mask(dst->shadow, + in->upd_group, dst->group_index, opt); + + if (cache->debug) { + char *vdst = m_option_print(opt, ddst); + char *vsrc = m_option_print(opt, dsrc); + mp_warn(cache->debug, "Option '%s' changed from " + "'%s' to' %s' (flags = 0x%"PRIx64")\n", + opt->name, vdst, vsrc, ch); + talloc_free(vdst); + talloc_free(vsrc); + } + + m_option_copy(opt, ddst, dsrc); + cache->change_flags |= ch; + + in->upd_opt++; // skip this next time + *p_opt = ddst; + return; + } + } + + in->upd_opt++; + } + + gdst->ts = gsrc->ts; + } + + in->upd_group++; + in->upd_opt = 0; + } + + in->upd_group = -1; +} + +static bool cache_check_update(struct m_config_cache *cache) +{ + struct config_cache *in = cache->internal; + struct m_config_shadow *shadow = in->shadow; + + // Using atomics and checking outside of the lock - it's unknown whether + // this makes it faster or slower. Just cargo culting it. + uint64_t new_ts = atomic_load(&shadow->ts); + if (in->ts >= new_ts) + return false; + + in->ts = new_ts; + in->upd_group = in->data->group_index; + in->upd_opt = 0; + return true; +} + +bool m_config_cache_update(struct m_config_cache *cache) +{ + struct config_cache *in = cache->internal; + struct m_config_shadow *shadow = in->shadow; + + if (!cache_check_update(cache)) + return false; + + mp_mutex_lock(&shadow->lock); + bool res = false; + while (1) { + void *p; + update_next_option(cache, &p); + if (!p) + break; + res = true; + } + mp_mutex_unlock(&shadow->lock); + return res; +} + +bool m_config_cache_get_next_changed(struct m_config_cache *cache, void **opt) +{ + struct config_cache *in = cache->internal; + struct m_config_shadow *shadow = in->shadow; + + *opt = NULL; + if (!cache_check_update(cache) && in->upd_group < 0) + return false; + + mp_mutex_lock(&shadow->lock); + update_next_option(cache, opt); + mp_mutex_unlock(&shadow->lock); + return !!*opt; +} + +static void find_opt(struct m_config_shadow *shadow, struct m_config_data *data, + void *ptr, int *group_idx, int *opt_idx) +{ + *group_idx = -1; + *opt_idx = -1; + + for (int n = data->group_index; n < data->group_index + data->num_gdata; n++) + { + struct m_group_data *gd = m_config_gdata(data, n); + struct m_config_group *g = &shadow->groups[n]; + const struct m_option *opts = g->group->opts; + + for (int i = 0; opts && opts[i].name; i++) { + const struct m_option *opt = &opts[i]; + + if (opt->offset >= 0 && opt->type->size && + ptr == gd->udata + opt->offset) + { + *group_idx = n; + *opt_idx = i; + return; + } + } + } +} + +bool m_config_cache_write_opt(struct m_config_cache *cache, void *ptr) +{ + struct config_cache *in = cache->internal; + struct m_config_shadow *shadow = in->shadow; + + int group_idx = -1; + int opt_idx = -1; + find_opt(shadow, in->data, ptr, &group_idx, &opt_idx); + + // ptr was not in cache->opts, or no option declaration matching it. + assert(group_idx >= 0); + + struct m_config_group *g = &shadow->groups[group_idx]; + const struct m_option *opt = &g->group->opts[opt_idx]; + + mp_mutex_lock(&shadow->lock); + + struct m_group_data *gdst = m_config_gdata(in->data, group_idx); + struct m_group_data *gsrc = m_config_gdata(in->src, group_idx); + assert(gdst && gsrc); + + bool changed = !m_option_equal(opt, gsrc->udata + opt->offset, ptr); + if (changed) { + m_option_copy(opt, gsrc->udata + opt->offset, ptr); + + gsrc->ts = atomic_fetch_add(&shadow->ts, 1) + 1; + + for (int n = 0; n < shadow->num_listeners; n++) { + struct config_cache *listener = shadow->listeners[n]; + if (listener->wakeup_cb && m_config_gdata(listener->data, group_idx)) + listener->wakeup_cb(listener->wakeup_cb_ctx); + } + } + + mp_mutex_unlock(&shadow->lock); + + return changed; +} + +void m_config_cache_set_wakeup_cb(struct m_config_cache *cache, + void (*cb)(void *ctx), void *cb_ctx) +{ + struct config_cache *in = cache->internal; + struct m_config_shadow *shadow = in->shadow; + + mp_mutex_lock(&shadow->lock); + if (in->in_list) { + for (int n = 0; n < shadow->num_listeners; n++) { + if (shadow->listeners[n] == in) { + MP_TARRAY_REMOVE_AT(shadow->listeners, shadow->num_listeners, n); + break; + } + } + for (int n = 0; n < shadow->num_listeners; n++) + assert(shadow->listeners[n] != in); // only 1 wakeup_cb per cache + // (The deinitialization path relies on this to free all memory.) + if (!shadow->num_listeners) { + talloc_free(shadow->listeners); + shadow->listeners = NULL; + } + } + if (cb) { + MP_TARRAY_APPEND(NULL, shadow->listeners, shadow->num_listeners, in); + in->in_list = true; + in->wakeup_cb = cb; + in->wakeup_cb_ctx = cb_ctx; + } + mp_mutex_unlock(&shadow->lock); +} + +static void dispatch_notify(void *p) +{ + struct config_cache *in = p; + + assert(in->wakeup_dispatch_queue); + mp_dispatch_enqueue_notify(in->wakeup_dispatch_queue, + in->wakeup_dispatch_cb, + in->wakeup_dispatch_cb_ctx); +} + +void m_config_cache_set_dispatch_change_cb(struct m_config_cache *cache, + struct mp_dispatch_queue *dispatch, + void (*cb)(void *ctx), void *cb_ctx) +{ + struct config_cache *in = cache->internal; + + // Removing the old one is tricky. First make sure no new notifications will + // come. + m_config_cache_set_wakeup_cb(cache, NULL, NULL); + // Remove any pending notifications (assume we're on the same thread as + // any potential mp_dispatch_queue_process() callers). + if (in->wakeup_dispatch_queue) { + mp_dispatch_cancel_fn(in->wakeup_dispatch_queue, + in->wakeup_dispatch_cb, + in->wakeup_dispatch_cb_ctx); + } + + in->wakeup_dispatch_queue = NULL; + in->wakeup_dispatch_cb = NULL; + in->wakeup_dispatch_cb_ctx = NULL; + + if (cb) { + in->wakeup_dispatch_queue = dispatch; + in->wakeup_dispatch_cb = cb; + in->wakeup_dispatch_cb_ctx = cb_ctx; + m_config_cache_set_wakeup_cb(cache, dispatch_notify, in); + } +} + +void *mp_get_config_group(void *ta_parent, struct mpv_global *global, + const struct m_sub_options *group) +{ + struct m_config_cache *cache = m_config_cache_alloc(NULL, global, group); + // Make talloc_free(cache->opts) free the entire cache. + ta_set_parent(cache->opts, ta_parent); + ta_set_parent(cache, cache->opts); + return cache->opts; +} + +static const struct m_config_group *find_group(struct mpv_global *global, + const struct m_option *cfg) +{ + struct m_config_shadow *shadow = global->config; + + for (int n = 0; n < shadow->num_groups; n++) { + if (shadow->groups[n].group->opts == cfg) + return &shadow->groups[n]; + } + + return NULL; +} + +void *m_config_group_from_desc(void *ta_parent, struct mp_log *log, + struct mpv_global *global, struct m_obj_desc *desc, const char *name) +{ + const struct m_config_group *group = find_group(global, desc->options); + if (group) { + return mp_get_config_group(ta_parent, global, group->group); + } else { + void *d = talloc_zero_size(ta_parent, desc->priv_size); + if (desc->priv_defaults) + memcpy(d, desc->priv_defaults, desc->priv_size); + return d; + } +} diff --git a/options/m_config_core.h b/options/m_config_core.h new file mode 100644 index 0000000..a955842 --- /dev/null +++ b/options/m_config_core.h @@ -0,0 +1,194 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_M_CONFIG_H +#define MPLAYER_M_CONFIG_H + +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> + +struct mp_dispatch_queue; +struct m_sub_options; +struct m_option_type; +struct m_option; +struct mpv_global; + +// This can be used to create and synchronize per-thread option structs, +// which then can be read without synchronization. No concurrent access to +// the cache itself is allowed. +struct m_config_cache { + // The struct as indicated by m_config_cache_alloc's group parameter. + // (Internally the same as internal->gdata[0]->udata.) + void *opts; + // Accumulated change flags. The user can set this to 0 to unset all flags. + // They are set when calling any of the update functions. A flag is only set + // once the new value is visible in ->opts. + uint64_t change_flags; + + // Set to non-NULL for logging all option changes as they are retrieved + // with one of the update functions (like m_config_cache_update()). + struct mp_log *debug; + + // Global instance of option data. Read only. + struct m_config_shadow *shadow; + + // Do not access. + struct config_cache *internal; +}; + +// Maximum possibly option name buffer length (as it appears to the user). +#define M_CONFIG_MAX_OPT_NAME_LEN 80 + +// Create a mirror copy from the global options. +// Keep in mind that a m_config_cache object is not thread-safe; it merely +// provides thread-safe access to the global options. All API functions for +// the same m_config_cache object must synchronized, unless otherwise noted. +// This does not create an initial change event (m_config_cache_update() will +// return false), but note that a change might be asynchronously signaled at any +// time. +// This simply calls m_config_cache_from_shadow(ta_parent, global->shadow, group). +// ta_parent: parent for the returned allocation +// global: option data source +// group: the option group to return +struct m_config_cache *m_config_cache_alloc(void *ta_parent, + struct mpv_global *global, + const struct m_sub_options *group); + +// If any of the options in the group possibly changes, call this callback. The +// callback must not actually access the cache or anything option related. +// Instead, it must wake up the thread that normally accesses the cache. +void m_config_cache_set_wakeup_cb(struct m_config_cache *cache, + void (*cb)(void *ctx), void *cb_ctx); + +// If any of the options in the group change, call this callback on the given +// dispatch queue. This is higher level than m_config_cache_set_wakeup_cb(), +// and you can do anything you want in the callback (assuming the dispatch +// queue is processed in the same thread that accesses m_config_cache API). +// To ensure clean shutdown, you must destroy the m_config_cache (or unset the +// callback) before the dispatch queue is destroyed. +void m_config_cache_set_dispatch_change_cb(struct m_config_cache *cache, + struct mp_dispatch_queue *dispatch, + void (*cb)(void *ctx), void *cb_ctx); + +// Update the options in cache->opts to current global values. Return whether +// there was an update notification at all (which may or may not indicate that +// some options have changed). +// Keep in mind that while the cache->opts pointer does not change, the option +// data itself will (e.g. string options might be reallocated). +// New change flags are or-ed into cache->change_flags with this call (if you +// use them, you should probably do cache->change_flags=0 before this call). +bool m_config_cache_update(struct m_config_cache *cache); + +// Check for changes and return fine grained change information. +// Warning: this conflicts with m_config_cache_update(). If you call +// m_config_cache_update(), all options will be marked as "not changed", +// and this function will return false. Also, calling this function and +// then m_config_cache_update() is not supported, and may skip updating +// some fields. +// This returns true as long as there is a changed option, and false if all +// changed options have been returned. +// If multiple options have changed, the new option value is visible only once +// this function has returned the change for it. +// out_ptr: pointer to a void*, which is set to the cache->opts field associated +// with the changed option if the function returns true; set to NULL +// if no option changed. +// returns: *out_ptr!=NULL (true if there was a changed option) +bool m_config_cache_get_next_changed(struct m_config_cache *cache, void **out_ptr); + +// Copy the option field pointed to by ptr to the global option storage. This +// is sort of similar to m_config_set_option_raw(), except doesn't require +// access to the main thread. (And you can't pass any flags.) +// You write the new value to the option struct, and then call this function +// with the pointer to it. You will not get a change notification for it (though +// you might still get a redundant wakeup callback). +// Changing the option struct and not calling this function before any update +// function (like m_config_cache_update()) will leave the value inconsistent, +// and will possibly (but not necessarily) overwrite it with the next update +// call. +// ptr: points to any field in cache->opts that is managed by an option. If +// this is not the case, the function crashes for your own good. +// returns: if true, this was an update; if false, shadow had same value +bool m_config_cache_write_opt(struct m_config_cache *cache, void *ptr); + +// Like m_config_cache_alloc(), but return the struct (m_config_cache->opts) +// directly, with no way to update the config. Basically this returns a copy +// with a snapshot of the current option values. +void *mp_get_config_group(void *ta_parent, struct mpv_global *global, + const struct m_sub_options *group); + +// Allocate a priv struct that is backed by global options (like AOs and VOs, +// anything that uses m_obj_list.use_global_options == true). +// The result contains a snapshot of the current option values of desc->options. +// For convenience, desc->options can be NULL; then priv struct is allocated +// with just zero (or priv_defaults if set). +// Bad function. +struct m_obj_desc; +void *m_config_group_from_desc(void *ta_parent, struct mp_log *log, + struct mpv_global *global, struct m_obj_desc *desc, const char *name); + +// Allocate new option shadow storage with all options set to defaults. +// root must stay valid for the lifetime of the return value. +// Result can be freed with ta_free(). +struct m_config_shadow *m_config_shadow_new(const struct m_sub_options *root); + +// See m_config_cache_alloc(). +struct m_config_cache *m_config_cache_from_shadow(void *ta_parent, + struct m_config_shadow *shadow, + const struct m_sub_options *group); + +// Iterate over all registered global options. *p_id must be set to -1 when this +// is called for the first time. Each time this call returns true, *p_id is set +// to a new valid option ID. p_id must not be changed for the next call. If +// false is returned, iteration ends. +bool m_config_shadow_get_next_opt(struct m_config_shadow *shadow, int32_t *p_id); + +// Similar to m_config_shadow_get_next_opt(), but return only options that are +// covered by the m_config_cache. +bool m_config_cache_get_next_opt(struct m_config_cache *cache, int32_t *p_id); + +// Return the m_option that was used to declare this option. +// id must be a valid option ID as returned by m_config_shadow_get_next_opt() or +// m_config_cache_get_next_opt(). +const struct m_option *m_config_shadow_get_opt(struct m_config_shadow *shadow, + int32_t id); + +// Return the full (global) option name. buf must be supplied, but may not +// always be used. It should have the size M_CONFIG_MAX_OPT_NAME_LEN. +// The returned point points either to buf or a static string. +// id must be a valid option ID as returned by m_config_shadow_get_next_opt() or +// m_config_cache_get_next_opt(). +const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow, + int32_t id, char *buf, size_t buf_size); + +// Pointer to default value, using m_option.type. NULL if option without data. +// id must be a valid option ID as returned by m_config_shadow_get_next_opt() or +// m_config_cache_get_next_opt(). +const void *m_config_shadow_get_opt_default(struct m_config_shadow *shadow, + int32_t id); + +// Return the pointer to the allocated option data (the same pointers that are +// returned by m_config_cache_get_next_changed()). NULL if option without data. +// id must be a valid option ID as returned by m_config_cache_get_next_opt(). +void *m_config_cache_get_opt_data(struct m_config_cache *cache, int32_t id); + +// Return or-ed UPDATE_OPTS_MASK part of the option and containing sub-options. +// id must be a valid option ID as returned by m_config_cache_get_next_opt(). +uint64_t m_config_cache_get_option_change_mask(struct m_config_cache *cache, + int32_t id); + +#endif /* MPLAYER_M_CONFIG_H */ diff --git a/options/m_config_frontend.c b/options/m_config_frontend.c new file mode 100644 index 0000000..9b54389 --- /dev/null +++ b/options/m_config_frontend.c @@ -0,0 +1,1080 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <errno.h> +#include <float.h> +#include <stdatomic.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +#include "libmpv/client.h" + +#include "common/common.h" +#include "common/global.h" +#include "common/msg_control.h" +#include "common/msg.h" +#include "m_config_frontend.h" +#include "m_config.h" +#include "misc/dispatch.h" +#include "misc/node.h" +#include "options/m_option.h" +#include "osdep/threads.h" + +extern const char mp_help_text[]; + +// Profiles allow to predefine some sets of options that can then +// be applied later on with the internal -profile option. +#define MAX_PROFILE_DEPTH 20 +// Maximal include depth. +#define MAX_RECURSION_DEPTH 8 + +struct m_profile { + struct m_profile *next; + char *name; + char *desc; + char *cond; + int restore_mode; + int num_opts; + // Option/value pair array. + // name,value = opts[n*2+0],opts[n*2+1] + char **opts; + // For profile restoring. + struct m_opt_backup *backups; +}; + +// In the file local case, this contains the old global value. +// It's also used for profile restoring. +struct m_opt_backup { + struct m_opt_backup *next; + struct m_config_option *co; + int flags; + void *backup, *nval; +}; + +static const struct m_option profile_restore_mode_opt = { + .name = "profile-restore", + .type = &m_option_type_choice, + M_CHOICES({"default", 0}, {"copy", 1}, {"copy-equal", 2}), +}; + +static void list_profiles(struct m_config *config) +{ + MP_INFO(config, "Available profiles:\n"); + for (struct m_profile *p = config->profiles; p; p = p->next) + MP_INFO(config, "\t%s\t%s\n", p->name, p->desc ? p->desc : ""); + MP_INFO(config, "\n"); +} + +static int show_profile(struct m_config *config, bstr param) +{ + struct m_profile *p; + if (!param.len) { + list_profiles(config); + return M_OPT_EXIT; + } + if (!(p = m_config_get_profile(config, param))) { + MP_ERR(config, "Unknown profile '%.*s'.\n", BSTR_P(param)); + return M_OPT_EXIT; + } + if (!config->profile_depth) + 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]); + + if (config->profile_depth < MAX_PROFILE_DEPTH + && !strcmp(p->opts[2*i], "profile")) { + char *e, *list = p->opts[2 * i + 1]; + while ((e = strchr(list, ','))) { + int l = e - list; + if (!l) + continue; + show_profile(config, (bstr){list, e - list}); + list = e + 1; + } + if (list[0] != '\0') + show_profile(config, bstr0(list)); + } + } + config->profile_depth--; + if (!config->profile_depth) + MP_INFO(config, "\n"); + return M_OPT_EXIT; +} + +static struct m_config *m_config_from_obj_desc(void *talloc_ctx, + struct mp_log *log, + struct mpv_global *global, + struct m_obj_desc *desc) +{ + struct m_sub_options *root = talloc_ptrtype(NULL, root); + *root = (struct m_sub_options){ + .opts = desc->options, + // (global == NULL got repurposed to mean "no alloc") + .size = global ? desc->priv_size : 0, + .defaults = desc->priv_defaults, + }; + + struct m_config *c = m_config_new(talloc_ctx, log, root); + talloc_steal(c, root); + c->global = global; + return c; +} + +struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx, + struct mp_log *log, + struct m_obj_desc *desc) +{ + return m_config_from_obj_desc(talloc_ctx, log, NULL, desc); +} + +static int m_config_set_obj_params(struct m_config *config, struct mp_log *log, + struct mpv_global *global, + struct m_obj_desc *desc, char **args) +{ + for (int n = 0; args && args[n * 2 + 0]; n++) { + bstr opt = bstr0(args[n * 2 + 0]); + bstr val = bstr0(args[n * 2 + 1]); + if (m_config_set_option_cli(config, opt, val, 0) < 0) + return -1; + } + + return 0; +} + +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, + char **args) +{ + struct m_config *config = m_config_from_obj_desc(ta_parent, log, global, desc); + if (m_config_set_obj_params(config, log, global, desc, args) < 0) + goto error; + + return config; +error: + talloc_free(config); + return NULL; +} + +static void backup_dtor(void *p) +{ + struct m_opt_backup *bc = p; + m_option_free(bc->co->opt, bc->backup); + if (bc->nval) + m_option_free(bc->co->opt, bc->nval); +} + +#define BACKUP_LOCAL 1 +#define BACKUP_NVAL 2 +static void ensure_backup(struct m_opt_backup **list, int flags, + struct m_config_option *co) +{ + if (!co->data) + return; + for (struct m_opt_backup *cur = *list; cur; cur = cur->next) { + if (cur->co->data == co->data) // comparing data ptr catches aliases + return; + } + struct m_opt_backup *bc = talloc_ptrtype(NULL, bc); + talloc_set_destructor(bc, backup_dtor); + *bc = (struct m_opt_backup) { + .co = co, + .backup = talloc_zero_size(bc, co->opt->type->size), + .nval = flags & BACKUP_NVAL + ? talloc_zero_size(bc, co->opt->type->size) : NULL, + .flags = flags, + }; + m_option_copy(co->opt, bc->backup, co->data); + bc->next = *list; + *list = bc; + if (bc->flags & BACKUP_LOCAL) + co->is_set_locally = true; +} + +static void restore_backups(struct m_opt_backup **list, struct m_config *config) +{ + while (*list) { + struct m_opt_backup *bc = *list; + *list = bc->next; + + if (!bc->nval || m_option_equal(bc->co->opt, bc->co->data, bc->nval)) + m_config_set_option_raw(config, bc->co, bc->backup, 0); + + if (bc->flags & BACKUP_LOCAL) + bc->co->is_set_locally = false; + talloc_free(bc); + } +} + +void m_config_restore_backups(struct m_config *config) +{ + restore_backups(&config->backup_opts, config); +} + +bool m_config_watch_later_backup_opt_changed(struct m_config *config, + char *opt_name) +{ + struct m_config_option *co = m_config_get_co(config, bstr0(opt_name)); + if (!co) { + MP_ERR(config, "Option %s not found.\n", opt_name); + return false; + } + + for (struct m_opt_backup *bc = config->watch_later_backup_opts; bc; + bc = bc->next) { + if (strcmp(bc->co->name, co->name) == 0) { + struct m_config_option *bc_co = (struct m_config_option *)bc->backup; + return !m_option_equal(co->opt, co->data, bc_co); + } + } + + return false; +} + +void m_config_backup_opt(struct m_config *config, const char *opt) +{ + struct m_config_option *co = m_config_get_co(config, bstr0(opt)); + if (co) { + ensure_backup(&config->backup_opts, BACKUP_LOCAL, co); + } else { + MP_ERR(config, "Option %s not found.\n", opt); + } +} + +void m_config_backup_all_opts(struct m_config *config) +{ + for (int n = 0; n < config->num_opts; n++) + ensure_backup(&config->backup_opts, BACKUP_LOCAL, &config->opts[n]); +} + +void m_config_backup_watch_later_opts(struct m_config *config) +{ + for (int n = 0; n < config->num_opts; n++) + ensure_backup(&config->watch_later_backup_opts, 0, &config->opts[n]); +} + +struct m_config_option *m_config_get_co_raw(const struct m_config *config, + struct bstr name) +{ + if (!name.len) + return NULL; + + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co = &config->opts[n]; + struct bstr coname = bstr0(co->name); + if (bstrcmp(coname, name) == 0) + return co; + } + + return NULL; +} + +// Like m_config_get_co_raw(), but resolve aliases. +static struct m_config_option *m_config_get_co_any(const struct m_config *config, + struct bstr name) +{ + struct m_config_option *co = m_config_get_co_raw(config, name); + if (!co) + return NULL; + + const char *prefix = config->is_toplevel ? "--" : ""; + if (co->opt->type == &m_option_type_alias) { + const char *alias = (const char *)co->opt->priv; + if (co->opt->deprecation_message && !co->warning_was_printed) { + if (co->opt->deprecation_message[0]) { + MP_WARN(config, "Warning: option %s%s was replaced with " + "%s%s: %s\n", prefix, co->name, prefix, alias, + co->opt->deprecation_message); + } else { + MP_WARN(config, "Warning: option %s%s was replaced with " + "%s%s and might be removed in the future.\n", + prefix, co->name, prefix, alias); + } + co->warning_was_printed = true; + } + return m_config_get_co_any(config, bstr0(alias)); + } else if (co->opt->type == &m_option_type_removed) { + if (!co->warning_was_printed) { + char *msg = co->opt->priv; + if (msg) { + MP_FATAL(config, "Option %s%s was removed: %s\n", + prefix, co->name, msg); + } else { + MP_FATAL(config, "Option %s%s was removed.\n", + prefix, co->name); + } + co->warning_was_printed = true; + } + return NULL; + } else if (co->opt->deprecation_message) { + if (!co->warning_was_printed) { + MP_WARN(config, "Warning: option %s%s is deprecated " + "and might be removed in the future (%s).\n", + prefix, co->name, co->opt->deprecation_message); + co->warning_was_printed = true; + } + } + return co; +} + +struct m_config_option *m_config_get_co(const struct m_config *config, + struct bstr name) +{ + struct m_config_option *co = m_config_get_co_any(config, name); + // CLI aliases should not be real options, and are explicitly handled by + // m_config_set_option_cli(). So pretend it does not exist. + if (co && co->opt->type == &m_option_type_cli_alias) + co = NULL; + return co; +} + +int m_config_get_co_count(struct m_config *config) +{ + return config->num_opts; +} + +struct m_config_option *m_config_get_co_index(struct m_config *config, int index) +{ + return &config->opts[index]; +} + +const void *m_config_get_co_default(const struct m_config *config, + struct m_config_option *co) +{ + return m_config_shadow_get_opt_default(config->shadow, co->opt_id); +} + +const char *m_config_get_positional_option(const struct m_config *config, int p) +{ + int pos = 0; + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co = &config->opts[n]; + if (!co->opt->deprecation_message) { + if (pos == p) + return co->name; + pos++; + } + } + return NULL; +} + +// return: <0: M_OPT_ error, 0: skip, 1: check, 2: set +static int handle_set_opt_flags(struct m_config *config, + struct m_config_option *co, int flags) +{ + int optflags = co->opt->flags; + bool set = !(flags & M_SETOPT_CHECK_ONLY); + + if ((flags & M_SETOPT_PRE_PARSE_ONLY) && !(optflags & M_OPT_PRE_PARSE)) + return 0; + + if ((flags & M_SETOPT_PRESERVE_CMDLINE) && co->is_set_from_cmdline) + set = false; + + if ((flags & M_SETOPT_NO_OVERWRITE) && + (co->is_set_from_cmdline || co->is_set_from_config)) + set = false; + + if ((flags & M_SETOPT_NO_PRE_PARSE) && (optflags & M_OPT_PRE_PARSE)) + return M_OPT_INVALID; + + // Check if this option isn't forbidden in the current mode + if ((flags & M_SETOPT_FROM_CONFIG_FILE) && (optflags & M_OPT_NOCFG)) { + MP_ERR(config, "The %s option can't be used in a config file.\n", + co->name); + return M_OPT_INVALID; + } + if ((flags & M_SETOPT_BACKUP) && set) + ensure_backup(&config->backup_opts, BACKUP_LOCAL, co); + + return set ? 2 : 1; +} + +void m_config_mark_co_flags(struct m_config_option *co, int flags) +{ + if (flags & M_SETOPT_FROM_CMDLINE) + co->is_set_from_cmdline = true; + + if (flags & M_SETOPT_FROM_CONFIG_FILE) + co->is_set_from_config = true; +} + +// Special options that don't really fit into the option handling model. They +// usually store no data, but trigger actions. Caller is assumed to have called +// handle_set_opt_flags() to make sure the option can be set. +// Returns M_OPT_UNKNOWN if the option is not a special option. +static int m_config_handle_special_options(struct m_config *config, + struct m_config_option *co, + void *data, int flags) +{ + if (config->use_profiles && strcmp(co->name, "profile") == 0) { + char **list = *(char ***)data; + + if (list && list[0] && !list[1] && strcmp(list[0], "help") == 0) { + if (!config->profiles) { + MP_INFO(config, "No profiles have been defined.\n"); + return M_OPT_EXIT; + } + list_profiles(config); + return M_OPT_EXIT; + } + + for (int n = 0; list && list[n]; n++) { + int r = m_config_set_profile(config, list[n], flags); + if (r < 0) + return r; + } + return 0; + } + + if (config->includefunc && strcmp(co->name, "include") == 0) { + char *param = *(char **)data; + if (!param || !param[0]) + return M_OPT_MISSING_PARAM; + if (config->recursion_depth >= MAX_RECURSION_DEPTH) { + MP_ERR(config, "Maximum 'include' nesting depth exceeded.\n"); + return M_OPT_INVALID; + } + config->recursion_depth += 1; + config->includefunc(config->includefunc_ctx, param, flags); + config->recursion_depth -= 1; + if (config->recursion_depth == 0 && config->profile_depth == 0) + m_config_finish_default_profile(config, flags); + return 1; + } + + if (config->use_profiles && strcmp(co->name, "show-profile") == 0) + return show_profile(config, bstr0(*(char **)data)); + + if (config->is_toplevel && (strcmp(co->name, "h") == 0 || + strcmp(co->name, "help") == 0)) + { + char *h = *(char **)data; + mp_info(config->log, "%s", mp_help_text); + if (h && h[0]) + m_config_print_option_list(config, h); + return M_OPT_EXIT; + } + + if (strcmp(co->name, "list-options") == 0) { + m_config_print_option_list(config, "*"); + return M_OPT_EXIT; + } + + return M_OPT_UNKNOWN; +} + +// This notification happens when anyone other than m_config->cache (i.e. not +// through m_config_set_option_raw() or related) changes any options. +static void async_change_cb(void *p) +{ + struct m_config *config = p; + + void *ptr; + while (m_config_cache_get_next_changed(config->cache, &ptr)) { + // Regrettable linear search, might degenerate to quadratic. + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co = &config->opts[n]; + if (co->data == ptr) { + if (config->option_change_callback) { + config->option_change_callback( + config->option_change_callback_ctx, co, + config->cache->change_flags, false); + } + break; + } + } + config->cache->change_flags = 0; + } +} + +void m_config_set_update_dispatch_queue(struct m_config *config, + struct mp_dispatch_queue *dispatch) +{ + m_config_cache_set_dispatch_change_cb(config->cache, dispatch, + async_change_cb, config); +} + +static void config_destroy(void *p) +{ + struct m_config *config = p; + config->option_change_callback = NULL; + m_config_restore_backups(config); + + struct m_opt_backup **list = &config->watch_later_backup_opts; + while (*list) { + struct m_opt_backup *bc = *list; + *list = bc->next; + talloc_free(bc); + } + + talloc_free(config->cache); + talloc_free(config->shadow); +} + +struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log, + const struct m_sub_options *root) +{ + struct m_config *config = talloc(talloc_ctx, struct m_config); + talloc_set_destructor(config, config_destroy); + *config = (struct m_config){.log = log,}; + + config->shadow = m_config_shadow_new(root); + + if (root->size) { + config->cache = m_config_cache_from_shadow(config, config->shadow, root); + config->optstruct = config->cache->opts; + } + + int32_t optid = -1; + while (m_config_shadow_get_next_opt(config->shadow, &optid)) { + char buf[M_CONFIG_MAX_OPT_NAME_LEN]; + const char *opt_name = + m_config_shadow_get_opt_name(config->shadow, optid, buf, sizeof(buf)); + + struct m_config_option co = { + .name = talloc_strdup(config, opt_name), + .opt = m_config_shadow_get_opt(config->shadow, optid), + .opt_id = optid, + }; + + if (config->cache) + co.data = m_config_cache_get_opt_data(config->cache, optid); + + MP_TARRAY_APPEND(config, config->opts, config->num_opts, co); + } + + return config; +} + +// Normally m_config_cache will not send notifications when _we_ change our +// own stuff. For whatever funny reasons, we need that, though. +static void force_self_notify_change_opt(struct m_config *config, + struct m_config_option *co, + bool self_notification) +{ + int changed = + m_config_cache_get_option_change_mask(config->cache, co->opt_id); + + if (config->option_change_callback) { + config->option_change_callback(config->option_change_callback_ctx, co, + changed, self_notification); + } +} + +static void notify_opt(struct m_config *config, void *ptr, bool self_notification) +{ + for (int n = 0; n < config->num_opts; n++) { + struct m_config_option *co = &config->opts[n]; + if (co->data == ptr) { + if (m_config_cache_write_opt(config->cache, co->data)) + force_self_notify_change_opt(config, co, self_notification); + return; + } + } + // ptr doesn't point to any config->optstruct field declared in the + // option list? + assert(false); +} + +void m_config_notify_change_opt_ptr(struct m_config *config, void *ptr) +{ + notify_opt(config, ptr, true); +} + +void m_config_notify_change_opt_ptr_notify(struct m_config *config, void *ptr) +{ + // (the notify bool is inverted: by not marking it as self-notification, + // the mpctx option change handler actually applies it) + notify_opt(config, ptr, false); +} + +int m_config_set_option_raw(struct m_config *config, + struct m_config_option *co, + void *data, int flags) +{ + if (!co) + return M_OPT_UNKNOWN; + + int r = handle_set_opt_flags(config, co, flags); + if (r <= 1) + return r; + + r = m_config_handle_special_options(config, co, data, flags); + if (r != M_OPT_UNKNOWN) + return r; + + // This affects some special options like "playlist", "v". Maybe these + // should work, or maybe not. For now they would require special code. + if (!co->data) + return flags & M_SETOPT_FROM_CMDLINE ? 0 : M_OPT_UNKNOWN; + + if (config->profile_backup_tmp) + ensure_backup(config->profile_backup_tmp, config->profile_backup_flags, co); + + m_config_mark_co_flags(co, flags); + + m_option_copy(co->opt, co->data, data); + if (m_config_cache_write_opt(config->cache, co->data)) + force_self_notify_change_opt(config, co, false); + + return 0; +} + +// Handle CLI exceptions to option handling. +// Used to turn "--no-foo" into "--foo=no". +// It also handles looking up "--vf-add" as "--vf". +static struct m_config_option *m_config_mogrify_cli_opt(struct m_config *config, + struct bstr *name, + bool *out_negate, + int *out_add_flags) +{ + *out_negate = false; + *out_add_flags = 0; + + struct m_config_option *co = m_config_get_co(config, *name); + if (co) + return co; + + // Turn "--no-foo" into "foo" + set *out_negate. + bstr no_name = *name; + if (!co && bstr_eatstart0(&no_name, "no-")) { + co = m_config_get_co(config, no_name); + + // Not all choice types have this value - if they don't, then parsing + // them will simply result in an error. Good enough. + if (!co || !(co->opt->type->flags & M_OPT_TYPE_CHOICE)) + return NULL; + + *name = no_name; + *out_negate = true; + return co; + } + + // Resolve CLI alias. (We don't allow you to combine them with "--no-".) + co = m_config_get_co_any(config, *name); + if (co && co->opt->type == &m_option_type_cli_alias) + *name = bstr0((char *)co->opt->priv); + + // Might be a suffix "action", like "--vf-add". Expensively check for + // matches. (We don't allow you to combine them with "--no-".) + for (int n = 0; n < config->num_opts; n++) { + co = &config->opts[n]; + struct bstr basename = bstr0(co->name); + + if (!bstr_startswith(*name, basename)) + continue; + + // Aliased option + a suffix action, e.g. --opengl-shaders-append + if (co->opt->type == &m_option_type_alias) + co = m_config_get_co_any(config, basename); + if (!co) + continue; + + const struct m_option_type *type = co->opt->type; + for (int i = 0; type->actions && type->actions[i].name; i++) { + const struct m_option_action *action = &type->actions[i]; + bstr suffix = bstr0(action->name); + + if (bstr_endswith(*name, suffix) && + (name->len == basename.len + 1 + suffix.len) && + name->start[basename.len] == '-') + { + *out_add_flags = action->flags; + return co; + } + } + } + + return NULL; +} + +int m_config_set_option_cli(struct m_config *config, struct bstr name, + struct bstr param, int flags) +{ + int r; + assert(config != NULL); + + bool negate; + struct m_config_option *co = + m_config_mogrify_cli_opt(config, &name, &negate, &(int){0}); + + if (!co) { + r = M_OPT_UNKNOWN; + goto done; + } + + if (negate) { + if (param.len) { + r = M_OPT_DISALLOW_PARAM; + goto done; + } + + param = bstr0("no"); + } + + // This is the only mandatory function + assert(co->opt->type->parse); + + r = handle_set_opt_flags(config, co, flags); + if (r <= 0) + goto done; + + if (r == 2) { + MP_VERBOSE(config, "Setting option '%.*s' = '%.*s' (flags = %d)\n", + BSTR_P(name), BSTR_P(param), flags); + } + + union m_option_value val = m_option_value_default; + + // Some option types are "impure" and work on the existing data. + // (Prime examples: --vf-add, --sub-file) + if (co->data) + m_option_copy(co->opt, &val, co->data); + + r = m_option_parse(config->log, co->opt, name, param, &val); + + if (r >= 0) + r = m_config_set_option_raw(config, co, &val, flags); + + m_option_free(co->opt, &val); + +done: + if (r < 0 && r != M_OPT_EXIT) { + MP_ERR(config, "Error parsing option %.*s (%s)\n", + BSTR_P(name), m_option_strerror(r)); + r = M_OPT_INVALID; + } + return r; +} + +int m_config_set_option_node(struct m_config *config, bstr name, + struct mpv_node *data, int flags) +{ + int r; + + struct m_config_option *co = m_config_get_co(config, name); + if (!co) + return M_OPT_UNKNOWN; + + // Do this on an "empty" type to make setting the option strictly overwrite + // the old value, as opposed to e.g. appending to lists. + union m_option_value val = m_option_value_default; + + if (data->format == MPV_FORMAT_STRING) { + bstr param = bstr0(data->u.string); + r = m_option_parse(mp_null_log, co->opt, name, param, &val); + } else { + r = m_option_set_node(co->opt, &val, data); + } + + if (r >= 0) + r = m_config_set_option_raw(config, co, &val, flags); + + if (mp_msg_test(config->log, MSGL_V)) { + char *s = m_option_type_node.print(NULL, data); + MP_DBG(config, "Setting option '%.*s' = %s (flags = %d) -> %d\n", + BSTR_P(name), s ? s : "?", flags, r); + talloc_free(s); + } + + m_option_free(co->opt, &val); + return r; +} + +int m_config_option_requires_param(struct m_config *config, bstr name) +{ + bool negate; + int flags; + struct m_config_option *co = + m_config_mogrify_cli_opt(config, &name, &negate, &flags); + + if (!co) + return M_OPT_UNKNOWN; + + if (negate || (flags & M_OPT_TYPE_OPTIONAL_PARAM)) + return 0; + + return m_option_required_params(co->opt); +} + +static int sort_opt_compare(const void *pa, const void *pb) +{ + const struct m_config_option *a = pa; + const struct m_config_option *b = pb; + return strcasecmp(a->name, b->name); +} + +void m_config_print_option_list(const struct m_config *config, const char *name) +{ + char min[50], max[50]; + int count = 0; + const char *prefix = config->is_toplevel ? "--" : ""; + + struct m_config_option *sorted = + talloc_memdup(NULL, config->opts, config->num_opts * sizeof(sorted[0])); + if (config->is_toplevel) + qsort(sorted, config->num_opts, sizeof(sorted[0]), sort_opt_compare); + + MP_INFO(config, "Options:\n\n"); + for (int i = 0; i < config->num_opts; i++) { + struct m_config_option *co = &sorted[i]; + const struct m_option *opt = co->opt; + if (strcmp(name, "*") != 0 && !strstr(co->name, name)) + continue; + MP_INFO(config, " %s%-30s", prefix, co->name); + if (opt->type == &m_option_type_choice) { + MP_INFO(config, " Choices:"); + const struct m_opt_choice_alternatives *alt = opt->priv; + for (int n = 0; alt[n].name; n++) + MP_INFO(config, " %s", alt[n].name); + if (opt->min < opt->max) + MP_INFO(config, " (or an integer)"); + } else { + MP_INFO(config, " %s", opt->type->name); + } + if ((opt->type->flags & M_OPT_TYPE_USES_RANGE) && opt->min < opt->max) { + snprintf(min, sizeof(min), "any"); + snprintf(max, sizeof(max), "any"); + if (opt->min != DBL_MIN) + snprintf(min, sizeof(min), "%.14g", opt->min); + if (opt->max != DBL_MAX) + snprintf(max, sizeof(max), "%.14g", opt->max); + MP_INFO(config, " (%s to %s)", min, max); + } + char *def = NULL; + const void *defptr = m_config_get_co_default(config, co); + if (!defptr) + defptr = &m_option_value_default; + if (defptr) + def = m_option_pretty_print(opt, defptr); + if (def) { + MP_INFO(config, " (default: %s)", def); + talloc_free(def); + } + if (opt->flags & M_OPT_NOCFG) + MP_INFO(config, " [not in config files]"); + if (opt->flags & M_OPT_FILE) + MP_INFO(config, " [file]"); + if (opt->deprecation_message) + MP_INFO(config, " [deprecated]"); + if (opt->type == &m_option_type_alias) + MP_INFO(config, " for %s", (char *)opt->priv); + if (opt->type == &m_option_type_cli_alias) + MP_INFO(config, " for --%s (CLI/config files only)", (char *)opt->priv); + MP_INFO(config, "\n"); + for (int n = 0; opt->type->actions && opt->type->actions[n].name; n++) { + const struct m_option_action *action = &opt->type->actions[n]; + MP_INFO(config, " %s%s-%s\n", prefix, co->name, action->name); + count++; + } + count++; + } + MP_INFO(config, "\nTotal: %d options\n", count); + talloc_free(sorted); +} + +char **m_config_list_options(void *ta_parent, const struct m_config *config) +{ + char **list = talloc_new(ta_parent); + int count = 0; + for (int i = 0; i < config->num_opts; i++) { + struct m_config_option *co = &config->opts[i]; + // For use with CONF_TYPE_STRING_LIST, it's important not to set list + // as allocation parent. + char *s = talloc_strdup(ta_parent, co->name); + MP_TARRAY_APPEND(ta_parent, list, count, s); + } + MP_TARRAY_APPEND(ta_parent, list, count, NULL); + return list; +} + +struct m_profile *m_config_get_profile(const struct m_config *config, bstr name) +{ + for (struct m_profile *p = config->profiles; p; p = p->next) { + if (bstr_equals0(name, p->name)) + return p; + } + return NULL; +} + +struct m_profile *m_config_get_profile0(const struct m_config *config, + char *name) +{ + return m_config_get_profile(config, bstr0(name)); +} + +struct m_profile *m_config_add_profile(struct m_config *config, char *name) +{ + if (!name || !name[0]) + name = "default"; + struct m_profile *p = m_config_get_profile0(config, name); + if (p) + return p; + p = talloc_zero(config, struct m_profile); + p->name = talloc_strdup(p, name); + p->next = config->profiles; + config->profiles = p; + return p; +} + +int m_config_set_profile_option(struct m_config *config, struct m_profile *p, + bstr name, bstr val) +{ + if (bstr_equals0(name, "profile-desc")) { + talloc_free(p->desc); + p->desc = bstrto0(p, val); + return 0; + } + if (bstr_equals0(name, "profile-cond")) { + TA_FREEP(&p->cond); + val = bstr_strip(val); + if (val.len) + p->cond = bstrto0(p, val); + return 0; + } + if (bstr_equals0(name, profile_restore_mode_opt.name)) { + return m_option_parse(config->log, &profile_restore_mode_opt, name, val, + &p->restore_mode); + } + + int i = m_config_set_option_cli(config, name, val, + M_SETOPT_CHECK_ONLY | + M_SETOPT_FROM_CONFIG_FILE); + if (i < 0) + return i; + p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2)); + p->opts[p->num_opts * 2] = bstrto0(p, name); + p->opts[p->num_opts * 2 + 1] = bstrto0(p, val); + p->num_opts++; + p->opts[p->num_opts * 2] = p->opts[p->num_opts * 2 + 1] = NULL; + return 1; +} + +static struct m_profile *find_check_profile(struct m_config *config, char *name) +{ + struct m_profile *p = m_config_get_profile0(config, name); + if (!p) { + MP_WARN(config, "Unknown profile '%s'.\n", name); + return NULL; + } + if (config->profile_depth > MAX_PROFILE_DEPTH) { + MP_WARN(config, "WARNING: Profile inclusion too deep.\n"); + return NULL; + } + return p; +} + +int m_config_set_profile(struct m_config *config, char *name, int flags) +{ + MP_VERBOSE(config, "Applying profile '%s'...\n", name); + struct m_profile *p = find_check_profile(config, name); + if (!p) + return M_OPT_INVALID; + + if (!config->profile_backup_tmp && p->restore_mode) { + config->profile_backup_tmp = &p->backups; + config->profile_backup_flags = p->restore_mode == 2 ? BACKUP_NVAL : 0; + } + + config->profile_depth++; + for (int i = 0; i < p->num_opts; i++) { + m_config_set_option_cli(config, + bstr0(p->opts[2 * i]), + bstr0(p->opts[2 * i + 1]), + flags | M_SETOPT_FROM_CONFIG_FILE); + } + config->profile_depth--; + + if (config->profile_backup_tmp == &p->backups) { + config->profile_backup_tmp = NULL; + + for (struct m_opt_backup *bc = p->backups; bc; bc = bc->next) { + if (bc && bc->nval) + m_option_copy(bc->co->opt, bc->nval, bc->co->data); + talloc_steal(p, bc); + } + } + + return 0; +} + +int m_config_restore_profile(struct m_config *config, char *name) +{ + MP_VERBOSE(config, "Restoring from profile '%s'...\n", name); + struct m_profile *p = find_check_profile(config, name); + if (!p) + return M_OPT_INVALID; + + if (!p->backups) + MP_WARN(config, "Profile '%s' contains no restore data.\n", name); + + restore_backups(&p->backups, config); + + return 0; +} + +void m_config_finish_default_profile(struct m_config *config, int flags) +{ + struct m_profile *p = m_config_add_profile(config, NULL); + m_config_set_profile(config, p->name, flags); + p->num_opts = 0; +} + +struct mpv_node m_config_get_profiles(struct m_config *config) +{ + struct mpv_node root; + node_init(&root, MPV_FORMAT_NODE_ARRAY, NULL); + + for (m_profile_t *profile = config->profiles; profile; profile = profile->next) + { + struct mpv_node *entry = node_array_add(&root, MPV_FORMAT_NODE_MAP); + + 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); + if (profile->restore_mode) { + char *s = + m_option_print(&profile_restore_mode_opt, &profile->restore_mode); + node_map_add_string(entry, profile_restore_mode_opt.name, s); + talloc_free(s); + } + + struct mpv_node *opts = + node_map_add(entry, "options", MPV_FORMAT_NODE_ARRAY); + + for (int n = 0; n < profile->num_opts; n++) { + struct mpv_node *opt_entry = node_array_add(opts, MPV_FORMAT_NODE_MAP); + node_map_add_string(opt_entry, "key", profile->opts[n * 2 + 0]); + node_map_add_string(opt_entry, "value", profile->opts[n * 2 + 1]); + } + } + + return root; +} diff --git a/options/m_config_frontend.h b/options/m_config_frontend.h new file mode 100644 index 0000000..6108d9f --- /dev/null +++ b/options/m_config_frontend.h @@ -0,0 +1,266 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include <stdatomic.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#include "common/common.h" +#include "common/global.h" +#include "common/msg.h" +#include "common/msg_control.h" +#include "m_config_core.h" +#include "misc/bstr.h" +#include "misc/dispatch.h" +#include "options/m_option.h" + +// m_config provides an API to manipulate the config variables in MPlayer. +// It makes use of the Options API to provide a context stack that +// allows saving and later restoring the state of all variables. + +typedef struct m_profile m_profile_t; +struct m_option; +struct m_option_type; +struct m_sub_options; +struct m_obj_desc; +struct m_obj_settings; +struct mp_log; +struct mp_dispatch_queue; + +// Config option +struct m_config_option { + bool is_set_from_cmdline : 1; // Set by user from command line + bool is_set_from_config : 1; // Set by a config file + bool is_set_locally : 1; // Has a backup entry + bool warning_was_printed : 1; + int32_t opt_id; // For some m_config APIs + const char *name; // Full name (ie option-subopt) + const struct m_option *opt; // Option description + void *data; // Raw value of the option +}; + +// Config object +/** \ingroup Config */ +typedef struct m_config { + struct mp_log *log; + struct mpv_global *global; // can be NULL + + // Registered options. + struct m_config_option *opts; // all options, even suboptions + int num_opts; + + // List of defined profiles. + struct m_profile *profiles; + // Depth when recursively including profiles. + int profile_depth; + // Temporary during profile application. + struct m_opt_backup **profile_backup_tmp; + int profile_backup_flags; + + struct m_opt_backup *backup_opts; + struct m_opt_backup *watch_later_backup_opts; + + bool use_profiles; + bool is_toplevel; + int (*includefunc)(void *ctx, char *filename, int flags); + void *includefunc_ctx; + + // Notification after an option was successfully written to. + // Uses flags as set in UPDATE_OPTS_MASK. + // self_update==true means the update was caused by a call to + // m_config_notify_change_opt_ptr(). If false, it's caused either by + // m_config_set_option_*() (and similar) calls or external updates. + void (*option_change_callback)(void *ctx, struct m_config_option *co, + int flags, bool self_update); + void *option_change_callback_ctx; + + // For the command line parser + int recursion_depth; + + void *optstruct; // struct mpopts or other + + // Private. Non-NULL if data was allocated. m_config_option.data uses it. + // API users call m_config_set_update_dispatch_queue() to get async updates. + struct m_config_cache *cache; + + // Private. Thread-safe shadow memory; only set for the main m_config. + struct m_config_shadow *shadow; +} m_config_t; + +// Create a new config object. +// talloc_ctx: talloc parent context for the m_config allocation +// root: description of all options +// Note that the m_config object will keep pointers to root and log. +struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log, + const struct m_sub_options *root); + +// Create a m_config for the given desc. This is for --af/--vf, which have +// different sub-options for every filter (represented by separate desc +// structs). +// args is an array of key/value pairs (args=[k0, v0, k1, v1, ..., NULL]). +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, + char **args); + +// Like m_config_from_obj_desc_and_args(), but don't allocate option the +// struct, i.e. m_config.optstruct==NULL. This is used by the sub-option +// parser (--af/--vf, to a lesser degree --ao/--vo) to check sub-option names +// and types. +struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx, + struct mp_log *log, + struct m_obj_desc *desc); + +// 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(). +void m_config_backup_opt(struct m_config *config, const char *opt); + +// Call m_config_backup_opt() on all options. +void m_config_backup_all_opts(struct m_config *config); + +// Backup options on startup so that quit-watch-later can compare the current +// values to their backups, and save them only if they have been changed. +void m_config_backup_watch_later_opts(struct m_config *config); + +// Restore all options backed up with m_config_backup_opt(), and delete the +// backups afterwards. +void m_config_restore_backups(struct m_config *config); + +// Whether opt_name is different from its initial value. +bool m_config_watch_later_backup_opt_changed(struct m_config *config, + char *opt_name); + +enum { + M_SETOPT_PRE_PARSE_ONLY = 1, // Silently ignore non-M_OPT_PRE_PARSE opt. + M_SETOPT_CHECK_ONLY = 2, // Don't set, just check name/value + M_SETOPT_FROM_CONFIG_FILE = 4, // Reject M_OPT_NOCFG opt. (print error) + M_SETOPT_FROM_CMDLINE = 8, // Mark as set by command line + M_SETOPT_BACKUP = 16, // Call m_config_backup_opt() before + M_SETOPT_PRESERVE_CMDLINE = 32, // Don't set if already marked as FROM_CMDLINE + M_SETOPT_NO_PRE_PARSE = 128, // Reject M_OPT_PREPARSE options + M_SETOPT_NO_OVERWRITE = 256, // Skip options marked with FROM_* +}; + +// Set the named option to the given string. This is for command line and config +// file use only. +// flags: combination of M_SETOPT_* flags (0 for normal operation) +// Returns >= 0 on success, otherwise see OptionParserReturn. +int m_config_set_option_cli(struct m_config *config, struct bstr name, + struct bstr param, int flags); + +// Similar to m_config_set_option_cli(), but set as data in its native format. +// This takes care of some details like sending change notifications. +// The type data points to is as in: co->opt +int m_config_set_option_raw(struct m_config *config, struct m_config_option *co, + void *data, int flags); + +void m_config_mark_co_flags(struct m_config_option *co, int flags); + +// Convert the mpv_node to raw option data, then call m_config_set_option_raw(). +struct mpv_node; +int m_config_set_option_node(struct m_config *config, bstr name, + struct mpv_node *data, int flags); + +// Return option descriptor. You shouldn't use this. +struct m_config_option *m_config_get_co(const struct m_config *config, + struct bstr name); +// Same as above, but does not resolve aliases or trigger warning messages. +struct m_config_option *m_config_get_co_raw(const struct m_config *config, + struct bstr name); + +// Special uses only. Look away. +int m_config_get_co_count(struct m_config *config); +struct m_config_option *m_config_get_co_index(struct m_config *config, int index); +const void *m_config_get_co_default(const struct m_config *config, + struct m_config_option *co); + +// Return the n-th option by position. n==0 is the first option. If there are +// less than (n + 1) options, return NULL. +const char *m_config_get_positional_option(const struct m_config *config, int n); + +// Return a hint to the option parser whether a parameter is/may be required. +// The option may still accept empty/non-empty parameters independent from +// this, and this function is useful only for handling ambiguous options like +// flags (e.g. "--a" is ok, "--a=yes" is also ok). +// Returns: error code (<0), or number of expected params (0, 1) +int m_config_option_requires_param(struct m_config *config, bstr name); + +// Notify m_config_cache users that the option has (probably) changed its value. +// This will force a self-notification back to config->option_change_callback. +void m_config_notify_change_opt_ptr(struct m_config *config, void *ptr); + +// Exactly like m_config_notify_change_opt_ptr(), but the option change callback +// (config->option_change_callback()) is invoked with self_update=false, if at all. +void m_config_notify_change_opt_ptr_notify(struct m_config *config, void *ptr); + +// Return all (visible) option names as NULL terminated string list. +char **m_config_list_options(void *ta_parent, const struct m_config *config); + +void m_config_print_option_list(const struct m_config *config, const char *name); + + +/* Find the profile with the given name. + * \param config The config object. + * \param arg The profile's name. + * \return The profile object or NULL. + */ +struct m_profile *m_config_get_profile0(const struct m_config *config, + char *name); +struct m_profile *m_config_get_profile(const struct m_config *config, bstr name); + +// Apply and clear the default profile - it's the only profile that new config +// files do not simply append to (for configfile parser). +void m_config_finish_default_profile(struct m_config *config, int flags); + +/* Get the profile with the given name, creating it if necessary. + * \param config The config object. + * \param arg The profile's name. + * \return The profile object. + */ +struct m_profile *m_config_add_profile(struct m_config *config, char *name); + +/* Add an option to a profile. + * Used by the config file parser when defining a profile. + * + * \param config The config object. + * \param p The profile object. + * \param name The option's name. + * \param val The option's value. + */ +int m_config_set_profile_option(struct m_config *config, struct m_profile *p, + bstr name, bstr val); + +/* Enables profile usage + * Used by the config file parser when loading a profile. + * + * \param config The config object. + * \param p The profile object. + * \param flags M_SETOPT_* bits + * Returns error code (<0) or 0 on success + */ +int m_config_set_profile(struct m_config *config, char *name, int flags); + +// Attempt to "unset" a profile if possible. +int m_config_restore_profile(struct m_config *config, char *name); + +struct mpv_node m_config_get_profiles(struct m_config *config); + +// Run async option updates here. This will call option_change_callback() on it. +void m_config_set_update_dispatch_queue(struct m_config *config, + struct mp_dispatch_queue *dispatch); diff --git a/options/m_option.c b/options/m_option.c new file mode 100644 index 0000000..1b1ac0a --- /dev/null +++ b/options/m_option.c @@ -0,0 +1,3866 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +/// \file +/// \ingroup Options + +#include "config.h" + +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <math.h> +#include <stdio.h> +#include <stdarg.h> +#include <limits.h> +#include <inttypes.h> +#include <unistd.h> +#include <assert.h> + +#include <libavutil/common.h> + +#include "libmpv/client.h" +#include "player/client.h" + +#include "mpv_talloc.h" +#include "common/common.h" +#include "common/msg.h" +#include "common/msg_control.h" +#include "misc/json.h" +#include "misc/node.h" +#include "m_option.h" +#include "m_config_frontend.h" + +#if HAVE_DOS_PATHS +#define OPTION_PATH_SEPARATOR ';' +#else +#define OPTION_PATH_SEPARATOR ':' +#endif + +const char m_option_path_separator = OPTION_PATH_SEPARATOR; + +// For integer types: since min/max are floats and may not be able to represent +// the real min/max, and since opt.min/.max may use +/-INFINITY, some care has +// to be taken. (Also tricky rounding.) +#define OPT_INT_MIN(opt, T, Tm) ((opt)->min < (opt)->max \ + ? ((opt)->min <= (double)(Tm) ? (Tm) : (T)((opt)->min)) : (Tm)) +#define OPT_INT_MAX(opt, T, Tm) ((opt)->min < (opt)->max \ + ? ((opt)->max >= (double)(Tm) ? (Tm) : (T)((opt)->max)) : (Tm)) + +int m_option_parse(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + int r = M_OPT_INVALID; + if (bstr_equals0(param, "help") && opt->help) { + r = opt->help(log, opt, name); + if (r < 0) + return r; + } + + r = opt->type->parse(log, opt, name, param, dst); + if (r < 0) + return r; + + if (opt->validate) { + r = opt->validate(log, opt, name, dst); + if (r < 0) { + if (opt->type->free) + opt->type->free(dst); + return r; + } + } + return 1; +} + +char *m_option_strerror(int code) +{ + switch (code) { + case M_OPT_UNKNOWN: + return "option not found"; + case M_OPT_MISSING_PARAM: + return "option requires parameter"; + case M_OPT_INVALID: + return "option parameter could not be parsed"; + case M_OPT_OUT_OF_RANGE: + return "parameter is outside values allowed for option"; + case M_OPT_DISALLOW_PARAM: + return "option doesn't take a parameter"; + default: + return "parser error"; + } +} + +int m_option_required_params(const m_option_t *opt) +{ + if (opt->type->flags & M_OPT_TYPE_OPTIONAL_PARAM) + return 0; + if (opt->flags & M_OPT_OPTIONAL_PARAM) + return 0; + if (opt->type == &m_option_type_choice) { + const struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) { + if (strcmp(alt->name, "yes") == 0) + return 0; + } + } + return 1; +} + +int m_option_set_node_or_string(struct mp_log *log, const m_option_t *opt, + const char *name, void *dst, struct mpv_node *src) +{ + if (src->format == MPV_FORMAT_STRING) { + // The af and vf option unfortunately require this, because the + // option name includes the "action". + bstr optname = bstr0(name), a, b; + if (bstr_split_tok(optname, "/", &a, &b)) + optname = b; + return m_option_parse(log, opt, optname, bstr0(src->u.string), dst); + } else { + return m_option_set_node(opt, dst, src); + } +} + +// Default function that just does a memcpy + +static void copy_opt(const m_option_t *opt, void *dst, const void *src) +{ + if (dst && src) + memcpy(dst, src, opt->type->size); +} + +// Bool + +#define VAL(x) (*(bool *)(x)) + +static int parse_bool(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (bstr_equals0(param, "yes") || !param.len) { + if (dst) + VAL(dst) = 1; + return 1; + } + if (bstr_equals0(param, "no")) { + if (dst) + VAL(dst) = 0; + return 1; + } + bool is_help = bstr_equals0(param, "help"); + if (is_help) { + mp_info(log, "Valid values for %.*s flag are:\n", BSTR_P(name)); + } else { + mp_fatal(log, "Invalid parameter for %.*s flag: %.*s\n", + BSTR_P(name), BSTR_P(param)); + mp_info(log, "Valid values are:\n"); + } + mp_info(log, " yes\n"); + mp_info(log, " no\n"); + mp_info(log, " (passing nothing)\n"); + return is_help ? M_OPT_EXIT : M_OPT_INVALID; +} + +static char *print_bool(const m_option_t *opt, const void *val) +{ + return talloc_strdup(NULL, VAL(val) ? "yes" : "no"); +} + +static void add_bool(const m_option_t *opt, void *val, double add, bool wrap) +{ + if (fabs(add) < 0.5) + return; + bool state = !!VAL(val); + state = wrap ? !state : add > 0; + VAL(val) = state ? 1 : 0; +} + +static int bool_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + if (src->format != MPV_FORMAT_FLAG) + return M_OPT_UNKNOWN; + VAL(dst) = !!src->u.flag; + return 1; +} + +static int bool_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + dst->format = MPV_FORMAT_FLAG; + dst->u.flag = !!VAL(src); + return 1; +} + +static bool bool_equal(const m_option_t *opt, void *a, void *b) +{ + return VAL(a) == VAL(b); +} + +const m_option_type_t m_option_type_bool = { + .name = "Flag", // same as m_option_type_flag; transparent to user + .size = sizeof(bool), + .flags = M_OPT_TYPE_OPTIONAL_PARAM | M_OPT_TYPE_CHOICE, + .parse = parse_bool, + .print = print_bool, + .copy = copy_opt, + .add = add_bool, + .set = bool_set, + .get = bool_get, + .equal = bool_equal, +}; + +#undef VAL + +// Flag + +#define VAL(x) (*(int *)(x)) + +static int parse_flag(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + bool bdst = false; + int r = parse_bool(log, opt, name, param, &bdst); + if (dst) + VAL(dst) = bdst; + return r; +} + +static char *print_flag(const m_option_t *opt, const void *val) +{ + return print_bool(opt, &(bool){VAL(val)}); +} + +static void add_flag(const m_option_t *opt, void *val, double add, bool wrap) +{ + bool bval = VAL(val); + add_bool(opt, &bval, add, wrap); + VAL(val) = bval; +} + +static int flag_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + bool bdst = false; + int r = bool_set(opt, &bdst, src); + if (r >= 0) + VAL(dst) = bdst; + return r; +} + +static int flag_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + return bool_get(opt, ta_parent, dst, &(bool){VAL(src)}); +} + +static bool flag_equal(const m_option_t *opt, void *a, void *b) +{ + return VAL(a) == VAL(b); +} + +// Only exists for libmpv interopability and should not be used anywhere. +const m_option_type_t m_option_type_flag = { + // need yes or no in config files + .name = "Flag", + .size = sizeof(int), + .flags = M_OPT_TYPE_OPTIONAL_PARAM | M_OPT_TYPE_CHOICE, + .parse = parse_flag, + .print = print_flag, + .copy = copy_opt, + .add = add_flag, + .set = flag_set, + .get = flag_get, + .equal = flag_equal, +}; + +// Integer + +#undef VAL + +static int clamp_longlong(const m_option_t *opt, long long i_min, long long i_max, + void *val) +{ + long long v = *(long long *)val; + int r = 0; + long long min = OPT_INT_MIN(opt, long long, i_min); + long long max = OPT_INT_MAX(opt, long long, i_max); + if (v > max) { + v = max; + r = M_OPT_OUT_OF_RANGE; + } + if (v < min) { + v = min; + r = M_OPT_OUT_OF_RANGE; + } + *(long long *)val = v; + return r; +} + +static int parse_longlong(struct mp_log *log, const m_option_t *opt, + long long i_min, long long i_max, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + struct bstr rest; + long long tmp_int = bstrtoll(param, &rest, 10); + if (rest.len) + tmp_int = bstrtoll(param, &rest, 0); + if (rest.len) { + mp_err(log, "The %.*s option must be an integer: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + long long min = OPT_INT_MIN(opt, long long, i_min); + if (tmp_int < min) { + mp_err(log, "The %.*s option must be >= %lld: %.*s\n", + BSTR_P(name), min, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + long long max = OPT_INT_MAX(opt, long long, i_max); + if (tmp_int > max) { + mp_err(log, "The %.*s option must be <= %lld: %.*s\n", + BSTR_P(name), max, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (dst) + *(long long *)dst = tmp_int; + + return 1; +} + +static int clamp_int64(const m_option_t *opt, void *val) +{ + long long tmp = *(int64_t *)val; + int r = clamp_longlong(opt, INT64_MIN, INT64_MAX, &tmp); + *(int64_t *)val = tmp; + return r; +} + +static int parse_int(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + long long tmp; + int r = parse_longlong(log, opt, INT_MIN, INT_MAX, name, param, &tmp); + if (r >= 0 && dst) + *(int *)dst = tmp; + return r; +} + +static int parse_int64(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + long long tmp; + int r = parse_longlong(log, opt, INT64_MIN, INT64_MAX, name, param, &tmp); + if (r >= 0 && dst) + *(int64_t *)dst = tmp; + return r; +} + +static char *print_int(const m_option_t *opt, const void *val) +{ + if (opt->type->size == sizeof(int64_t)) + return talloc_asprintf(NULL, "%"PRId64, *(const int64_t *)val); + return talloc_asprintf(NULL, "%d", *(const int *)val); +} + +static void add_int64(const m_option_t *opt, void *val, double add, bool wrap) +{ + int64_t v = *(int64_t *)val; + + clamp_int64(opt, &v); + + v = v + add; + + bool is64 = opt->type->size == sizeof(int64_t); + int64_t nmin = is64 ? INT64_MIN : INT_MIN; + int64_t nmax = is64 ? INT64_MAX : INT_MAX; + + int64_t min = OPT_INT_MIN(opt, int64_t, nmin); + int64_t max = OPT_INT_MAX(opt, int64_t, nmax); + + if (v < min) + v = wrap ? max : min; + if (v > max) + v = wrap ? min : max; + + *(int64_t *)val = v; +} + +static void add_int(const m_option_t *opt, void *val, double add, bool wrap) +{ + int64_t tmp = *(int *)val; + add_int64(opt, &tmp, add, wrap); + *(int *)val = tmp; +} + +static void multiply_int64(const m_option_t *opt, void *val, double f) +{ + double v = *(int64_t *)val * f; + int64_t iv = v; + if (v < INT64_MIN) + iv = INT64_MIN; + if (v >= (double)INT64_MAX) + iv = INT64_MAX; + *(int64_t *)val = iv; + clamp_int64(opt, val); +} + +static void multiply_int(const m_option_t *opt, void *val, double f) +{ + int64_t tmp = *(int *)val; + multiply_int64(opt, &tmp, f); + *(int *)val = MPCLAMP(tmp, INT_MIN, INT_MAX); +} + +static int int64_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + if (src->format != MPV_FORMAT_INT64) + return M_OPT_UNKNOWN; + int64_t val = src->u.int64; + if (val < OPT_INT_MIN(opt, int64_t, INT64_MIN)) + return M_OPT_OUT_OF_RANGE; + if (val > OPT_INT_MAX(opt, int64_t, INT64_MAX)) + return M_OPT_OUT_OF_RANGE; + *(int64_t *)dst = val; + return 1; +} + +static int int_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + int64_t val; + int r = int64_set(opt, &val, src); + if (r >= 0) { + if (val < INT_MIN || val > INT_MAX) + return M_OPT_OUT_OF_RANGE; + *(int *)dst = val; + } + return r; +} + +static int int64_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + dst->format = MPV_FORMAT_INT64; + dst->u.int64 = *(int64_t *)src; + return 1; +} + +static int int_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + dst->format = MPV_FORMAT_INT64; + dst->u.int64 = *(int *)src; + return 1; +} + +static bool int_equal(const m_option_t *opt, void *a, void *b) +{ + return *(int *)a == *(int *)b; +} + +static bool int64_equal(const m_option_t *opt, void *a, void *b) +{ + return *(int64_t *)a == *(int64_t *)b; +} + +const m_option_type_t m_option_type_int = { + .name = "Integer", + .flags = M_OPT_TYPE_USES_RANGE, + .size = sizeof(int), + .parse = parse_int, + .print = print_int, + .copy = copy_opt, + .add = add_int, + .multiply = multiply_int, + .set = int_set, + .get = int_get, + .equal = int_equal, +}; + +const m_option_type_t m_option_type_int64 = { + .name = "Integer64", + .flags = M_OPT_TYPE_USES_RANGE, + .size = sizeof(int64_t), + .parse = parse_int64, + .print = print_int, + .copy = copy_opt, + .add = add_int64, + .multiply = multiply_int64, + .set = int64_set, + .get = int64_get, + .equal = int64_equal, +}; + +static int parse_byte_size(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + struct bstr r; + long long tmp_int = bstrtoll(param, &r, 0); + int64_t unit = 1; + if (r.len) { + if (bstrcasecmp0(r, "b") == 0) { + unit = 1; + } else if (bstrcasecmp0(r, "kib") == 0 || bstrcasecmp0(r, "k") == 0) { + unit = 1024; + } else if (bstrcasecmp0(r, "mib") == 0 || bstrcasecmp0(r, "m") == 0) { + unit = 1024 * 1024; + } else if (bstrcasecmp0(r, "gib") == 0 || bstrcasecmp0(r, "g") == 0) { + unit = 1024 * 1024 * 1024; + } else if (bstrcasecmp0(r, "tib") == 0 || bstrcasecmp0(r, "t") == 0) { + unit = 1024 * 1024 * 1024 * 1024LL; + } else { + mp_err(log, "The %.*s option must be an integer: %.*s\n", + BSTR_P(name), BSTR_P(param)); + mp_err(log, "The following suffixes are also allowed: " + "KiB, MiB, GiB, TiB, B, K, M, G, T.\n"); + return M_OPT_INVALID; + } + } + + if (tmp_int < 0) { + mp_err(log, "The %.*s option does not support negative numbers: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (INT64_MAX / unit < tmp_int) { + mp_err(log, "The %.*s option overflows: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + tmp_int *= unit; + + int64_t min = OPT_INT_MIN(opt, int64_t, INT64_MIN); + if (tmp_int < min) { + mp_err(log, "The %.*s option must be >= %"PRId64": %.*s\n", + BSTR_P(name), min, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + int64_t max = OPT_INT_MAX(opt, int64_t, INT64_MAX); + if (tmp_int > max) { + mp_err(log, "The %.*s option must be <= %"PRId64": %.*s\n", + BSTR_P(name), max, BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + + if (dst) + *(int64_t *)dst = tmp_int; + + return 1; +} + +char *format_file_size(int64_t size) +{ + double s = size; + if (size < 1024) + return talloc_asprintf(NULL, "%.0f B", s); + + if (size < (1024 * 1024)) + return talloc_asprintf(NULL, "%.3f KiB", s / (1024.0)); + + if (size < (1024 * 1024 * 1024)) + return talloc_asprintf(NULL, "%.3f MiB", s / (1024.0 * 1024.0)); + + if (size < (1024LL * 1024LL * 1024LL * 1024LL)) + return talloc_asprintf(NULL, "%.3f GiB", s / (1024.0 * 1024.0 * 1024.0)); + + return talloc_asprintf(NULL, "%.3f TiB", s / (1024.0 * 1024.0 * 1024.0 * 1024.0)); +} + +static char *pretty_print_byte_size(const m_option_t *opt, const void *val) +{ + return format_file_size(*(int64_t *)val); +} + +const m_option_type_t m_option_type_byte_size = { + .name = "ByteSize", + .flags = M_OPT_TYPE_USES_RANGE, + .size = sizeof(int64_t), + .parse = parse_byte_size, + .print = print_int, + .pretty_print = pretty_print_byte_size, + .copy = copy_opt, + .add = add_int64, + .multiply = multiply_int64, + .set = int64_set, + .get = int64_get, + .equal = int64_equal, +}; + +const char *m_opt_choice_str(const struct m_opt_choice_alternatives *choices, + int value) +{ + for (const struct m_opt_choice_alternatives *c = choices; c->name; c++) { + if (c->value == value) + return c->name; + } + return NULL; +} + +static void print_choice_values(struct mp_log *log, const struct m_option *opt) +{ + const struct m_opt_choice_alternatives *alt = opt->priv; + for ( ; alt->name; alt++) + mp_info(log, " %s\n", alt->name[0] ? alt->name : "(passing nothing)"); + if (opt->min < opt->max) + mp_info(log, " %g-%g (integer range)\n", opt->min, opt->max); +} + +static int parse_choice(struct mp_log *log, const struct m_option *opt, + struct bstr name, struct bstr param, void *dst) +{ + const struct m_opt_choice_alternatives *alt = opt->priv; + for ( ; alt->name; alt++) { + if (!bstrcmp0(param, alt->name)) + break; + } + if (!alt->name && param.len == 0) { + // allow flag-style options, e.g. "--mute" implies "--mute=yes" + for (alt = opt->priv; alt->name; alt++) { + if (!strcmp("yes", alt->name)) + break; + } + } + if (!alt->name) { + if (!bstrcmp0(param, "help")) { + mp_info(log, "Valid values for option %.*s are:\n", BSTR_P(name)); + print_choice_values(log, opt); + return M_OPT_EXIT; + } + if (param.len == 0) + return M_OPT_MISSING_PARAM; + if (opt->min < opt->max) { + long long val; + if (parse_longlong(mp_null_log, opt, INT_MIN, INT_MAX, name, param, + &val) == 1) + { + if (dst) + *(int *)dst = val; + return 1; + } + } + mp_fatal(log, "Invalid value for option %.*s: %.*s\n", + BSTR_P(name), BSTR_P(param)); + mp_info(log, "Valid values are:\n"); + print_choice_values(log, opt); + return M_OPT_INVALID; + } + if (dst) + *(int *)dst = alt->value; + + return 1; +} + +static void choice_get_min_max(const struct m_option *opt, int *min, int *max) +{ + assert(opt->type == &m_option_type_choice); + *min = INT_MAX; + *max = INT_MIN; + for (const struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) { + *min = MPMIN(*min, alt->value); + *max = MPMAX(*max, alt->value); + } + if (opt->min < opt->max) { + *min = MPMIN(*min, opt->min); + *max = MPMAX(*max, opt->max); + } +} + +static void check_choice(int dir, int val, bool *found, int *best, int choice) +{ + if ((dir == -1 && (!(*found) || choice > (*best)) && choice < val) || + (dir == +1 && (!(*found) || choice < (*best)) && choice > val)) + { + *found = true; + *best = choice; + } +} + +static void add_choice(const m_option_t *opt, void *val, double add, bool wrap) +{ + assert(opt->type == &m_option_type_choice); + int dir = add > 0 ? +1 : -1; + bool found = false; + int ival = *(int *)val; + int best = 0; // init. value unused + + if (fabs(add) < 0.5) + return; + + if (opt->min < opt->max) { + int newval = ival + add; + if (ival >= opt->min && ival <= opt->max && + newval >= opt->min && newval <= opt->max) + { + found = true; + best = newval; + } else { + check_choice(dir, ival, &found, &best, opt->min); + check_choice(dir, ival, &found, &best, opt->max); + } + } + + for (const struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) + check_choice(dir, ival, &found, &best, alt->value); + + if (!found) { + int min, max; + choice_get_min_max(opt, &min, &max); + best = (dir == -1) ^ wrap ? min : max; + } + + *(int *)val = best; +} + +static int choice_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + char buf[80]; + char *src_str = NULL; + if (src->format == MPV_FORMAT_INT64) { + snprintf(buf, sizeof(buf), "%" PRId64, src->u.int64); + src_str = buf; + } else if (src->format == MPV_FORMAT_STRING) { + src_str = src->u.string; + } else if (src->format == MPV_FORMAT_FLAG) { + src_str = src->u.flag ? "yes" : "no"; + } + if (!src_str) + return M_OPT_UNKNOWN; + int val = 0; + int r = parse_choice(mp_null_log, opt, (bstr){0}, bstr0(src_str), &val); + if (r >= 0) + *(int *)dst = val; + return r; +} + +static const struct m_opt_choice_alternatives *get_choice(const m_option_t *opt, + const void *val, + int *out_val) +{ + int v = *(int *)val; + const struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) { + if (alt->value == v) + return alt; + } + if (opt->min < opt->max) { + if (v >= opt->min && v <= opt->max) { + *out_val = v; + return NULL; + } + } + MP_ASSERT_UNREACHABLE(); +} + +static int choice_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + int ival = 0; + const struct m_opt_choice_alternatives *alt = get_choice(opt, src, &ival); + // If a choice string looks like a number, return it as number + if (alt) { + char *end = NULL; + ival = strtol(alt->name, &end, 10); + if (end && !end[0]) + alt = NULL; + } + if (alt) { + int b = -1; + if (strcmp(alt->name, "yes") == 0) { + b = 1; + } else if (strcmp(alt->name, "no") == 0) { + b = 0; + } + if (b >= 0) { + dst->format = MPV_FORMAT_FLAG; + dst->u.flag = b; + } else { + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(ta_parent, alt->name); + } + } else { + dst->format = MPV_FORMAT_INT64; + dst->u.int64 = ival; + } + return 1; +} + +static char *print_choice(const m_option_t *opt, const void *val) +{ + int ival = 0; + const struct m_opt_choice_alternatives *alt = get_choice(opt, val, &ival); + return alt ? talloc_strdup(NULL, alt->name) + : talloc_asprintf(NULL, "%d", ival); +} + +const struct m_option_type m_option_type_choice = { + .name = "Choice", + .size = sizeof(int), + .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, + .parse = parse_choice, + .print = print_choice, + .copy = copy_opt, + .add = add_choice, + .set = choice_set, + .get = choice_get, + .equal = int_equal, +}; + +static int apply_flag(const struct m_option *opt, int *val, bstr flag) +{ + const struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) { + if (bstr_equals0(flag, alt->name)) { + if (*val & alt->value) + return M_OPT_INVALID; + *val |= alt->value; + return 0; + } + } + return M_OPT_UNKNOWN; +} + +static const char *find_next_flag(const struct m_option *opt, int *val) +{ + const struct m_opt_choice_alternatives *best = NULL; + const struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) { + if (alt->value && (alt->value & (*val)) == alt->value) { + if (!best || av_popcount64(alt->value) > av_popcount64(best->value)) + best = alt; + } + } + if (best) { + *val = *val & ~(unsigned)best->value; + return best->name; + } + *val = 0; // if there are still flags left, there's not much we can do + return NULL; +} + +static int parse_flags(struct mp_log *log, const struct m_option *opt, + struct bstr name, struct bstr param, void *dst) +{ + int value = 0; + while (param.len) { + bstr flag; + bstr_split_tok(param, "+", &flag, ¶m); + int r = apply_flag(opt, &value, flag); + if (r == M_OPT_UNKNOWN) { + mp_fatal(log, "Invalid flag for option %.*s: %.*s\n", + BSTR_P(name), BSTR_P(flag)); + mp_info(log, "Valid flags are:\n"); + const struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) + mp_info(log, " %s\n", alt->name); + mp_info(log, "Flags can usually be combined with '+'.\n"); + return M_OPT_INVALID; + } else if (r < 0) { + mp_fatal(log, "Option %.*s: flag '%.*s' conflicts with a previous " + "flag value.\n", BSTR_P(name), BSTR_P(flag)); + return M_OPT_INVALID; + } + } + if (dst) + *(int *)dst = value; + return 1; +} + +static int flags_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + int value = 0; + if (src->format != MPV_FORMAT_NODE_ARRAY) + return M_OPT_UNKNOWN; + struct mpv_node_list *srclist = src->u.list; + for (int n = 0; n < srclist->num; n++) { + if (srclist->values[n].format != MPV_FORMAT_STRING) + return M_OPT_INVALID; + if (apply_flag(opt, &value, bstr0(srclist->values[n].u.string)) < 0) + return M_OPT_INVALID; + } + *(int *)dst = value; + return 0; +} + +static int flags_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + int value = *(int *)src; + + dst->format = MPV_FORMAT_NODE_ARRAY; + dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); + struct mpv_node_list *list = dst->u.list; + while (1) { + const char *flag = find_next_flag(opt, &value); + if (!flag) + break; + + struct mpv_node node; + node.format = MPV_FORMAT_STRING; + node.u.string = (char *)flag; + MP_TARRAY_APPEND(list, list->values, list->num, node); + } + + return 1; +} + +static char *print_flags(const m_option_t *opt, const void *val) +{ + int value = *(int *)val; + char *res = talloc_strdup(NULL, ""); + while (1) { + const char *flag = find_next_flag(opt, &value); + if (!flag) + break; + + res = talloc_asprintf_append_buffer(res, "%s%s", res[0] ? "+" : "", flag); + } + return res; +} + +const struct m_option_type m_option_type_flags = { + .name = "Flags", + .size = sizeof(int), + .parse = parse_flags, + .print = print_flags, + .copy = copy_opt, + .set = flags_set, + .get = flags_get, + .equal = int_equal, +}; + +// Float + +#undef VAL +#define VAL(x) (*(double *)(x)) + +static int clamp_double(const m_option_t *opt, void *val) +{ + double v = VAL(val); + int r = 0; + if (opt->min < opt->max) { + if (v > opt->max) { + v = opt->max; + r = M_OPT_OUT_OF_RANGE; + } + if (v < opt->min) { + v = opt->min; + r = M_OPT_OUT_OF_RANGE; + } + } + // (setting max/min to INFINITY/-INFINITY is allowed) + if (!isfinite(v) && v != opt->max && v != opt->min) { + v = opt->min; + r = M_OPT_OUT_OF_RANGE; + } + VAL(val) = v; + return r; +} + +static int parse_double(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + struct bstr rest; + double tmp_float = bstrtod(param, &rest); + + if (bstr_eatstart0(&rest, ":") || bstr_eatstart0(&rest, "/")) + tmp_float /= bstrtod(rest, &rest); + + if ((opt->flags & M_OPT_DEFAULT_NAN) && bstr_equals0(param, "default")) { + tmp_float = NAN; + goto done; + } + + if (rest.len) { + mp_err(log, "The %.*s option must be a floating point number or a " + "ratio (numerator[:/]denominator): %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + if (clamp_double(opt, &tmp_float) < 0) { + mp_err(log, "The %.*s option is out of range: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_OUT_OF_RANGE; + } + +done: + if (dst) + VAL(dst) = tmp_float; + return 1; +} + +static char *print_double(const m_option_t *opt, const void *val) +{ + double f = VAL(val); + if (isnan(f) && (opt->flags & M_OPT_DEFAULT_NAN)) + return talloc_strdup(NULL, "default"); + return talloc_asprintf(NULL, "%f", f); +} + +static char *print_double_7g(const m_option_t *opt, const void *val) +{ + double f = VAL(val); + if (isnan(f)) + return print_double(opt, val); + // Truncate anything < 1e-4 to avoid switching to scientific notation + if (fabs(f) < 1e-4) { + return talloc_strdup(NULL, "0"); + } else { + return talloc_asprintf(NULL, "%.7g", f); + } +} + +static void add_double(const m_option_t *opt, void *val, double add, bool wrap) +{ + double v = VAL(val); + + v = v + add; + + double min = opt->min < opt->max ? opt->min : -INFINITY; + double max = opt->min < opt->max ? opt->max : +INFINITY; + + if (v < min) + v = wrap ? max : min; + if (v > max) + v = wrap ? min : max; + + VAL(val) = v; +} + +static void multiply_double(const m_option_t *opt, void *val, double f) +{ + *(double *)val *= f; + clamp_double(opt, val); +} + +static int double_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + double val; + if (src->format == MPV_FORMAT_INT64) { + // Can't always be represented exactly, but don't care. + val = src->u.int64; + } else if (src->format == MPV_FORMAT_DOUBLE) { + val = src->u.double_; + } else { + return M_OPT_UNKNOWN; + } + if (clamp_double(opt, &val) < 0) + return M_OPT_OUT_OF_RANGE; + *(double *)dst = val; + return 1; +} + +static int double_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + double f = *(double *)src; + if (isnan(f) && (opt->flags & M_OPT_DEFAULT_NAN)) { + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(ta_parent, "default"); + } else { + dst->format = MPV_FORMAT_DOUBLE; + dst->u.double_ = f; + } + return 1; +} + +static bool double_equal(const m_option_t *opt, void *a, void *b) +{ + double fa = VAL(a), fb = VAL(b); + if (isnan(fa) || isnan(fb)) + return isnan(fa) == isnan(fb); + return fa == fb; +} + +const m_option_type_t m_option_type_double = { + // double precision float or ratio (numerator[:/]denominator) + .name = "Double", + .flags = M_OPT_TYPE_USES_RANGE, + .size = sizeof(double), + .parse = parse_double, + .print = print_double, + .pretty_print = print_double_7g, + .copy = copy_opt, + .add = add_double, + .multiply = multiply_double, + .set = double_set, + .get = double_get, + .equal = double_equal, +}; + +static int parse_double_aspect(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (bstr_equals0(param, "no")) { + if (dst) + VAL(dst) = 0.0; + return 1; + } + return parse_double(log, opt, name, param, dst); +} + +const m_option_type_t m_option_type_aspect = { + .name = "Aspect", + .size = sizeof(double), + .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, + .parse = parse_double_aspect, + .print = print_double, + .pretty_print = print_double_7g, + .copy = copy_opt, + .add = add_double, + .multiply = multiply_double, + .set = double_set, + .get = double_get, + .equal = double_equal, +}; + +#undef VAL +#define VAL(x) (*(float *)(x)) + +static int parse_float(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + double tmp; + int r = parse_double(log, opt, name, param, &tmp); + if (r == 1 && dst) + VAL(dst) = tmp; + return r; +} + +static char *print_float(const m_option_t *opt, const void *val) +{ + double tmp = VAL(val); + return print_double(opt, &tmp); +} + +static char *print_float_f3(const m_option_t *opt, const void *val) +{ + double tmp = VAL(val); + return print_double_7g(opt, &tmp); +} + +static void add_float(const m_option_t *opt, void *val, double add, bool wrap) +{ + double tmp = VAL(val); + add_double(opt, &tmp, add, wrap); + VAL(val) = tmp; +} + +static void multiply_float(const m_option_t *opt, void *val, double f) +{ + double tmp = VAL(val); + multiply_double(opt, &tmp, f); + VAL(val) = tmp; +} + +static int float_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + double tmp; + int r = double_set(opt, &tmp, src); + if (r >= 0) + VAL(dst) = tmp; + return r; +} + +static int float_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + double tmp = VAL(src); + return double_get(opt, ta_parent, dst, &tmp); +} + +static bool float_equal(const m_option_t *opt, void *a, void *b) +{ + return double_equal(opt, &(double){VAL(a)}, &(double){VAL(b)}); +} + +const m_option_type_t m_option_type_float = { + // floating point number or ratio (numerator[:/]denominator) + .name = "Float", + .flags = M_OPT_TYPE_USES_RANGE, + .size = sizeof(float), + .parse = parse_float, + .print = print_float, + .pretty_print = print_float_f3, + .copy = copy_opt, + .add = add_float, + .multiply = multiply_float, + .set = float_set, + .get = float_get, + .equal = float_equal, +}; + +///////////// String + +#undef VAL +#define VAL(x) (*(char **)(x)) + +static int parse_str(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (dst) { + talloc_free(VAL(dst)); + VAL(dst) = bstrdup0(NULL, param); + } + + return 0; +} + +static char *print_str(const m_option_t *opt, const void *val) +{ + return talloc_strdup(NULL, VAL(val) ? VAL(val) : ""); +} + +static void copy_str(const m_option_t *opt, void *dst, const void *src) +{ + if (dst && src) { + talloc_free(VAL(dst)); + VAL(dst) = talloc_strdup(NULL, VAL(src)); + } +} + +static int str_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + if (src->format != MPV_FORMAT_STRING) + return M_OPT_UNKNOWN; + char *s = src->u.string; + int r = s ? 0 : M_OPT_INVALID; + if (r >= 0) + copy_str(opt, dst, &s); + return r; +} + +static int str_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(ta_parent, VAL(src) ? VAL(src) : ""); + return 1; +} + +static bool str_equal(const m_option_t *opt, void *a, void *b) +{ + return bstr_equals(bstr0(VAL(a)), bstr0(VAL(b))); +} + +static void free_str(void *src) +{ + if (src && VAL(src)) { + talloc_free(VAL(src)); + VAL(src) = NULL; + } +} + +const m_option_type_t m_option_type_string = { + .name = "String", + .size = sizeof(char *), + .parse = parse_str, + .print = print_str, + .copy = copy_str, + .free = free_str, + .set = str_set, + .get = str_get, + .equal = str_equal, +}; + +//////////// String list + +#undef VAL +#define VAL(x) (*(char ***)(x)) + +#define OP_NONE 0 +#define OP_ADD 1 +#define OP_PRE 2 +#define OP_CLR 3 +#define OP_TOGGLE 4 +#define OP_APPEND 5 +#define OP_REMOVE 6 + +static void free_str_list(void *dst) +{ + char **d; + int i; + + if (!dst || !VAL(dst)) + return; + d = VAL(dst); + + for (i = 0; d[i] != NULL; i++) + talloc_free(d[i]); + talloc_free(d); + VAL(dst) = NULL; +} + +static int str_list_add(char **add, int n, void *dst, int pre) +{ + char **lst = VAL(dst); + + int ln; + for (ln = 0; lst && lst[ln]; ln++) + /**/; + + lst = talloc_realloc(NULL, lst, char *, n + ln + 1); + + if (pre) { + memmove(&lst[n], lst, ln * sizeof(char *)); + memcpy(lst, add, n * sizeof(char *)); + } else + memcpy(&lst[ln], add, n * sizeof(char *)); + // (re-)add NULL-termination + lst[ln + n] = NULL; + + talloc_free(add); + + VAL(dst) = lst; + + return 1; +} + +static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify) +{ + struct bstr str = *ptr; + struct bstr orig = str; + for (;;) { + int idx = sep ? bstrchr(str, sep) : -1; + if (idx > 0 && str.start[idx - 1] == '\\') { + if (modify) { + memmove(str.start + idx - 1, str.start + idx, str.len - idx); + str.len--; + str = bstr_cut(str, idx); + } else + str = bstr_cut(str, idx + 1); + } else { + str = bstr_cut(str, idx < 0 ? str.len : idx); + break; + } + } + *ptr = str; + return bstr_splice(orig, 0, str.start - orig.start); +} + +static int find_list_bstr(char **list, bstr item) +{ + for (int n = 0; list && list[n]; n++) { + if (bstr_equals0(item, list[n])) + return n; + } + return -1; +} + +static int parse_str_list_impl(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst, + int default_op) +{ + char **res; + int op = default_op; + bool multi = true; + + if (bstr_endswith0(name, "-add")) { + op = OP_ADD; + } else if (bstr_endswith0(name, "-append")) { + op = OP_ADD; + multi = false; + } else if (bstr_endswith0(name, "-pre")) { + op = OP_PRE; + } else if (bstr_endswith0(name, "-clr")) { + op = OP_CLR; + } else if (bstr_endswith0(name, "-set")) { + op = OP_NONE; + } else if (bstr_endswith0(name, "-toggle")) { + op = OP_TOGGLE; + } else if (bstr_endswith0(name, "-remove")) { + op = OP_REMOVE; + } + + if (op == OP_TOGGLE || op == OP_REMOVE) { + if (dst) { + char **list = VAL(dst); + bool found = false; + int index = 0; + do { + index = find_list_bstr(list, param); + if (index >= 0) { + found = true; + char *old = list[index]; + for (int n = index; list[n]; n++) + list[n] = list[n + 1]; + talloc_free(old); + } + } while (index >= 0); + if (found) + return 1; + } + if (op == OP_REMOVE) + return 1; // ignore if not found + op = OP_ADD; + multi = false; + } + + // Clear the list ?? + if (op == OP_CLR) { + if (dst) + free_str_list(dst); + return 0; + } + + // All other ops need a param + if (param.len == 0 && op != OP_NONE) + return M_OPT_MISSING_PARAM; + + char separator = opt->priv ? *(char *)opt->priv : OPTION_LIST_SEPARATOR; + if (!multi) + separator = 0; // specially handled + int n = 0; + struct bstr str = param; + while (str.len) { + get_nextsep(&str, separator, 0); + str = bstr_cut(str, 1); + n++; + } + if (n == 0 && op != OP_NONE) + return M_OPT_INVALID; + + if (!dst) + return 1; + + res = talloc_array(NULL, char *, n + 2); + str = bstrdup(NULL, param); + char *ptr = str.start; + n = 0; + + while (1) { + struct bstr el = get_nextsep(&str, separator, 1); + res[n] = bstrdup0(NULL, el); + n++; + if (!str.len) + break; + str = bstr_cut(str, 1); + } + res[n] = NULL; + talloc_free(ptr); + + if (op != OP_NONE && n > 1) { + mp_warn(log, "Passing multiple arguments to %.*s is deprecated!\n", + BSTR_P(name)); + } + + switch (op) { + case OP_ADD: + return str_list_add(res, n, dst, 0); + case OP_PRE: + return str_list_add(res, n, dst, 1); + } + + if (VAL(dst)) + free_str_list(dst); + VAL(dst) = res; + + if (!res[0]) + free_str_list(dst); + + return 1; +} + +static void copy_str_list(const m_option_t *opt, void *dst, const void *src) +{ + int n; + char **d, **s; + + if (!(dst && src)) + return; + s = VAL(src); + + if (VAL(dst)) + free_str_list(dst); + + if (!s) { + VAL(dst) = NULL; + return; + } + + for (n = 0; s[n] != NULL; n++) + /* NOTHING */; + d = talloc_array(NULL, char *, n + 1); + for (; n >= 0; n--) + d[n] = talloc_strdup(NULL, s[n]); + + VAL(dst) = d; +} + +static char *print_str_list(const m_option_t *opt, const void *src) +{ + char **lst = NULL; + char *ret = NULL; + + if (!(src && VAL(src))) + return talloc_strdup(NULL, ""); + lst = VAL(src); + + for (int i = 0; lst[i]; i++) { + if (ret) + ret = talloc_strdup_append_buffer(ret, ","); + ret = talloc_strdup_append_buffer(ret, lst[i]); + } + return ret; +} + +static int str_list_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + if (src->format != MPV_FORMAT_NODE_ARRAY) + return M_OPT_UNKNOWN; + struct mpv_node_list *srclist = src->u.list; + for (int n = 0; n < srclist->num; n++) { + if (srclist->values[n].format != MPV_FORMAT_STRING) + return M_OPT_INVALID; + } + free_str_list(dst); + if (srclist->num > 0) { + VAL(dst) = talloc_array(NULL, char*, srclist->num + 1); + for (int n = 0; n < srclist->num; n++) + VAL(dst)[n] = talloc_strdup(NULL, srclist->values[n].u.string); + VAL(dst)[srclist->num] = NULL; + } + return 1; +} + +static int str_list_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + dst->format = MPV_FORMAT_NODE_ARRAY; + dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); + struct mpv_node_list *list = dst->u.list; + for (int n = 0; VAL(src) && VAL(src)[n]; n++) { + struct mpv_node node; + node.format = MPV_FORMAT_STRING; + node.u.string = talloc_strdup(list, VAL(src)[n]); + MP_TARRAY_APPEND(list, list->values, list->num, node); + } + return 1; +} + +static int parse_str_list(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + return parse_str_list_impl(log, opt, name, param, dst, OP_NONE); +} + +static bool str_list_equal(const m_option_t *opt, void *a, void *b) +{ + char **la = VAL(a); + char **lb = VAL(b); + + bool a_empty = !la || !la[0]; + bool b_empty = !lb || !lb[0]; + if (a_empty || b_empty) + return a_empty == b_empty; + + for (int n = 0; la[n] || lb[n]; n++) { + if (!la[n] || !lb[n]) + return false; + if (strcmp(la[n], lb[n]) != 0) + return false; + } + + return true; +} + +const m_option_type_t m_option_type_string_list = { + .name = "String list", + .size = sizeof(char **), + .parse = parse_str_list, + .print = print_str_list, + .copy = copy_str_list, + .free = free_str_list, + .get = str_list_get, + .set = str_list_set, + .equal = str_list_equal, + .actions = (const struct m_option_action[]){ + {"add"}, + {"append"}, + {"clr", M_OPT_TYPE_OPTIONAL_PARAM}, + {"pre"}, + {"set"}, + {"toggle"}, + {"remove"}, + {0} + }, +}; + +static int read_subparam(struct mp_log *log, bstr optname, char *termset, + bstr *str, bstr *out_subparam); + +static int keyvalue_list_find_key(char **lst, bstr str) +{ + for (int n = 0; lst && lst[n] && lst[n + 1]; n += 2) { + if (bstr_equals0(str, lst[n])) + return n / 2; + } + return -1; +} + +static void keyvalue_list_del_key(char **lst, int index) +{ + int count = 0; + for (int n = 0; lst && lst[n]; n++) + count++; + assert(index * 2 + 1 < count); + count += 1; // terminating item + talloc_free(lst[index * 2 + 0]); + talloc_free(lst[index * 2 + 1]); + MP_TARRAY_REMOVE_AT(lst, count, index * 2 + 1); + MP_TARRAY_REMOVE_AT(lst, count, index * 2 + 0); +} + +static int parse_keyvalue_list(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + char **lst = NULL; + int num = 0; + int r = 0; + bool append = false; + bool full_value = false; + + if ((opt->flags & M_OPT_HAVE_HELP) && bstr_equals0(param, "help")) + param = bstr0("help="); + + if (bstr_endswith0(name, "-add")) { + append = true; + } else if (bstr_endswith0(name, "-append")) { + append = full_value = true; + } else if (bstr_endswith0(name, "-remove")) { + lst = dst ? VAL(dst) : NULL; + int index = dst ? keyvalue_list_find_key(lst, param) : -1; + if (index >= 0) { + keyvalue_list_del_key(lst, index); + VAL(dst) = lst; + } + return 1; + } + + if (append && dst) { + lst = VAL(dst); + for (int n = 0; lst && lst[n]; n++) + num++; + } + + while (param.len) { + bstr key, val; + r = read_subparam(log, name, "=", ¶m, &key); + if (r < 0) + break; + if (!bstr_eatstart0(¶m, "=")) { + mp_err(log, "Expected '=' and a value.\n"); + r = M_OPT_INVALID; + break; + } + if (full_value) { + val = param; + param.len = 0; + } else { + r = read_subparam(log, name, ",", ¶m, &val); + if (r < 0) + break; + } + if (dst) { + int index = keyvalue_list_find_key(lst, key); + if (index >= 0) { + keyvalue_list_del_key(lst, index); + num -= 2; + } + MP_TARRAY_APPEND(NULL, lst, num, bstrto0(NULL, key)); + MP_TARRAY_APPEND(NULL, lst, num, bstrto0(NULL, val)); + MP_TARRAY_APPEND(NULL, lst, num, NULL); + num -= 1; + } + + if (!bstr_eatstart0(¶m, ",") && !bstr_eatstart0(¶m, ":")) + break; + + if (append) { + mp_warn(log, "Passing more than 1 argument to %.*s is deprecated!\n", + BSTR_P(name)); + } + } + + if (param.len) { + mp_err(log, "Unparsable garbage at end of option value: '%.*s'\n", + BSTR_P(param)); + r = M_OPT_INVALID; + } + + if (dst) { + if (!append) + free_str_list(dst); + VAL(dst) = lst; + if (r < 0) + free_str_list(dst); + } else { + free_str_list(&lst); + } + return r; +} + +static char *print_keyvalue_list(const m_option_t *opt, const void *src) +{ + char **lst = VAL(src); + char *ret = talloc_strdup(NULL, ""); + for (int n = 0; lst && lst[n] && lst[n + 1]; n += 2) { + if (ret[0]) + ret = talloc_strdup_append(ret, ","); + ret = talloc_asprintf_append(ret, "%s=%s", lst[n], lst[n + 1]); + } + return ret; +} + +static int keyvalue_list_set(const m_option_t *opt, void *dst, + struct mpv_node *src) +{ + if (src->format != MPV_FORMAT_NODE_MAP) + return M_OPT_UNKNOWN; + struct mpv_node_list *srclist = src->u.list; + for (int n = 0; n < srclist->num; n++) { + if (srclist->values[n].format != MPV_FORMAT_STRING) + return M_OPT_INVALID; + } + free_str_list(dst); + if (srclist->num > 0) { + VAL(dst) = talloc_array(NULL, char*, (srclist->num + 1) * 2); + for (int n = 0; n < srclist->num; n++) { + VAL(dst)[n * 2 + 0] = talloc_strdup(NULL, srclist->keys[n]); + VAL(dst)[n * 2 + 1] = talloc_strdup(NULL, srclist->values[n].u.string); + } + VAL(dst)[srclist->num * 2 + 0] = NULL; + VAL(dst)[srclist->num * 2 + 1] = NULL; + } + return 1; +} + +static int keyvalue_list_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + dst->format = MPV_FORMAT_NODE_MAP; + dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); + struct mpv_node_list *list = dst->u.list; + for (int n = 0; VAL(src) && VAL(src)[n * 2 + 0]; n++) { + MP_TARRAY_GROW(list, list->values, list->num); + MP_TARRAY_GROW(list, list->keys, list->num); + list->keys[list->num] = talloc_strdup(list, VAL(src)[n * 2 + 0]); + list->values[list->num] = (struct mpv_node){ + .format = MPV_FORMAT_STRING, + .u.string = talloc_strdup(list, VAL(src)[n * 2 + 1]), + }; + list->num++; + } + return 1; +} + +const m_option_type_t m_option_type_keyvalue_list = { + .name = "Key/value list", + .size = sizeof(char **), + .parse = parse_keyvalue_list, + .print = print_keyvalue_list, + .copy = copy_str_list, + .free = free_str_list, + .get = keyvalue_list_get, + .set = keyvalue_list_set, + .equal = str_list_equal, + .actions = (const struct m_option_action[]){ + {"add"}, + {"append"}, + {"set"}, + {"remove"}, + {0} + }, +}; + + +#undef VAL +#define VAL(x) (*(char **)(x)) + +static int check_msg_levels(struct mp_log *log, char **list) +{ + for (int n = 0; list && list[n * 2 + 0]; n++) { + char *level = list[n * 2 + 1]; + if (mp_msg_find_level(level) < 0 && strcmp(level, "no") != 0) { + mp_err(log, "Invalid message level '%s'\n", level); + return M_OPT_INVALID; + } + } + return 1; +} + +static int parse_msglevels(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (bstr_equals0(param, "help")) { + mp_info(log, "Syntax:\n\n --msg-level=module1=level,module2=level,...\n\n" + "'module' is output prefix as shown with -v, or a prefix\n" + "of it. level is one of:\n\n" + " fatal error warn info status v debug trace\n\n" + "The level specifies the minimum log level a message\n" + "must have to be printed.\n" + "The special module name 'all' affects all modules.\n"); + return M_OPT_EXIT; + } + + char **dst_copy = NULL; + int r = m_option_type_keyvalue_list.parse(log, opt, name, param, &dst_copy); + if (r >= 0) + r = check_msg_levels(log, dst_copy); + + if (r >= 0) + m_option_type_keyvalue_list.copy(opt, dst, &dst_copy); + m_option_type_keyvalue_list.free(&dst_copy); + return r; +} + +static int set_msglevels(const m_option_t *opt, void *dst, + struct mpv_node *src) +{ + char **dst_copy = NULL; + int r = m_option_type_keyvalue_list.set(opt, &dst_copy, src); + if (r >= 0) + r = check_msg_levels(mp_null_log, dst_copy); + + if (r >= 0) + m_option_type_keyvalue_list.copy(opt, dst, &dst_copy); + m_option_type_keyvalue_list.free(&dst_copy); + return r; +} + +const m_option_type_t m_option_type_msglevels = { + .name = "Output verbosity levels", + .size = sizeof(char **), + .parse = parse_msglevels, + .print = print_keyvalue_list, + .copy = copy_str_list, + .free = free_str_list, + .get = keyvalue_list_get, + .set = set_msglevels, + .equal = str_list_equal, +}; + +static int parse_print(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + ((m_opt_print_fn) opt->priv)(log); + return M_OPT_EXIT; +} + +const m_option_type_t m_option_type_print_fn = { + .name = "Print", + .flags = M_OPT_TYPE_OPTIONAL_PARAM, + .parse = parse_print, +}; + +static int parse_dummy_flag(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len) { + mp_err(log, "Invalid parameter for %.*s flag: %.*s\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_DISALLOW_PARAM; + } + return 0; +} + +const m_option_type_t m_option_type_dummy_flag = { + // can only be activated + .name = "Flag", + .flags = M_OPT_TYPE_OPTIONAL_PARAM, + .parse = parse_dummy_flag, +}; + +#undef VAL + +// Read s sub-option name, or a positional sub-opt value. +// termset is a string containing the set of chars that terminate an option. +// Return 0 on success, M_OPT_ error code otherwise. +// optname is for error reporting. +static int read_subparam(struct mp_log *log, bstr optname, char *termset, + bstr *str, bstr *out_subparam) +{ + bstr p = *str; + bstr subparam = {0}; + + if (bstr_eatstart0(&p, "\"")) { + int optlen = bstrcspn(p, "\""); + subparam = bstr_splice(p, 0, optlen); + p = bstr_cut(p, optlen); + if (!bstr_startswith0(p, "\"")) { + mp_err(log, "Terminating '\"' missing for '%.*s'\n", + BSTR_P(optname)); + return M_OPT_INVALID; + } + p = bstr_cut(p, 1); + } else if (bstr_eatstart0(&p, "[")) { + bstr s = p; + int balance = 1; + while (p.len && balance > 0) { + if (p.start[0] == '[') { + balance++; + } else if (p.start[0] == ']') { + balance--; + } + p = bstr_cut(p, 1); + } + if (balance != 0) { + mp_err(log, "Terminating ']' missing for '%.*s'\n", + BSTR_P(optname)); + return M_OPT_INVALID; + } + subparam = bstr_splice(s, 0, s.len - p.len - 1); + } else if (bstr_eatstart0(&p, "%")) { + int optlen = bstrtoll(p, &p, 0); + if (!bstr_startswith0(p, "%") || (optlen > p.len - 1)) { + mp_err(log, "Invalid length %d for '%.*s'\n", + optlen, BSTR_P(optname)); + return M_OPT_INVALID; + } + subparam = bstr_splice(p, 1, optlen + 1); + p = bstr_cut(p, optlen + 1); + } else { + // Skip until the next character that could possibly be a meta + // character in option parsing. + int optlen = bstrcspn(p, termset); + subparam = bstr_splice(p, 0, optlen); + p = bstr_cut(p, optlen); + } + + *str = p; + *out_subparam = subparam; + return 0; +} + +// Return 0 on success, otherwise error code +// On success, set *out_name and *out_val, and advance *str +// out_val.start is NULL if there was no parameter. +// optname is for error reporting. +static int split_subconf(struct mp_log *log, bstr optname, bstr *str, + bstr *out_name, bstr *out_val) +{ + bstr p = *str; + bstr subparam = {0}; + bstr subopt; + int r = read_subparam(log, optname, ":=,\\%\"'[]", &p, &subopt); + if (r < 0) + return r; + if (bstr_eatstart0(&p, "=")) { + r = read_subparam(log, subopt, ":=,\\%\"'[]", &p, &subparam); + if (r < 0) + return r; + } + *str = p; + *out_name = subopt; + *out_val = subparam; + return 0; +} + +#undef VAL + +// Split the string on the given split character. +// out_arr is at least max entries long. +// Return number of out_arr entries filled. +static int split_char(bstr str, unsigned char split, int max, bstr *out_arr) +{ + if (max < 1) + return 0; + + int count = 0; + while (1) { + int next = bstrchr(str, split); + if (next >= 0 && max - count > 1) { + out_arr[count++] = bstr_splice(str, 0, next); + str = bstr_cut(str, next + 1); + } else { + out_arr[count++] = str; + break; + } + } + return count; +} + +static int parse_color(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + bool is_help = bstr_equals0(param, "help"); + if (is_help) + goto exit; + + bstr val = param; + struct m_color color = {0}; + + if (bstr_eatstart0(&val, "#")) { + // #[AA]RRGGBB + if (val.len != 6 && val.len != 8) + goto exit; + bool has_alpha = val.len == 8; + uint32_t c = bstrtoll(val, &val, 16); + if (val.len) + goto exit; + color = (struct m_color) { + (c >> 16) & 0xFF, + (c >> 8) & 0xFF, + c & 0xFF, + has_alpha ? (c >> 24) & 0xFF : 0xFF, + }; + } else { + bstr comp_str[5]; + int num = split_char(param, '/', 5, comp_str); + if (num < 1 || num > 4) + goto exit; + double comp[4] = {0, 0, 0, 1}; + for (int n = 0; n < num; n++) { + bstr rest; + double d = bstrtod(comp_str[n], &rest); + if (rest.len || !comp_str[n].len || d < 0 || d > 1 || !isfinite(d)) + goto exit; + comp[n] = d; + } + if (num == 2) + comp[3] = comp[1]; + if (num < 3) + comp[2] = comp[1] = comp[0]; + color = (struct m_color) { comp[0] * 0xFF, comp[1] * 0xFF, + comp[2] * 0xFF, comp[3] * 0xFF }; + } + + if (dst) + *((struct m_color *)dst) = color; + + return 1; + +exit: + if (!is_help) { + mp_err(log, "Option %.*s: invalid color: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + } + mp_info(log, "Valid colors must be in the form #RRGGBB or #AARRGGBB (in hex)\n" + "or in the form 'r/g/b/a', where each component is a value in the\n" + "range 0.0-1.0. (Also allowed: 'gray', 'gray/a', 'r/g/b').\n"); + return is_help ? M_OPT_EXIT : M_OPT_INVALID; +} + +static char *print_color(const m_option_t *opt, const void *val) +{ + const struct m_color *c = val; + return talloc_asprintf(NULL, "#%02X%02X%02X%02X", c->a, c->r, c->g, c->b); +} + +static bool color_equal(const m_option_t *opt, void *a, void *b) +{ + struct m_color *ca = a; + struct m_color *cb = b; + return ca->a == cb->a && ca->r == cb->r && ca->g == cb->g && ca->b == cb->b; +} + +const m_option_type_t m_option_type_color = { + .name = "Color", + .size = sizeof(struct m_color), + .parse = parse_color, + .print = print_color, + .copy = copy_opt, + .equal = color_equal, +}; + + +// Parse a >=0 number starting at s. Set s to the string following the number. +// If the number ends with '%', eat that and set *out_per to true, but only +// if the number is between 0-100; if not, don't eat anything, even the number. +static bool eat_num_per(bstr *s, int *out_num, bool *out_per) +{ + bstr rest; + long long v = bstrtoll(*s, &rest, 10); + if (s->len == rest.len || v < INT_MIN || v > INT_MAX) + return false; + *out_num = v; + *out_per = false; + *s = rest; + if (bstr_eatstart0(&rest, "%") && v >= 0 && v <= 100) { + *out_per = true; + *s = rest; + } + return true; +} + +static bool parse_geometry_str(struct m_geometry *gm, bstr s) +{ + *gm = (struct m_geometry) { .x = INT_MIN, .y = INT_MIN }; + if (s.len == 0) + return true; + // Approximate grammar: + // [[W][xH]][{+-}X{+-}Y][/WS] | [X:Y] + // (meaning: [optional] {one character of} one|alternative) + // Every number can be followed by '%' + int num; + bool per; + +#define READ_NUM(F, F_PER) do { \ + if (!eat_num_per(&s, &num, &per)) \ + goto error; \ + gm->F = num; \ + gm->F_PER = per; \ +} while(0) + +#define READ_SIGN(F) do { \ + if (bstr_eatstart0(&s, "+")) { \ + gm->F = false; \ + } else if (bstr_eatstart0(&s, "-")) {\ + gm->F = true; \ + } else goto error; \ +} while(0) + + if (bstrchr(s, ':') < 0) { + gm->wh_valid = true; + if (!bstr_startswith0(s, "+") && !bstr_startswith0(s, "-")) { + if (!bstr_startswith0(s, "x")) + READ_NUM(w, w_per); + if (bstr_eatstart0(&s, "x")) + READ_NUM(h, h_per); + } + if (s.len > 0) { + gm->xy_valid = true; + READ_SIGN(x_sign); + READ_NUM(x, x_per); + READ_SIGN(y_sign); + READ_NUM(y, y_per); + } + if (bstr_eatstart0(&s, "/")) { + bstr rest; + long long v = bstrtoll(s, &rest, 10); + if (s.len == rest.len || v < 1 || v > INT_MAX) + goto error; + s = rest; + gm->ws = v; + } + } else { + gm->xy_valid = true; + READ_NUM(x, x_per); + if (!bstr_eatstart0(&s, ":")) + goto error; + READ_NUM(y, y_per); + } + + return s.len == 0; + +error: + return false; +} + +#undef READ_NUM +#undef READ_SIGN + +#define APPEND_PER(F, F_PER) \ + res = talloc_asprintf_append(res, "%d%s", gm->F, gm->F_PER ? "%" : "") + +static char *print_geometry(const m_option_t *opt, const void *val) +{ + const struct m_geometry *gm = val; + char *res = talloc_strdup(NULL, ""); + if (gm->wh_valid || gm->xy_valid) { + if (gm->wh_valid) { + APPEND_PER(w, w_per); + res = talloc_asprintf_append(res, "x"); + APPEND_PER(h, h_per); + } + if (gm->xy_valid) { + res = talloc_asprintf_append(res, gm->x_sign ? "-" : "+"); + APPEND_PER(x, x_per); + res = talloc_asprintf_append(res, gm->y_sign ? "-" : "+"); + APPEND_PER(y, y_per); + } + if (gm->ws > 0) + res = talloc_asprintf_append(res, "/%d", gm->ws); + } + return res; +} + +#undef APPEND_PER + +// xpos,ypos: position of the left upper corner +// widw,widh: width and height of the window +// scrw,scrh: width and height of the current screen +// The input parameters should be set to a centered window (default fallbacks). +void m_geometry_apply(int *xpos, int *ypos, int *widw, int *widh, + int scrw, int scrh, struct m_geometry *gm) +{ + if (gm->wh_valid) { + int prew = *widw, preh = *widh; + if (gm->w > 0) + *widw = gm->w_per ? scrw * (gm->w / 100.0) : gm->w; + if (gm->h > 0) + *widh = gm->h_per ? scrh * (gm->h / 100.0) : gm->h; + // keep aspect if the other value is not set + double asp = (double)prew / preh; + if (gm->w > 0 && !(gm->h > 0)) { + *widh = *widw / asp; + } else if (!(gm->w > 0) && gm->h > 0) { + *widw = *widh * asp; + } + // Center window after resize. If valid x:y values are passed to + // geometry, then those values will be overridden. + *xpos += prew / 2 - *widw / 2; + *ypos += preh / 2 - *widh / 2; + } + + if (gm->xy_valid) { + if (gm->x != INT_MIN) { + *xpos = gm->x; + if (gm->x_per) + *xpos = (scrw - *widw) * (*xpos / 100.0); + if (gm->x_sign) + *xpos = scrw - *widw - *xpos; + } + if (gm->y != INT_MIN) { + *ypos = gm->y; + if (gm->y_per) + *ypos = (scrh - *widh) * (*ypos / 100.0); + if (gm->y_sign) + *ypos = scrh - *widh - *ypos; + } + } +} + +static int parse_geometry(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + bool is_help = bstr_equals0(param, "help"); + if (is_help) + goto exit; + + struct m_geometry gm; + if (!parse_geometry_str(&gm, param)) + goto exit; + + if (dst) + *((struct m_geometry *)dst) = gm; + + return 1; + +exit: + if (!is_help) { + mp_err(log, "Option %.*s: invalid geometry: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + } + mp_info(log, + "Valid format: [W[%%][xH[%%]]][{+-}X[%%]{+-}Y[%%]] | [X[%%]:Y[%%]]\n"); + return is_help ? M_OPT_EXIT : M_OPT_INVALID; +} + +static bool geometry_equal(const m_option_t *opt, void *a, void *b) +{ + struct m_geometry *ga = a; + struct m_geometry *gb = b; + return ga->x == gb->x && ga->y == gb->y && ga->w == gb->w && ga->h == gb->h && + ga->xy_valid == gb->xy_valid && ga->wh_valid == gb->wh_valid && + ga->w_per == gb->w_per && ga->h_per == gb->h_per && + ga->x_per == gb->x_per && ga->y_per == gb->y_per && + ga->x_sign == gb->x_sign && ga->y_sign == gb->y_sign && + ga->ws == gb->ws; +} + +const m_option_type_t m_option_type_geometry = { + .name = "Window geometry", + .size = sizeof(struct m_geometry), + .parse = parse_geometry, + .print = print_geometry, + .copy = copy_opt, + .equal = geometry_equal, +}; + +static int parse_size_box(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + bool is_help = bstr_equals0(param, "help"); + if (is_help) + goto exit; + + struct m_geometry gm; + if (!parse_geometry_str(&gm, param)) + goto exit; + + if (gm.xy_valid) + goto exit; + + if (dst) + *((struct m_geometry *)dst) = gm; + + return 1; + +exit: + if (!is_help) { + mp_err(log, "Option %.*s: invalid size: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + } + mp_info(log, "Valid format: W[%%][xH[%%]] or empty string\n"); + return is_help ? M_OPT_EXIT : M_OPT_INVALID; +} + +const m_option_type_t m_option_type_size_box = { + .name = "Window size", + .size = sizeof(struct m_geometry), + .parse = parse_size_box, + .print = print_geometry, + .copy = copy_opt, + .equal = geometry_equal, +}; + +void m_rect_apply(struct mp_rect *rc, int w, int h, struct m_geometry *gm) +{ + *rc = (struct mp_rect){0, 0, w, h}; + if (!w || !h) + return; + m_geometry_apply(&rc->x0, &rc->y0, &rc->x1, &rc->y1, w, h, gm); + if (!gm->xy_valid && gm->wh_valid && rc->x1 == 0 && rc->y1 == 0) + return; + if (!gm->wh_valid || rc->x1 == 0 || rc->x1 == INT_MIN) + rc->x1 = w - rc->x0; + if (!gm->wh_valid || rc->y1 == 0 || rc->y1 == INT_MIN) + rc->y1 = h - rc->y0; + if (gm->wh_valid && (gm->w || gm->h)) + rc->x1 += rc->x0; + if (gm->wh_valid && (gm->w || gm->h)) + rc->y1 += rc->y0; +} + +static int parse_rect(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + bool is_help = bstr_equals0(param, "help"); + if (is_help) + goto exit; + + struct m_geometry gm; + if (!parse_geometry_str(&gm, param)) + goto exit; + + bool invalid = gm.x_sign || gm.y_sign || gm.ws; + invalid |= gm.wh_valid && (gm.w < 0 || gm.h < 0); + invalid |= gm.wh_valid && !gm.xy_valid && gm.w <= 0 && gm.h <= 0; + + if (invalid) + goto exit; + + if (dst) + *((struct m_geometry *)dst) = gm; + + return 1; + +exit: + if (!is_help) { + mp_err(log, "Option %.*s: invalid rect: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + } + mp_info(log, "Valid format: W[%%][xH[%%]][+x+y]\n"); + return is_help ? M_OPT_EXIT : M_OPT_INVALID; +} + +const m_option_type_t m_option_type_rect = { + .name = "Video rect", + .size = sizeof(struct m_geometry), + .parse = parse_rect, + .print = print_geometry, + .copy = copy_opt, + .equal = geometry_equal, +}; + +#include "video/img_format.h" + +static int parse_imgfmt(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + if (!bstrcmp0(param, "help")) { + mp_info(log, "Available formats:"); + char **list = mp_imgfmt_name_list(); + for (int i = 0; list[i]; i++) + mp_info(log, " %s", list[i]); + mp_info(log, " no"); + mp_info(log, "\n"); + talloc_free(list); + return M_OPT_EXIT; + } + + unsigned int fmt = mp_imgfmt_from_name(param); + if (!fmt && !bstr_equals0(param, "no")) { + mp_err(log, "Option %.*s: unknown format name: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + if (dst) + *((int *)dst) = fmt; + + return 1; +} + +static char *print_imgfmt(const m_option_t *opt, const void *val) +{ + int fmt = *(int *)val; + return talloc_strdup(NULL, fmt ? mp_imgfmt_to_name(fmt) : "no"); +} + +const m_option_type_t m_option_type_imgfmt = { + .name = "Image format", + .size = sizeof(int), + .parse = parse_imgfmt, + .print = print_imgfmt, + .copy = copy_opt, + .equal = int_equal, +}; + +static int parse_fourcc(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + unsigned int value; + + if (param.len == 4) { + uint8_t *s = param.start; + value = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24); + } else { + bstr rest; + value = bstrtoll(param, &rest, 16); + if (rest.len != 0) { + mp_err(log, "Option %.*s: invalid FourCC: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + } + + if (dst) + *((unsigned int *)dst) = value; + + return 1; +} + +static char *print_fourcc(const m_option_t *opt, const void *val) +{ + unsigned int fourcc = *(unsigned int *)val; + return talloc_asprintf(NULL, "%08x", fourcc); +} + +const m_option_type_t m_option_type_fourcc = { + .name = "FourCC", + .size = sizeof(unsigned int), + .parse = parse_fourcc, + .print = print_fourcc, + .copy = copy_opt, + .equal = int_equal, +}; + +#include "audio/format.h" + +static int parse_afmt(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + if (!bstrcmp0(param, "help")) { + mp_info(log, "Available formats:"); + for (int i = 1; i < AF_FORMAT_COUNT; i++) + mp_info(log, " %s", af_fmt_to_str(i)); + mp_info(log, "\n"); + return M_OPT_EXIT; + } + + int fmt = 0; + for (int i = 1; i < AF_FORMAT_COUNT; i++) { + if (bstr_equals0(param, af_fmt_to_str(i))) + fmt = i; + } + if (!fmt) { + mp_err(log, "Option %.*s: unknown format name: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + if (dst) + *((int *)dst) = fmt; + + return 1; +} + +static char *print_afmt(const m_option_t *opt, const void *val) +{ + int fmt = *(int *)val; + return talloc_strdup(NULL, fmt ? af_fmt_to_str(fmt) : "no"); +} + +const m_option_type_t m_option_type_afmt = { + .name = "Audio format", + .size = sizeof(int), + .parse = parse_afmt, + .print = print_afmt, + .copy = copy_opt, + .equal = int_equal, +}; + +#include "audio/chmap.h" + +static int parse_channels(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + bool limited = opt->flags & M_OPT_CHANNELS_LIMITED; + + struct m_channels res = {0}; + + if (bstr_equals0(param, "help")) { + mp_chmap_print_help(log); + if (!limited) { + mp_info(log, "\nOther values:\n" + " auto-safe\n"); + } + return M_OPT_EXIT; + } + + bool auto_safe = bstr_equals0(param, "auto-safe"); + if (bstr_equals0(param, "auto") || bstr_equals0(param, "empty") || auto_safe) { + if (limited) { + mp_err(log, "Disallowed parameter.\n"); + return M_OPT_INVALID; + } + param.len = 0; + res.set = true; + res.auto_safe = auto_safe; + } + + while (param.len) { + bstr item; + if (limited) { + item = param; + param.len = 0; + } else { + bstr_split_tok(param, ",", &item, ¶m); + } + + struct mp_chmap map = {0}; + if (!mp_chmap_from_str(&map, item) || !mp_chmap_is_valid(&map)) { + mp_err(log, "Invalid channel layout: %.*s\n", BSTR_P(item)); + talloc_free(res.chmaps); + return M_OPT_INVALID; + } + + MP_TARRAY_APPEND(NULL, res.chmaps, res.num_chmaps, map); + res.set = true; + } + + if (dst) { + *(struct m_channels *)dst = res; + } else { + talloc_free(res.chmaps); + } + + return 1; +} + +static char *print_channels(const m_option_t *opt, const void *val) +{ + const struct m_channels *ch = val; + if (!ch->set) + return talloc_strdup(NULL, ""); + if (ch->auto_safe) + return talloc_strdup(NULL, "auto-safe"); + if (ch->num_chmaps > 0) { + char *res = talloc_strdup(NULL, ""); + for (int n = 0; n < ch->num_chmaps; n++) { + if (n > 0) + res = talloc_strdup_append(res, ","); + res = talloc_strdup_append(res, mp_chmap_to_str(&ch->chmaps[n])); + } + return res; + } + return talloc_strdup(NULL, "auto"); +} + +static void free_channels(void *src) +{ + if (!src) + return; + + struct m_channels *ch = src; + talloc_free(ch->chmaps); + *ch = (struct m_channels){0}; +} + +static void copy_channels(const m_option_t *opt, void *dst, const void *src) +{ + if (!(dst && src)) + return; + + struct m_channels *ch = dst; + free_channels(dst); + *ch = *(struct m_channels *)src; + ch->chmaps = + talloc_memdup(NULL, ch->chmaps, sizeof(ch->chmaps[0]) * ch->num_chmaps); +} + +static bool channels_equal(const m_option_t *opt, void *a, void *b) +{ + struct m_channels *ca = a; + struct m_channels *cb = b; + + if (ca->set != cb->set || + ca->auto_safe != cb->auto_safe || + ca->num_chmaps != cb->num_chmaps) + return false; + + for (int n = 0; n < ca->num_chmaps; n++) { + if (!mp_chmap_equals(&ca->chmaps[n], &cb->chmaps[n])) + return false; + } + + return true; +} + +const m_option_type_t m_option_type_channels = { + .name = "Audio channels or channel map", + .size = sizeof(struct m_channels), + .parse = parse_channels, + .print = print_channels, + .copy = copy_channels, + .free = free_channels, + .equal = channels_equal, +}; + +static int parse_timestring(struct bstr str, double *time, char endchar) +{ + int h, m, len; + double s; + *time = 0; /* ensure initialization for error cases */ + bool neg = bstr_eatstart0(&str, "-"); + if (!neg) + bstr_eatstart0(&str, "+"); + if (bstrchr(str, '-') >= 0 || bstrchr(str, '+') >= 0) + return 0; /* the timestamp shouldn't contain anymore +/- after this point */ + if (bstr_sscanf(str, "%d:%d:%lf%n", &h, &m, &s, &len) >= 3) { + if (m >= 60 || s >= 60) + return 0; /* minutes or seconds are out of range */ + *time = 3600 * h + 60 * m + s; + } else if (bstr_sscanf(str, "%d:%lf%n", &m, &s, &len) >= 2) { + if (s >= 60) + return 0; /* seconds are out of range */ + *time = 60 * m + s; + } else if (bstr_sscanf(str, "%lf%n", &s, &len) >= 1) { + *time = s; + } else { + return 0; /* unsupported time format */ + } + if (len < str.len && str.start[len] != endchar) + return 0; /* invalid extra characters at the end */ + if (!isfinite(*time)) + return 0; + if (neg) + *time = -*time; + return len; +} + +#define HAS_NOPTS(opt) ((opt)->flags & M_OPT_ALLOW_NO) + +static int parse_time(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + double time = MP_NOPTS_VALUE; + if (HAS_NOPTS(opt) && bstr_equals0(param, "no")) { + // nothing + } else if (!parse_timestring(param, &time, 0)) { + mp_err(log, "Option %.*s: invalid time: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + } + + if (dst) + *(double *)dst = time; + return 1; +} + +static char *print_time(const m_option_t *opt, const void *val) +{ + double pts = *(double *)val; + if (pts == MP_NOPTS_VALUE && HAS_NOPTS(opt)) + return talloc_strdup(NULL, "no"); // symmetry with parsing + return talloc_asprintf(NULL, "%f", pts); +} + +static char *pretty_print_time(const m_option_t *opt, const void *val) +{ + double pts = *(double *)val; + if (pts == MP_NOPTS_VALUE && HAS_NOPTS(opt)) + return talloc_strdup(NULL, "no"); // symmetry with parsing + return mp_format_time(pts, false); +} + +static int time_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + if (HAS_NOPTS(opt) && src->format == MPV_FORMAT_STRING) { + if (strcmp(src->u.string, "no") == 0) { + *(double *)dst = MP_NOPTS_VALUE; + return 1; + } + } + return double_set(opt, dst, src); +} + +static int time_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + if (HAS_NOPTS(opt) && *(double *)src == MP_NOPTS_VALUE) { + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(ta_parent, "no"); + return 1; + } + return double_get(opt, ta_parent, dst, src); +} + +const m_option_type_t m_option_type_time = { + .name = "Time", + .size = sizeof(double), + .parse = parse_time, + .print = print_time, + .pretty_print = pretty_print_time, + .copy = copy_opt, + .add = add_double, + .set = time_set, + .get = time_get, + .equal = double_equal, +}; + + +// Relative time + +static int parse_rel_time(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + struct m_rel_time t = {0}; + + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + if (bstr_equals0(param, "none")) { + t.type = REL_TIME_NONE; + goto out; + } + + // Percent pos + if (bstr_endswith0(param, "%")) { + double percent = bstrtod(bstr_splice(param, 0, -1), ¶m); + if (param.len == 0 && percent >= 0 && percent <= 100) { + t.type = REL_TIME_PERCENT; + t.pos = percent; + goto out; + } + } + + // Chapter pos + if (bstr_startswith0(param, "#")) { + int chapter = bstrtoll(bstr_cut(param, 1), ¶m, 10); + if (param.len == 0 && chapter >= 1) { + t.type = REL_TIME_CHAPTER; + t.pos = chapter - 1; + goto out; + } + } + + double time; + if (parse_timestring(param, &time, 0)) { + if (bstr_startswith0(param, "+") || bstr_startswith0(param, "-")) { + t.type = REL_TIME_RELATIVE; + } else { + t.type = REL_TIME_ABSOLUTE; + } + t.pos = time; + goto out; + } + + mp_err(log, "Option %.*s: invalid time or position: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + return M_OPT_INVALID; + +out: + if (dst) + *(struct m_rel_time *)dst = t; + return 1; +} + +static char *print_rel_time(const m_option_t *opt, const void *val) +{ + const struct m_rel_time *t = val; + switch(t->type) { + case REL_TIME_ABSOLUTE: + return talloc_asprintf(NULL, "%g", t->pos); + case REL_TIME_RELATIVE: + return talloc_asprintf(NULL, "%s%g", + (t->pos >= 0) ? "+" : "-", fabs(t->pos)); + case REL_TIME_CHAPTER: + return talloc_asprintf(NULL, "#%g", t->pos); + case REL_TIME_PERCENT: + return talloc_asprintf(NULL, "%g%%", t->pos); + } + return talloc_strdup(NULL, "none"); +} + +static bool rel_time_equal(const m_option_t *opt, void *a, void *b) +{ + struct m_rel_time *ta = a; + struct m_rel_time *tb = b; + return ta->type == tb->type && ta->pos == tb->pos; +} + +const m_option_type_t m_option_type_rel_time = { + .name = "Relative time or percent position", + .size = sizeof(struct m_rel_time), + .parse = parse_rel_time, + .print = print_rel_time, + .copy = copy_opt, + .equal = rel_time_equal, +}; + + +//// Objects (i.e. filters, etc) settings + +#undef VAL +#define VAL(x) (*(m_obj_settings_t **)(x)) + +bool m_obj_list_find(struct m_obj_desc *dst, const struct m_obj_list *l, + bstr name) +{ + for (int i = 0; ; i++) { + if (!l->get_desc(dst, i)) + break; + if (bstr_equals0(name, dst->name)) + return true; + } + for (int i = 0; l->aliases[i][0]; i++) { + const char *aname = l->aliases[i][0]; + const char *alias = l->aliases[i][1]; + if (bstr_equals0(name, aname) && m_obj_list_find(dst, l, bstr0(alias))) + { + dst->replaced_name = aname; + return true; + } + } + return false; +} + +static void obj_setting_free(m_obj_settings_t *item) +{ + talloc_free(item->name); + talloc_free(item->label); + free_str_list(&(item->attribs)); +} + +// If at least one item has a label, compare labels only - otherwise ignore them. +static bool obj_setting_match(m_obj_settings_t *a, m_obj_settings_t *b) +{ + bstr la = bstr0(a->label), lb = bstr0(b->label); + if (la.len || lb.len) + return bstr_equals(la, lb); + + return m_obj_settings_equal(a, b); +} + +static int obj_settings_list_num_items(m_obj_settings_t *obj_list) +{ + int num = 0; + while (obj_list && obj_list[num].name) + num++; + return num; +} + +static void obj_settings_list_del_at(m_obj_settings_t **p_obj_list, int idx) +{ + m_obj_settings_t *obj_list = *p_obj_list; + int num = obj_settings_list_num_items(obj_list); + + assert(idx >= 0 && idx < num); + + obj_setting_free(&obj_list[idx]); + + // Note: the NULL-terminating element is moved down as part of this + memmove(&obj_list[idx], &obj_list[idx + 1], + sizeof(m_obj_settings_t) * (num - idx)); + + *p_obj_list = talloc_realloc(NULL, obj_list, struct m_obj_settings, num); +} + +// Insert such that *p_obj_list[idx] is set to item. +// If idx < 0, set idx = count + idx + 1 (i.e. -1 inserts it as last element). +// Memory referenced by *item is not copied. +static void obj_settings_list_insert_at(m_obj_settings_t **p_obj_list, int idx, + m_obj_settings_t *item) +{ + int num = obj_settings_list_num_items(*p_obj_list); + if (idx < 0) + idx = num + idx + 1; + assert(idx >= 0 && idx <= num); + *p_obj_list = talloc_realloc(NULL, *p_obj_list, struct m_obj_settings, + num + 2); + memmove(*p_obj_list + idx + 1, *p_obj_list + idx, + (num - idx) * sizeof(m_obj_settings_t)); + (*p_obj_list)[idx] = *item; + (*p_obj_list)[num + 1] = (m_obj_settings_t){0}; +} + +static int obj_settings_list_find_by_label(m_obj_settings_t *obj_list, + bstr label) +{ + for (int n = 0; obj_list && obj_list[n].name; n++) { + if (label.len && bstr_equals0(label, obj_list[n].label)) + return n; + } + return -1; +} + +static int obj_settings_list_find_by_label0(m_obj_settings_t *obj_list, + const char *label) +{ + return obj_settings_list_find_by_label(obj_list, bstr0(label)); +} + +static int obj_settings_find_by_content(m_obj_settings_t *obj_list, + m_obj_settings_t *item) +{ + for (int n = 0; obj_list && obj_list[n].name; n++) { + if (obj_setting_match(&obj_list[n], item)) + return n; + } + return -1; +} + +static void free_obj_settings_list(void *dst) +{ + int n; + m_obj_settings_t *d; + + if (!dst || !VAL(dst)) + return; + + d = VAL(dst); + for (n = 0; d[n].name; n++) + obj_setting_free(&d[n]); + talloc_free(d); + VAL(dst) = NULL; +} + +static void copy_obj_settings_list(const m_option_t *opt, void *dst, + const void *src) +{ + m_obj_settings_t *d, *s; + int n; + + if (!(dst && src)) + return; + + s = VAL(src); + + if (VAL(dst)) + free_obj_settings_list(dst); + if (!s) + return; + + for (n = 0; s[n].name; n++) + /* NOP */; + d = talloc_array(NULL, struct m_obj_settings, n + 1); + for (n = 0; s[n].name; n++) { + d[n].name = talloc_strdup(NULL, s[n].name); + d[n].label = talloc_strdup(NULL, s[n].label); + d[n].enabled = s[n].enabled; + d[n].attribs = NULL; + copy_str_list(NULL, &(d[n].attribs), &(s[n].attribs)); + } + d[n].name = NULL; + d[n].label = NULL; + d[n].attribs = NULL; + VAL(dst) = d; +} + +// Consider -vf a=b=c:d=e. This verifies "b"="c" and "d"="e" and that the +// option names/values are correct. Try to determine whether an option +// without '=' sets a flag, or whether it's a positional argument. +static int get_obj_param(struct mp_log *log, bstr opt_name, bstr obj_name, + struct m_config *config, bstr name, bstr val, + int flags, bool nopos, + int *nold, bstr *out_name, bstr *out_val, + char *tmp, size_t tmp_size) +{ + int r; + + if (!config) { + // Duplicates the logic below, but with unknown parameter types/names. + if (val.start || nopos) { + *out_name = name; + *out_val = val; + } else { + val = name; + // positional fields + if (val.len == 0) { // Empty field, count it and go on + (*nold)++; + return 0; + } + // Positional naming convention for/followed by mp_set_avopts(). + snprintf(tmp, tmp_size, "@%d", *nold); + *out_name = bstr0(tmp); + *out_val = val; + (*nold)++; + } + return 1; + } + + // val.start != NULL => of the form name=val (not positional) + // If it's just "name", and the associated option exists and is a flag, + // don't accept it as positional argument. + if (val.start || m_config_option_requires_param(config, name) == 0 || nopos) { + r = m_config_set_option_cli(config, name, val, flags); + if (r < 0) { + if (r == M_OPT_UNKNOWN) { + mp_err(log, "Option %.*s: %.*s doesn't have a %.*s parameter.\n", + BSTR_P(opt_name), BSTR_P(obj_name), BSTR_P(name)); + return M_OPT_UNKNOWN; + } + if (r != M_OPT_EXIT) + mp_err(log, "Option %.*s: " + "Error while parsing %.*s parameter %.*s (%.*s)\n", + BSTR_P(opt_name), BSTR_P(obj_name), BSTR_P(name), + BSTR_P(val)); + return r; + } + *out_name = name; + *out_val = val; + return 1; + } else { + val = name; + // positional fields + if (val.len == 0) { // Empty field, count it and go on + (*nold)++; + return 0; + } + const char *opt = m_config_get_positional_option(config, *nold); + if (!opt) { + mp_err(log, "Option %.*s: %.*s has only %d " + "params, so you can't give more than %d unnamed params.\n", + BSTR_P(opt_name), BSTR_P(obj_name), *nold, *nold); + return M_OPT_OUT_OF_RANGE; + } + r = m_config_set_option_cli(config, bstr0(opt), val, flags); + if (r < 0) { + if (r != M_OPT_EXIT) + mp_err(log, "Option %.*s: " + "Error while parsing %.*s parameter %s (%.*s)\n", + BSTR_P(opt_name), BSTR_P(obj_name), opt, BSTR_P(val)); + return r; + } + *out_name = bstr0(opt); + *out_val = val; + (*nold)++; + return 1; + } +} + +// Consider -vf a=b:c:d. This parses "b:c:d" into name/value pairs, stored as +// linear array in *_ret. In particular, config contains what options a the +// object takes, and verifies the option values as well. +// If config is NULL, all parameters are accepted without checking. +// _ret set to NULL can be used for checking-only. +// flags can contain any M_SETOPT_* flag. +// desc is optional. +static int m_obj_parse_sub_config(struct mp_log *log, struct bstr opt_name, + struct bstr name, struct bstr *pstr, + struct m_config *config, int flags, bool nopos, + struct m_obj_desc *desc, + const struct m_obj_list *list, char ***ret) +{ + int nold = 0; + char **args = NULL; + int num_args = 0; + int r = 1; + char tmp[80]; + + if (ret) { + args = *ret; + while (args && args[num_args]) + num_args++; + } + + while (pstr->len > 0) { + bstr fname, fval; + r = split_subconf(log, opt_name, pstr, &fname, &fval); + if (r < 0) + goto exit; + + if (list->use_global_options) { + mp_err(log, "Option %.*s: this option does not accept sub-options.\n", + BSTR_P(opt_name)); + mp_err(log, "Sub-options for --vo and --ao were removed from mpv in " + "release 0.23.0.\nSee https://0x0.st/uM for details.\n"); + r = M_OPT_INVALID; + goto exit; + } + + if (bstr_equals0(fname, "help")) + goto print_help; + r = get_obj_param(log, opt_name, name, config, fname, fval, flags, + nopos, &nold, &fname, &fval, tmp, sizeof(tmp)); + if (r < 0) + goto exit; + + if (r > 0 && ret) { + MP_TARRAY_APPEND(NULL, args, num_args, bstrto0(NULL, fname)); + MP_TARRAY_APPEND(NULL, args, num_args, bstrto0(NULL, fval)); + MP_TARRAY_APPEND(NULL, args, num_args, NULL); + MP_TARRAY_APPEND(NULL, args, num_args, NULL); + num_args -= 2; + } + + if (!bstr_eatstart0(pstr, ":")) + break; + } + + if (ret) { + if (num_args > 0) { + *ret = args; + args = NULL; + } else { + *ret = NULL; + } + } + + goto exit; + +print_help: ; + if (config) { + if (desc->print_help) + desc->print_help(log); + m_config_print_option_list(config, "*"); + } else if (list->print_unknown_entry_help) { + list->print_unknown_entry_help(log, mp_tprintf(80, "%.*s", BSTR_P(name))); + } else { + mp_warn(log, "Option %.*s: item %.*s doesn't exist.\n", + BSTR_P(opt_name), BSTR_P(name)); + } + r = M_OPT_EXIT; + +exit: + free_str_list(&args); + return r; +} + +// Characters which may appear in a filter name +#define NAMECH "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" + +// Parse one item, e.g. -vf a=b:c:d,e=f:g => parse a=b:c:d into "a" and "b:c:d" +static int parse_obj_settings(struct mp_log *log, struct bstr opt, int op, + struct bstr *pstr, const struct m_obj_list *list, + m_obj_settings_t **_ret) +{ + int r; + char **plist = NULL; + struct m_obj_desc desc; + bstr str = {0}; + bstr label = {0}; + bool nopos = list->disallow_positional_parameters; + bool enabled = true; + + if (bstr_eatstart0(pstr, "@")) { + bstr rest; + if (!bstr_split_tok(*pstr, ":", &label, &rest)) { + // "@labelname" is the special enable/disable toggle syntax + if (op == OP_TOGGLE) { + int idx = bstrspn(*pstr, NAMECH); + label = bstr_splice(*pstr, 0, idx); + if (label.len) { + *pstr = bstr_cut(*pstr, idx); + goto done; + } + } + mp_err(log, "Option %.*s: ':' expected after label.\n", BSTR_P(opt)); + return M_OPT_INVALID; + } + *pstr = rest; + if (label.len == 0) { + mp_err(log, "Option %.*s: label name expected.\n", BSTR_P(opt)); + return M_OPT_INVALID; + } + } + + if (list->allow_disable_entries && bstr_eatstart0(pstr, "!")) + enabled = false; + + bool has_param = false; + int idx = bstrspn(*pstr, NAMECH); + str = bstr_splice(*pstr, 0, idx); + if (!str.len) { + mp_err(log, "Option %.*s: filter name expected.\n", BSTR_P(opt)); + return M_OPT_INVALID; + } + *pstr = bstr_cut(*pstr, idx); + // video filters use "=", VOs use ":" + if (bstr_eatstart0(pstr, "=") || bstr_eatstart0(pstr, ":")) + has_param = true; + + bool skip = false; + if (m_obj_list_find(&desc, list, str)) { + if (desc.replaced_name) + mp_warn(log, "Driver '%s' has been replaced with '%s'!\n", + desc.replaced_name, desc.name); + } else { + char name[80]; + snprintf(name, sizeof(name), "%.*s", BSTR_P(str)); + if (list->check_unknown_entry && !list->check_unknown_entry(name)) { + mp_err(log, "Option %.*s: %.*s doesn't exist.\n", + BSTR_P(opt), BSTR_P(str)); + return M_OPT_INVALID; + } + desc = (struct m_obj_desc){0}; + skip = true; + } + + if (has_param) { + struct m_config *config = NULL; + if (!skip) + config = m_config_from_obj_desc_noalloc(NULL, log, &desc); + r = m_obj_parse_sub_config(log, opt, str, pstr, config, + M_SETOPT_CHECK_ONLY, nopos, &desc, list, + _ret ? &plist : NULL); + talloc_free(config); + if (r < 0) + return r; + } + if (!_ret) + return 1; + +done: ; + m_obj_settings_t item = { + .name = bstrto0(NULL, str), + .label = bstrdup0(NULL, label), + .enabled = enabled, + .attribs = plist, + }; + obj_settings_list_insert_at(_ret, -1, &item); + return 1; +} + +// Parse a single entry for -vf-remove (return 0 if not applicable) +// mark_del is bounded by the number of items in dst +static int parse_obj_settings_del(struct mp_log *log, struct bstr opt_name, + struct bstr *param, void *dst, bool *mark_del) +{ + bstr s = *param; + if (bstr_eatstart0(&s, "@")) { + // '@name:' -> parse as normal filter entry + // '@name,' or '@name<end>' -> parse here + int idx = bstrspn(s, NAMECH); + bstr label = bstr_splice(s, 0, idx); + s = bstr_cut(s, idx); + if (bstr_startswith0(s, ":")) + return 0; + if (dst) { + int label_index = 0; + label_index = obj_settings_list_find_by_label(VAL(dst), label); + if (label_index >= 0) { + mark_del[label_index] = true; + } else { + mp_warn(log, "Option %.*s: item label @%.*s not found.\n", + BSTR_P(opt_name), BSTR_P(label)); + } + } + *param = s; + return 1; + } + return 0; +} + +static int parse_obj_settings_list(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + m_obj_settings_t *res = NULL; + int op = OP_NONE; + bool *mark_del = NULL; + int num_items = obj_settings_list_num_items(dst ? VAL(dst) : 0); + const struct m_obj_list *ol = opt->priv; + + assert(opt->priv); + + if (bstr_endswith0(name, "-add")) { + op = OP_ADD; + } else if (bstr_endswith0(name, "-append")) { + op = OP_APPEND; + } else if (bstr_endswith0(name, "-set")) { + op = OP_NONE; + } else if (bstr_endswith0(name, "-pre")) { + op = OP_PRE; + } else if (bstr_endswith0(name, "-remove")) { + op = OP_REMOVE; + } else if (bstr_endswith0(name, "-clr")) { + op = OP_CLR; + } else if (bstr_endswith0(name, "-toggle")) { + op = OP_TOGGLE; + } else if (bstr_endswith0(name, "-help")) { + mp_err(log, "Option %s:\n" + "Supported operations are:\n" + " %s-set\n" + " Overwrite the old list with the given list\n\n" + " %s-append\n" + " Append the given filter to the current list\n\n" + " %s-add\n" + " Append the given list to the current list\n\n" + " %s-pre\n" + " Prepend the given list to the current list\n\n" + " %s-remove\n" + " Remove the given filter from the current list\n\n" + " %s-toggle\n" + " Add the filter to the list, or remove it if it's already added.\n\n" + " %s-clr\n" + " Clear the current list.\n\n", + opt->name, opt->name, opt->name, opt->name, opt->name, + opt->name, opt->name, opt->name); + + return M_OPT_EXIT; + } + + if (!bstrcmp0(param, "help")) { + mp_info(log, "Available %s:\n", ol->description); + for (int n = 0; ; n++) { + struct m_obj_desc desc; + if (!ol->get_desc(&desc, n)) + break; + if (!desc.hidden) { + mp_info(log, " %-16s %s\n", + desc.name, desc.description); + } + } + mp_info(log, "\n"); + if (ol->print_help_list) + ol->print_help_list(log); + if (!ol->use_global_options) { + mp_info(log, "Get help on individual entries via: --%s=entry=help\n", + opt->name); + } + return M_OPT_EXIT; + } + + if (op == OP_CLR) { + if (param.len) { + mp_err(log, "Option %.*s: -clr does not take an argument.\n", + BSTR_P(name)); + return M_OPT_INVALID; + } + if (dst) + free_obj_settings_list(dst); + return 0; + } else if (op == OP_REMOVE) { + mark_del = talloc_zero_array(NULL, bool, num_items + 1); + } + + if (op != OP_NONE && param.len == 0) + return M_OPT_MISSING_PARAM; + + while (param.len > 0) { + int r = 0; + if (op == OP_REMOVE) + r = parse_obj_settings_del(log, name, ¶m, dst, mark_del); + if (r == 0) { + r = parse_obj_settings(log, name, op, ¶m, ol, dst ? &res : NULL); + } + if (r < 0) + return r; + if (param.len > 0) { + const char sep[2] = {OPTION_LIST_SEPARATOR, 0}; + if (!bstr_eatstart0(¶m, sep)) + return M_OPT_INVALID; + if (param.len == 0) { + if (!ol->allow_trailer) + return M_OPT_INVALID; + if (dst) { + m_obj_settings_t item = { + .name = talloc_strdup(NULL, ""), + }; + obj_settings_list_insert_at(&res, -1, &item); + } + } + } + } + + if (op != OP_NONE && res && res[0].name && res[1].name) { + if (op == OP_APPEND) { + mp_err(log, "Option %.*s: -append takes only 1 filter (no ',').\n", + BSTR_P(name)); + return M_OPT_INVALID; + } + mp_warn(log, "Passing more than 1 argument to %.*s is deprecated!\n", + BSTR_P(name)); + } + + if (dst) { + m_obj_settings_t *list = VAL(dst); + if (op == OP_PRE) { + int prepend_counter = 0; + for (int n = 0; res && res[n].name; n++) { + int label = obj_settings_list_find_by_label0(list, res[n].label); + if (label < 0) { + obj_settings_list_insert_at(&list, prepend_counter, &res[n]); + prepend_counter++; + } else { + // Prefer replacement semantics, instead of actually + // prepending. + obj_setting_free(&list[label]); + list[label] = res[n]; + } + } + talloc_free(res); + } else if (op == OP_ADD || op == OP_APPEND) { + for (int n = 0; res && res[n].name; n++) { + int label = obj_settings_list_find_by_label0(list, res[n].label); + if (label < 0) { + obj_settings_list_insert_at(&list, -1, &res[n]); + } else { + // Prefer replacement semantics, instead of actually + // appending. + obj_setting_free(&list[label]); + list[label] = res[n]; + } + } + talloc_free(res); + } else if (op == OP_TOGGLE) { + for (int n = 0; res && res[n].name; n++) { + if (res[n].label && !res[n].name[0]) { + // Toggle enable/disable special case. + int found = + obj_settings_list_find_by_label0(list, res[n].label); + if (found < 0) { + mp_warn(log, "Option %.*s: Label %s not found\n", + BSTR_P(name), res[n].label); + } else { + list[found].enabled = !list[found].enabled; + } + obj_setting_free(&res[n]); + } else { + int found = obj_settings_find_by_content(list, &res[n]); + if (found < 0) { + obj_settings_list_insert_at(&list, -1, &res[n]); + } else { + obj_settings_list_del_at(&list, found); + obj_setting_free(&res[n]); + } + } + } + talloc_free(res); + } else if (op == OP_REMOVE) { + for (int n = num_items - 1; n >= 0; n--) { + if (mark_del[n]) + obj_settings_list_del_at(&list, n); + } + for (int n = 0; res && res[n].name; n++) { + int found = obj_settings_find_by_content(list, &res[n]); + if (found >= 0) + obj_settings_list_del_at(&list, found); + } + free_obj_settings_list(&res); + } else { + assert(op == OP_NONE); + free_obj_settings_list(&list); + list = res; + } + VAL(dst) = list; + } + + talloc_free(mark_del); + return 1; +} + +static void append_param(char **res, char *param) +{ + if (strspn(param, NAMECH) == strlen(param)) { + *res = talloc_strdup_append(*res, param); + } else { + // Simple escaping: %BYTECOUNT%STRING + *res = talloc_asprintf_append(*res, "%%%zd%%%s", strlen(param), param); + } +} + +static char *print_obj_settings_list(const m_option_t *opt, const void *val) +{ + m_obj_settings_t *list = VAL(val); + char *res = talloc_strdup(NULL, ""); + for (int n = 0; list && list[n].name; n++) { + m_obj_settings_t *entry = &list[n]; + if (n > 0) + res = talloc_strdup_append(res, ","); + // Assume labels and names don't need escaping + if (entry->label && entry->label[0]) + res = talloc_asprintf_append(res, "@%s:", entry->label); + if (!entry->enabled) + res = talloc_strdup_append(res, "!"); + res = talloc_strdup_append(res, entry->name); + if (entry->attribs && entry->attribs[0]) { + res = talloc_strdup_append(res, "="); + for (int i = 0; entry->attribs[i * 2 + 0]; i++) { + if (i > 0) + res = talloc_strdup_append(res, ":"); + append_param(&res, entry->attribs[i * 2 + 0]); + res = talloc_strdup_append(res, "="); + append_param(&res, entry->attribs[i * 2 + 1]); + } + } + } + return res; +} + +static int set_obj_settings_list(const m_option_t *opt, void *dst, + struct mpv_node *src) +{ + if (src->format != MPV_FORMAT_NODE_ARRAY) + return M_OPT_INVALID; + m_obj_settings_t *entries = + talloc_zero_array(NULL, m_obj_settings_t, src->u.list->num + 1); + for (int n = 0; n < src->u.list->num; n++) { + m_obj_settings_t *entry = &entries[n]; + entry->enabled = true; + if (src->u.list->values[n].format != MPV_FORMAT_NODE_MAP) + goto error; + struct mpv_node_list *src_entry = src->u.list->values[n].u.list; + for (int i = 0; i < src_entry->num; i++) { + const char *key = src_entry->keys[i]; + struct mpv_node *val = &src_entry->values[i]; + if (strcmp(key, "name") == 0) { + if (val->format != MPV_FORMAT_STRING) + goto error; + entry->name = talloc_strdup(NULL, val->u.string); + } else if (strcmp(key, "label") == 0) { + if (val->format != MPV_FORMAT_STRING) + goto error; + entry->label = talloc_strdup(NULL, val->u.string); + } else if (strcmp(key, "enabled") == 0) { + if (val->format != MPV_FORMAT_FLAG) + goto error; + entry->enabled = val->u.flag; + } else if (strcmp(key, "params") == 0) { + if (val->format != MPV_FORMAT_NODE_MAP) + goto error; + struct mpv_node_list *src_params = val->u.list; + entry->attribs = + talloc_zero_array(NULL, char*, (src_params->num + 1) * 2); + for (int x = 0; x < src_params->num; x++) { + if (src_params->values[x].format != MPV_FORMAT_STRING) + goto error; + entry->attribs[x * 2 + 0] = + talloc_strdup(NULL, src_params->keys[x]); + entry->attribs[x * 2 + 1] = + talloc_strdup(NULL, src_params->values[x].u.string); + } + } + } + } + free_obj_settings_list(dst); + VAL(dst) = entries; + return 0; +error: + free_obj_settings_list(&entries); + return M_OPT_INVALID; +} + +static struct mpv_node *add_array_entry(struct mpv_node *dst) +{ + struct mpv_node_list *list = dst->u.list; + assert(dst->format == MPV_FORMAT_NODE_ARRAY&& dst->u.list); + MP_TARRAY_GROW(list, list->values, list->num); + return &list->values[list->num++]; +} + +static struct mpv_node *add_map_entry(struct mpv_node *dst, const char *key) +{ + struct mpv_node_list *list = dst->u.list; + assert(dst->format == MPV_FORMAT_NODE_MAP && dst->u.list); + MP_TARRAY_GROW(list, list->values, list->num); + MP_TARRAY_GROW(list, list->keys, list->num); + list->keys[list->num] = talloc_strdup(list, key); + return &list->values[list->num++]; +} + +static void add_map_string(struct mpv_node *dst, const char *key, const char *val) +{ + struct mpv_node *entry = add_map_entry(dst, key); + entry->format = MPV_FORMAT_STRING; + entry->u.string = talloc_strdup(dst->u.list, val); +} + +static int get_obj_settings_list(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *val) +{ + m_obj_settings_t *list = VAL(val); + dst->format = MPV_FORMAT_NODE_ARRAY; + dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); + ta_parent = dst->u.list; + for (int n = 0; list && list[n].name; n++) { + m_obj_settings_t *entry = &list[n]; + struct mpv_node *nentry = add_array_entry(dst); + nentry->format = MPV_FORMAT_NODE_MAP; + nentry->u.list = talloc_zero(ta_parent, struct mpv_node_list); + add_map_string(nentry, "name", entry->name); + if (entry->label && entry->label[0]) + add_map_string(nentry, "label", entry->label); + struct mpv_node *enabled = add_map_entry(nentry, "enabled"); + enabled->format = MPV_FORMAT_FLAG; + enabled->u.flag = entry->enabled; + struct mpv_node *params = add_map_entry(nentry, "params"); + params->format = MPV_FORMAT_NODE_MAP; + params->u.list = talloc_zero(ta_parent, struct mpv_node_list); + for (int i = 0; entry->attribs && entry->attribs[i * 2 + 0]; i++) { + add_map_string(params, entry->attribs[i * 2 + 0], + entry->attribs[i * 2 + 1]); + } + } + return 1; +} + +static bool obj_settings_list_equal(const m_option_t *opt, void *pa, void *pb) +{ + struct m_obj_settings *a = VAL(pa); + struct m_obj_settings *b = VAL(pb); + + if (a == b || !a || !b) + return a == b || (!a && !b[0].name) || (!b && !a[0].name); + + for (int n = 0; a[n].name || b[n].name; n++) { + if (!a[n].name || !b[n].name) + return false; + if (!m_obj_settings_equal(&a[n], &b[n])) + return false; + } + + return true; +} + +bool m_obj_settings_equal(struct m_obj_settings *a, struct m_obj_settings *b) +{ + if (!str_equal(NULL, &a->name, &b->name)) + return false; + + if (!str_equal(NULL, &a->label, &b->label)) + return false; + + if (a->enabled != b->enabled) + return false; + + return str_list_equal(NULL, &a->attribs, &b->attribs); +} + +const m_option_type_t m_option_type_obj_settings_list = { + .name = "Object settings list", + .size = sizeof(m_obj_settings_t *), + .parse = parse_obj_settings_list, + .print = print_obj_settings_list, + .copy = copy_obj_settings_list, + .free = free_obj_settings_list, + .set = set_obj_settings_list, + .get = get_obj_settings_list, + .equal = obj_settings_list_equal, + .actions = (const struct m_option_action[]){ + {"add"}, + {"append"}, + {"clr", M_OPT_TYPE_OPTIONAL_PARAM}, + {"help", M_OPT_TYPE_OPTIONAL_PARAM}, + {"pre"}, + {"set"}, + {"toggle"}, + {"remove"}, + {0} + }, +}; + +#undef VAL +#define VAL(x) (*(struct mpv_node *)(x)) + +static int parse_node(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + // Maybe use JSON? + mp_err(log, "option type doesn't accept strings"); + return M_OPT_INVALID; +} + +static char *print_node(const m_option_t *opt, const void *val) +{ + char *t = talloc_strdup(NULL, ""); + if (json_write(&t, &VAL(val)) < 0) { + talloc_free(t); + t = NULL; + } + return t; +} + +static char *pretty_print_node(const m_option_t *opt, const void *val) +{ + char *t = talloc_strdup(NULL, ""); + if (json_write_pretty(&t, &VAL(val)) < 0) { + talloc_free(t); + t = NULL; + } + return t; +} + +static void dup_node(void *ta_parent, struct mpv_node *node) +{ + switch (node->format) { + case MPV_FORMAT_STRING: + node->u.string = talloc_strdup(ta_parent, node->u.string); + break; + case MPV_FORMAT_NODE_ARRAY: + case MPV_FORMAT_NODE_MAP: { + struct mpv_node_list *oldlist = node->u.list; + struct mpv_node_list *new = talloc_zero(ta_parent, struct mpv_node_list); + node->u.list = new; + if (oldlist->num > 0) { + *new = *oldlist; + new->values = talloc_array(new, struct mpv_node, new->num); + for (int n = 0; n < new->num; n++) { + new->values[n] = oldlist->values[n]; + dup_node(new, &new->values[n]); + } + if (node->format == MPV_FORMAT_NODE_MAP) { + new->keys = talloc_array(new, char*, new->num); + for (int n = 0; n < new->num; n++) + new->keys[n] = talloc_strdup(new, oldlist->keys[n]); + } + } + break; + } + case MPV_FORMAT_BYTE_ARRAY: { + struct mpv_byte_array *old = node->u.ba; + struct mpv_byte_array *new = talloc_zero(ta_parent, struct mpv_byte_array); + node->u.ba = new; + if (old->size > 0) { + *new = *old; + new->data = talloc_memdup(new, old->data, old->size); + } + break; + } + case MPV_FORMAT_NONE: + case MPV_FORMAT_FLAG: + case MPV_FORMAT_INT64: + case MPV_FORMAT_DOUBLE: + break; + default: + // unknown entry - mark as invalid + node->format = (mpv_format)-1; + } +} + +static void copy_node(const m_option_t *opt, void *dst, const void *src) +{ + assert(sizeof(struct mpv_node) <= sizeof(union m_option_value)); + + if (!(dst && src)) + return; + + opt->type->free(dst); + VAL(dst) = VAL(src); + dup_node(NULL, &VAL(dst)); +} + +void *node_get_alloc(struct mpv_node *node) +{ + // Assume it was allocated with copy_node(), which allocates all + // sub-nodes with the parent node as talloc parent. + switch (node->format) { + case MPV_FORMAT_STRING: + return node->u.string; + case MPV_FORMAT_NODE_ARRAY: + case MPV_FORMAT_NODE_MAP: + return node->u.list; + default: + return NULL; + } +} + +static void free_node(void *src) +{ + if (src) { + struct mpv_node *node = &VAL(src); + talloc_free(node_get_alloc(node)); + *node = (struct mpv_node){{0}}; + } +} + +// idempotent functions for convenience +static int node_set(const m_option_t *opt, void *dst, struct mpv_node *src) +{ + copy_node(opt, dst, src); + return 1; +} + +static int node_get(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + *dst = VAL(src); + dup_node(ta_parent, dst); + return 1; +} + +static bool node_equal(const m_option_t *opt, void *a, void *b) +{ + return equal_mpv_node(&VAL(a), &VAL(b)); +} + +const m_option_type_t m_option_type_node = { + .name = "Complex", + .size = sizeof(struct mpv_node), + .parse = parse_node, + .print = print_node, + .pretty_print = pretty_print_node, + .copy = copy_node, + .free = free_node, + .set = node_set, + .get = node_get, + .equal = node_equal, +}; + +// Special-cased by m_config.c. +const m_option_type_t m_option_type_alias = { + .name = "alias", +}; +const m_option_type_t m_option_type_cli_alias = { + .name = "alias", +}; +const m_option_type_t m_option_type_removed = { + .name = "removed", +}; +const m_option_type_t m_option_type_subconfig = { + .name = "Subconfig", +}; diff --git a/options/m_option.h b/options/m_option.h new file mode 100644 index 0000000..e62fa0f --- /dev/null +++ b/options/m_option.h @@ -0,0 +1,764 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_M_OPTION_H +#define MPLAYER_M_OPTION_H + +#include <float.h> +#include <string.h> +#include <stddef.h> +#include <stdbool.h> + +#include "misc/bstr.h" +#include "audio/chmap.h" +#include "common/common.h" + +// m_option allows to parse, print and copy data of various types. + +typedef struct m_option_type m_option_type_t; +typedef struct m_option m_option_t; +struct m_config; +struct mp_log; +struct mpv_node; +struct mpv_global; + +///////////////////////////// Options types declarations //////////////////// + +// Simple types +extern const m_option_type_t m_option_type_bool; +extern const m_option_type_t m_option_type_flag; +extern const m_option_type_t m_option_type_dummy_flag; +extern const m_option_type_t m_option_type_int; +extern const m_option_type_t m_option_type_int64; +extern const m_option_type_t m_option_type_byte_size; +extern const m_option_type_t m_option_type_float; +extern const m_option_type_t m_option_type_double; +extern const m_option_type_t m_option_type_string; +extern const m_option_type_t m_option_type_string_list; +extern const m_option_type_t m_option_type_string_append_list; +extern const m_option_type_t m_option_type_keyvalue_list; +extern const m_option_type_t m_option_type_time; +extern const m_option_type_t m_option_type_rel_time; +extern const m_option_type_t m_option_type_choice; +extern const m_option_type_t m_option_type_flags; +extern const m_option_type_t m_option_type_msglevels; +extern const m_option_type_t m_option_type_print_fn; +extern const m_option_type_t m_option_type_imgfmt; +extern const m_option_type_t m_option_type_fourcc; +extern const m_option_type_t m_option_type_afmt; +extern const m_option_type_t m_option_type_color; +extern const m_option_type_t m_option_type_geometry; +extern const m_option_type_t m_option_type_size_box; +extern const m_option_type_t m_option_type_channels; +extern const m_option_type_t m_option_type_aspect; +extern const m_option_type_t m_option_type_obj_settings_list; +extern const m_option_type_t m_option_type_node; +extern const m_option_type_t m_option_type_rect; + +// Used internally by m_config.c +extern const m_option_type_t m_option_type_alias; +extern const m_option_type_t m_option_type_cli_alias; +extern const m_option_type_t m_option_type_removed; +extern const m_option_type_t m_option_type_subconfig; + +// Callback used by m_option_type_print_fn options. +typedef void (*m_opt_print_fn)(struct mp_log *log); + +enum m_rel_time_type { + REL_TIME_NONE, + REL_TIME_ABSOLUTE, + REL_TIME_RELATIVE, + REL_TIME_PERCENT, + REL_TIME_CHAPTER, +}; + +struct m_rel_time { + double pos; + enum m_rel_time_type type; +}; + +struct m_color { + uint8_t r, g, b, a; +}; + +struct m_geometry { + int x, y, w, h; + bool xy_valid : 1, wh_valid : 1; + bool w_per : 1, h_per : 1; + bool x_sign : 1, y_sign : 1, x_per : 1, y_per : 1; + int ws; // workspace; valid if !=0 +}; + +void m_geometry_apply(int *xpos, int *ypos, int *widw, int *widh, + int scrw, int scrh, struct m_geometry *gm); +void m_rect_apply(struct mp_rect *rc, int w, int h, struct m_geometry *gm); + +struct m_channels { + bool set : 1; + bool auto_safe : 1; + struct mp_chmap *chmaps; + int num_chmaps; +}; + +struct m_obj_desc { + // Name which will be used in the option string + const char *name; + // Will be printed when "help" is passed + const char *description; + // Size of the private struct + int priv_size; + // If not NULL, default values for private struct + const void *priv_defaults; + // Options which refer to members in the private struct + const struct m_option *options; + // Prefix for each of the above options (none if NULL). + const char *options_prefix; + // For free use by the implementer of m_obj_list.get_desc + const void *p; + // Don't list entry with "help" + bool hidden; + // Callback to print custom help if "vf=entry=help" is passed + void (*print_help)(struct mp_log *log); + // Set by m_obj_list_find(). If the requested name is an old alias, this + // is set to the old name (while the name field uses the new name). + const char *replaced_name; + // For convenience: these are added as global command-line options. + const struct m_sub_options *global_opts; +}; + +// Extra definition needed for \ref m_option_type_obj_settings_list options. +struct m_obj_list { + bool (*get_desc)(struct m_obj_desc *dst, int index); + const char *description; + // Can be set to a NULL terminated array of aliases + const char *aliases[5][2]; + // Allow a trailing ",", which adds an entry with name="" + bool allow_trailer; + // Callback to test whether an unknown entry should be allowed. (This can + // be useful if adding them as explicit entries is too much work.) + bool (*check_unknown_entry)(const char *name); + // Allow syntax for disabling entries. + bool allow_disable_entries; + // This helps with confusing error messages if unknown flag options are used. + bool disallow_positional_parameters; + // Each sub-item is backed by global options (for AOs and VOs). + bool use_global_options; + // Callback to print additional custom help if "vf=help" is passed + void (*print_help_list)(struct mp_log *log); + // Callback to print help for _unknown_ entries with "vf=entry=help" + void (*print_unknown_entry_help)(struct mp_log *log, const char *name); +}; + +// Find entry by name +bool m_obj_list_find(struct m_obj_desc *dst, const struct m_obj_list *list, + bstr name); + +// The data type used by \ref m_option_type_obj_settings_list. +typedef struct m_obj_settings { + // Type of the object. + char *name; + // Optional user-defined name. + char *label; + // User enable flag. + bool enabled; + // NULL terminated array of parameter/value pairs. + char **attribs; +} m_obj_settings_t; + +bool m_obj_settings_equal(struct m_obj_settings *a, struct m_obj_settings *b); + +struct m_opt_choice_alternatives { + char *name; + int value; +}; + +const char *m_opt_choice_str(const struct m_opt_choice_alternatives *choices, + int value); + +// Validator function signatures. Required to properly type the param value. +typedef int (*m_opt_generic_validate_fn)(struct mp_log *log, const m_option_t *opt, + struct bstr name, void *value); + +typedef int (*m_opt_string_validate_fn)(struct mp_log *log, const m_option_t *opt, + struct bstr name, const char **value); +typedef int (*m_opt_int_validate_fn)(struct mp_log *log, const m_option_t *opt, + struct bstr name, const int *value); + + +// m_option.priv points to this if OPT_SUBSTRUCT is used +struct m_sub_options { + const char *prefix; + const struct m_option *opts; + size_t size; + const void *defaults; + // Change flags passed to mp_option_change_callback() if any option that is + // directly or indirectly part of this group is changed. + int change_flags; + // Return further sub-options, for example for optional components. If set, + // this is called with increasing index (starting from 0), as long as true + // is returned. If true is returned and *sub is set in any of these calls, + // they are added as options. + bool (*get_sub_options)(int index, const struct m_sub_options **sub); +}; + +#define CONF_TYPE_BOOL (&m_option_type_bool) +#define CONF_TYPE_FLAG (&m_option_type_flag) +#define CONF_TYPE_INT (&m_option_type_int) +#define CONF_TYPE_INT64 (&m_option_type_int64) +#define CONF_TYPE_FLOAT (&m_option_type_float) +#define CONF_TYPE_DOUBLE (&m_option_type_double) +#define CONF_TYPE_STRING (&m_option_type_string) +#define CONF_TYPE_STRING_LIST (&m_option_type_string_list) +#define CONF_TYPE_IMGFMT (&m_option_type_imgfmt) +#define CONF_TYPE_FOURCC (&m_option_type_fourcc) +#define CONF_TYPE_AFMT (&m_option_type_afmt) +#define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list) +#define CONF_TYPE_TIME (&m_option_type_time) +#define CONF_TYPE_CHOICE (&m_option_type_choice) +#define CONF_TYPE_NODE (&m_option_type_node) + +// Possible option values. Code is allowed to access option data without going +// through this union. It serves for self-documentation and to get minimal +// size/alignment requirements for option values in general. +union m_option_value { + bool bool_; + int flag; // not the C type "bool"! + int int_; + int64_t int64; + float float_; + double double_; + char *string; + char **string_list; + char **keyvalue_list; + int imgfmt; + unsigned int fourcc; + int afmt; + m_obj_settings_t *obj_settings_list; + double time; + struct m_rel_time rel_time; + struct m_color color; + struct m_geometry geometry; + struct m_geometry size_box; + struct m_channels channels; +}; + +// Keep fully zeroed instance of m_option_value to use as a default value, before +// any specific union member is used. C standard says that `= {0}` activates and +// initializes only the first member of the union, leaving padding bits undefined. +static const union m_option_value m_option_value_default; + +//////////////////////////////////////////////////////////////////////////// + +struct m_option_action { + // The name of the suffix, e.g. "add" for a list. If the option is named + // "foo", this will be available as "--foo-add". Note that no suffix (i.e. + // "--foo" is implicitly always available. + const char *name; + // One of M_OPT_TYPE*. + unsigned int flags; +}; + +// Option type description +struct m_option_type { + const char *name; + // Size needed for the data. + unsigned int size; + // One of M_OPT_TYPE*. + unsigned int flags; + + // Parse the data from a string. + /** It is the only required function, all others can be NULL. + * Generally should not be called directly outside of the options module, + * but instead through \ref m_option_parse which calls additional option + * specific callbacks during the process. + * + * \param log for outputting parser error or help messages + * \param opt The option that is parsed. + * \param name The full option name. + * \param param The parameter to parse. + * may not be an argument meant for this option + * \param dst Pointer to the memory where the data should be written. + * If NULL the parameter validity should still be checked. + * \return On error a negative value is returned, on success the number + * of arguments consumed. For details see \ref OptionParserReturn. + */ + int (*parse)(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst); + + // Print back a value in string form. + /** \param opt The option to print. + * \param val Pointer to the memory holding the data to be printed. + * \return An allocated string containing the text value or (void*)-1 + * on error. + */ + char *(*print)(const m_option_t *opt, const void *val); + + // Print the value in a human readable form. Unlike print(), it doesn't + // necessarily return the exact value, and is generally not parseable with + // parse(). + char *(*pretty_print)(const m_option_t *opt, const void *val); + + // Copy data between two locations. Deep copy if the data has pointers. + // The implementation must free *dst if memory allocation is involved. + /** \param opt The option to copy. + * \param dst Pointer to the destination memory. + * \param src Pointer to the source memory. + */ + void (*copy)(const m_option_t *opt, void *dst, const void *src); + + // Free the data allocated for a save slot. + /** This is only needed for dynamic types like strings. + * \param dst Pointer to the data, usually a pointer that should be freed and + * set to NULL. + */ + void (*free)(void *dst); + + // Add the value add to the value in val. For types that are not numeric, + // add gives merely the direction. The wrap parameter determines whether + // the value is clipped, or wraps around to the opposite max/min. + void (*add)(const m_option_t *opt, void *val, double add, bool wrap); + + // Multiply the value with the factor f. The callback must clip the result + // to the valid value range of the option. + void (*multiply)(const m_option_t *opt, void *val, double f); + + // Set the option value in dst to the contents of src. + // (If the option is dynamic, the old value in *dst has to be freed.) + // Return values: + // M_OPT_UNKNOWN: src is in an unknown format + // M_OPT_INVALID: src is incorrectly formatted + // >= 0: success + // other error code: some other error, essentially M_OPT_INVALID refined + int (*set)(const m_option_t *opt, void *dst, struct mpv_node *src); + + // Copy the option value in src to dst. Use ta_parent for any dynamic + // memory allocations. It's explicitly allowed to have mpv_node reference + // static strings (and even mpv_node_list.keys), though. + int (*get)(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, + void *src); + + // Return whether the values are the same. (There are no "unordered" + // results; for example, two floats with the value NaN compare equal. Other + // ambiguous floats, such as +0 and -0 compare equal. Some option types may + // incorrectly report unequal for values that are equal, such as sets (if + // the element order is different, which incorrectly matters), but values + // duplicated with m_option_copy() always return as equal. Empty strings + // and NULL strings are equal. Ambiguous unicode representations compare + // unequal.) + // If not set, values are always considered equal (=> not really optional). + bool (*equal)(const m_option_t *opt, void *a, void *b); + + // Optional: list of suffixes, terminated with a {0} entry. An empty list + // behaves like the list being NULL. + const struct m_option_action *actions; +}; + +// Option description +struct m_option { + // Option name. + // Option declarations can use this as positional field. + const char *name; + + // Option type. + const m_option_type_t *type; + + // See \ref OptionFlags. + unsigned int flags; + + int offset; + + // Most numeric types restrict the range to [min, max] if min<max (this + // implies that if min/max are not set, the full range is used). In all + // cases, the actual range is clamped to the type's native range. + // Float types use [DBL_MIN, DBL_MAX], though by setting min or max to + // -/+INFINITY, the range can be extended to INFINITY. (This part is buggy + // for "float".) + // Preferably use M_RANGE() to set these fields. + double min, max; + + // Type dependent data (for all kinds of extended settings). + void *priv; + + // Initialize variable to given default before parsing options + const void *defval; + + // Print a warning when this option is used (for options with no direct + // replacement.) + const char *deprecation_message; + + // Optional function that validates a param value for this option. + m_opt_generic_validate_fn validate; + + // Optional function that displays help. Will replace type-specific help. + int (*help)(struct mp_log *log, const m_option_t *opt, struct bstr name); +}; + +char *format_file_size(int64_t size); + +// The option is forbidden in config files. +#define M_OPT_NOCFG (1 << 2) + +// The option should be set during command line pre-parsing +#define M_OPT_PRE_PARSE (1 << 4) + +// The option expects a file name (or a list of file names) +#define M_OPT_FILE (1 << 5) + +// Do not add as property. +#define M_OPT_NOPROP (1 << 6) + +// Enable special semantics for some options when parsing the string "help". +#define M_OPT_HAVE_HELP (1 << 7) + +// The following are also part of the M_OPT_* flags, and are used to update +// certain groups of options. +#define UPDATE_OPT_FIRST (1 << 8) +#define UPDATE_TERM (1 << 8) // terminal options +#define UPDATE_SUB_FILT (1 << 9) // subtitle filter options +#define UPDATE_OSD (1 << 10) // related to OSD rendering +#define UPDATE_BUILTIN_SCRIPTS (1 << 11) // osc/ytdl/stats +#define UPDATE_IMGPAR (1 << 12) // video image params overrides +#define UPDATE_INPUT (1 << 13) // mostly --input-* options +#define UPDATE_AUDIO (1 << 14) // --audio-channels etc. +#define UPDATE_PRIORITY (1 << 15) // --priority (Windows-only) +#define UPDATE_SCREENSAVER (1 << 16) // --stop-screensaver +#define UPDATE_VOL (1 << 17) // softvol related options +#define UPDATE_LAVFI_COMPLEX (1 << 18) // --lavfi-complex +#define UPDATE_HWDEC (1 << 20) // --hwdec +#define UPDATE_DVB_PROG (1 << 21) // some --dvbin-... +#define UPDATE_SUB_HARD (1 << 22) // subtitle opts. that need full reinit +#define UPDATE_SUB_EXTS (1 << 23) // update internal list of sub exts +#define UPDATE_OPT_LAST (1 << 23) + +// All bits between _FIRST and _LAST (inclusive) +#define UPDATE_OPTS_MASK \ + (((UPDATE_OPT_LAST << 1) - 1) & ~(unsigned)(UPDATE_OPT_FIRST - 1)) + +// type_float/type_double: string "default" is parsed as NaN (and reverse) +#define M_OPT_DEFAULT_NAN (1 << 25) + +// type time: string "no" maps to MP_NOPTS_VALUE (if unset, NOPTS is rejected) +#define M_OPT_ALLOW_NO (1 << 26) + +// type channels: disallow "auto" (still accept ""), limit list to at most 1 item. +#define M_OPT_CHANNELS_LIMITED (1 << 27) + +// Like M_OPT_TYPE_OPTIONAL_PARAM. +#define M_OPT_OPTIONAL_PARAM (1 << 30) + +// These are kept for compatibility with older code. +#define CONF_NOCFG M_OPT_NOCFG +#define CONF_PRE_PARSE M_OPT_PRE_PARSE + +// These flags are used to describe special parser capabilities or behavior. + +// The parameter is optional and by default no parameter is preferred. If +// ambiguous syntax is used ("--opt value"), the command line parser will +// assume that the argument takes no parameter. In config files, these +// options can be used without "=" and value. +#define M_OPT_TYPE_OPTIONAL_PARAM (1 << 0) + +// Behaves fundamentally like a choice or a superset of it (all allowed string +// values are from a fixed set, although other types of values like numbers +// might be allowed too). E.g. m_option_type_choice and m_option_type_flag. +#define M_OPT_TYPE_CHOICE (1 << 1) + +// When m_option.min/max are set, they denote a value range. +#define M_OPT_TYPE_USES_RANGE (1 << 2) + +///////////////////////////// Parser flags ///////////////////////////////// + +// OptionParserReturn +// +// On success parsers return a number >= 0. +// +// To indicate that MPlayer should exit without playing anything, +// parsers return M_OPT_EXIT. +// +// On error one of the following (negative) error codes is returned: + +// For use by higher level APIs when the option name is invalid. +#define M_OPT_UNKNOWN -1 + +// Returned when a parameter is needed but wasn't provided. +#define M_OPT_MISSING_PARAM -2 + +// Returned when the given parameter couldn't be parsed. +#define M_OPT_INVALID -3 + +// Returned if the value is "out of range". The exact meaning may +// vary from type to type. +#define M_OPT_OUT_OF_RANGE -4 + +// The option doesn't take a parameter. +#define M_OPT_DISALLOW_PARAM -5 + +// Returned when MPlayer should exit. Used by various help stuff. +#define M_OPT_EXIT -6 + +char *m_option_strerror(int code); + +// Base function to parse options. Includes calling help and validation +// callbacks. Only when this functionality is for some reason required to not +// happen should the parse function pointer be utilized by itself. +// +// See \ref m_option_type::parse. +int m_option_parse(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst); + +// Helper to print options, see \ref m_option_type::print. +static inline char *m_option_print(const m_option_t *opt, const void *val_ptr) +{ + if (opt->type->print) + return opt->type->print(opt, val_ptr); + else + return NULL; +} + +static inline char *m_option_pretty_print(const m_option_t *opt, + const void *val_ptr) +{ + if (opt->type->pretty_print) + return opt->type->pretty_print(opt, val_ptr); + else + return m_option_print(opt, val_ptr); +} + +// Helper around \ref m_option_type::copy. +static inline void m_option_copy(const m_option_t *opt, void *dst, + const void *src) +{ + if (opt->type->copy) + opt->type->copy(opt, dst, src); +} + +// Helper around \ref m_option_type::free. +static inline void m_option_free(const m_option_t *opt, void *dst) +{ + if (opt->type->free) + opt->type->free(dst); +} + +// see m_option_type.set +static inline int m_option_set_node(const m_option_t *opt, void *dst, + struct mpv_node *src) +{ + if (opt->type->set) + return opt->type->set(opt, dst, src); + return M_OPT_UNKNOWN; +} + +// Call m_option_parse for strings, m_option_set_node otherwise. +int m_option_set_node_or_string(struct mp_log *log, const m_option_t *opt, + const char *name, void *dst, struct mpv_node *src); + +// see m_option_type.get +static inline int m_option_get_node(const m_option_t *opt, void *ta_parent, + struct mpv_node *dst, void *src) +{ + if (opt->type->get) + return opt->type->get(opt, ta_parent, dst, src); + return M_OPT_UNKNOWN; +} + +static inline bool m_option_equal(const m_option_t *opt, void *a, void *b) +{ + // Handle trivial equivalence. + // If not implemented, assume this type has no actual values => always equal. + if (a == b || !opt->type->equal) + return true; + return opt->type->equal(opt, a, b); +} + +int m_option_required_params(const m_option_t *opt); + +extern const char m_option_path_separator; + +// Cause a compilation warning if typeof(expr) != type. +// Should be used with pointer types only. +#define MP_EXPECT_TYPE(type, expr) (0 ? (type)0 : (expr)) + +// This behaves like offsetof(type, member), but will cause a compilation +// warning if typeof(member) != expected_member_type. +// It uses some trickery to make it compile as expression. +#define MP_CHECKED_OFFSETOF(type, member, expected_member_type) \ + (offsetof(type, member) + (0 && MP_EXPECT_TYPE(expected_member_type*, \ + &((type*)0)->member))) + +#define OPT_TYPED_FIELD(type_, c_type, field) \ + .type = &type_, \ + .offset = MP_CHECKED_OFFSETOF(OPT_BASE_STRUCT, field, c_type) + +#define OPTION_LIST_SEPARATOR ',' + +#define OPTDEF_STR(s) .defval = (void *)&(char * const){s} +#define OPTDEF_INT(i) .defval = (void *)&(const int){i} +#define OPTDEF_INT64(i) .defval = (void *)&(const int64_t){i} +#define OPTDEF_FLOAT(f) .defval = (void *)&(const float){f} +#define OPTDEF_DOUBLE(d) .defval = (void *)&(const double){d} + +#define M_RANGE(a, b) .min = (double) (a), .max = (double) (b) + +#define OPT_BOOL(field) \ + OPT_TYPED_FIELD(m_option_type_bool, bool, field) + +#define OPT_INT(field) \ + OPT_TYPED_FIELD(m_option_type_int, int, field) + +#define OPT_INT64(field) \ + OPT_TYPED_FIELD(m_option_type_int64, int64_t, field) + +#define OPT_FLOAT(field) \ + OPT_TYPED_FIELD(m_option_type_float, float, field) + +#define OPT_DOUBLE(field) \ + OPT_TYPED_FIELD(m_option_type_double, double, field) + +#define OPT_STRING(field) \ + OPT_TYPED_FIELD(m_option_type_string, char*, field) + +#define OPT_STRINGLIST(field) \ + OPT_TYPED_FIELD(m_option_type_string_list, char**, field) + +#define OPT_KEYVALUELIST(field) \ + OPT_TYPED_FIELD(m_option_type_keyvalue_list, char**, field) + +#define OPT_PATHLIST(field) \ + OPT_TYPED_FIELD(m_option_type_string_list, char**, field), \ + .priv = (void *)&m_option_path_separator + +#define OPT_TIME(field) \ + OPT_TYPED_FIELD(m_option_type_time, double, field) + +#define OPT_REL_TIME(field) \ + OPT_TYPED_FIELD(m_option_type_rel_time, struct m_rel_time, field) + +#define OPT_COLOR(field) \ + OPT_TYPED_FIELD(m_option_type_color, struct m_color, field) + +#define OPT_BYTE_SIZE(field) \ + OPT_TYPED_FIELD(m_option_type_byte_size, int64_t, field) + +// (Approximation of x<=SIZE_MAX/2 for m_option.max, which is double.) +#define M_MAX_MEM_BYTES MPMIN((1ULL << 62), (size_t)-1 / 2) + +#define OPT_GEOMETRY(field) \ + OPT_TYPED_FIELD(m_option_type_geometry, struct m_geometry, field) + +#define OPT_SIZE_BOX(field) \ + OPT_TYPED_FIELD(m_option_type_size_box, struct m_geometry, field) + +#define OPT_RECT(field) \ + OPT_TYPED_FIELD(m_option_type_rect, struct m_geometry, field) + +#define OPT_TRACKCHOICE(field) \ + OPT_CHOICE(field, {"no", -2}, {"auto", -1}), \ + M_RANGE(0, 8190) + +#define OPT_MSGLEVELS(field) \ + OPT_TYPED_FIELD(m_option_type_msglevels, char **, field) + +#define OPT_ASPECT(field) \ + OPT_TYPED_FIELD(m_option_type_aspect, double, field) + +#define OPT_IMAGEFORMAT(field) \ + OPT_TYPED_FIELD(m_option_type_imgfmt, int, field) + +#define OPT_AUDIOFORMAT(field) \ + OPT_TYPED_FIELD(m_option_type_afmt, int, field) + +#define OPT_CHANNELS(field) \ + OPT_TYPED_FIELD(m_option_type_channels, struct m_channels, field) + +#define OPT_INT_VALIDATE(field, validate_fn) \ + OPT_TYPED_FIELD(m_option_type_int, int, field), \ + .validate = (m_opt_generic_validate_fn) \ + MP_EXPECT_TYPE(m_opt_int_validate_fn, validate_fn) + +#define OPT_STRING_VALIDATE(field, validate_fn) \ + OPT_TYPED_FIELD(m_option_type_string, char*, field), \ + .validate = (m_opt_generic_validate_fn) \ + MP_EXPECT_TYPE(m_opt_string_validate_fn, validate_fn) + +#define M_CHOICES(...) \ + .priv = (void *)&(const struct m_opt_choice_alternatives[]){ __VA_ARGS__, {0}} + +// Variant which takes a pointer to struct m_opt_choice_alternatives directly +#define OPT_CHOICE_C(field, choices) \ + OPT_TYPED_FIELD(m_option_type_choice, int, field), \ + .priv = (void *)MP_EXPECT_TYPE(const struct m_opt_choice_alternatives*, choices) + +// Variant where you pass a struct m_opt_choice_alternatives initializer +#define OPT_CHOICE(field, ...) \ + OPT_TYPED_FIELD(m_option_type_choice, int, field), \ + M_CHOICES(__VA_ARGS__) + +#define OPT_FLAGS(field, ...) \ + OPT_TYPED_FIELD(m_option_type_flags, int, field), \ + M_CHOICES(__VA_ARGS__) + +#define OPT_SETTINGSLIST(field, objlist) \ + OPT_TYPED_FIELD(m_option_type_obj_settings_list, m_obj_settings_t*, field), \ + .priv = (void*)MP_EXPECT_TYPE(const struct m_obj_list*, objlist) + +#define OPT_FOURCC(field) \ + OPT_TYPED_FIELD(m_option_type_fourcc, int, field) + +#define OPT_CYCLEDIR(field) \ + OPT_TYPED_FIELD(m_option_type_cycle_dir, double, field) + +// subconf must have the type struct m_sub_options. +// All sub-options are prefixed with "name-" and are added to the current +// (containing) option list. +// If name is "", add the sub-options directly instead. +// "field" refers to the field, that must be a pointer to a field described by +// the subconf struct. +#define OPT_SUBSTRUCT(field, subconf) \ + .offset = offsetof(OPT_BASE_STRUCT, field), \ + .type = &m_option_type_subconfig, .priv = (void*)&subconf + +// Non-fields + +#define OPT_ALIAS(newname) \ + .type = &m_option_type_alias, .priv = newname, .offset = -1 + +// If "--optname" was removed, but "--newname" has the same semantics. +// It will be redirected, and a warning will be printed on first use. +#define OPT_REPLACED_MSG(newname, msg) \ + .type = &m_option_type_alias, .priv = newname, \ + .deprecation_message = (msg), .offset = -1 + +// Same, with a generic deprecation message. +#define OPT_REPLACED(newname) OPT_REPLACED_MSG(newname, "") + +// Alias, resolved on the CLI/config file/profile parser level only. +#define OPT_CLI_ALIAS(newname) \ + .type = &m_option_type_cli_alias, .priv = newname, \ + .flags = M_OPT_NOPROP, .offset = -1 + +// "--optname" doesn't exist, but inform the user about a replacement with msg. +#define OPT_REMOVED(msg) \ + .type = &m_option_type_removed, .priv = msg, \ + .deprecation_message = "", .flags = M_OPT_NOPROP, .offset = -1 + +#define OPT_PRINT(fn) \ + .flags = M_OPT_NOCFG | M_OPT_PRE_PARSE | M_OPT_NOPROP, \ + .type = &m_option_type_print_fn, \ + .priv = MP_EXPECT_TYPE(m_opt_print_fn, fn), \ + .offset = -1 + +#endif /* MPLAYER_M_OPTION_H */ diff --git a/options/m_property.c b/options/m_property.c new file mode 100644 index 0000000..1b76f05 --- /dev/null +++ b/options/m_property.c @@ -0,0 +1,630 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +/// \file +/// \ingroup Properties + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include <assert.h> + +#include <libavutil/common.h> + +#include "libmpv/client.h" + +#include "mpv_talloc.h" +#include "m_option.h" +#include "m_property.h" +#include "common/msg.h" +#include "common/common.h" + +static int m_property_multiply(struct mp_log *log, + const struct m_property *prop_list, + const char *property, double f, void *ctx) +{ + union m_option_value val = m_option_value_default; + struct m_option opt = {0}; + int r; + + r = m_property_do(log, prop_list, property, M_PROPERTY_GET_CONSTRICTED_TYPE, + &opt, ctx); + if (r != M_PROPERTY_OK) + return r; + assert(opt.type); + + if (!opt.type->multiply) + return M_PROPERTY_NOT_IMPLEMENTED; + + r = m_property_do(log, prop_list, property, M_PROPERTY_GET, &val, ctx); + if (r != M_PROPERTY_OK) + return r; + opt.type->multiply(&opt, &val, f); + r = m_property_do(log, prop_list, property, M_PROPERTY_SET, &val, ctx); + m_option_free(&opt, &val); + return r; +} + +struct m_property *m_property_list_find(const struct m_property *list, + const char *name) +{ + for (int n = 0; list && list[n].name; n++) { + if (strcmp(list[n].name, name) == 0) + return (struct m_property *)&list[n]; + } + return NULL; +} + +static int do_action(const struct m_property *prop_list, const char *name, + int action, void *arg, void *ctx) +{ + struct m_property *prop; + struct m_property_action_arg ka; + const char *sep = strchr(name, '/'); + if (sep && sep[1]) { + char base[128]; + snprintf(base, sizeof(base), "%.*s", (int)(sep - name), name); + prop = m_property_list_find(prop_list, base); + ka = (struct m_property_action_arg) { + .key = sep + 1, + .action = action, + .arg = arg, + }; + action = M_PROPERTY_KEY_ACTION; + arg = &ka; + } else + prop = m_property_list_find(prop_list, name); + if (!prop) + return M_PROPERTY_UNKNOWN; + return prop->call(ctx, prop, action, arg); +} + +// (as a hack, log can be NULL on read-only paths) +int m_property_do(struct mp_log *log, const struct m_property *prop_list, + const char *name, int action, void *arg, void *ctx) +{ + union m_option_value val = m_option_value_default; + int r; + + struct m_option opt = {0}; + r = do_action(prop_list, name, M_PROPERTY_GET_TYPE, &opt, ctx); + if (r <= 0) + return r; + assert(opt.type); + + switch (action) { + case M_PROPERTY_PRINT: { + if ((r = do_action(prop_list, name, M_PROPERTY_PRINT, arg, ctx)) >= 0) + return r; + // Fallback to m_option + if ((r = do_action(prop_list, name, M_PROPERTY_GET, &val, ctx)) <= 0) + return r; + char *str = m_option_pretty_print(&opt, &val); + m_option_free(&opt, &val); + *(char **)arg = str; + return str != NULL; + } + case M_PROPERTY_GET_STRING: { + if ((r = do_action(prop_list, name, M_PROPERTY_GET, &val, ctx)) <= 0) + return r; + char *str = m_option_print(&opt, &val); + m_option_free(&opt, &val); + *(char **)arg = str; + return str != NULL; + } + case M_PROPERTY_SET_STRING: { + struct mpv_node node = { .format = MPV_FORMAT_STRING, .u.string = arg }; + return m_property_do(log, prop_list, name, M_PROPERTY_SET_NODE, &node, ctx); + } + case M_PROPERTY_MULTIPLY: { + return m_property_multiply(log, prop_list, name, *(double *)arg, ctx); + } + case M_PROPERTY_SWITCH: { + if (!log) + return M_PROPERTY_ERROR; + struct m_property_switch_arg *sarg = arg; + if ((r = do_action(prop_list, name, M_PROPERTY_SWITCH, arg, ctx)) != + M_PROPERTY_NOT_IMPLEMENTED) + return r; + // Fallback to m_option + r = m_property_do(log, prop_list, name, M_PROPERTY_GET_CONSTRICTED_TYPE, + &opt, ctx); + if (r <= 0) + return r; + assert(opt.type); + if (!opt.type->add) + return M_PROPERTY_NOT_IMPLEMENTED; + if ((r = do_action(prop_list, name, M_PROPERTY_GET, &val, ctx)) <= 0) + return r; + opt.type->add(&opt, &val, sarg->inc, sarg->wrap); + r = do_action(prop_list, name, M_PROPERTY_SET, &val, ctx); + m_option_free(&opt, &val); + return r; + } + case M_PROPERTY_GET_CONSTRICTED_TYPE: { + r = do_action(prop_list, name, action, arg, ctx); + if (r >= 0 || r == M_PROPERTY_UNAVAILABLE) + return r; + if ((r = do_action(prop_list, name, M_PROPERTY_GET_TYPE, arg, ctx)) >= 0) + return r; + return M_PROPERTY_NOT_IMPLEMENTED; + } + case M_PROPERTY_SET: { + return do_action(prop_list, name, M_PROPERTY_SET, arg, ctx); + } + case M_PROPERTY_GET_NODE: { + if ((r = do_action(prop_list, name, M_PROPERTY_GET_NODE, arg, ctx)) != + M_PROPERTY_NOT_IMPLEMENTED) + return r; + if ((r = do_action(prop_list, name, M_PROPERTY_GET, &val, ctx)) <= 0) + return r; + struct mpv_node *node = arg; + int err = m_option_get_node(&opt, NULL, node, &val); + if (err == M_OPT_UNKNOWN) { + r = M_PROPERTY_NOT_IMPLEMENTED; + } else if (err < 0) { + r = M_PROPERTY_INVALID_FORMAT; + } else { + r = M_PROPERTY_OK; + } + m_option_free(&opt, &val); + return r; + } + case M_PROPERTY_SET_NODE: { + if (!log) + return M_PROPERTY_ERROR; + if ((r = do_action(prop_list, name, M_PROPERTY_SET_NODE, arg, ctx)) != + M_PROPERTY_NOT_IMPLEMENTED) + return r; + int err = m_option_set_node_or_string(log, &opt, name, &val, arg); + if (err == M_OPT_UNKNOWN) { + r = M_PROPERTY_NOT_IMPLEMENTED; + } else if (err < 0) { + r = M_PROPERTY_INVALID_FORMAT; + } else { + r = do_action(prop_list, name, M_PROPERTY_SET, &val, ctx); + } + m_option_free(&opt, &val); + return r; + } + default: + return do_action(prop_list, name, action, arg, ctx); + } +} + +bool m_property_split_path(const char *path, bstr *prefix, char **rem) +{ + char *next = strchr(path, '/'); + if (next) { + *prefix = bstr_splice(bstr0(path), 0, next - path); + *rem = next + 1; + return true; + } else { + *prefix = bstr0(path); + *rem = ""; + return false; + } +} + +// If *action is M_PROPERTY_KEY_ACTION, but the associated path is "", then +// make this into a top-level action. +static void m_property_unkey(int *action, void **arg) +{ + if (*action == M_PROPERTY_KEY_ACTION) { + struct m_property_action_arg *ka = *arg; + if (!ka->key[0]) { + *action = ka->action; + *arg = ka->arg; + } + } +} + +static int m_property_do_bstr(const struct m_property *prop_list, bstr name, + int action, void *arg, void *ctx) +{ + char *name0 = bstrdup0(NULL, name); + int ret = m_property_do(NULL, prop_list, name0, action, arg, ctx); + talloc_free(name0); + return ret; +} + +static void append_str(char **s, int *len, bstr append) +{ + MP_TARRAY_GROW(NULL, *s, *len + append.len); + if (append.len) + memcpy(*s + *len, append.start, append.len); + *len = *len + append.len; +} + +static int expand_property(const struct m_property *prop_list, char **ret, + int *ret_len, bstr prop, bool silent_error, void *ctx) +{ + bool cond_yes = bstr_eatstart0(&prop, "?"); + bool cond_no = !cond_yes && bstr_eatstart0(&prop, "!"); + bool test = cond_yes || cond_no; + bool raw = bstr_eatstart0(&prop, "="); + bstr comp_with = {0}; + bool comp = test && bstr_split_tok(prop, "==", &prop, &comp_with); + if (test && !comp) + raw = true; + int method = raw ? M_PROPERTY_GET_STRING : M_PROPERTY_PRINT; + + char *s = NULL; + int r = m_property_do_bstr(prop_list, prop, method, &s, ctx); + bool skip; + if (comp) { + skip = ((s && bstr_equals0(comp_with, s)) != cond_yes); + } else if (test) { + skip = (!!s != cond_yes); + } else { + skip = !!s; + char *append = s; + if (!s && !silent_error && !raw) + append = (r == M_PROPERTY_UNAVAILABLE) ? "(unavailable)" : "(error)"; + append_str(ret, ret_len, bstr0(append)); + } + talloc_free(s); + return skip; +} + +char *m_properties_expand_string(const struct m_property *prop_list, + const char *str0, void *ctx) +{ + char *ret = NULL; + int ret_len = 0; + bool skip = false; + int level = 0, skip_level = 0; + bstr str = bstr0(str0); + + while (str.len) { + if (level > 0 && bstr_eatstart0(&str, "}")) { + if (skip && level <= skip_level) + skip = false; + level--; + } else if (bstr_startswith0(str, "${") && bstr_find0(str, "}") >= 0) { + str = bstr_cut(str, 2); + level++; + + // Assume ":" and "}" can't be part of the property name + // => if ":" comes before "}", it must be for the fallback + int term_pos = bstrcspn(str, ":}"); + bstr name = bstr_splice(str, 0, term_pos < 0 ? str.len : term_pos); + str = bstr_cut(str, term_pos); + bool have_fallback = bstr_eatstart0(&str, ":"); + + if (!skip) { + skip = expand_property(prop_list, &ret, &ret_len, name, + have_fallback, ctx); + if (skip) + skip_level = level; + } + } else if (level == 0 && bstr_eatstart0(&str, "$>")) { + append_str(&ret, &ret_len, str); + break; + } else { + char c; + + // Other combinations, e.g. "$x", are added verbatim + if (bstr_eatstart0(&str, "$$")) { + c = '$'; + } else if (bstr_eatstart0(&str, "$}")) { + c = '}'; + } else { + c = str.start[0]; + str = bstr_cut(str, 1); + } + + if (!skip) + MP_TARRAY_APPEND(NULL, ret, ret_len, c); + } + } + + MP_TARRAY_APPEND(NULL, ret, ret_len, '\0'); + return ret; +} + +void m_properties_print_help_list(struct mp_log *log, + const struct m_property *list) +{ + int count = 0; + + mp_info(log, "Name\n\n"); + for (int i = 0; list[i].name; i++) { + const struct m_property *p = &list[i]; + mp_info(log, " %s\n", p->name); + count++; + } + mp_info(log, "\nTotal: %d properties\n", count); +} + +int m_property_bool_ro(int action, void* arg, bool var) +{ + switch (action) { + case M_PROPERTY_GET: + *(bool *)arg = !!var; + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_BOOL}; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +int m_property_int_ro(int action, void *arg, int var) +{ + switch (action) { + case M_PROPERTY_GET: + *(int *)arg = var; + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_INT}; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +int m_property_int64_ro(int action, void* arg, int64_t var) +{ + switch (action) { + case M_PROPERTY_GET: + *(int64_t *)arg = var; + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_INT64}; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +int m_property_float_ro(int action, void *arg, float var) +{ + switch (action) { + case M_PROPERTY_GET: + *(float *)arg = var; + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_FLOAT}; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +int m_property_double_ro(int action, void *arg, double var) +{ + switch (action) { + case M_PROPERTY_GET: + *(double *)arg = var; + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_DOUBLE}; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +int m_property_strdup_ro(int action, void* arg, const char *var) +{ + if (!var) + return M_PROPERTY_UNAVAILABLE; + switch (action) { + case M_PROPERTY_GET: + *(char **)arg = talloc_strdup(NULL, var); + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_STRING}; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +int m_property_read_sub_validate(void *ctx, struct m_property *prop, + int action, void *arg) +{ + m_property_unkey(&action, &arg); + switch (action) { + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_NODE}; + return M_PROPERTY_OK; + case M_PROPERTY_GET: + case M_PROPERTY_PRINT: + case M_PROPERTY_KEY_ACTION: + return M_PROPERTY_VALID; + default: + return M_PROPERTY_NOT_IMPLEMENTED; + }; +} + +// This allows you to make a list of values (like from a struct) available +// as a number of sub-properties. The property list is set up with the current +// property values on the stack before calling this function. +// This does not support write access. +int m_property_read_sub(const struct m_sub_property *props, int action, void *arg) +{ + m_property_unkey(&action, &arg); + switch (action) { + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_NODE}; + return M_PROPERTY_OK; + case M_PROPERTY_GET: { + struct mpv_node node; + node.format = MPV_FORMAT_NODE_MAP; + node.u.list = talloc_zero(NULL, mpv_node_list); + mpv_node_list *list = node.u.list; + for (int n = 0; props && props[n].name; n++) { + const struct m_sub_property *prop = &props[n]; + if (prop->unavailable) + continue; + MP_TARRAY_GROW(list, list->values, list->num); + MP_TARRAY_GROW(list, list->keys, list->num); + mpv_node *val = &list->values[list->num]; + if (m_option_get_node(&prop->type, list, val, (void*)&prop->value) < 0) + { + char *s = m_option_print(&prop->type, &prop->value); + val->format = MPV_FORMAT_STRING; + val->u.string = talloc_steal(list, s); + } + list->keys[list->num] = (char *)prop->name; + list->num++; + } + *(struct mpv_node *)arg = node; + return M_PROPERTY_OK; + } + case M_PROPERTY_PRINT: { + // Output "something" - what it really should return is not yet decided. + // It should probably be something that is easy to consume by slave + // mode clients. (M_PROPERTY_PRINT on the other hand can return this + // as human readable version just fine). + char *res = NULL; + for (int n = 0; props && props[n].name; n++) { + const struct m_sub_property *prop = &props[n]; + if (prop->unavailable) + continue; + char *s = m_option_print(&prop->type, &prop->value); + ta_xasprintf_append(&res, "%s=%s\n", prop->name, s); + talloc_free(s); + } + *(char **)arg = res; + return M_PROPERTY_OK; + } + case M_PROPERTY_KEY_ACTION: { + struct m_property_action_arg *ka = arg; + const struct m_sub_property *prop = NULL; + for (int n = 0; props && props[n].name; n++) { + if (strcmp(props[n].name, ka->key) == 0) { + prop = &props[n]; + break; + } + } + if (!prop) + return M_PROPERTY_UNKNOWN; + if (prop->unavailable) + return M_PROPERTY_UNAVAILABLE; + switch (ka->action) { + case M_PROPERTY_GET: { + memset(ka->arg, 0, prop->type.type->size); + m_option_copy(&prop->type, ka->arg, &prop->value); + return M_PROPERTY_OK; + } + case M_PROPERTY_GET_TYPE: + *(struct m_option *)ka->arg = prop->type; + return M_PROPERTY_OK; + } + } + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + + +// Make a list of items available as indexed sub-properties. E.g. you can access +// item 0 as "property/0", item 1 as "property/1", etc., where each of these +// properties is redirected to the get_item(0, ...), get_item(1, ...), callback. +// Additionally, the number of entries is made available as "property/count". +// action, arg: property access. +// count: number of items. +// get_item: callback to access a single item. +// ctx: userdata passed to get_item. +int m_property_read_list(int action, void *arg, int count, + m_get_item_cb get_item, void *ctx) +{ + m_property_unkey(&action, &arg); + switch (action) { + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_NODE}; + return M_PROPERTY_OK; + case M_PROPERTY_GET: { + struct mpv_node node; + node.format = MPV_FORMAT_NODE_ARRAY; + node.u.list = talloc_zero(NULL, mpv_node_list); + node.u.list->num = count; + node.u.list->values = talloc_array(node.u.list, mpv_node, count); + for (int n = 0; n < count; n++) { + struct mpv_node *sub = &node.u.list->values[n]; + sub->format = MPV_FORMAT_NONE; + int r; + r = get_item(n, M_PROPERTY_GET_NODE, sub, ctx); + if (r == M_PROPERTY_NOT_IMPLEMENTED) { + struct m_option opt = {0}; + r = get_item(n, M_PROPERTY_GET_TYPE, &opt, ctx); + if (r != M_PROPERTY_OK) + goto err; + union m_option_value val = m_option_value_default; + r = get_item(n, M_PROPERTY_GET, &val, ctx); + if (r != M_PROPERTY_OK) + goto err; + m_option_get_node(&opt, node.u.list, sub, &val); + m_option_free(&opt, &val); + err: ; + } + } + *(struct mpv_node *)arg = node; + return M_PROPERTY_OK; + } + case M_PROPERTY_PRINT: { + // See m_property_read_sub() remarks. + char *res = NULL; + for (int n = 0; n < count; n++) { + char *s = NULL; + int r = get_item(n, M_PROPERTY_PRINT, &s, ctx); + if (r != M_PROPERTY_OK) { + talloc_free(res); + return r; + } + ta_xasprintf_append(&res, "%d: %s\n", n, s); + talloc_free(s); + } + *(char **)arg = res; + return M_PROPERTY_OK; + } + case M_PROPERTY_KEY_ACTION: { + struct m_property_action_arg *ka = arg; + if (strcmp(ka->key, "count") == 0) { + switch (ka->action) { + case M_PROPERTY_GET_TYPE: { + struct m_option opt = {.type = CONF_TYPE_INT}; + *(struct m_option *)ka->arg = opt; + return M_PROPERTY_OK; + } + case M_PROPERTY_GET: + *(int *)ka->arg = MPMAX(0, count); + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; + } + // This is expected of the form "123" or "123/rest" + char *next = strchr(ka->key, '/'); + char *end = NULL; + const char *key_end = ka->key + strlen(ka->key); + long int item = strtol(ka->key, &end, 10); + // not a number, trailing characters, etc. + if ((end != key_end || ka->key == key_end) && end != next) + return M_PROPERTY_UNKNOWN; + if (item < 0 || item >= count) + return M_PROPERTY_UNKNOWN; + if (next) { + // Sub-path + struct m_property_action_arg n_ka = *ka; + n_ka.key = next + 1; + return get_item(item, M_PROPERTY_KEY_ACTION, &n_ka, ctx); + } else { + // Direct query + return get_item(item, ka->action, ka->arg, ctx); + } + } + } + return M_PROPERTY_NOT_IMPLEMENTED; +} diff --git a/options/m_property.h b/options/m_property.h new file mode 100644 index 0000000..0dce246 --- /dev/null +++ b/options/m_property.h @@ -0,0 +1,234 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_M_PROPERTY_H +#define MPLAYER_M_PROPERTY_H + +#include <stdbool.h> +#include <stdint.h> + +#include "m_option.h" + +struct mp_log; + +enum mp_property_action { + // Get the property type. This defines the fundamental data type read from + // or written to the property. + // If unimplemented, the m_option entry that defines the property is used. + // arg: m_option* + M_PROPERTY_GET_TYPE, + + // Get the current value. + // arg: pointer to a variable of the type according to the property type + M_PROPERTY_GET, + + // Set a new value. The property wrapper will make sure that only valid + // values are set (e.g. according to the property type's min/max range). + // If unimplemented, the property is read-only. + // arg: pointer to a variable of the type according to the property type + M_PROPERTY_SET, + + // Get human readable string representing the current value. + // If unimplemented, the property wrapper uses the property type as + // fallback. + // arg: char** + M_PROPERTY_PRINT, + + // Like M_PROPERTY_GET_TYPE, but get a type that is compatible to the real + // type, but reflect practical limits, such as runtime-available values. + // This is mostly used for "UI" related things. + // (Example: volume property.) + M_PROPERTY_GET_CONSTRICTED_TYPE, + + // Switch the property up/down by a given value. + // If unimplemented, the property wrapper uses the property type as + // fallback. + // arg: struct m_property_switch_arg* + M_PROPERTY_SWITCH, + + // Get a string containing a parseable representation. + // Can't be overridden by property implementations. + // arg: char** + M_PROPERTY_GET_STRING, + + // Set a new value from a string. The property wrapper parses this using the + // parse function provided by the property type. + // Can't be overridden by property implementations. + // arg: char* + M_PROPERTY_SET_STRING, + + // Set a mpv_node value. + // arg: mpv_node* + M_PROPERTY_GET_NODE, + + // Get a mpv_node value. + // arg: mpv_node* + M_PROPERTY_SET_NODE, + + // Multiply numeric property with a factor. + // arg: double* + M_PROPERTY_MULTIPLY, + + // Pass down an action to a sub-property. + // arg: struct m_property_action_arg* + M_PROPERTY_KEY_ACTION, + + // Delete a value. + // Most properties do not implement this. + // arg: (ignored) + M_PROPERTY_DELETE, +}; + +// Argument for M_PROPERTY_SWITCH +struct m_property_switch_arg { + double inc; // value to add to property, or cycle direction + bool wrap; // whether value should wrap around on over/underflow +}; + +// Argument for M_PROPERTY_KEY_ACTION +struct m_property_action_arg { + const char* key; + int action; + void* arg; +}; + +enum mp_property_return { + // Returned from validator if action should be executed. + M_PROPERTY_VALID = 2, + + // Returned on success. + M_PROPERTY_OK = 1, + + // Returned on error. + M_PROPERTY_ERROR = 0, + + // Returned when the property can't be used, for example video related + // properties while playing audio only. + M_PROPERTY_UNAVAILABLE = -1, + + // Returned if the requested action is not implemented. + M_PROPERTY_NOT_IMPLEMENTED = -2, + + // Returned when asking for a property that doesn't exist. + M_PROPERTY_UNKNOWN = -3, + + // When trying to set invalid or incorrectly formatted data. + M_PROPERTY_INVALID_FORMAT = -4, +}; + +struct m_property { + const char *name; + // ctx: opaque caller context, which the property might use + // prop: pointer to this struct + // action: one of enum mp_property_action + // arg: specific to the action + // returns: one of enum mp_property_return + int (*call)(void *ctx, struct m_property *prop, int action, void *arg); + void *priv; + // Special-case: mark options for which command.c uses the option-bridge + bool is_option; +}; + +struct m_property *m_property_list_find(const struct m_property *list, + const char *name); + +// Access a property. +// action: one of m_property_action +// ctx: opaque value passed through to property implementation +// returns: one of mp_property_return +int m_property_do(struct mp_log *log, const struct m_property* prop_list, + const char* property_name, int action, void* arg, void *ctx); + +// Given a path of the form "a/b/c", this function will set *prefix to "a", +// and rem to "b/c", and return true. +// If there is no '/' in the path, set prefix to path, and rem to "", and +// return false. +bool m_property_split_path(const char *path, bstr *prefix, char **rem); + +// Print a list of properties. +void m_properties_print_help_list(struct mp_log *log, + const struct m_property *list); + +// Expand a property string. +// This function allows to print strings containing property values. +// ${NAME} is expanded to the value of property NAME. +// If NAME starts with '=', use the raw value of the property. +// ${NAME:STR} expands to the property, or STR if the property is not +// available. +// ${?NAME:STR} expands to STR if the property is available. +// ${!NAME:STR} expands to STR if the property is not available. +// General syntax: "${" ["?" | "!"] ["="] NAME ":" STR "}" +// STR is recursively expanded using the same rules. +// "$$" can be used to escape "$", and "$}" to escape "}". +// "$>" disables parsing of "$" for the rest of the string. +char* m_properties_expand_string(const struct m_property *prop_list, + const char *str, void *ctx); + +// Trivial helpers for implementing properties. +int m_property_bool_ro(int action, void* arg, bool var); +int m_property_int_ro(int action, void* arg, int var); +int m_property_int64_ro(int action, void* arg, int64_t var); +int m_property_float_ro(int action, void* arg, float var); +int m_property_double_ro(int action, void* arg, double var); +int m_property_strdup_ro(int action, void* arg, const char *var); + +struct m_sub_property { + // Name of the sub-property - this will be prefixed with the parent + // property's name. + const char *name; + // Type of the data stored in the value member. See m_option. + struct m_option type; + // Data returned by the sub-property. m_property_read_sub() will make a + // copy of this if needed. It will never write or free the data. + union m_option_value value; + // This can be set to true if the property should be hidden. + bool unavailable; +}; + +// Convenience macros which can be used as part of a sub_property entry. +#define SUB_PROP_INT(i) \ + .type = {.type = CONF_TYPE_INT}, .value = {.int_ = (i)} +#define SUB_PROP_INT64(i) \ + .type = {.type = CONF_TYPE_INT64}, .value = {.int64 = (i)} +#define SUB_PROP_STR(s) \ + .type = {.type = CONF_TYPE_STRING}, .value = {.string = (char *)(s)} +#define SUB_PROP_FLOAT(f) \ + .type = {.type = CONF_TYPE_FLOAT}, .value = {.float_ = (f)} +#define SUB_PROP_DOUBLE(f) \ + .type = {.type = CONF_TYPE_DOUBLE}, .value = {.double_ = (f)} +#define SUB_PROP_BOOL(f) \ + .type = {.type = CONF_TYPE_BOOL}, .value = {.bool_ = (f)} +#define SUB_PROP_PTS(f) \ + .type = {.type = &m_option_type_time}, .value = {.double_ = (f)} + +int m_property_read_sub_validate(void *ctx, struct m_property *prop, + int action, void *arg); +int m_property_read_sub(const struct m_sub_property *props, int action, void *arg); + + +// Used with m_property_read_list(). +// Get an entry. item is the 0-based index of the item. This behaves like a +// top-level property request (but you must implement M_PROPERTY_GET_TYPE). +// item will be in range [0, count), for count see m_property_read_list() +// action, arg are for property access. +// ctx is userdata passed to m_property_read_list. +typedef int (*m_get_item_cb)(int item, int action, void *arg, void *ctx); + +int m_property_read_list(int action, void *arg, int count, + m_get_item_cb get_item, void *ctx); + +#endif /* MPLAYER_M_PROPERTY_H */ diff --git a/options/options.c b/options/options.c new file mode 100644 index 0000000..7c6ffa5 --- /dev/null +++ b/options/options.c @@ -0,0 +1,1097 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_CFG_MPLAYER_H +#define MPLAYER_CFG_MPLAYER_H + +/* + * config for cfgparser + */ + +#include <float.h> +#include <stddef.h> +#include <sys/types.h> +#include <limits.h> +#include <math.h> + +#include "config.h" + +#ifdef _WIN32 +#include <windows.h> +#include <dwmapi.h> +#endif + +#include "options.h" +#include "m_config.h" +#include "m_option.h" +#include "common/common.h" +#include "input/event.h" +#include "stream/stream.h" +#include "video/csputils.h" +#include "video/hwdec.h" +#include "video/image_writer.h" +#include "sub/osd.h" +#include "player/core.h" +#include "player/command.h" +#include "stream/stream.h" +#include "demux/demux.h" + +static void print_version(struct mp_log *log) +{ + mp_print_version(log, true); +} + +extern const struct m_sub_options tv_params_conf; +extern const struct m_sub_options stream_bluray_conf; +extern const struct m_sub_options stream_cdda_conf; +extern const struct m_sub_options stream_dvb_conf; +extern const struct m_sub_options stream_lavf_conf; +extern const struct m_sub_options sws_conf; +extern const struct m_sub_options zimg_conf; +extern const struct m_sub_options drm_conf; +extern const struct m_sub_options demux_rawaudio_conf; +extern const struct m_sub_options demux_rawvideo_conf; +extern const struct m_sub_options demux_playlist_conf; +extern const struct m_sub_options demux_lavf_conf; +extern const struct m_sub_options demux_mkv_conf; +extern const struct m_sub_options demux_cue_conf; +extern const struct m_sub_options vd_lavc_conf; +extern const struct m_sub_options ad_lavc_conf; +extern const struct m_sub_options input_config; +extern const struct m_sub_options encode_config; +extern const struct m_sub_options ra_ctx_conf; +extern const struct m_sub_options gl_video_conf; +extern const struct m_sub_options ao_alsa_conf; + +extern const struct m_sub_options demux_conf; +extern const struct m_sub_options demux_cache_conf; + +extern const struct m_obj_list vf_obj_list; +extern const struct m_obj_list af_obj_list; +extern const struct m_obj_list vo_obj_list; + +extern const struct m_sub_options ao_conf; + +extern const struct m_sub_options opengl_conf; +extern const struct m_sub_options vulkan_conf; +extern const struct m_sub_options vulkan_display_conf; +extern const struct m_sub_options spirv_conf; +extern const struct m_sub_options d3d11_conf; +extern const struct m_sub_options d3d11va_conf; +extern const struct m_sub_options angle_conf; +extern const struct m_sub_options macos_conf; +extern const struct m_sub_options wayland_conf; +extern const struct m_sub_options wingl_conf; +extern const struct m_sub_options vaapi_conf; + +static const struct m_sub_options screenshot_conf = { + .opts = image_writer_opts, + .size = sizeof(struct image_writer_opts), + .defaults = &image_writer_opts_defaults, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct mp_vo_opts + +static const m_option_t mp_vo_opt_list[] = { + {"vo", OPT_SETTINGSLIST(video_driver_list, &vo_obj_list)}, + {"taskbar-progress", OPT_BOOL(taskbar_progress)}, + {"drag-and-drop", OPT_CHOICE(drag_and_drop, {"no", -2}, {"auto", -1}, + {"replace", DND_REPLACE}, {"append", DND_APPEND})}, + {"snap-window", OPT_BOOL(snap_window)}, + {"ontop", OPT_BOOL(ontop)}, + {"ontop-level", OPT_CHOICE(ontop_level, {"window", -1}, {"system", -2}, + {"desktop", -3}), M_RANGE(0, INT_MAX)}, + {"border", OPT_BOOL(border)}, + {"title-bar", OPT_BOOL(title_bar)}, + {"on-all-workspaces", OPT_BOOL(all_workspaces)}, + {"geometry", OPT_GEOMETRY(geometry)}, + {"autofit", OPT_SIZE_BOX(autofit)}, + {"autofit-larger", OPT_SIZE_BOX(autofit_larger)}, + {"autofit-smaller", OPT_SIZE_BOX(autofit_smaller)}, + {"auto-window-resize", OPT_BOOL(auto_window_resize)}, + {"window-scale", OPT_DOUBLE(window_scale), M_RANGE(0.001, 100)}, + {"window-minimized", OPT_BOOL(window_minimized)}, + {"window-maximized", OPT_BOOL(window_maximized)}, + {"focus-on-open", OPT_BOOL(focus_on_open)}, + {"force-render", OPT_BOOL(force_render)}, + {"force-window-position", OPT_BOOL(force_window_position)}, + {"x11-name", OPT_STRING(winname)}, + {"wayland-app-id", OPT_STRING(appid)}, + {"monitoraspect", OPT_FLOAT(force_monitor_aspect), M_RANGE(0.0, 9.0)}, + {"monitorpixelaspect", OPT_FLOAT(monitor_pixel_aspect), + M_RANGE(1.0/32.0, 32.0)}, + {"fullscreen", OPT_BOOL(fullscreen)}, + {"fs", OPT_ALIAS("fullscreen")}, + {"input-cursor-passthrough", OPT_BOOL(cursor_passthrough)}, + {"native-keyrepeat", OPT_BOOL(native_keyrepeat)}, + {"panscan", OPT_FLOAT(panscan), M_RANGE(0.0, 1.0)}, + {"video-zoom", OPT_FLOAT(zoom), M_RANGE(-20.0, 20.0)}, + {"video-pan-x", OPT_FLOAT(pan_x)}, + {"video-pan-y", OPT_FLOAT(pan_y)}, + {"video-align-x", OPT_FLOAT(align_x), M_RANGE(-1.0, 1.0)}, + {"video-align-y", OPT_FLOAT(align_y), M_RANGE(-1.0, 1.0)}, + {"video-scale-x", OPT_FLOAT(scale_x), M_RANGE(0, 10000.0)}, + {"video-scale-y", OPT_FLOAT(scale_y), M_RANGE(0, 10000.0)}, + {"video-margin-ratio-left", OPT_FLOAT(margin_x[0]), M_RANGE(0.0, 1.0)}, + {"video-margin-ratio-right", OPT_FLOAT(margin_x[1]), M_RANGE(0.0, 1.0)}, + {"video-margin-ratio-top", OPT_FLOAT(margin_y[0]), M_RANGE(0.0, 1.0)}, + {"video-margin-ratio-bottom", OPT_FLOAT(margin_y[1]), M_RANGE(0.0, 1.0)}, + {"video-crop", OPT_RECT(video_crop), .flags = UPDATE_IMGPAR}, + {"video-unscaled", OPT_CHOICE(unscaled, + {"no", 0}, {"yes", 1}, {"downscale-big", 2})}, + {"wid", OPT_INT64(WinID)}, + {"screen", OPT_CHOICE(screen_id, {"default", -1}), M_RANGE(0, 32)}, + {"screen-name", OPT_STRING(screen_name)}, + {"fs-screen", OPT_CHOICE(fsscreen_id, {"all", -2}, {"current", -1}), + M_RANGE(0, 32)}, + {"fs-screen-name", OPT_STRING(fsscreen_name)}, + {"keepaspect", OPT_BOOL(keepaspect)}, + {"keepaspect-window", OPT_BOOL(keepaspect_window)}, + {"hidpi-window-scale", OPT_BOOL(hidpi_window_scale)}, + {"native-fs", OPT_BOOL(native_fs)}, + {"display-fps-override", OPT_DOUBLE(display_fps_override), + M_RANGE(0, DBL_MAX)}, + {"video-timing-offset", OPT_DOUBLE(timing_offset), M_RANGE(0.0, 1.0)}, + {"video-sync", OPT_CHOICE(video_sync, + {"audio", VS_DEFAULT}, + {"display-resample", VS_DISP_RESAMPLE}, + {"display-resample-vdrop", VS_DISP_RESAMPLE_VDROP}, + {"display-resample-desync", VS_DISP_RESAMPLE_NONE}, + {"display-tempo", VS_DISP_TEMPO}, + {"display-adrop", VS_DISP_ADROP}, + {"display-vdrop", VS_DISP_VDROP}, + {"display-desync", VS_DISP_NONE}, + {"desync", VS_NONE})}, +#if HAVE_X11 + {"x11-netwm", OPT_CHOICE(x11_netwm, {"auto", 0}, {"no", -1}, {"yes", 1})}, + {"x11-bypass-compositor", OPT_CHOICE(x11_bypass_compositor, + {"no", 0}, {"yes", 1}, {"fs-only", 2}, {"never", 3})}, + {"x11-present", OPT_CHOICE(x11_present, + {"no", 0}, {"auto", 1}, {"yes", 2})}, + {"x11-wid-title", OPT_BOOL(x11_wid_title)}, +#endif +#if HAVE_WAYLAND + {"wayland-content-type", OPT_CHOICE(content_type, {"auto", -1}, {"none", 0}, + {"photo", 1}, {"video", 2}, {"game", 3})}, +#endif +#if HAVE_WIN32_DESKTOP +// For old MinGW-w64 compatibility +#define DWMWCP_DEFAULT 0 +#define DWMWCP_DONOTROUND 1 +#define DWMWCP_ROUND 2 +#define DWMWCP_ROUNDSMALL 3 + +#define DWMSBT_AUTO 0 +#define DWMSBT_NONE 1 +#define DWMSBT_MAINWINDOW 2 +#define DWMSBT_TRANSIENTWINDOW 3 +#define DWMSBT_TABBEDWINDOW 4 + + {"backdrop-type", OPT_CHOICE(backdrop_type, {"auto", DWMSBT_AUTO}, {"none", DWMSBT_NONE}, + {"mica", DWMSBT_MAINWINDOW}, {"acrylic", DWMSBT_TRANSIENTWINDOW}, {"mica-alt", DWMSBT_TABBEDWINDOW})}, + {"window-affinity", OPT_CHOICE(window_affinity, {"default", WDA_NONE}, + {"excludefromcapture", WDA_EXCLUDEFROMCAPTURE}, {"monitor", WDA_MONITOR})}, + {"vo-mmcss-profile", OPT_STRING(mmcss_profile)}, + {"window-corners", OPT_CHOICE(window_corners, + {"default", DWMWCP_DEFAULT}, + {"donotround", DWMWCP_DONOTROUND}, + {"round", DWMWCP_ROUND}, + {"roundsmall", DWMWCP_ROUNDSMALL})}, +#endif +#if HAVE_EGL_ANDROID + {"android-surface-size", OPT_SIZE_BOX(android_surface_size)}, +#endif + {"swapchain-depth", OPT_INT(swapchain_depth), M_RANGE(1, VO_MAX_SWAPCHAIN_DEPTH)}, + {"override-display-fps", OPT_REPLACED("display-fps-override")}, + {0} +}; + +const struct m_sub_options vo_sub_opts = { + .opts = mp_vo_opt_list, + .size = sizeof(struct mp_vo_opts), + .defaults = &(const struct mp_vo_opts){ + .video_driver_list = NULL, + .drag_and_drop = -1, + .monitor_pixel_aspect = 1.0, + .screen_id = -1, + .fsscreen_id = -1, + .panscan = 0.0f, + .scale_x = 1.0f, + .scale_y = 1.0f, + .auto_window_resize = true, + .keepaspect = true, + .keepaspect_window = true, + .hidpi_window_scale = true, + .native_fs = true, + .taskbar_progress = true, + .border = true, + .title_bar = true, + .appid = "mpv", + .content_type = -1, + .WinID = -1, + .window_scale = 1.0, + .x11_bypass_compositor = 2, + .x11_present = 1, + .mmcss_profile = "Playback", + .ontop_level = -1, + .timing_offset = 0.050, + .swapchain_depth = 3, + .focus_on_open = true, + }, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct mp_sub_filter_opts + +const struct m_sub_options mp_sub_filter_opts = { + .opts = (const struct m_option[]){ + {"sub-filter-sdh", OPT_BOOL(sub_filter_SDH)}, + {"sub-filter-sdh-harder", OPT_BOOL(sub_filter_SDH_harder)}, + {"sub-filter-regex-enable", OPT_BOOL(rf_enable)}, + {"sub-filter-regex-plain", OPT_BOOL(rf_plain)}, + {"sub-filter-regex", OPT_STRINGLIST(rf_items)}, + {"sub-filter-jsre", OPT_STRINGLIST(jsre_items)}, + {"sub-filter-regex-warn", OPT_BOOL(rf_warn)}, + {0} + }, + .size = sizeof(OPT_BASE_STRUCT), + .defaults = &(OPT_BASE_STRUCT){ + .rf_enable = true, + }, + .change_flags = UPDATE_SUB_FILT, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct mp_subtitle_opts + +const struct m_sub_options mp_subtitle_sub_opts = { + .opts = (const struct m_option[]){ + {"sub-delay", OPT_FLOAT(sub_delay)}, + {"sub-fps", OPT_FLOAT(sub_fps)}, + {"sub-speed", OPT_FLOAT(sub_speed)}, + {"sub-visibility", OPT_BOOL(sub_visibility)}, + {"secondary-sub-visibility", OPT_BOOL(sec_sub_visibility)}, + {"sub-forced-events-only", OPT_BOOL(sub_forced_events_only)}, + {"stretch-dvd-subs", OPT_BOOL(stretch_dvd_subs)}, + {"stretch-image-subs-to-screen", OPT_BOOL(stretch_image_subs)}, + {"image-subs-video-resolution", OPT_BOOL(image_subs_video_res)}, + {"sub-fix-timing", OPT_BOOL(sub_fix_timing)}, + {"sub-stretch-durations", OPT_BOOL(sub_stretch_durations)}, + {"sub-pos", OPT_FLOAT(sub_pos), M_RANGE(0.0, 150.0)}, + {"sub-gauss", OPT_FLOAT(sub_gauss), M_RANGE(0.0, 3.0)}, + {"sub-gray", OPT_BOOL(sub_gray)}, + {"sub-ass", OPT_BOOL(ass_enabled), .flags = UPDATE_SUB_HARD}, + {"sub-scale", OPT_FLOAT(sub_scale), M_RANGE(0, 100)}, + {"sub-ass-line-spacing", OPT_FLOAT(ass_line_spacing), + M_RANGE(-1000, 1000)}, + {"sub-use-margins", OPT_BOOL(sub_use_margins)}, + {"sub-ass-force-margins", OPT_BOOL(ass_use_margins)}, + {"sub-ass-vsfilter-aspect-compat", OPT_BOOL(ass_vsfilter_aspect_compat)}, + {"sub-ass-vsfilter-color-compat", OPT_CHOICE(ass_vsfilter_color_compat, + {"no", 0}, {"basic", 1}, {"full", 2}, {"force-601", 3})}, + {"sub-ass-vsfilter-blur-compat", OPT_BOOL(ass_vsfilter_blur_compat)}, + {"embeddedfonts", OPT_BOOL(use_embedded_fonts), .flags = UPDATE_SUB_HARD}, + {"sub-ass-style-overrides", OPT_STRINGLIST(ass_style_override_list), + .flags = UPDATE_SUB_HARD}, + {"sub-ass-styles", OPT_STRING(ass_styles_file), + .flags = M_OPT_FILE | UPDATE_SUB_HARD}, + {"sub-ass-hinting", OPT_CHOICE(ass_hinting, + {"none", 0}, {"light", 1}, {"normal", 2}, {"native", 3})}, + {"sub-ass-shaper", OPT_CHOICE(ass_shaper, + {"simple", 0}, {"complex", 1})}, + {"sub-ass-justify", OPT_BOOL(ass_justify)}, + {"sub-ass-override", OPT_CHOICE(ass_style_override, + {"no", 0}, {"yes", 1}, {"force", 3}, {"scale", 4}, {"strip", 5}), + .flags = UPDATE_SUB_HARD}, + {"sub-scale-by-window", OPT_BOOL(sub_scale_by_window)}, + {"sub-scale-with-window", OPT_BOOL(sub_scale_with_window)}, + {"sub-ass-scale-with-window", OPT_BOOL(ass_scale_with_window)}, + {"sub", OPT_SUBSTRUCT(sub_style, sub_style_conf)}, + {"sub-clear-on-seek", OPT_BOOL(sub_clear_on_seek)}, + {"teletext-page", OPT_INT(teletext_page), M_RANGE(1, 999)}, + {"sub-past-video-end", OPT_BOOL(sub_past_video_end)}, + {"sub-ass-force-style", OPT_REPLACED("sub-ass-style-overrides")}, + {0} + }, + .size = sizeof(OPT_BASE_STRUCT), + .defaults = &(OPT_BASE_STRUCT){ + .sub_visibility = true, + .sec_sub_visibility = true, + .sub_pos = 100, + .sub_speed = 1.0, + .ass_enabled = true, + .sub_scale_by_window = true, + .sub_use_margins = true, + .sub_scale_with_window = true, + .teletext_page = 100, + .sub_scale = 1, + .ass_vsfilter_aspect_compat = true, + .ass_vsfilter_color_compat = 1, + .ass_vsfilter_blur_compat = true, + .ass_style_override = 1, + .ass_shaper = 1, + .use_embedded_fonts = true, + }, + .change_flags = UPDATE_OSD, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct mp_osd_render_opts + +const struct m_sub_options mp_osd_render_sub_opts = { + .opts = (const struct m_option[]){ + {"osd-bar-align-x", OPT_FLOAT(osd_bar_align_x), M_RANGE(-1.0, +1.0)}, + {"osd-bar-align-y", OPT_FLOAT(osd_bar_align_y), M_RANGE(-1.0, +1.0)}, + {"osd-bar-w", OPT_FLOAT(osd_bar_w), M_RANGE(1, 100)}, + {"osd-bar-h", OPT_FLOAT(osd_bar_h), M_RANGE(0.1, 50)}, + {"osd", OPT_SUBSTRUCT(osd_style, osd_style_conf)}, + {"osd-scale", OPT_FLOAT(osd_scale), M_RANGE(0, 100)}, + {"osd-scale-by-window", OPT_BOOL(osd_scale_by_window)}, + {"force-rgba-osd-rendering", OPT_BOOL(force_rgba_osd)}, + {0} + }, + .size = sizeof(OPT_BASE_STRUCT), + .defaults = &(OPT_BASE_STRUCT){ + .osd_bar_align_y = 0.5, + .osd_bar_w = 75.0, + .osd_bar_h = 3.125, + .osd_scale = 1, + .osd_scale_by_window = true, + }, + .change_flags = UPDATE_OSD, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct cuda_opts + +const struct m_sub_options cuda_conf = { + .opts = (const struct m_option[]){ + {"decode-device", OPT_CHOICE(cuda_device, {"auto", -1}), + M_RANGE(0, INT_MAX)}, + {0} + }, + .size = sizeof(struct cuda_opts), + .defaults = &(const struct cuda_opts){ + .cuda_device = -1, + }, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct dvd_opts + +const struct m_sub_options dvd_conf = { + .opts = (const struct m_option[]){ + {"dvd-device", OPT_STRING(device), .flags = M_OPT_FILE}, + {"dvd-speed", OPT_INT(speed)}, + {"dvd-angle", OPT_INT(angle), M_RANGE(1, 99)}, + {0} + }, + .size = sizeof(struct dvd_opts), + .defaults = &(const struct dvd_opts){ + .angle = 1, + }, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct filter_opts + +const struct m_sub_options filter_conf = { + .opts = (const struct m_option[]){ + {"deinterlace", OPT_BOOL(deinterlace)}, + {0} + }, + .size = sizeof(OPT_BASE_STRUCT), + .change_flags = UPDATE_IMGPAR, +}; + +#undef OPT_BASE_STRUCT +#define OPT_BASE_STRUCT struct MPOpts + +static const m_option_t mp_opts[] = { + // handled in command line pre-parser (parse_commandline.c) + {"v", &m_option_type_dummy_flag, CONF_NOCFG | M_OPT_NOPROP, + .offset = -1}, + {"playlist", CONF_TYPE_STRING, CONF_NOCFG | M_OPT_FILE, .offset = -1}, + {"{", &m_option_type_dummy_flag, CONF_NOCFG | M_OPT_NOPROP, + .offset = -1}, + {"}", &m_option_type_dummy_flag, CONF_NOCFG | M_OPT_NOPROP, + .offset = -1}, + + // handled in m_config.c + { "include", CONF_TYPE_STRING, M_OPT_FILE, .offset = -1}, + { "profile", CONF_TYPE_STRING_LIST, 0, .offset = -1}, + { "show-profile", CONF_TYPE_STRING, CONF_NOCFG | M_OPT_NOPROP | + M_OPT_OPTIONAL_PARAM, .offset = -1}, + { "list-options", &m_option_type_dummy_flag, CONF_NOCFG | M_OPT_NOPROP, + .offset = -1}, + {"list-properties", OPT_BOOL(property_print_help), + .flags = CONF_NOCFG | M_OPT_NOPROP}, + { "help", CONF_TYPE_STRING, CONF_NOCFG | M_OPT_NOPROP | M_OPT_OPTIONAL_PARAM, + .offset = -1}, + { "h", CONF_TYPE_STRING, CONF_NOCFG | M_OPT_NOPROP | M_OPT_OPTIONAL_PARAM, + .offset = -1}, + + {"list-protocols", OPT_PRINT(stream_print_proto_list)}, + {"version", OPT_PRINT(print_version)}, + {"V", OPT_PRINT(print_version)}, + + {"player-operation-mode", OPT_CHOICE(operation_mode, + {"cplayer", 0}, {"pseudo-gui", 1}), + .flags = M_OPT_PRE_PARSE | M_OPT_NOPROP}, + + {"shuffle", OPT_BOOL(shuffle)}, + +// ------------------------- common options -------------------- + {"quiet", OPT_BOOL(quiet)}, + {"really-quiet", OPT_BOOL(msg_really_quiet), + .flags = CONF_PRE_PARSE | UPDATE_TERM}, + {"terminal", OPT_BOOL(use_terminal), .flags = CONF_PRE_PARSE | UPDATE_TERM}, + {"msg-level", OPT_MSGLEVELS(msg_levels), + .flags = CONF_PRE_PARSE | UPDATE_TERM}, + {"dump-stats", OPT_STRING(dump_stats), + .flags = UPDATE_TERM | CONF_PRE_PARSE | M_OPT_FILE}, + {"msg-color", OPT_BOOL(msg_color), .flags = CONF_PRE_PARSE | UPDATE_TERM}, + {"log-file", OPT_STRING(log_file), + .flags = CONF_PRE_PARSE | M_OPT_FILE | UPDATE_TERM}, + {"msg-module", OPT_BOOL(msg_module), .flags = UPDATE_TERM}, + {"msg-time", OPT_BOOL(msg_time), .flags = UPDATE_TERM}, +#if HAVE_WIN32_DESKTOP + {"priority", OPT_CHOICE(w32_priority, + {"no", 0}, + {"realtime", REALTIME_PRIORITY_CLASS}, + {"high", HIGH_PRIORITY_CLASS}, + {"abovenormal", ABOVE_NORMAL_PRIORITY_CLASS}, + {"normal", NORMAL_PRIORITY_CLASS}, + {"belownormal", BELOW_NORMAL_PRIORITY_CLASS}, + {"idle", IDLE_PRIORITY_CLASS}), + .flags = UPDATE_PRIORITY}, +#endif + {"config", OPT_BOOL(load_config), .flags = CONF_PRE_PARSE}, + {"config-dir", OPT_STRING(force_configdir), + .flags = CONF_NOCFG | CONF_PRE_PARSE | M_OPT_FILE}, + {"reset-on-next-file", OPT_STRINGLIST(reset_options)}, + +#if HAVE_LUA || HAVE_JAVASCRIPT || HAVE_CPLUGINS + {"scripts", OPT_PATHLIST(script_files), .flags = M_OPT_FILE}, + {"script", OPT_CLI_ALIAS("scripts-append")}, + {"script-opts", OPT_KEYVALUELIST(script_opts)}, + {"load-scripts", OPT_BOOL(auto_load_scripts)}, +#endif +#if HAVE_JAVASCRIPT + {"js-memory-report", OPT_BOOL(js_memory_report)}, +#endif +#if HAVE_LUA + {"osc", OPT_BOOL(lua_load_osc), .flags = UPDATE_BUILTIN_SCRIPTS}, + {"ytdl", OPT_BOOL(lua_load_ytdl), .flags = UPDATE_BUILTIN_SCRIPTS}, + {"ytdl-format", OPT_STRING(lua_ytdl_format)}, + {"ytdl-raw-options", OPT_KEYVALUELIST(lua_ytdl_raw_options)}, + {"load-stats-overlay", OPT_BOOL(lua_load_stats), + .flags = UPDATE_BUILTIN_SCRIPTS}, + {"load-osd-console", OPT_BOOL(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 -------------------- + +#if HAVE_DVDNAV + {"", OPT_SUBSTRUCT(dvd_opts, dvd_conf)}, +#endif + {"edition", OPT_CHOICE(edition_id, {"auto", -1}), M_RANGE(0, 8190)}, +#if HAVE_LIBBLURAY + {"bluray", OPT_SUBSTRUCT(stream_bluray_opts, stream_bluray_conf)}, +#endif /* HAVE_LIBBLURAY */ + +// ------------------------- demuxer options -------------------- + + {"frames", OPT_CHOICE(play_frames, {"all", -1}), M_RANGE(0, INT_MAX)}, + + {"start", OPT_REL_TIME(play_start)}, + {"end", OPT_REL_TIME(play_end)}, + {"length", OPT_REL_TIME(play_length)}, + + {"play-direction", OPT_CHOICE(play_dir, + {"forward", 1}, {"+", 1}, {"backward", -1}, {"-", -1})}, + + {"rebase-start-time", OPT_BOOL(rebase_start_time)}, + + {"ab-loop-a", OPT_TIME(ab_loop[0]), .flags = M_OPT_ALLOW_NO}, + {"ab-loop-b", OPT_TIME(ab_loop[1]), .flags = M_OPT_ALLOW_NO}, + {"ab-loop-count", OPT_CHOICE(ab_loop_count, {"inf", -1}), + M_RANGE(0, INT_MAX)}, + + {"playlist-start", OPT_CHOICE(playlist_pos, {"auto", -1}, {"no", -1}), + M_RANGE(0, INT_MAX)}, + + {"pause", OPT_BOOL(pause)}, + {"keep-open", OPT_CHOICE(keep_open, + {"no", 0}, + {"yes", 1}, + {"always", 2})}, + {"keep-open-pause", OPT_BOOL(keep_open_pause)}, + {"image-display-duration", OPT_DOUBLE(image_display_duration), + M_RANGE(0, INFINITY)}, + + // select audio/video/subtitle stream + // keep in sync with num_ptracks[] and MAX_PTRACKS + {"aid", OPT_TRACKCHOICE(stream_id[0][STREAM_AUDIO])}, + {"vid", OPT_TRACKCHOICE(stream_id[0][STREAM_VIDEO])}, + {"sid", OPT_TRACKCHOICE(stream_id[0][STREAM_SUB])}, + {"secondary-sid", OPT_TRACKCHOICE(stream_id[1][STREAM_SUB])}, + {"sub", OPT_ALIAS("sid")}, + {"video", OPT_ALIAS("vid")}, + {"audio", OPT_ALIAS("aid")}, + {"alang", OPT_STRINGLIST(stream_lang[STREAM_AUDIO])}, + {"slang", OPT_STRINGLIST(stream_lang[STREAM_SUB])}, + {"vlang", OPT_STRINGLIST(stream_lang[STREAM_VIDEO])}, + {"track-auto-selection", OPT_BOOL(stream_auto_sel)}, + {"subs-with-matching-audio", OPT_BOOL(subs_with_matching_audio)}, + {"subs-match-os-language", OPT_BOOL(subs_match_os_language)}, + {"subs-fallback", OPT_CHOICE(subs_fallback, {"no", 0}, {"default", 1}, {"yes", 2})}, + {"subs-fallback-forced", OPT_CHOICE(subs_fallback_forced, {"no", 0}, + {"yes", 1}, {"always", 2})}, + + {"lavfi-complex", OPT_STRING(lavfi_complex), .flags = UPDATE_LAVFI_COMPLEX}, + + {"audio-display", OPT_CHOICE(audio_display, {"no", 0}, + {"embedded-first", 1}, {"external-first", 2})}, + + {"hls-bitrate", OPT_CHOICE(hls_bitrate, + {"no", -1}, {"min", 0}, {"max", INT_MAX}), M_RANGE(0, INT_MAX)}, + + {"display-tags", OPT_STRINGLIST(display_tags)}, + +#if HAVE_CDDA + {"cdda", OPT_SUBSTRUCT(stream_cdda_opts, stream_cdda_conf)}, + {"cdrom-device", OPT_REPLACED("cdda-device")}, +#endif + + // demuxer.c - select audio/sub file/demuxer + {"demuxer", OPT_STRING(demuxer_name), .help = demuxer_help}, + {"audio-demuxer", OPT_STRING(audio_demuxer_name), .help = demuxer_help}, + {"sub-demuxer", OPT_STRING(sub_demuxer_name), .help = demuxer_help}, + {"demuxer-thread", OPT_BOOL(demuxer_thread)}, + {"demuxer-termination-timeout", OPT_DOUBLE(demux_termination_timeout)}, + {"demuxer-cache-wait", OPT_BOOL(demuxer_cache_wait)}, + {"prefetch-playlist", OPT_BOOL(prefetch_open)}, + {"cache-pause", OPT_BOOL(cache_pause)}, + {"cache-pause-initial", OPT_BOOL(cache_pause_initial)}, + {"cache-pause-wait", OPT_FLOAT(cache_pause_wait), M_RANGE(0, DBL_MAX)}, + +#if HAVE_DVBIN + {"dvbin", OPT_SUBSTRUCT(stream_dvb_opts, stream_dvb_conf)}, +#endif + {"", OPT_SUBSTRUCT(stream_lavf_opts, stream_lavf_conf)}, + +// ------------------------- a-v sync options -------------------- + + // set A-V sync correction speed (0=disables it): + {"mc", OPT_FLOAT(default_max_pts_correction), M_RANGE(0, 100)}, + + {"audio-samplerate", OPT_INT(force_srate), .flags = UPDATE_AUDIO, + M_RANGE(0, 16*48000)}, + {"audio-channels", OPT_CHANNELS(audio_output_channels), .flags = UPDATE_AUDIO}, + {"audio-format", OPT_AUDIOFORMAT(audio_output_format), .flags = UPDATE_AUDIO}, + {"speed", OPT_DOUBLE(playback_speed), M_RANGE(0.01, 100.0)}, + + {"audio-pitch-correction", OPT_BOOL(pitch_correction)}, + + // set a-v distance + {"audio-delay", OPT_FLOAT(audio_delay)}, + +// ------------------------- codec/vfilter options -------------------- + + {"af", OPT_SETTINGSLIST(af_settings, &af_obj_list)}, + {"vf", OPT_SETTINGSLIST(vf_settings, &vf_obj_list)}, + + {"", OPT_SUBSTRUCT(filter_opts, filter_conf)}, + + {"", OPT_SUBSTRUCT(dec_wrapper, dec_wrapper_conf)}, + {"", OPT_SUBSTRUCT(vd_lavc_params, vd_lavc_conf)}, + {"ad-lavc", OPT_SUBSTRUCT(ad_lavc_params, ad_lavc_conf)}, + + {"", OPT_SUBSTRUCT(demux_lavf, demux_lavf_conf)}, + {"demuxer-rawaudio", OPT_SUBSTRUCT(demux_rawaudio, demux_rawaudio_conf)}, + {"demuxer-rawvideo", OPT_SUBSTRUCT(demux_rawvideo, demux_rawvideo_conf)}, + {"", OPT_SUBSTRUCT(demux_playlist, demux_playlist_conf)}, + {"demuxer-mkv", OPT_SUBSTRUCT(demux_mkv, demux_mkv_conf)}, + {"demuxer-cue", OPT_SUBSTRUCT(demux_cue, demux_cue_conf)}, + +// ------------------------- subtitles options -------------------- + + {"sub-files", OPT_PATHLIST(sub_name), .flags = M_OPT_FILE}, + {"sub-file", OPT_CLI_ALIAS("sub-files-append")}, + {"audio-files", OPT_PATHLIST(audio_files), .flags = M_OPT_FILE}, + {"audio-file", OPT_CLI_ALIAS("audio-files-append")}, + {"cover-art-files", OPT_PATHLIST(coverart_files), .flags = M_OPT_FILE}, + {"cover-art-file", OPT_CLI_ALIAS("cover-art-files-append")}, + + {"sub-file-paths", OPT_PATHLIST(sub_paths), .flags = M_OPT_FILE}, + {"audio-file-paths", OPT_PATHLIST(audiofile_paths), .flags = M_OPT_FILE}, + + {"external-files", OPT_PATHLIST(external_files), .flags = M_OPT_FILE}, + {"external-file", OPT_CLI_ALIAS("external-files-append")}, + {"autoload-files", OPT_BOOL(autoload_files)}, + + {"sub-auto", OPT_CHOICE(sub_auto, + {"no", -1}, {"exact", 0}, {"fuzzy", 1}, {"all", 2})}, + {"sub-auto-exts", OPT_STRINGLIST(sub_auto_exts), .flags = UPDATE_SUB_EXTS}, + {"audio-file-auto", OPT_CHOICE(audiofile_auto, + {"no", -1}, {"exact", 0}, {"fuzzy", 1}, {"all", 2})}, + {"audio-file-auto-exts", OPT_STRINGLIST(audiofile_auto_exts)}, + {"cover-art-auto", OPT_CHOICE(coverart_auto, + {"no", -1}, {"exact", 0}, {"fuzzy", 1}, {"all", 2})}, + {"cover-art-auto-exts", OPT_STRINGLIST(coverart_auto_exts)}, + {"cover-art-whitelist", OPT_BOOL(coverart_whitelist)}, + + {"", OPT_SUBSTRUCT(subs_rend, mp_subtitle_sub_opts)}, + {"", OPT_SUBSTRUCT(subs_filt, mp_sub_filter_opts)}, + {"", OPT_SUBSTRUCT(osd_rend, mp_osd_render_sub_opts)}, + + {"osd-bar", OPT_BOOL(osd_bar_visible), .flags = UPDATE_OSD}, + +//---------------------- libao/libvo options ------------------------ + {"", OPT_SUBSTRUCT(ao_opts, ao_conf)}, + {"audio-exclusive", OPT_BOOL(audio_exclusive), .flags = UPDATE_AUDIO}, + {"audio-fallback-to-null", OPT_BOOL(ao_null_fallback)}, + {"audio-stream-silence", OPT_BOOL(audio_stream_silence)}, + {"audio-wait-open", OPT_FLOAT(audio_wait_open), M_RANGE(0, 60)}, + {"force-window", OPT_CHOICE(force_vo, + {"no", 0}, {"yes", 1}, {"immediate", 2})}, + + {"volume-max", OPT_FLOAT(softvol_max), M_RANGE(100, 1000)}, + // values <0 for volume and mute are legacy and ignored + {"volume", OPT_FLOAT(softvol_volume), .flags = UPDATE_VOL, + M_RANGE(-1, 1000)}, + {"mute", OPT_CHOICE(softvol_mute, + {"no", 0}, + {"auto", 0}, + {"yes", 1}), + .flags = UPDATE_VOL}, + {"replaygain", OPT_CHOICE(rgain_mode, + {"no", 0}, + {"track", 1}, + {"album", 2}), + .flags = UPDATE_VOL}, + {"replaygain-preamp", OPT_FLOAT(rgain_preamp), .flags = UPDATE_VOL, + M_RANGE(-150, 150)}, + {"replaygain-clip", OPT_BOOL(rgain_clip), .flags = UPDATE_VOL}, + {"replaygain-fallback", OPT_FLOAT(rgain_fallback), .flags = UPDATE_VOL, + M_RANGE(-200, 60)}, + {"gapless-audio", OPT_CHOICE(gapless_audio, + {"no", 0}, + {"yes", 1}, + {"weak", -1})}, + + {"title", OPT_STRING(wintitle)}, + {"force-media-title", OPT_STRING(media_title)}, + + {"cursor-autohide", OPT_CHOICE(cursor_autohide_delay, + {"no", -1}, {"always", -2}), M_RANGE(0, 30000)}, + {"cursor-autohide-fs-only", OPT_BOOL(cursor_autohide_fs)}, + {"stop-screensaver", OPT_CHOICE(stop_screensaver, + {"no", 0}, + {"yes", 1}, + {"always", 2}), + .flags = UPDATE_SCREENSAVER}, + + {"", OPT_SUBSTRUCT(video_equalizer, mp_csp_equalizer_conf)}, + + {"use-filedir-conf", OPT_BOOL(use_filedir_conf)}, + {"osd-level", OPT_CHOICE(osd_level, + {"0", 0}, {"1", 1}, {"2", 2}, {"3", 3})}, + {"osd-on-seek", OPT_CHOICE(osd_on_seek, + {"no", 0}, + {"bar", 1}, + {"msg", 2}, + {"msg-bar", 3})}, + {"osd-duration", OPT_INT(osd_duration), M_RANGE(0, 3600000)}, + {"osd-fractions", OPT_BOOL(osd_fractions)}, + + {"sstep", OPT_DOUBLE(step_sec), M_RANGE(0, DBL_MAX)}, + + {"framedrop", OPT_CHOICE(frame_dropping, + {"no", 0}, + {"vo", 1}, + {"decoder", 2}, + {"decoder+vo", 3})}, + {"video-latency-hacks", OPT_BOOL(video_latency_hacks)}, + + {"untimed", OPT_BOOL(untimed)}, + + {"stream-dump", OPT_STRING(stream_dump), .flags = M_OPT_FILE}, + + {"stop-playback-on-init-failure", OPT_BOOL(stop_playback_on_init_failure)}, + + {"loop-playlist", OPT_CHOICE(loop_times, + {"no", 1}, + {"inf", -1}, {"yes", -1}, + {"force", -2}), + M_RANGE(1, 10000)}, + {"loop-file", OPT_CHOICE(loop_file, + {"no", 0}, + {"inf", -1}, + {"yes", -1}), + M_RANGE(0, 10000)}, + {"loop", OPT_ALIAS("loop-file")}, + + {"resume-playback", OPT_BOOL(position_resume)}, + {"resume-playback-check-mtime", OPT_BOOL(position_check_mtime)}, + {"save-position-on-quit", OPT_BOOL(position_save_on_quit)}, + {"write-filename-in-watch-later-config", + OPT_BOOL(write_filename_in_watch_later_config)}, + {"ignore-path-in-watch-later-config", + OPT_BOOL(ignore_path_in_watch_later_config)}, + {"watch-later-dir", OPT_STRING(watch_later_dir), + .flags = M_OPT_FILE}, + {"watch-later-directory", OPT_ALIAS("watch-later-dir")}, + {"watch-later-options", OPT_STRINGLIST(watch_later_options)}, + + {"ordered-chapters", OPT_BOOL(ordered_chapters)}, + {"ordered-chapters-files", OPT_STRING(ordered_chapters_files), + .flags = M_OPT_FILE}, + {"chapter-merge-threshold", OPT_INT(chapter_merge_threshold), + M_RANGE(0, 10000)}, + + {"chapter-seek-threshold", OPT_DOUBLE(chapter_seek_threshold)}, + + {"chapters-file", OPT_STRING(chapter_file), .flags = M_OPT_FILE}, + + {"merge-files", OPT_BOOL(merge_files)}, + + // a-v sync stuff: + {"initial-audio-sync", OPT_BOOL(initial_audio_sync)}, + {"video-sync-max-video-change", OPT_DOUBLE(sync_max_video_change), + M_RANGE(0, DBL_MAX)}, + {"video-sync-max-audio-change", OPT_DOUBLE(sync_max_audio_change), + M_RANGE(0, 1)}, + {"video-sync-max-factor", OPT_INT(sync_max_factor), M_RANGE(1, 10)}, + {"hr-seek", OPT_CHOICE(hr_seek, + {"no", -1}, {"absolute", 0}, {"yes", 1}, {"always", 1}, {"default", 2})}, + {"hr-seek-demuxer-offset", OPT_FLOAT(hr_seek_demuxer_offset)}, + {"hr-seek-framedrop", OPT_BOOL(hr_seek_framedrop)}, + {"autosync", OPT_CHOICE(autosync, {"no", -1}), M_RANGE(0, 10000)}, + + {"term-osd", OPT_CHOICE(term_osd, + {"force", 1}, {"auto", 2}, {"no", 0}), .flags = UPDATE_OSD}, + + {"term-osd-bar", OPT_BOOL(term_osd_bar), .flags = UPDATE_OSD}, + {"term-osd-bar-chars", OPT_STRING(term_osd_bar_chars), .flags = UPDATE_OSD}, + {"term-remaining-playtime", OPT_BOOL(term_remaining_playtime), .flags = UPDATE_OSD}, + {"term-title", OPT_STRING(term_title), .flags = UPDATE_OSD}, + + {"term-playing-msg", OPT_STRING(playing_msg)}, + {"osd-playing-msg", OPT_STRING(osd_playing_msg)}, + {"osd-playing-msg-duration", OPT_INT(osd_playing_msg_duration), + M_RANGE(0, 3600000)}, + {"term-status-msg", OPT_STRING(status_msg), .flags = UPDATE_OSD}, + {"osd-status-msg", OPT_STRING(osd_status_msg), .flags = UPDATE_OSD}, + {"osd-msg1", OPT_STRING(osd_msg[0]), .flags = UPDATE_OSD}, + {"osd-msg2", OPT_STRING(osd_msg[1]), .flags = UPDATE_OSD}, + {"osd-msg3", OPT_STRING(osd_msg[2]), .flags = UPDATE_OSD}, + + {"video-osd", OPT_BOOL(video_osd), .flags = UPDATE_OSD}, + + {"idle", OPT_CHOICE(player_idle_mode, + {"no", 0}, {"once", 1}, {"yes", 2})}, + + {"input-terminal", OPT_BOOL(consolecontrols), .flags = UPDATE_TERM}, + + {"input-ipc-server", OPT_STRING(ipc_path), .flags = M_OPT_FILE}, +#if HAVE_POSIX + {"input-ipc-client", OPT_STRING(ipc_client)}, +#endif + + {"screenshot", OPT_SUBSTRUCT(screenshot_image_opts, screenshot_conf)}, + {"screenshot-template", OPT_STRING(screenshot_template)}, + {"screenshot-dir", OPT_STRING(screenshot_dir), + .flags = M_OPT_FILE}, + {"screenshot-directory", OPT_ALIAS("screenshot-dir")}, + {"screenshot-sw", OPT_BOOL(screenshot_sw)}, + + {"", OPT_SUBSTRUCT(resample_opts, resample_conf)}, + + {"", OPT_SUBSTRUCT(input_opts, input_config)}, + + {"", OPT_SUBSTRUCT(vo, vo_sub_opts)}, + {"", OPT_SUBSTRUCT(demux_opts, demux_conf)}, + {"", OPT_SUBSTRUCT(demux_cache_opts, demux_cache_conf)}, + {"", OPT_SUBSTRUCT(stream_opts, stream_conf)}, + + {"", OPT_SUBSTRUCT(ra_ctx_opts, ra_ctx_conf)}, + {"", OPT_SUBSTRUCT(gl_video_opts, gl_video_conf)}, + {"", OPT_SUBSTRUCT(spirv_opts, spirv_conf)}, + +#if HAVE_GL + {"", OPT_SUBSTRUCT(opengl_opts, opengl_conf)}, +#endif + +#if HAVE_VULKAN + {"", OPT_SUBSTRUCT(vulkan_opts, vulkan_conf)}, +#if HAVE_VK_KHR_DISPLAY + {"", OPT_SUBSTRUCT(vulkan_display_opts, vulkan_display_conf)}, +#endif +#endif + +#if HAVE_D3D11 + {"", OPT_SUBSTRUCT(d3d11_opts, d3d11_conf)}, +#if HAVE_D3D_HWACCEL + {"", OPT_SUBSTRUCT(d3d11va_opts, d3d11va_conf)}, +#endif +#endif + +#if HAVE_EGL_ANGLE_WIN32 + {"", OPT_SUBSTRUCT(angle_opts, angle_conf)}, +#endif + +#if HAVE_COCOA + {"", OPT_SUBSTRUCT(macos_opts, macos_conf)}, +#endif + +#if HAVE_DRM + {"", OPT_SUBSTRUCT(drm_opts, drm_conf)}, +#endif + +#if HAVE_WAYLAND + {"", OPT_SUBSTRUCT(wayland_opts, wayland_conf)}, +#endif + +#if HAVE_GL_WIN32 + {"", OPT_SUBSTRUCT(wingl_opts, wingl_conf)}, +#endif + +#if HAVE_CUDA_HWACCEL + {"cuda", OPT_SUBSTRUCT(cuda_opts, cuda_conf)}, +#endif + +#if HAVE_VAAPI + {"vaapi", OPT_SUBSTRUCT(vaapi_opts, vaapi_conf)}, +#endif + + {"sws", OPT_SUBSTRUCT(sws_opts, sws_conf)}, + +#if HAVE_ZIMG + {"zimg", OPT_SUBSTRUCT(zimg_opts, zimg_conf)}, +#endif + + {"", OPT_SUBSTRUCT(encode_opts, encode_config)}, + + {"play-dir", OPT_REPLACED("play-direction")}, + {"sub-forced-only", OPT_REPLACED("sub-forced-events-only")}, + {0} +}; + +static const struct MPOpts mp_default_opts = { + .use_terminal = true, + .msg_color = true, + .softvol_max = 130, + .softvol_volume = 100, + .gapless_audio = -1, + .wintitle = "${?media-title:${media-title}}${!media-title:No file} - mpv", + .stop_screensaver = 1, + .cursor_autohide_delay = 1000, + .video_osd = true, + .osd_level = 1, + .osd_on_seek = 1, + .osd_duration = 1000, +#if HAVE_LUA + .lua_load_osc = true, + .lua_load_ytdl = true, + .lua_ytdl_format = NULL, + .lua_ytdl_raw_options = NULL, + .lua_load_stats = true, + .lua_load_console = true, + .lua_load_auto_profiles = -1, +#endif + .auto_load_scripts = true, + .loop_times = 1, + .ordered_chapters = true, + .chapter_merge_threshold = 100, + .chapter_seek_threshold = 5.0, + .hr_seek = 2, + .hr_seek_framedrop = true, + .sync_max_video_change = 1, + .sync_max_audio_change = 0.125, + .sync_max_factor = 5, + .load_config = true, + .position_resume = true, + .autoload_files = true, + .demuxer_thread = true, + .demux_termination_timeout = 0.1, + .hls_bitrate = INT_MAX, + .cache_pause = true, + .cache_pause_wait = 1.0, + .ab_loop = {MP_NOPTS_VALUE, MP_NOPTS_VALUE}, + .ab_loop_count = -1, + .edition_id = -1, + .default_max_pts_correction = -1, + .initial_audio_sync = true, + .frame_dropping = 1, + .term_osd = 2, + .term_osd_bar_chars = "[-+-]", + .term_remaining_playtime = true, + .consolecontrols = true, + .playlist_pos = -1, + .play_frames = -1, + .rebase_start_time = true, + .keep_open_pause = true, + .image_display_duration = 1.0, + .stream_id = { { [STREAM_AUDIO] = -1, + [STREAM_VIDEO] = -1, + [STREAM_SUB] = -1, }, + { [STREAM_AUDIO] = -2, + [STREAM_VIDEO] = -2, + [STREAM_SUB] = -2, }, }, + .stream_auto_sel = true, + .subs_with_matching_audio = true, + .subs_match_os_language = true, + .subs_fallback = 1, + .subs_fallback_forced = 1, + .audio_display = 1, + .audio_output_format = 0, // AF_FORMAT_UNKNOWN + .playback_speed = 1., + .pitch_correction = true, + .audiofile_auto = -1, + .coverart_whitelist = true, + .osd_bar_visible = true, + .screenshot_template = "mpv-shot%n", + .play_dir = 1, + + .audiofile_auto_exts = (char *[]){ + "aac", + "ac3", + "dts", + "eac3", + "flac", + "m4a", + "mka", + "mp3", + "ogg", + "opus", + "thd", + "wav", + "wv", + NULL + }, + + .coverart_auto_exts = (char *[]){ + "avif", + "bmp", + "gif", + "jpeg", + "jpg", + "jxl", + "png", + "tif", + "tiff", + "webp", + NULL + }, + + .sub_auto_exts = (char *[]){ + "ass", + "idx", + "lrc", + "mks", + "pgs", + "rt", + "sbv", + "scc", + "smi", + "srt", + "ssa", + "sub", + "sup", + "utf", + "utf-8", + "utf8", + "vtt", + NULL + }, + + .audio_output_channels = { + .set = 1, + .auto_safe = 1, + }, + + .display_tags = (char *[]){ + "Artist", "Album", "Album_Artist", "Comment", "Composer", + "Date", "Description", "Genre", "Performer", "Rating", + "Series", "Title", "Track", "icy-title", "service_name", + "Uploader", "Channel_URL", + NULL + }, + + .cuda_device = -1, + + .watch_later_options = (char *[]){ + "start", + "speed", + "edition", + "volume", + "mute", + "audio-delay", + "gamma", + "brightness", + "contrast", + "saturation", + "hue", + "deinterlace", + "vf", + "af", + "panscan", + "aid", + "vid", + "sid", + "sub-delay", + "sub-speed", + "sub-pos", + "sub-visibility", + "sub-scale", + "sub-use-margins", + "sub-ass-force-margins", + "sub-ass-vsfilter-aspect-compat", + "sub-ass-override", + "secondary-sub-visibility", + "ab-loop-a", + "ab-loop-b", + "video-aspect-override", + "video-aspect-method", + "video-unscaled", + "video-pan-x", + "video-pan-y", + "video-rotate", + "video-crop", + "video-zoom", + "video-scale-x", + "video-scale-y", + "video-align-x", + "video-align-y", + NULL + }, +}; + +const struct m_sub_options mp_opt_root = { + .opts = mp_opts, + .size = sizeof(struct MPOpts), + .defaults = &mp_default_opts, +}; + +#endif /* MPLAYER_CFG_MPLAYER_H */ diff --git a/options/options.h b/options/options.h new file mode 100644 index 0000000..aa071b2 --- /dev/null +++ b/options/options.h @@ -0,0 +1,406 @@ +#ifndef MPLAYER_OPTIONS_H +#define MPLAYER_OPTIONS_H + +#include <stdbool.h> +#include <stdint.h> +#include "m_option.h" +#include "common/common.h" + +typedef struct mp_vo_opts { + struct m_obj_settings *video_driver_list; + + bool taskbar_progress; + bool snap_window; + int drag_and_drop; + bool ontop; + int ontop_level; + bool fullscreen; + bool border; + bool title_bar; + bool all_workspaces; + bool window_minimized; + bool window_maximized; + bool focus_on_open; + + int screen_id; + char *screen_name; + int fsscreen_id; + char *fsscreen_name; + char *winname; + char *appid; + int content_type; + int x11_netwm; + int x11_bypass_compositor; + int x11_present; + bool x11_wid_title; + bool cursor_passthrough; + bool native_keyrepeat; + + float panscan; + float zoom; + float pan_x, pan_y; + float align_x, align_y; + float scale_x, scale_y; + float margin_x[2]; + float margin_y[2]; + int unscaled; + + struct m_geometry geometry; + struct m_geometry autofit; + struct m_geometry autofit_larger; + struct m_geometry autofit_smaller; + double window_scale; + + bool auto_window_resize; + bool keepaspect; + bool keepaspect_window; + bool hidpi_window_scale; + bool native_fs; + + int64_t WinID; + + float force_monitor_aspect; + float monitor_pixel_aspect; + bool force_render; + bool force_window_position; + + int backdrop_type; + int window_affinity; + char *mmcss_profile; + int window_corners; + + double display_fps_override; + double timing_offset; + int video_sync; + + struct m_geometry android_surface_size; + + int swapchain_depth; // max number of images to render ahead + + struct m_geometry video_crop; +} mp_vo_opts; + +// Subtitle options needed by the subtitle decoders/renderers. +struct mp_subtitle_opts { + bool sub_visibility; + bool sec_sub_visibility; + float sub_pos; + float sub_delay; + float sub_fps; + float sub_speed; + bool sub_forced_events_only; + bool stretch_dvd_subs; + bool stretch_image_subs; + bool image_subs_video_res; + bool sub_fix_timing; + bool sub_stretch_durations; + bool sub_scale_by_window; + bool sub_scale_with_window; + bool ass_scale_with_window; + struct osd_style_opts *sub_style; + float sub_scale; + float sub_gauss; + bool sub_gray; + bool ass_enabled; + float ass_line_spacing; + bool ass_use_margins; + bool sub_use_margins; + bool ass_vsfilter_aspect_compat; + int ass_vsfilter_color_compat; + bool ass_vsfilter_blur_compat; + bool use_embedded_fonts; + char **ass_style_override_list; + char *ass_styles_file; + int ass_style_override; + int ass_hinting; + int ass_shaper; + bool ass_justify; + bool sub_clear_on_seek; + int teletext_page; + bool sub_past_video_end; +}; + +struct mp_sub_filter_opts { + bool sub_filter_SDH; + bool sub_filter_SDH_harder; + bool rf_enable; + bool rf_plain; + char **rf_items; + char **jsre_items; + bool rf_warn; +}; + +struct mp_osd_render_opts { + float osd_bar_align_x; + float osd_bar_align_y; + float osd_bar_w; + float osd_bar_h; + float osd_scale; + bool osd_scale_by_window; + struct osd_style_opts *osd_style; + bool force_rgba_osd; +}; + +typedef struct MPOpts { + bool property_print_help; + bool use_terminal; + char *dump_stats; + int verbose; + bool msg_really_quiet; + char **msg_levels; + bool msg_color; + bool msg_module; + bool msg_time; + char *log_file; + + int operation_mode; + + char **reset_options; + char **script_files; + char **script_opts; + bool js_memory_report; + bool lua_load_osc; + bool lua_load_ytdl; + char *lua_ytdl_format; + char **lua_ytdl_raw_options; + bool lua_load_stats; + bool lua_load_console; + int lua_load_auto_profiles; + + bool auto_load_scripts; + + bool audio_exclusive; + bool ao_null_fallback; + bool audio_stream_silence; + float audio_wait_open; + int force_vo; + float softvol_volume; + int rgain_mode; + float rgain_preamp; // Set replaygain pre-amplification + bool rgain_clip; // Enable/disable clipping prevention + float rgain_fallback; + int softvol_mute; + float softvol_max; + int gapless_audio; + + mp_vo_opts *vo; + struct ao_opts *ao_opts; + + char *wintitle; + char *media_title; + + struct mp_csp_equalizer_opts *video_equalizer; + + int stop_screensaver; + int cursor_autohide_delay; + bool cursor_autohide_fs; + + struct mp_subtitle_opts *subs_rend; + struct mp_sub_filter_opts *subs_filt; + struct mp_osd_render_opts *osd_rend; + + int osd_level; + int osd_duration; + bool osd_fractions; + int osd_on_seek; + bool video_osd; + + bool untimed; + char *stream_dump; + bool stop_playback_on_init_failure; + int loop_times; + int loop_file; + bool shuffle; + bool ordered_chapters; + char *ordered_chapters_files; + int chapter_merge_threshold; + double chapter_seek_threshold; + char *chapter_file; + bool merge_files; + bool quiet; + bool load_config; + char *force_configdir; + bool use_filedir_conf; + int hls_bitrate; + int edition_id; + bool initial_audio_sync; + double sync_max_video_change; + double sync_max_audio_change; + int sync_max_factor; + int hr_seek; + float hr_seek_demuxer_offset; + bool hr_seek_framedrop; + float audio_delay; + float default_max_pts_correction; + int autosync; + int frame_dropping; + bool video_latency_hacks; + int term_osd; + bool term_osd_bar; + char *term_osd_bar_chars; + bool term_remaining_playtime; + char *term_title; + char *playing_msg; + char *osd_playing_msg; + int osd_playing_msg_duration; + char *status_msg; + char *osd_status_msg; + char *osd_msg[3]; + int player_idle_mode; + bool consolecontrols; + int playlist_pos; + struct m_rel_time play_start; + struct m_rel_time play_end; + struct m_rel_time play_length; + int play_dir; + bool rebase_start_time; + int play_frames; + double ab_loop[2]; + int ab_loop_count; + double step_sec; + bool position_resume; + bool position_check_mtime; + bool position_save_on_quit; + bool write_filename_in_watch_later_config; + bool ignore_path_in_watch_later_config; + char *watch_later_dir; + char **watch_later_options; + bool pause; + int keep_open; + bool keep_open_pause; + double image_display_duration; + char *lavfi_complex; + int stream_id[2][STREAM_TYPE_COUNT]; + char **stream_lang[STREAM_TYPE_COUNT]; + bool stream_auto_sel; + bool subs_with_matching_audio; + bool subs_match_os_language; + int subs_fallback; + int subs_fallback_forced; + int audio_display; + char **display_tags; + + char **audio_files; + char *demuxer_name; + bool demuxer_thread; + double demux_termination_timeout; + bool demuxer_cache_wait; + bool prefetch_open; + char *audio_demuxer_name; + char *sub_demuxer_name; + + bool cache_pause; + bool cache_pause_initial; + float cache_pause_wait; + + struct image_writer_opts *screenshot_image_opts; + char *screenshot_template; + char *screenshot_dir; + bool screenshot_sw; + + struct m_channels audio_output_channels; + int audio_output_format; + int force_srate; + double playback_speed; + bool pitch_correction; + struct m_obj_settings *vf_settings; + struct m_obj_settings *af_settings; + struct filter_opts *filter_opts; + struct dec_wrapper_opts *dec_wrapper; + char **sub_name; + char **sub_paths; + char **audiofile_paths; + char **coverart_files; + char **external_files; + bool autoload_files; + int sub_auto; + char **sub_auto_exts; + int audiofile_auto; + char **audiofile_auto_exts; + int coverart_auto; + char **coverart_auto_exts; + bool coverart_whitelist; + bool osd_bar_visible; + + int w32_priority; + + struct bluray_opts *stream_bluray_opts; + struct cdda_opts *stream_cdda_opts; + struct dvb_opts *stream_dvb_opts; + struct lavf_opts *stream_lavf_opts; + + char *bluray_device; + + struct demux_rawaudio_opts *demux_rawaudio; + struct demux_rawvideo_opts *demux_rawvideo; + struct demux_playlist_opts *demux_playlist; + struct demux_lavf_opts *demux_lavf; + struct demux_mkv_opts *demux_mkv; + struct demux_cue_opts *demux_cue; + + struct demux_opts *demux_opts; + struct demux_cache_opts *demux_cache_opts; + struct stream_opts *stream_opts; + + struct vd_lavc_params *vd_lavc_params; + struct ad_lavc_params *ad_lavc_params; + + struct input_opts *input_opts; + + // may be NULL if encoding is not compiled-in + struct encode_opts *encode_opts; + + char *ipc_path; + char *ipc_client; + + struct mp_resample_opts *resample_opts; + + struct ra_ctx_opts *ra_ctx_opts; + struct gl_video_opts *gl_video_opts; + struct angle_opts *angle_opts; + struct opengl_opts *opengl_opts; + struct vulkan_opts *vulkan_opts; + struct vulkan_display_opts *vulkan_display_opts; + struct spirv_opts *spirv_opts; + struct d3d11_opts *d3d11_opts; + struct d3d11va_opts *d3d11va_opts; + struct macos_opts *macos_opts; + struct drm_opts *drm_opts; + struct wayland_opts *wayland_opts; + struct wingl_opts *wingl_opts; + struct cuda_opts *cuda_opts; + struct dvd_opts *dvd_opts; + struct vaapi_opts *vaapi_opts; + struct sws_opts *sws_opts; + struct zimg_opts *zimg_opts; + + int cuda_device; +} MPOpts; + +struct cuda_opts { + int cuda_device; +}; + +struct dvd_opts { + int angle; + int speed; + char *device; +}; + +struct filter_opts { + bool deinterlace; +}; + +extern const struct m_sub_options vo_sub_opts; +extern const struct m_sub_options cuda_conf; +extern const struct m_sub_options dvd_conf; +extern const struct m_sub_options mp_subtitle_sub_opts; +extern const struct m_sub_options mp_sub_filter_opts; +extern const struct m_sub_options mp_osd_render_sub_opts; +extern const struct m_sub_options filter_conf; +extern const struct m_sub_options resample_conf; +extern const struct m_sub_options stream_conf; +extern const struct m_sub_options dec_wrapper_conf; +extern const struct m_sub_options mp_opt_root; + +#endif diff --git a/options/parse_commandline.c b/options/parse_commandline.c new file mode 100644 index 0000000..93120d3 --- /dev/null +++ b/options/parse_commandline.c @@ -0,0 +1,261 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <assert.h> +#include <stdbool.h> + +#include "osdep/io.h" +#include "common/global.h" +#include "common/msg.h" +#include "common/msg_control.h" +#include "m_option.h" +#include "m_config_frontend.h" +#include "options.h" +#include "common/playlist.h" +#include "parse_commandline.h" + +#define GLOBAL 0 +#define LOCAL 1 + +struct parse_state { + struct m_config *config; + char **argv; + struct mp_log *log; // silent if NULL + + bool no_more_opts; + bool error; + + bool is_opt; + struct bstr arg; + struct bstr param; +}; + +// Returns true if more args, false if all parsed or an error occurred. +static bool split_opt(struct parse_state *p) +{ + assert(!p->error); + + if (!p->argv || !p->argv[0]) + return false; + + p->is_opt = false; + p->arg = bstr0(p->argv[0]); + p->param = bstr0(NULL); + + p->argv++; + + if (p->no_more_opts || !bstr_startswith0(p->arg, "-") || p->arg.len == 1) + return true; + + if (bstrcmp0(p->arg, "--") == 0) { + p->no_more_opts = true; + return split_opt(p); + } + + p->is_opt = true; + + bool new_opt = bstr_eatstart0(&p->arg, "--"); + if (!new_opt) + bstr_eatstart0(&p->arg, "-"); + + bool ambiguous = !bstr_split_tok(p->arg, "=", &p->arg, &p->param); + + bool need_param = m_config_option_requires_param(p->config, p->arg) > 0; + + if (ambiguous && need_param) { + if (!p->argv[0] || new_opt) { + p->error = true; + MP_FATAL(p, "Error parsing commandline option %.*s: %s\n", + BSTR_P(p->arg), m_option_strerror(M_OPT_MISSING_PARAM)); + MP_WARN(p, "Make sure you're using e.g. '--%.*s=value' instead " + "of '--%.*s value'.\n", BSTR_P(p->arg), BSTR_P(p->arg)); + return false; + } + p->param = bstr0(p->argv[0]); + p->argv++; + } + + return true; +} + +#ifdef __MINGW32__ +static void process_non_option(struct playlist *files, const char *arg) +{ + glob_t gg; + + // Glob filenames on Windows (cmd.exe doesn't do this automatically) + if (glob(arg, 0, NULL, &gg)) { + playlist_add_file(files, arg); + } else { + for (int i = 0; i < gg.gl_pathc; i++) + playlist_add_file(files, gg.gl_pathv[i]); + + globfree(&gg); + } +} +#else +static void process_non_option(struct playlist *files, const char *arg) +{ + playlist_add_file(files, arg); +} +#endif + +// returns M_OPT_... error code +int m_config_parse_mp_command_line(m_config_t *config, struct playlist *files, + struct mpv_global *global, char **argv) +{ + int ret = M_OPT_UNKNOWN; + int mode = 0; + struct playlist_entry *local_start = NULL; + + int local_params_count = 0; + struct playlist_param *local_params = 0; + + assert(config != NULL); + + mode = GLOBAL; + + struct parse_state p = {config, argv, config->log}; + while (split_opt(&p)) { + if (p.is_opt) { + int flags = M_SETOPT_FROM_CMDLINE; + if (mode == LOCAL) + flags |= M_SETOPT_BACKUP | M_SETOPT_CHECK_ONLY; + int r = m_config_set_option_cli(config, p.arg, p.param, flags); + if (r == M_OPT_EXIT) { + ret = r; + goto err_out; + } else if (r < 0) { + MP_FATAL(config, "Setting commandline option --%.*s=%.*s failed.\n", + BSTR_P(p.arg), BSTR_P(p.param)); + goto err_out; + } + + // Handle some special arguments outside option parser. + + if (!bstrcmp0(p.arg, "{")) { + if (mode != GLOBAL) { + MP_ERR(config, "'--{' can not be nested.\n"); + goto err_out; + } + mode = LOCAL; + assert(!local_start); + local_start = playlist_get_last(files); + continue; + } + + if (!bstrcmp0(p.arg, "}")) { + if (mode != LOCAL) { + MP_ERR(config, "Too many closing '--}'.\n"); + goto err_out; + } + if (local_params_count) { + // The files added between '{' and '}' are the entries from + // the entry _after_ local_start, until the end of the list. + // If local_start is NULL, the list was empty on '{', and we + // want all files in the list. + struct playlist_entry *cur = local_start + ? playlist_entry_get_rel(local_start, 1) + : playlist_get_first(files); + if (!cur) + MP_WARN(config, "Ignored options!\n"); + while (cur) { + playlist_entry_add_params(cur, local_params, + local_params_count); + cur = playlist_entry_get_rel(cur, 1); + } + } + local_params_count = 0; + mode = GLOBAL; + m_config_restore_backups(config); + local_start = NULL; + continue; + } + + if (bstrcmp0(p.arg, "playlist") == 0) { + // append the playlist to the local args + char *param0 = bstrdup0(NULL, p.param); + struct playlist *pl = playlist_parse_file(param0, NULL, global); + if (!pl) { + MP_FATAL(config, "Error reading playlist '%.*s'\n", + BSTR_P(p.param)); + talloc_free(param0); + goto err_out; + } + playlist_transfer_entries(files, pl); + playlist_populate_playlist_path(files, param0); + talloc_free(param0); + talloc_free(pl); + continue; + } + + if (mode == LOCAL) { + MP_TARRAY_APPEND(NULL, local_params, local_params_count, + (struct playlist_param) {p.arg, p.param}); + } + } else { + // filename + void *tmp = talloc_new(NULL); + char *file0 = bstrdup0(tmp, p.arg); + process_non_option(files, file0); + talloc_free(tmp); + } + } + + if (p.error) + goto err_out; + + if (mode != GLOBAL) { + MP_ERR(config, "Missing closing --} on command line.\n"); + goto err_out; + } + + ret = 0; // success + +err_out: + talloc_free(local_params); + m_config_restore_backups(config); + return ret; +} + +/* Parse some command line options early before main parsing. + * --no-config prevents reading configuration files (otherwise done before + * command line parsing), and --really-quiet suppresses messages printed + * during normal options parsing. + */ +void m_config_preparse_command_line(m_config_t *config, struct mpv_global *global, + int *verbose, char **argv) +{ + struct parse_state p = {config, argv, mp_null_log}; + while (split_opt(&p)) { + if (p.is_opt) { + // Ignore non-pre-parse options. They will be set later. + // Option parsing errors will be handled later as well. + int flags = M_SETOPT_FROM_CMDLINE | M_SETOPT_PRE_PARSE_ONLY; + m_config_set_option_cli(config, p.arg, p.param, flags); + if (bstrcmp0(p.arg, "v") == 0) + (*verbose)++; + } + } + + for (int n = 0; n < config->num_opts; n++) + config->opts[n].warning_was_printed = false; +} diff --git a/options/parse_commandline.h b/options/parse_commandline.h new file mode 100644 index 0000000..1509fc9 --- /dev/null +++ b/options/parse_commandline.h @@ -0,0 +1,32 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_PARSER_MPCMD_H +#define MPLAYER_PARSER_MPCMD_H + +#include <stdbool.h> + +struct playlist; +struct m_config; +struct mpv_global; + +int m_config_parse_mp_command_line(m_config_t *config, struct playlist *files, + struct mpv_global *global, char **argv); +void m_config_preparse_command_line(m_config_t *config, struct mpv_global *global, + int *verbose, char **argv); + +#endif /* MPLAYER_PARSER_MPCMD_H */ diff --git a/options/parse_configfile.c b/options/parse_configfile.c new file mode 100644 index 0000000..edd6be9 --- /dev/null +++ b/options/parse_configfile.c @@ -0,0 +1,178 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <assert.h> + +#include "osdep/io.h" + +#include "parse_configfile.h" +#include "common/common.h" +#include "common/msg.h" +#include "misc/ctype.h" +#include "m_option.h" +#include "m_config.h" +#include "stream/stream.h" + +// Skip whitespace and comments (assuming there are no line breaks) +static bool skip_ws(bstr *s) +{ + *s = bstr_lstrip(*s); + if (bstr_startswith0(*s, "#")) + s->len = 0; + return s->len; +} + +int m_config_parse(m_config_t *config, const char *location, bstr data, + char *initial_section, int flags) +{ + m_profile_t *profile = m_config_add_profile(config, initial_section); + void *tmp = talloc_new(NULL); + int line_no = 0; + int errors = 0; + + bstr_eatstart0(&data, "\xEF\xBB\xBF"); // skip BOM + + while (data.len) { + talloc_free_children(tmp); + bool ok = false; + + line_no++; + char loc[512]; + snprintf(loc, sizeof(loc), "%s:%d:", location, line_no); + + bstr line = bstr_strip_linebreaks(bstr_getline(data, &data)); + if (!skip_ws(&line)) + continue; + + // Profile declaration + if (bstr_eatstart0(&line, "[")) { + bstr profilename; + if (!bstr_split_tok(line, "]", &profilename, &line)) { + MP_ERR(config, "%s missing closing ]\n", loc); + goto error; + } + if (skip_ws(&line)) { + MP_ERR(config, "%s unparsable extra characters: '%.*s'\n", + loc, BSTR_P(line)); + goto error; + } + profile = m_config_add_profile(config, bstrto0(tmp, profilename)); + continue; + } + + bstr_eatstart0(&line, "--"); + + bstr option = line; + while (line.len && (mp_isalnum(line.start[0]) || line.start[0] == '_' || + line.start[0] == '-')) + line = bstr_cut(line, 1); + option.len = option.len - line.len; + skip_ws(&line); + + bstr value = {0}; + if (bstr_eatstart0(&line, "=")) { + skip_ws(&line); + if (line.len && (line.start[0] == '"' || line.start[0] == '\'')) { + // Simple quoting, like "value" + char term[2] = {line.start[0], 0}; + line = bstr_cut(line, 1); + if (!bstr_split_tok(line, term, &value, &line)) { + MP_ERR(config, "%s unterminated quote\n", loc); + goto error; + } + } else if (bstr_eatstart0(&line, "%")) { + // Quoting with length, like %5%value + bstr rest; + long long len = bstrtoll(line, &rest, 10); + if (rest.len == line.len || !bstr_eatstart0(&rest, "%") || + len > rest.len) + { + MP_ERR(config, "%s fixed-length quoting expected - put " + "\"quotes\" around the option value if you did not " + "intend to use this, but your option value starts " + "with '%%'\n", loc); + goto error; + } + value = bstr_splice(rest, 0, len); + line = bstr_cut(rest, len); + } else { + // No quoting; take everything until the comment or end of line + int end = bstrchr(line, '#'); + value = bstr_strip(end < 0 ? line : bstr_splice(line, 0, end)); + line.len = 0; + } + } + if (skip_ws(&line)) { + MP_ERR(config, "%s unparsable extra characters: '%.*s'\n", + loc, BSTR_P(line)); + goto error; + } + + int res = m_config_set_profile_option(config, profile, option, value); + if (res < 0) { + MP_ERR(config, "%s setting option %.*s='%.*s' failed.\n", + loc, BSTR_P(option), BSTR_P(value)); + goto error; + } + + ok = true; + error: + if (!ok) + errors++; + if (errors > 16) { + MP_ERR(config, "%s: too many errors, stopping.\n", location); + break; + } + } + + if (config->recursion_depth == 0) + m_config_finish_default_profile(config, flags); + + talloc_free(tmp); + return 1; +} + +// Load options and profiles from a config file. +// conffile: path to the config file +// initial_section: default section where to add normal options +// flags: M_SETOPT_* bits +// returns: 1 on success, -1 on error, 0 if file not accessible. +int m_config_parse_config_file(m_config_t *config, struct mpv_global *global, + const char *conffile, char *initial_section, + int flags) +{ + flags = flags | M_SETOPT_FROM_CONFIG_FILE; + + MP_VERBOSE(config, "Reading config file %s\n", conffile); + + struct stream *s = stream_create(conffile, STREAM_READ | STREAM_ORIGIN_DIRECT, + NULL, global); + if (!s) + return 0; + bstr data = stream_read_complete(s, s, 1000000000); + if (!data.start) + return 0; + + int r = m_config_parse(config, conffile, data, initial_section, flags); + talloc_free(data.start); + free_stream(s); + return r; +} diff --git a/options/parse_configfile.h b/options/parse_configfile.h new file mode 100644 index 0000000..3622fc8 --- /dev/null +++ b/options/parse_configfile.h @@ -0,0 +1,30 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_PARSER_CFG_H +#define MPLAYER_PARSER_CFG_H + +#include "m_config_frontend.h" + +int m_config_parse_config_file(m_config_t* config, struct mpv_global *global, + const char *conffile, char *initial_section, + int flags); + +int m_config_parse(m_config_t *config, const char *location, bstr data, + char *initial_section, int flags); + +#endif /* MPLAYER_PARSER_CFG_H */ diff --git a/options/path.c b/options/path.c new file mode 100644 index 0000000..52dc113 --- /dev/null +++ b/options/path.c @@ -0,0 +1,410 @@ +/* + * This file is part of mpv. + * + * Get path to config dir/file. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <errno.h> + +#include "config.h" + +#include "common/common.h" +#include "common/global.h" +#include "common/msg.h" +#include "options/options.h" +#include "options/path.h" +#include "mpv_talloc.h" +#include "osdep/io.h" +#include "osdep/path.h" +#include "misc/ctype.h" + +// In order of decreasing priority: the first has highest priority. +static const mp_get_platform_path_cb path_resolvers[] = { +#if HAVE_COCOA + mp_get_platform_path_osx, +#endif +#if HAVE_DARWIN + mp_get_platform_path_darwin, +#elif !defined(_WIN32) || defined(__CYGWIN__) + mp_get_platform_path_unix, +#endif +#if HAVE_UWP + mp_get_platform_path_uwp, +#elif defined(_WIN32) + mp_get_platform_path_win, +#endif +}; + +// from highest (most preferred) to lowest priority +static const char *const config_dirs[] = { + "home", + "old_home", + "osxbundle", + "exe_dir", + "global", +}; + +// Return a platform specific path using a path type as defined in osdep/path.h. +// Keep in mind that the only way to free the return value is freeing talloc_ctx +// (or its children), as this function can return a statically allocated string. +static const char *mp_get_platform_path(void *talloc_ctx, + struct mpv_global *global, + const char *type) +{ + assert(talloc_ctx); + + if (global->configdir) { + // force all others to NULL, only first returns the path + for (int n = 0; n < MP_ARRAY_SIZE(config_dirs); n++) { + if (strcmp(config_dirs[n], type) == 0) + return (n == 0 && global->configdir[0]) ? global->configdir : NULL; + } + } + + // Return the native config path if the platform doesn't support the + // type we are trying to fetch. + const char *fallback_type = NULL; + if (!strcmp(type, "cache") || !strcmp(type, "state")) + fallback_type = "home"; + + for (int n = 0; n < MP_ARRAY_SIZE(path_resolvers); n++) { + const char *path = path_resolvers[n](talloc_ctx, type); + if (path && path[0]) + return path; + } + + if (fallback_type) { + assert(strcmp(fallback_type, type) != 0); + return mp_get_platform_path(talloc_ctx, global, fallback_type); + } + return NULL; +} + +void mp_init_paths(struct mpv_global *global, struct MPOpts *opts) +{ + TA_FREEP(&global->configdir); + + const char *force_configdir = getenv("MPV_HOME"); + if (opts->force_configdir && opts->force_configdir[0]) + force_configdir = opts->force_configdir; + if (!opts->load_config) + force_configdir = ""; + + global->configdir = talloc_strdup(global, force_configdir); +} + +char *mp_find_user_file(void *talloc_ctx, struct mpv_global *global, + const char *type, const char *filename) +{ + void *tmp = talloc_new(NULL); + char *res = (char *)mp_get_platform_path(tmp, global, type); + if (res) + res = mp_path_join(talloc_ctx, res, filename); + talloc_free(tmp); + MP_DBG(global, "%s path: '%s' -> '%s'\n", type, filename, res ? res : "-"); + return res; +} + +static char **mp_find_all_config_files_limited(void *talloc_ctx, + struct mpv_global *global, + int max_files, + const char *filename) +{ + char **ret = talloc_array(talloc_ctx, char*, 2); // 2 preallocated + int num_ret = 0; + + for (int i = 0; i < MP_ARRAY_SIZE(config_dirs); i++) { + const char *dir = mp_get_platform_path(ret, global, config_dirs[i]); + bstr s = bstr0(filename); + while (dir && num_ret < max_files && s.len) { + bstr fn; + bstr_split_tok(s, "|", &fn, &s); + + char *file = mp_path_join_bstr(ret, bstr0(dir), fn); + if (mp_path_exists(file)) { + MP_DBG(global, "config path: '%.*s' -> '%s'\n", + BSTR_P(fn), file); + MP_TARRAY_APPEND(NULL, ret, num_ret, file); + } else { + MP_DBG(global, "config path: '%.*s' -/-> '%s'\n", + BSTR_P(fn), file); + } + } + } + + MP_TARRAY_GROW(NULL, ret, num_ret); + ret[num_ret] = NULL; + + for (int n = 0; n < num_ret / 2; n++) + MPSWAP(char*, ret[n], ret[num_ret - n - 1]); + return ret; +} + +char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global, + const char *filename) +{ + return mp_find_all_config_files_limited(talloc_ctx, global, 64, filename); +} + +char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global, + const char *filename) +{ + char **l = mp_find_all_config_files_limited(talloc_ctx, global, 1, filename); + char *r = l && l[0] ? talloc_steal(talloc_ctx, l[0]) : NULL; + talloc_free(l); + return r; +} + +char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global, + const char *path) +{ + if (!path) + return NULL; + char *res = NULL; + bstr bpath = bstr0(path); + if (bstr_eatstart0(&bpath, "~")) { + // parse to "~" <prefix> "/" <rest> + bstr prefix, rest; + if (bstr_split_tok(bpath, "/", &prefix, &rest)) { + const char *rest0 = rest.start; // ok in this case + if (bstr_equals0(prefix, "~")) { + res = mp_find_config_file(talloc_ctx, global, rest0); + if (!res) { + void *tmp = talloc_new(NULL); + const char *p = mp_get_platform_path(tmp, global, "home"); + res = mp_path_join_bstr(talloc_ctx, bstr0(p), rest); + talloc_free(tmp); + } + } else if (bstr_equals0(prefix, "")) { + char *home = getenv("HOME"); + if (!home) + home = getenv("USERPROFILE"); + res = mp_path_join_bstr(talloc_ctx, bstr0(home), rest); + } else if (bstr_eatstart0(&prefix, "~")) { + void *tmp = talloc_new(NULL); + char type[80]; + snprintf(type, sizeof(type), "%.*s", BSTR_P(prefix)); + const char *p = mp_get_platform_path(tmp, global, type); + res = mp_path_join_bstr(talloc_ctx, bstr0(p), rest); + talloc_free(tmp); + } + } + } + if (!res) + res = talloc_strdup(talloc_ctx, path); + MP_DBG(global, "user path: '%s' -> '%s'\n", path, res); + return res; +} + +char *mp_basename(const char *path) +{ + char *s; + +#if HAVE_DOS_PATHS + if (!mp_is_url(bstr0(path))) { + s = strrchr(path, '\\'); + if (s) + path = s + 1; + s = strrchr(path, ':'); + if (s) + path = s + 1; + } +#endif + s = strrchr(path, '/'); + return s ? s + 1 : (char *)path; +} + +struct bstr mp_dirname(const char *path) +{ + struct bstr ret = { + (uint8_t *)path, mp_basename(path) - path + }; + if (ret.len == 0) + return bstr0("."); + return ret; +} + + +#if HAVE_DOS_PATHS +static const char mp_path_separators[] = "\\/"; +#else +static const char mp_path_separators[] = "/"; +#endif + +// Mutates path and removes a trailing '/' (or '\' on Windows) +void mp_path_strip_trailing_separator(char *path) +{ + size_t len = strlen(path); + if (len > 0 && strchr(mp_path_separators, path[len - 1])) + path[len - 1] = '\0'; +} + +char *mp_splitext(const char *path, bstr *root) +{ + assert(path); + int skip = (*path == '.'); // skip leading dot for "hidden" unix files + const char *split = strrchr(path + skip, '.'); + if (!split || !split[1] || strchr(split, '/')) + return NULL; + if (root) + *root = (bstr){(char *)path, split - path}; + return (char *)split + 1; +} + +bool mp_path_is_absolute(struct bstr path) +{ + if (path.len && strchr(mp_path_separators, path.start[0])) + return true; + +#if HAVE_DOS_PATHS + // Note: "X:filename" is a path relative to the current working directory + // of drive X, and thus is not an absolute path. It needs to be + // followed by \ or /. + if (path.len >= 3 && path.start[1] == ':' && + strchr(mp_path_separators, path.start[2])) + return true; +#endif + + return false; +} + +char *mp_path_join_bstr(void *talloc_ctx, struct bstr p1, struct bstr p2) +{ + if (p1.len == 0) + return bstrdup0(talloc_ctx, p2); + if (p2.len == 0) + return bstrdup0(talloc_ctx, p1); + + if (mp_path_is_absolute(p2)) + return bstrdup0(talloc_ctx, p2); + + bool have_separator = strchr(mp_path_separators, p1.start[p1.len - 1]); +#if HAVE_DOS_PATHS + // "X:" only => path relative to "X:" current working directory. + if (p1.len == 2 && p1.start[1] == ':') + have_separator = true; +#endif + + return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1), + have_separator ? "" : "/", BSTR_P(p2)); +} + +char *mp_path_join(void *talloc_ctx, const char *p1, const char *p2) +{ + return mp_path_join_bstr(talloc_ctx, bstr0(p1), bstr0(p2)); +} + +char *mp_getcwd(void *talloc_ctx) +{ + char *e_wd = getenv("PWD"); + if (e_wd) + return talloc_strdup(talloc_ctx, e_wd); + + char *wd = talloc_array(talloc_ctx, char, 20); + while (getcwd(wd, talloc_get_size(wd)) == NULL) { + if (errno != ERANGE) { + talloc_free(wd); + return NULL; + } + wd = talloc_realloc(talloc_ctx, wd, char, talloc_get_size(wd) * 2); + } + return wd; +} + +char *mp_normalize_path(void *talloc_ctx, const char *path) +{ + if (mp_is_url(bstr0(path))) + return talloc_strdup(talloc_ctx, path); + + return mp_path_join(talloc_ctx, mp_getcwd(talloc_ctx), path); +} + +bool mp_path_exists(const char *path) +{ + struct stat st; + return path && stat(path, &st) == 0; +} + +bool mp_path_isdir(const char *path) +{ + struct stat st; + return stat(path, &st) == 0 && S_ISDIR(st.st_mode); +} + +// Return false if it's considered a normal local filesystem path. +bool mp_is_url(bstr path) +{ + int proto = bstr_find0(path, "://"); + if (proto < 1) + return false; + // Per RFC3986, the first character of the protocol must be alphabetic. + // The rest must be alphanumeric plus -, + and . + for (int i = 0; i < proto; i++) { + unsigned char c = path.start[i]; + if ((i == 0 && !mp_isalpha(c)) || + (!mp_isalnum(c) && c != '.' && c != '-' && c != '+')) + { + return false; + } + } + return true; +} + +// Return the protocol part of path, e.g. "http" if path is "http://...". +// On success, out_url (if not NULL) is set to the part after the "://". +bstr mp_split_proto(bstr path, bstr *out_url) +{ + if (!mp_is_url(path)) + return (bstr){0}; + bstr r; + bstr_split_tok(path, "://", &r, out_url ? out_url : &(bstr){0}); + return r; +} + +void mp_mkdirp(const char *dir) +{ + char *path = talloc_strdup(NULL, dir); + char *cdir = path + 1; + + while (cdir) { + cdir = strchr(cdir, '/'); + if (cdir) + *cdir = 0; + + mkdir(path, 0700); + + if (cdir) + *cdir++ = '/'; + } + + talloc_free(path); +} + +void mp_mk_user_dir(struct mpv_global *global, const char *type, char *subdir) +{ + char *dir = mp_find_user_file(NULL, global, type, subdir); + if (dir) + mp_mkdirp(dir); + talloc_free(dir); +} diff --git a/options/path.h b/options/path.h new file mode 100644 index 0000000..7ec8f7b --- /dev/null +++ b/options/path.h @@ -0,0 +1,98 @@ +/* + * Get path to config dir/file. + * + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MPLAYER_PATH_H +#define MPLAYER_PATH_H + +#include <stdbool.h> +#include "misc/bstr.h" + +struct mpv_global; +struct MPOpts; + +void mp_init_paths(struct mpv_global *global, struct MPOpts *opts); + +// Search for the input filename in several paths. These include user and global +// config locations by default. Some platforms may implement additional platform +// related lookups (i.e.: OSX inside an application bundle). +char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global, + const char *filename); + +// Search for local writable user files within a specific kind of user dir +// as documented in osdep/path.h. This returns a result even if the file does +// not exist. Calling it with filename="" is equivalent to retrieving the path +// to the dir. +char *mp_find_user_file(void *talloc_ctx, struct mpv_global *global, + const char *type, const char *filename); + +// Find all instances of the given config file. Paths are returned in order +// from lowest to highest priority. filename can contain multiple names +// separated with '|', with the first having highest priority. +char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global, + const char *filename); + +// Normally returns a talloc_strdup'ed copy of the path, except for special +// paths starting with '~'. Used to allow the user explicitly reference a +// file from the user's home or mpv config directory. +char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global, + const char *path); + +// Return pointer to filename part of path + +char *mp_basename(const char *path); + +/* Return file extension, excluding the '.'. If root is not NULL, set it to the + * part of the path without extension. So: path == root + "." + extension + * Don't consider it a file extension if the only '.' is the first character. + * Return NULL if no extension and don't set *root in this case. + */ +char *mp_splitext(const char *path, bstr *root); + +/* Return struct bstr referencing directory part of path, or if that + * would be empty, ".". + */ +struct bstr mp_dirname(const char *path); + +void mp_path_strip_trailing_separator(char *path); + +/* Join two path components and return a newly allocated string + * for the result. '/' is inserted between the components if needed. + * If p2 is an absolute path then the value of p1 is ignored. + */ +char *mp_path_join(void *talloc_ctx, const char *p1, const char *p2); +char *mp_path_join_bstr(void *talloc_ctx, struct bstr p1, struct bstr p2); + +// Return whether the path is absolute. +bool mp_path_is_absolute(struct bstr path); + +char *mp_getcwd(void *talloc_ctx); + +char *mp_normalize_path(void *talloc_ctx, const char *path); + +bool mp_path_exists(const char *path); +bool mp_path_isdir(const char *path); + +bool mp_is_url(bstr path); + +bstr mp_split_proto(bstr path, bstr *out_url); + +void mp_mkdirp(const char *dir); +void mp_mk_user_dir(struct mpv_global *global, const char *type, char *subdir); + +#endif /* MPLAYER_PATH_H */ |