From 942fb43d0cdc58b0b2fad15bab564fd5105698ff Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 24 Feb 2014 20:39:51 +0100 Subject: client API: implement setting options using their native type too This is only half-implemented: actually the option will first be converted from mpv_node to its native type, then it's converted to a string, and then back to its native type. This is because the option API was made for strings and not anything else. Other than being grossly inelegant, the only downside is probably with string lists and key/value lists, which don't escape strings containing syntax elements correctly. --- options/m_config.c | 31 +++++++++++++++++++++++++++++++ options/m_config.h | 6 ++++++ 2 files changed, 37 insertions(+) (limited to 'options') diff --git a/options/m_config.c b/options/m_config.c index 4b520c758a..d2b75389fe 100644 --- a/options/m_config.c +++ b/options/m_config.c @@ -28,6 +28,8 @@ #include #include +#include "libmpv/client.h" + #include "talloc.h" #include "m_config.h" @@ -606,6 +608,35 @@ int m_config_set_option(struct m_config *config, struct bstr name, return m_config_set_option_ext(config, name, param, 0); } +int m_config_set_option_node(struct m_config *config, bstr name, + struct mpv_node *data, int flags) +{ + char *value = NULL; + if (data->format == MPV_FORMAT_STRING) { + value = *(char **)data; + } else { + // This is pretty lame, but the simplest for now. It will fail very + // hard for string lists with items that contain ',' characters. + union m_option_value val = {0}; + const struct m_option *opt = m_config_get_option(config, name); + if (!opt) + return M_OPT_UNKNOWN; + if (m_option_set_node(opt, &val, data) < 0) + return M_OPT_INVALID; + value = m_option_print(opt, &val); + m_option_free(opt, &val); + } + int r; + if (value) { + r = m_config_set_option_ext(config, name, bstr0(value), flags); + } else { + r = M_OPT_OUT_OF_RANGE; + } + if (data->format != MPV_FORMAT_STRING) + talloc_free(value); + return r; +} + const struct m_option *m_config_get_option(const struct m_config *config, struct bstr name) { diff --git a/options/m_config.h b/options/m_config.h index 2624f45edf..82382aaa27 100644 --- a/options/m_config.h +++ b/options/m_config.h @@ -139,6 +139,12 @@ static inline int m_config_set_option0(struct m_config *config, return m_config_set_option(config, bstr0(name), bstr0(param)); } +// Same as m_config_set_option_ext(), but set as data using mpv_node. +struct mpv_node; +int m_config_set_option_node(struct m_config *config, bstr name, + struct mpv_node *data, int flags); + + int m_config_parse_suboptions(struct m_config *config, char *name, char *subopts); -- cgit v1.2.3