summaryrefslogtreecommitdiffstats
path: root/options/m_property.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-02-15 16:41:48 +0100
committerwm4 <wm4@nowhere>2014-02-16 03:51:02 +0100
commit309ae76e0b159d4000e24f04aabef8e29f41e86a (patch)
treeb1c6ee2311c0366e7238abe4c7823eeea97ef735 /options/m_property.c
parentccfbe434558075c5d5dd9353a8c9dcc1b9954547 (diff)
downloadmpv-309ae76e0b159d4000e24f04aabef8e29f41e86a.tar.bz2
mpv-309ae76e0b159d4000e24f04aabef8e29f41e86a.tar.xz
m_property: add a sub-property mechanism
This adds a mechanism for easier export of sub-properties. The following commits will make use of it to export fine grained information about certain things. The sub-property mechanism reduces the amount of code needed to export a data value to 1 line.
Diffstat (limited to 'options/m_property.c')
-rw-r--r--options/m_property.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/options/m_property.c b/options/m_property.c
index 9c8649c5ab..d863a06ab3 100644
--- a/options/m_property.c
+++ b/options/m_property.c
@@ -386,3 +386,64 @@ int m_property_strdup_ro(const struct m_option* prop, int action, void* arg,
}
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)
+{
+ switch (action) {
+ case M_PROPERTY_GET_TYPE:
+ *(struct m_option *)arg = (struct m_option){
+ .name = "",
+ .type = CONF_TYPE_STRING,
+ };
+ return M_PROPERTY_OK;
+ case M_PROPERTY_GET:
+ 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;
+ struct m_option type = { .name = "", .type = prop->type, };
+ char *s = m_option_print(&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;
+ struct m_option type = { .name = "", .type = prop->type, };
+ switch (ka->action) {
+ case M_PROPERTY_GET: {
+ memset(ka->arg, 0, type.type->size);
+ m_option_copy(&type, ka->arg, &prop->value);
+ return M_PROPERTY_OK;
+ }
+ case M_PROPERTY_GET_TYPE:
+ *(struct m_option *)ka->arg = type;
+ return M_PROPERTY_OK;
+ }
+ }
+ }
+ return M_PROPERTY_NOT_IMPLEMENTED;
+}