/*
* 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 Config
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <stdbool.h>
#include <pthread.h>
#include "libmpv/client.h"
#include "mpv_talloc.h"
#include "m_config.h"
#include "options/m_option.h"
#include "common/common.h"
#include "common/global.h"
#include "common/msg.h"
#include "common/msg_control.h"
#include "misc/dispatch.h"
#include "misc/node.h"
#include "osdep/atomic.h"
extern const char mp_help_text[];
static const union m_option_value default_value;
// 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
// For use with m_config_cache.
struct m_config_shadow {
struct m_config *root;
pthread_mutex_t lock;
// -- protected by lock
struct m_config_data *data; // protected shadow copy of the option data
struct m_config_cache **listeners;
int num_listeners;
};
// Represents a sub-struct (OPT_SUBSTRUCT()).
struct m_config_group {
const struct m_sub_options *group;
int group_count; // 1 + number of all sub groups owned by this (so
// m_config.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.groups[], or
// -1 for group 0
int parent_ptr; // ptr offset in the parent group's data, or -1 if
// none
int co_index; // index of the first group opt into m_config.opts[]
int co_end_index; // index of the last group opt + 1 (i.e. exclusive)
};
// 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 *root; // root config (with up-to-date data)
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)
atomic_llong ts; // last change timestamp we've seen
};
// Per m_config_data state for each m_config_group.
struct m_group_data {
char *udata; // pointer to group user option struct
long long ts; // incremented on every write access
};
struct m_profile {
struct m_profile *next;
char *name;
char *desc;
int num_opts;
// Option/value pair array.
char **opts;
};
// In the file local case, this contains the old global value.
struct m_opt_backup {
struct m_opt_backup *next;
struct m_config_option *co;
void *backup;
};
static void add_sub_group(struct m_config *config, 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];
}
static int show_profile(struct m_config *config, bstr param)
{
struct m_profile *p;
if (!param.len)
return M_OPT_MISSING_PARAM;
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++;
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;
}
// 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->root->num_groups);
struct m_config_group *group = &data->root->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 = group->co_index; n < group->co_end_index; n++) {
assert(n >= 0 && n < data->root->num_opts);
struct m_config_option *co = &data->root->opts[n];
if (co->opt->offset < 0 || co->opt->type->size == 0)
continue;
void *dst = gdata->udata + co->opt->offset;
const void *defptr = co->opt->defval ? co->opt->defval : dst;
if (copy_src)
defptr = copy_src + co->opt->offset;
init_opt_inplace(co->opt, dst, defptr);
}
// If there's a parent, update its pointer to the new struct.
if (group->parent_
|