command: move property multiply code to m_property.c

I think this will help with reducing code duplication (see following
commit). The error messages loses the multiplication factor, but the
error message will be replaced by a generic one in the following commit
anyway.
This commit is contained in:
wm4 2018-03-23 15:06:27 +01:00
parent ef402a1c8c
commit 8ed76d2561
3 changed files with 36 additions and 26 deletions

View File

@ -36,6 +36,32 @@
#include "common/msg.h" #include "common/msg.h"
#include "common/common.h" #include "common/common.h"
static int m_property_multiply(struct mp_log *log,
const struct m_property *prop_list,
const char *property, double f, void *ctx)
{
union m_option_value val = {0};
struct m_option opt = {0};
int r;
r = m_property_do(log, prop_list, property, M_PROPERTY_GET_CONSTRICTED_TYPE,
&opt, ctx);
if (r != M_PROPERTY_OK)
return r;
assert(opt.type);
if (!opt.type->multiply)
return M_PROPERTY_NOT_IMPLEMENTED;
r = m_property_do(log, prop_list, property, M_PROPERTY_GET, &val, ctx);
if (r != M_PROPERTY_OK)
return r;
opt.type->multiply(&opt, &val, f);
r = m_property_do(log, prop_list, property, M_PROPERTY_SET, &val, ctx);
m_option_free(&opt, &val);
return r;
}
struct m_property *m_property_list_find(const struct m_property *list, struct m_property *m_property_list_find(const struct m_property *list,
const char *name) const char *name)
{ {
@ -107,6 +133,9 @@ int m_property_do(struct mp_log *log, const struct m_property *prop_list,
struct mpv_node node = { .format = MPV_FORMAT_STRING, .u.string = arg }; struct mpv_node node = { .format = MPV_FORMAT_STRING, .u.string = arg };
return m_property_do(log, prop_list, name, M_PROPERTY_SET_NODE, &node, ctx); return m_property_do(log, prop_list, name, M_PROPERTY_SET_NODE, &node, ctx);
} }
case M_PROPERTY_MULTIPLY: {
return m_property_multiply(log, prop_list, name, *(double *)arg, ctx);
}
case M_PROPERTY_SWITCH: { case M_PROPERTY_SWITCH: {
if (!log) if (!log)
return M_PROPERTY_ERROR; return M_PROPERTY_ERROR;

View File

@ -79,6 +79,10 @@ enum mp_property_action {
// arg: mpv_node* // arg: mpv_node*
M_PROPERTY_SET_NODE, M_PROPERTY_SET_NODE,
// Multiply numeric property with a factor.
// arg: double*
M_PROPERTY_MULTIPLY,
// Pass down an action to a sub-property. // Pass down an action to a sub-property.
// arg: struct m_property_action_arg* // arg: struct m_property_action_arg*
M_PROPERTY_KEY_ACTION, M_PROPERTY_KEY_ACTION,

View File

@ -4689,29 +4689,6 @@ static int *get_cmd_cycle_counter(struct MPContext *mpctx, char **args)
return &cmd->cycle_counters[cmd->num_cycle_counters - 1].counter; return &cmd->cycle_counters[cmd->num_cycle_counters - 1].counter;
} }
static int mp_property_multiply(char *property, double f, struct MPContext *mpctx)
{
union m_option_value val = {0};
struct m_option opt = {0};
int r;
r = mp_property_do(property, M_PROPERTY_GET_CONSTRICTED_TYPE, &opt, mpctx);
if (r != M_PROPERTY_OK)
return r;
assert(opt.type);
if (!opt.type->multiply)
return M_PROPERTY_NOT_IMPLEMENTED;
r = mp_property_do(property, M_PROPERTY_GET, &val, mpctx);
if (r != M_PROPERTY_OK)
return r;
opt.type->multiply(&opt, &val, f);
r = mp_property_do(property, M_PROPERTY_SET, &val, mpctx);
m_option_free(&opt, &val);
return r;
}
static struct track *find_track_with_url(struct MPContext *mpctx, int type, static struct track *find_track_with_url(struct MPContext *mpctx, int type,
const char *url) const char *url)
{ {
@ -4946,8 +4923,8 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re
case MP_CMD_MULTIPLY: { case MP_CMD_MULTIPLY: {
char *property = cmd->args[0].v.s; char *property = cmd->args[0].v.s;
double f = cmd->args[1].v.d; int r = mp_property_do(cmd->args[0].v.s, M_PROPERTY_MULTIPLY,
int r = mp_property_multiply(property, f, mpctx); &cmd->args[1].v.d, mpctx);
if (r == M_PROPERTY_OK || r == M_PROPERTY_UNAVAILABLE) { if (r == M_PROPERTY_OK || r == M_PROPERTY_UNAVAILABLE) {
show_property_osd(mpctx, property, on_osd); show_property_osd(mpctx, property, on_osd);
@ -4957,7 +4934,7 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re
return -1; return -1;
} else if (r <= 0) { } else if (r <= 0) {
set_osd_msg(mpctx, osdl, osd_duration, set_osd_msg(mpctx, osdl, osd_duration,
"Failed to multiply property '%s' by %g", property, f); "Failed to multiply property '%s'", property);
return -1; return -1;
} }
break; break;