summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2018-03-23 14:53:18 +0100
committerwm4 <wm4@nowhere>2018-03-26 19:47:08 +0200
commitef402a1c8c9e3bb369c6192ff25be5ca2435b5a5 (patch)
tree655367436ee5859883db9368842c5a32c26cc0ee
parent4655923d387047d51f1d1face5d40d757a76123e (diff)
downloadmpv-ef402a1c8c9e3bb369c6192ff25be5ca2435b5a5.tar.bz2
mpv-ef402a1c8c9e3bb369c6192ff25be5ca2435b5a5.tar.xz
command: use mpv_node helpers instead of duplicated code
They didn't exist yet when this code was added. Completely untested.
-rw-r--r--misc/node.c5
-rw-r--r--player/command.c34
2 files changed, 12 insertions, 27 deletions
diff --git a/misc/node.c b/misc/node.c
index 73e95e61b0..b7bf06d9c1 100644
--- a/misc/node.c
+++ b/misc/node.c
@@ -11,7 +11,8 @@ void node_init(struct mpv_node *dst, int format, struct mpv_node *parent)
// Other formats need to be initialized manually.
assert(format == MPV_FORMAT_NODE_MAP || format == MPV_FORMAT_NODE_ARRAY ||
format == MPV_FORMAT_FLAG || format == MPV_FORMAT_INT64 ||
- format == MPV_FORMAT_DOUBLE || format == MPV_FORMAT_NONE);
+ format == MPV_FORMAT_DOUBLE || format == MPV_FORMAT_BYTE_ARRAY ||
+ format == MPV_FORMAT_NONE);
void *ta_parent = NULL;
if (parent) {
@@ -23,6 +24,8 @@ void node_init(struct mpv_node *dst, int format, struct mpv_node *parent)
*dst = (struct mpv_node){ .format = format };
if (format == MPV_FORMAT_NODE_MAP || format == MPV_FORMAT_NODE_ARRAY)
dst->u.list = talloc_zero(ta_parent, struct mpv_node_list);
+ if (format == MPV_FORMAT_BYTE_ARRAY)
+ dst->u.ba = talloc_zero(ta_parent, struct mpv_byte_array);
}
// Add an entry to a MPV_FORMAT_NODE_ARRAY.
diff --git a/player/command.c b/player/command.c
index 7b532cc0c6..b855f1dd96 100644
--- a/player/command.c
+++ b/player/command.c
@@ -4752,22 +4752,6 @@ static bool check_property_scalable(char *property, struct MPContext *mpctx)
prop.type == &m_option_type_aspect;
}
-static struct mpv_node *add_map_entry(struct mpv_node *dst, const char *key)
-{
- struct mpv_node_list *list = dst->u.list;
- assert(dst->format == MPV_FORMAT_NODE_MAP && dst->u.list);
- MP_TARRAY_GROW(list, list->values, list->num);
- MP_TARRAY_GROW(list, list->keys, list->num);
- list->keys[list->num] = talloc_strdup(list, key);
- return &list->values[list->num++];
-}
-
-#define ADD_MAP_INT(dst, name, i) (*add_map_entry(dst, name) = \
- (struct mpv_node){.format = MPV_FORMAT_INT64, .u.int64 = (i)});
-
-#define ADD_MAP_CSTR(dst, name, s) (*add_map_entry(dst, name) = \
- (struct mpv_node){.format = MPV_FORMAT_STRING, .u.string = (s)});
-
int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *res)
{
struct command_ctx *cmdctx = mpctx->command_ctx;
@@ -5370,20 +5354,18 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re
struct mp_image *img = screenshot_get_rgb(mpctx, cmd->args[0].v.i);
if (!img)
return -1;
- struct mpv_node_list *info = talloc_zero(NULL, struct mpv_node_list);
- talloc_steal(info, img);
- *res = (mpv_node){.format = MPV_FORMAT_NODE_MAP, .u.list = info};
- ADD_MAP_INT(res, "w", img->w);
- ADD_MAP_INT(res, "h", img->h);
- ADD_MAP_INT(res, "stride", img->stride[0]);
- ADD_MAP_CSTR(res, "format", "bgr0");
- struct mpv_byte_array *ba = talloc_ptrtype(info, ba);
+ node_init(res, MPV_FORMAT_NODE_MAP, NULL);
+ node_map_add_int64(res, "w", img->w);
+ node_map_add_int64(res, "h", img->h);
+ node_map_add_int64(res, "stride", img->stride[0]);
+ node_map_add_string(res, "format", "bgr0");
+ struct mpv_byte_array *ba =
+ node_map_add(res, "data", MPV_FORMAT_BYTE_ARRAY)->u.ba;
*ba = (struct mpv_byte_array){
.data = img->planes[0],
.size = img->stride[0] * img->h,
};
- *add_map_entry(res, "data") =
- (struct mpv_node){.format = MPV_FORMAT_BYTE_ARRAY, .u.ba = ba,};
+ talloc_steal(ba, img);
break;
}