From 86ed6efd8acbf86c7f85d69e51c40d08f421a1ca Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 18 Sep 2012 17:05:11 +0200 Subject: commands: handle property clamping in m_option Instead of clamping property values to the valid range in each property implementation, handle it in the property layer. The functionality to handle clamping for each type is in m_option.c. It's not really clear whether this is really needed. Normally, the raw values for M_PROPERTY_SET come only from m_option_type.parse (setting properties as string) or from m_option_parse.add (using the "switch" input command). However, since this was already done before, and since we _really_ want to be sure only to write valid values, add this code anyway. The newly added warnings/error messages should never actually be printed during normal operation and are for debugging (if they happen, we definitely want to see them). --- m_property.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'm_property.c') diff --git a/m_property.c b/m_property.c index cb19ff08cb..af5d51dd21 100644 --- a/m_property.c +++ b/m_property.c @@ -129,6 +129,24 @@ int m_property_do(const m_option_t *prop_list, const char *name, r = do_action(prop_list, name, M_PROPERTY_SET, &val, ctx); m_option_free(opt, &val); return r; + case M_PROPERTY_SET: + if ((r = + do_action(prop_list, name, M_PROPERTY_GET_TYPE, &opt, ctx)) <= 0) + return r; + if (!opt->type->clamp) { + mp_msg(MSGT_CPLAYER, MSGL_WARN, "Property '%s' without clamp().\n", + name); + } else { + m_option_copy(opt, &val, arg); + r = opt->type->clamp(opt, arg); + m_option_free(opt, &val); + if (r != 0) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, + "Property '%s': invalid value.\n", name); + return M_PROPERTY_ERROR; + } + } + return do_action(prop_list, name, M_PROPERTY_SET, arg, ctx); } return do_action(prop_list, name, action, arg, ctx); } @@ -270,7 +288,6 @@ int m_property_int_range(const m_option_t *prop, int action, { switch (action) { case M_PROPERTY_SET: - M_PROPERTY_CLAMP(prop, *(int *)arg); *var = *(int *)arg; return 1; } @@ -305,7 +322,6 @@ int m_property_float_range(const m_option_t *prop, int action, { switch (action) { case M_PROPERTY_SET: - M_PROPERTY_CLAMP(prop, *(float *)arg); *var = *(float *)arg; return 1; } -- cgit v1.2.3