mirror of
https://github.com/mpv-player/mpv
synced 2025-01-18 21:31:13 +00:00
command: use mpv_node helpers instead of duplicated code
They didn't exist yet when this code was added. Completely untested.
This commit is contained in:
parent
4655923d38
commit
ef402a1c8c
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user