command: make sub-properties more flexible

This makes it work with all kind of types, instead of just some simple
ones.
This commit is contained in:
wm4 2014-11-13 17:25:59 +01:00
parent dbf59abe05
commit 13033ce2c4
3 changed files with 13 additions and 15 deletions

View File

@ -477,10 +477,10 @@ int m_property_read_sub(const struct m_sub_property *props, int action, void *ar
continue;
MP_TARRAY_GROW(list, list->values, list->num);
MP_TARRAY_GROW(list, list->keys, list->num);
struct m_option type = {.type = prop->type};
mpv_node *val = &list->values[list->num];
if (m_option_get_node(&type, list, val, (void*)&prop->value) < 0) {
char *s = m_option_print(&type, &prop->value);
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);
}
@ -500,8 +500,7 @@ int m_property_read_sub(const struct m_sub_property *props, int action, void *ar
const struct m_sub_property *prop = &props[n];
if (prop->unavailable)
continue;
struct m_option type = {.type = prop->type};
char *s = m_option_print(&type, &prop->value);
char *s = m_option_print(&prop->type, &prop->value);
ta_xasprintf_append(&res, "%s=%s\n", prop->name, s);
talloc_free(s);
}
@ -521,15 +520,14 @@ int m_property_read_sub(const struct m_sub_property *props, int action, void *ar
return M_PROPERTY_UNKNOWN;
if (prop->unavailable)
return M_PROPERTY_UNAVAILABLE;
struct m_option type = {.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);
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 = type;
*(struct m_option *)ka->arg = prop->type;
return M_PROPERTY_OK;
}
}

View File

@ -174,7 +174,7 @@ struct m_sub_property {
// property's name.
const char *name;
// Type of the data stored in the value member. See m_option.
const struct m_option_type *type;
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;
@ -184,13 +184,13 @@ struct m_sub_property {
// Convenience macros which can be used as part of a sub_property entry.
#define SUB_PROP_INT(i) \
.type = CONF_TYPE_INT, .value = {.int_ = (i)}
.type = {.type = CONF_TYPE_INT}, .value = {.int_ = (i)}
#define SUB_PROP_STR(s) \
.type = CONF_TYPE_STRING, .value = {.string = (char *)(s)}
.type = {.type = CONF_TYPE_STRING}, .value = {.string = (char *)(s)}
#define SUB_PROP_FLOAT(f) \
.type = CONF_TYPE_FLOAT, .value = {.float_ = (f)}
.type = {.type = CONF_TYPE_FLOAT}, .value = {.float_ = (f)}
#define SUB_PROP_FLAG(f) \
.type = CONF_TYPE_FLAG, .value = {.flag = (f)}
.type = {.type = CONF_TYPE_FLAG}, .value = {.flag = (f)}
int m_property_read_sub(const struct m_sub_property *props, int action, void *arg);

View File

@ -747,7 +747,7 @@ static int get_chapter_entry(int item, int action, void *arg, void *ctx)
double time = chapter_start_time(mpctx, item);
struct m_sub_property props[] = {
{"title", SUB_PROP_STR(name)},
{"time", CONF_TYPE_TIME, {.time = time}},
{"time", {.type = CONF_TYPE_TIME}, {.time = time}},
{0}
};