Reorganise SPA tree

Reorganise the SPA includes to make it more extensible later
Simplify the naming of the buffer and meta params
This commit is contained in:
Wim Taymans 2017-11-10 13:36:14 +01:00
parent 58451d626c
commit caaeaff223
151 changed files with 1353 additions and 964 deletions

View file

@ -0,0 +1,591 @@
/* Simple Plugin API
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_POD_BUILDER_H__
#define __SPA_POD_BUILDER_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <spa/pod/pod.h>
struct spa_pod_frame {
struct spa_pod pod;
uint32_t ref;
};
struct spa_pod_builder {
void *data;
uint32_t size;
uint32_t offset;
uint32_t (*write) (struct spa_pod_builder *builder, const void *data, uint32_t size);
void * (*deref) (struct spa_pod_builder *builder, uint32_t ref);
void (*reset) (struct spa_pod_builder *builder, uint32_t offset);
bool in_array;
bool first;
int depth;
struct spa_pod_frame frame[SPA_POD_MAX_DEPTH];
};
#define SPA_POD_BUILDER_INIT(buffer,size) { buffer, size, }
static inline void
spa_pod_builder_reset(struct spa_pod_builder *builder, uint32_t offset)
{
if (builder->reset)
builder->reset(builder, offset);
builder->offset = offset;
builder->depth = 0;
builder->in_array = builder->first = false;
}
static inline void spa_pod_builder_init(struct spa_pod_builder *builder, void *data, uint32_t size)
{
*builder = (struct spa_pod_builder) SPA_POD_BUILDER_INIT(data, size);
}
static inline void *
spa_pod_builder_deref(struct spa_pod_builder *builder, uint32_t ref)
{
if (builder->deref)
return builder->deref(builder, ref);
else
return SPA_MEMBER(builder->data, ref, void);
}
static inline uint32_t
spa_pod_builder_push(struct spa_pod_builder *builder,
const struct spa_pod *pod,
uint32_t ref)
{
struct spa_pod_frame *frame = &builder->frame[builder->depth++];
frame->pod = *pod;
frame->ref = ref;
builder->in_array = builder->first = (pod->type == SPA_POD_TYPE_ARRAY ||
pod->type == SPA_POD_TYPE_PROP);
return ref;
}
static inline uint32_t
spa_pod_builder_raw(struct spa_pod_builder *builder, const void *data, uint32_t size)
{
uint32_t ref;
int i;
if (builder->write) {
ref = builder->write(builder, data, size);
} else {
ref = builder->offset;
if (ref + size > builder->size)
ref = -1;
else
memcpy(builder->data + ref, data, size);
}
builder->offset += size;
for (i = 0; i < builder->depth; i++)
builder->frame[i].pod.size += size;
return ref;
}
static inline void spa_pod_builder_pad(struct spa_pod_builder *builder, uint32_t size)
{
uint64_t zeroes = 0;
size = SPA_ROUND_UP_N(size, 8) - size;
if (size)
spa_pod_builder_raw(builder, &zeroes, size);
}
static inline uint32_t
spa_pod_builder_raw_padded(struct spa_pod_builder *builder, const void *data, uint32_t size)
{
uint32_t ref = size ? spa_pod_builder_raw(builder, data, size) : -1;
spa_pod_builder_pad(builder, size);
return ref;
}
static inline uint32_t spa_pod_builder_pop(struct spa_pod_builder *builder)
{
struct spa_pod_frame *frame = &builder->frame[--builder->depth], *top;
if (frame->ref != -1) {
struct spa_pod *pod = spa_pod_builder_deref(builder, frame->ref);
*pod = frame->pod;
}
top = builder->depth > 0 ? &builder->frame[builder->depth-1] : NULL;
builder->in_array = (top && (top->pod.type == SPA_POD_TYPE_ARRAY ||
top->pod.type == SPA_POD_TYPE_PROP));
spa_pod_builder_pad(builder, builder->offset);
return frame->ref;
}
#define spa_pod_builder_pop_deref(b) \
spa_pod_builder_deref((b), spa_pod_builder_pop(b))
static inline uint32_t
spa_pod_builder_primitive(struct spa_pod_builder *builder, const struct spa_pod *p)
{
const void *data;
uint32_t size, ref;
if (builder->in_array && !builder->first) {
data = SPA_POD_BODY_CONST(p);
size = SPA_POD_BODY_SIZE(p);
} else {
data = p;
size = SPA_POD_SIZE(p);
builder->first = false;
}
ref = spa_pod_builder_raw(builder, data, size);
if (!builder->in_array)
spa_pod_builder_pad(builder, size);
return ref;
}
#define SPA_POD_NONE_INIT() { 0, SPA_POD_TYPE_NONE }
static inline uint32_t spa_pod_builder_none(struct spa_pod_builder *builder)
{
const struct spa_pod p = SPA_POD_NONE_INIT();
return spa_pod_builder_primitive(builder, &p);
}
#define SPA_POD_BOOL_INIT(val) { { sizeof(uint32_t), SPA_POD_TYPE_BOOL }, val ? 1 : 0 }
static inline uint32_t spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
{
const struct spa_pod_bool p = SPA_POD_BOOL_INIT(val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_ID_INIT(val) { { sizeof(uint32_t), SPA_POD_TYPE_ID }, val }
static inline uint32_t spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
{
const struct spa_pod_id p = SPA_POD_ID_INIT(val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_INT_INIT(val) { { sizeof(uint32_t), SPA_POD_TYPE_INT }, val }
static inline uint32_t spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
{
const struct spa_pod_int p = SPA_POD_INT_INIT(val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_LONG_INIT(val) { { sizeof(uint64_t), SPA_POD_TYPE_LONG }, val }
static inline uint32_t spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
{
const struct spa_pod_long p = SPA_POD_LONG_INIT(val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_FLOAT_INIT(val) { { sizeof(float), SPA_POD_TYPE_FLOAT }, val }
static inline uint32_t spa_pod_builder_float(struct spa_pod_builder *builder, float val)
{
const struct spa_pod_float p = SPA_POD_FLOAT_INIT(val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_DOUBLE_INIT(val) { { sizeof(double), SPA_POD_TYPE_DOUBLE }, val }
static inline uint32_t spa_pod_builder_double(struct spa_pod_builder *builder, double val)
{
const struct spa_pod_double p = SPA_POD_DOUBLE_INIT(val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_STRING_INIT(len) { { len, SPA_POD_TYPE_STRING } }
static inline uint32_t
spa_pod_builder_write_string(struct spa_pod_builder *builder, const char *str, uint32_t len)
{
uint32_t ref = 0;
if (spa_pod_builder_raw(builder, str, len) == -1)
ref = -1;
if (spa_pod_builder_raw(builder, "", 1) == -1)
ref = -1;
spa_pod_builder_pad(builder, builder->offset);
return ref;
}
static inline uint32_t
spa_pod_builder_string_len(struct spa_pod_builder *builder, const char *str, uint32_t len)
{
const struct spa_pod_string p = SPA_POD_STRING_INIT(len+1);
uint32_t ref = spa_pod_builder_raw(builder, &p, sizeof(p));
if (spa_pod_builder_write_string(builder, str, len) == -1)
ref = -1;
return ref;
}
static inline uint32_t spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
{
uint32_t len = str ? strlen(str) : 0;
return spa_pod_builder_string_len(builder, str ? str : "", len);
}
#define SPA_POD_BYTES_INIT(len) { { len, SPA_POD_TYPE_BYTES } }
static inline uint32_t
spa_pod_builder_bytes(struct spa_pod_builder *builder, const void *bytes, uint32_t len)
{
const struct spa_pod_bytes p = SPA_POD_BYTES_INIT(len);
uint32_t ref = spa_pod_builder_raw(builder, &p, sizeof(p));
if (spa_pod_builder_raw_padded(builder, bytes, len) == -1)
ref = -1;
return ref;
}
#define SPA_POD_POINTER_INIT(type,value) { { sizeof(struct spa_pod_pointer_body), SPA_POD_TYPE_POINTER }, { type, value } }
static inline uint32_t
spa_pod_builder_pointer(struct spa_pod_builder *builder, uint32_t type, void *val)
{
const struct spa_pod_pointer p = SPA_POD_POINTER_INIT(type, val);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_FD_INIT(fd) { { sizeof(int), SPA_POD_TYPE_FD }, fd }
static inline uint32_t spa_pod_builder_fd(struct spa_pod_builder *builder, int fd)
{
const struct spa_pod_fd p = SPA_POD_FD_INIT(fd);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_RECTANGLE_INIT(width,height) { { sizeof(struct spa_rectangle), SPA_POD_TYPE_RECTANGLE }, { width, height } }
static inline uint32_t
spa_pod_builder_rectangle(struct spa_pod_builder *builder, uint32_t width, uint32_t height)
{
const struct spa_pod_rectangle p = SPA_POD_RECTANGLE_INIT(width, height);
return spa_pod_builder_primitive(builder, &p.pod);
}
#define SPA_POD_FRACTION_INIT(num,denom) { { sizeof(struct spa_fraction), SPA_POD_TYPE_FRACTION }, { num, denom } }
static inline uint32_t
spa_pod_builder_fraction(struct spa_pod_builder *builder, uint32_t num, uint32_t denom)
{
const struct spa_pod_fraction p = SPA_POD_FRACTION_INIT(num, denom);
return spa_pod_builder_primitive(builder, &p.pod);
}
static inline uint32_t
spa_pod_builder_push_array(struct spa_pod_builder *builder)
{
const struct spa_pod_array p =
{ {sizeof(struct spa_pod_array_body) - sizeof(struct spa_pod), SPA_POD_TYPE_ARRAY},
{{0, 0}} };
return spa_pod_builder_push(builder, &p.pod,
spa_pod_builder_raw(builder, &p,
sizeof(p) - sizeof(struct spa_pod)));
}
static inline uint32_t
spa_pod_builder_array(struct spa_pod_builder *builder,
uint32_t child_size, uint32_t child_type, uint32_t n_elems, const void *elems)
{
const struct spa_pod_array p = {
{sizeof(struct spa_pod_array_body) + n_elems * child_size, SPA_POD_TYPE_ARRAY},
{{child_size, child_type}}
};
uint32_t ref = spa_pod_builder_raw(builder, &p, sizeof(p));
if (spa_pod_builder_raw_padded(builder, elems, child_size * n_elems) == -1)
ref = -1;
return ref;
}
#define SPA_POD_STRUCT_INIT(size) { { size, SPA_POD_TYPE_STRUCT } }
static inline uint32_t
spa_pod_builder_push_struct(struct spa_pod_builder *builder)
{
const struct spa_pod_struct p = SPA_POD_STRUCT_INIT(0);
return spa_pod_builder_push(builder, &p.pod,
spa_pod_builder_raw(builder, &p, sizeof(p)));
}
#define SPA_POD_OBJECT_INIT(size,id,type) { { size, SPA_POD_TYPE_OBJECT }, { id, type } }
#define SPA_POD_OBJECT_INIT_COMPLEX(size,id,type,...) { { size, SPA_POD_TYPE_OBJECT }, { id, type }, __VA_ARGS__ }
static inline uint32_t
spa_pod_builder_push_object(struct spa_pod_builder *builder, uint32_t id, uint32_t type)
{
const struct spa_pod_object p =
SPA_POD_OBJECT_INIT(sizeof(struct spa_pod_object_body), id, type);
return spa_pod_builder_push(builder, &p.pod,
spa_pod_builder_raw(builder, &p, sizeof(p)));
}
#define SPA_POD_PROP_INIT(size,key,flags,val_size,val_type) \
{ { size, SPA_POD_TYPE_PROP}, {key, flags, { val_size, val_type } } }
static inline uint32_t
spa_pod_builder_push_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
{
const struct spa_pod_prop p = SPA_POD_PROP_INIT (sizeof(struct spa_pod_prop_body) -
sizeof(struct spa_pod), key, flags, 0, 0);
return spa_pod_builder_push(builder, &p.pod,
spa_pod_builder_raw(builder, &p,
sizeof(p) - sizeof(struct spa_pod)));
}
static inline uint32_t spa_pod_range_from_id(char id)
{
switch (id) {
case 'r':
return SPA_POD_PROP_RANGE_MIN_MAX;
case 's':
return SPA_POD_PROP_RANGE_STEP;
case 'e':
return SPA_POD_PROP_RANGE_ENUM;
case 'f':
return SPA_POD_PROP_RANGE_FLAGS;
default:
return SPA_POD_PROP_RANGE_NONE;
}
}
static inline uint32_t spa_pod_flag_from_id(char id)
{
switch (id) {
case 'u':
return SPA_POD_PROP_FLAG_UNSET;
case 'o':
return SPA_POD_PROP_FLAG_OPTIONAL;
case 'r':
return SPA_POD_PROP_FLAG_READONLY;
case 'd':
return SPA_POD_PROP_FLAG_DEPRECATED;
default:
return 0;
}
}
#define SPA_POD_BUILDER_COLLECT(builder,type,args) \
do { \
switch (type) { \
case 'b': \
spa_pod_builder_bool(builder, va_arg(args, int)); \
break; \
case 'I': \
spa_pod_builder_id(builder, va_arg(args, uint32_t)); \
break; \
case 'i': \
spa_pod_builder_int(builder, va_arg(args, int)); \
break; \
case 'l': \
spa_pod_builder_long(builder, va_arg(args, int64_t)); \
break; \
case 'f': \
spa_pod_builder_float(builder, va_arg(args, double)); \
break; \
case 'd': \
spa_pod_builder_double(builder, va_arg(args, double)); \
break; \
case 's': \
{ \
char *strval = va_arg(args, char *); \
if (strval != NULL) { \
size_t len = strlen(strval); \
spa_pod_builder_string_len(builder, strval, len); \
} \
else \
spa_pod_builder_none(builder); \
break; \
} \
case 'S': \
{ \
char *strval = va_arg(args, char *); \
size_t len = va_arg(args, int); \
spa_pod_builder_string_len(builder, strval, len); \
break; \
} \
case 'z': \
{ \
void *ptr = va_arg(args, void *); \
int len = va_arg(args, int); \
spa_pod_builder_bytes(builder, ptr, len); \
break; \
} \
case 'R': \
{ \
struct spa_rectangle *rectval = \
va_arg(args, struct spa_rectangle *); \
spa_pod_builder_rectangle(builder, \
rectval->width, rectval->height); \
break; \
} \
case 'F': \
{ \
struct spa_fraction *fracval = \
va_arg(args, struct spa_fraction *); \
spa_pod_builder_fraction(builder, fracval->num, fracval->denom);\
break; \
} \
case 'a': \
{ \
int child_size = va_arg(args, int); \
int child_type = va_arg(args, int); \
int n_elems = va_arg(args, int); \
void *elems = va_arg(args, void *); \
spa_pod_builder_array(builder, child_size, \
child_type, n_elems, elems); \
break; \
} \
case 'p': \
{ \
int t = va_arg(args, uint32_t); \
spa_pod_builder_pointer(builder, t, va_arg(args, void *)); \
break; \
} \
case 'h': \
spa_pod_builder_fd(builder, va_arg(args, int)); \
break; \
case 'P': \
{ \
struct spa_pod *pod = va_arg(args, struct spa_pod *); \
if (pod == NULL) \
spa_pod_builder_none(builder); \
else \
spa_pod_builder_primitive(builder, pod); \
break; \
} \
} \
} while(false)
static inline void *
spa_pod_builder_addv(struct spa_pod_builder *builder,
const char *format, va_list args)
{
while (format) {
switch (*format) {
case '<':
{
uint32_t id = va_arg(args, uint32_t);
uint32_t type = va_arg(args, uint32_t);
spa_pod_builder_push_object(builder, id, type);
break;
}
case '[':
spa_pod_builder_push_struct(builder);
break;
case '(':
spa_pod_builder_push_array(builder);
break;
case ':':
{
const char *spec;
char type;
int n_values;
uint32_t key, flags;
key = va_arg(args, uint32_t);
format = spec = va_arg(args, const char *);
type = *spec;
if (*spec != '\0')
spec++;
flags = spa_pod_range_from_id(*spec);
if (*spec != '\0')
spec++;
for (;*spec;spec++)
flags |= spa_pod_flag_from_id(*spec);
spa_pod_builder_push_prop(builder, key, flags);
if (type == '{' || type == '[')
continue;
n_values = -1;
while (n_values-- != 0) {
SPA_POD_BUILDER_COLLECT(builder, type, args);
if ((flags & SPA_POD_PROP_RANGE_MASK) == 0)
break;
if (n_values == -2)
n_values = va_arg(args, int);
}
spa_pod_builder_pop(builder);
break;
}
case ']': case ')': case '>':
spa_pod_builder_pop(builder);
if (builder->depth > 0 &&
builder->frame[builder->depth-1].pod.type == SPA_POD_TYPE_PROP)
spa_pod_builder_pop(builder);
break;
case ' ': case '\n': case '\t': case '\r':
break;
case '\0':
format = va_arg(args, const char *);
continue;
default:
SPA_POD_BUILDER_COLLECT(builder,*format,args);
break;;
}
format++;
}
return spa_pod_builder_deref(builder, builder->frame[builder->depth].ref);
}
static inline void *spa_pod_builder_add(struct spa_pod_builder *builder, const char *format, ...)
{
void *res;
va_list args;
va_start(args, format);
res = spa_pod_builder_addv(builder, format, args);
va_end(args);
return res;
}
#define SPA_POD_OBJECT(id,type,...) \
"<", id, type, ##__VA_ARGS__, ">"
#define SPA_POD_STRUCT(...) \
"[", ##__VA_ARGS__, "]"
#define SPA_POD_PROP(key,spec,type,value,...) \
":", key, spec, value, ##__VA_ARGS__
#define spa_pod_builder_object(b,id,type,...) \
spa_pod_builder_add(b, SPA_POD_OBJECT(id,type,##__VA_ARGS__), NULL)
#define spa_pod_builder_struct(b,...) \
spa_pod_builder_add(b, SPA_POD_STRUCT(__VA_ARGS__), NULL)
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_POD_BUILDER_H__ */

View file

@ -0,0 +1,56 @@
/* Simple Plugin API
* Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_COMMAND_H__
#define __SPA_COMMAND_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/utils/defs.h>
#include <spa/pod/pod.h>
#define SPA_TYPE__Command SPA_TYPE_POD_OBJECT_BASE "Command"
#define SPA_TYPE_COMMAND_BASE SPA_TYPE__Command ":"
struct spa_command_body {
struct spa_pod_object_body body;
};
struct spa_command {
struct spa_pod pod;
struct spa_command_body body;
};
#define SPA_COMMAND_TYPE(cmd) ((cmd)->body.body.type)
#define SPA_COMMAND_INIT(type) (struct spa_command) \
{ { sizeof(struct spa_command_body), SPA_POD_TYPE_OBJECT }, \
{ { 0, type } } } \
#define SPA_COMMAND_INIT_COMPLEX(t,size,type,...) (t) \
{ { size, SPA_POD_TYPE_OBJECT }, \
{ { 0, type }, __VA_ARGS__ } } \
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_COMMAND_H__ */

View file

@ -0,0 +1,55 @@
/* Simple Plugin API
* Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_EVENT_H__
#define __SPA_EVENT_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/pod/pod.h>
#define SPA_TYPE__Event SPA_TYPE_POD_OBJECT_BASE "Event"
#define SPA_TYPE_EVENT_BASE SPA_TYPE__Event ":"
struct spa_event_body {
struct spa_pod_object_body body;
};
struct spa_event {
struct spa_pod pod;
struct spa_event_body body;
};
#define SPA_EVENT_TYPE(ev) ((ev)->body.body.type)
#define SPA_EVENT_INIT(type) (struct spa_event) \
{ { sizeof(struct spa_event_body), SPA_POD_TYPE_OBJECT }, \
{ { 0, type } } } \
#define SPA_EVENT_INIT_COMPLEX(t,size,type,...) (t) \
{ { size, SPA_POD_TYPE_OBJECT }, \
{ { 0, type }, __VA_ARGS__ } } \
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_EVENT_H__ */

141
spa/include/spa/pod/iter.h Normal file
View file

@ -0,0 +1,141 @@
/* Simple Plugin API
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_POD_ITER_H__
#define __SPA_POD_ITER_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <spa/pod/pod.h>
struct spa_pod_iter {
const void *data;
uint32_t size;
uint32_t offset;
};
static inline void spa_pod_iter_init(struct spa_pod_iter *iter, const void *data, uint32_t size, uint32_t offset)
{
iter->data = data;
iter->size = size;
iter->offset = offset;
}
static inline struct spa_pod *spa_pod_iter_current(struct spa_pod_iter *iter)
{
if (iter->offset + 8 <= iter->size) {
struct spa_pod *pod = SPA_MEMBER(iter->data, iter->offset, struct spa_pod);
if (SPA_POD_SIZE(pod) <= iter->size)
return pod;
}
return NULL;
}
static inline void spa_pod_iter_advance(struct spa_pod_iter *iter, struct spa_pod *current)
{
if (current)
iter->offset += SPA_ROUND_UP_N(SPA_POD_SIZE(current), 8);
}
static inline bool spa_pod_is_inside(const void *pod, uint32_t size, const struct spa_pod *iter)
{
return iter < SPA_MEMBER(pod, size, struct spa_pod);
}
static inline struct spa_pod *spa_pod_next(const struct spa_pod *iter)
{
return SPA_MEMBER(iter, SPA_ROUND_UP_N (SPA_POD_SIZE (iter), 8), struct spa_pod);
}
#define SPA_POD_ARRAY_BODY_FOREACH(body, _size, iter) \
for ((iter) = SPA_MEMBER((body), sizeof(struct spa_pod_array_body), __typeof__(*(iter))); \
(iter) < SPA_MEMBER((body), (_size), __typeof__(*(iter))); \
(iter) = SPA_MEMBER((iter), (body)->child.size, __typeof__(*(iter))))
#define SPA_POD_FOREACH(pod, size, iter) \
for ((iter) = (pod); \
spa_pod_is_inside(pod, size, iter); \
(iter) = spa_pod_next(iter))
#define SPA_POD_FOREACH_SAFE(pod, size, iter, tmp) \
for ((iter) = (pod), (tmp) = spa_pod_next(iter); \
spa_pod_is_inside(pod, size, iter); \
(iter) = (tmp), \
(tmp) = spa_pod_next(iter))
#define SPA_POD_CONTENTS_FOREACH(pod, offset, iter) \
SPA_POD_FOREACH(SPA_MEMBER((pod), (offset), struct spa_pod),SPA_POD_SIZE (pod)-(offset),iter)
#define SPA_POD_OBJECT_BODY_FOREACH(body, size, iter) \
for ((iter) = SPA_MEMBER((body), sizeof(struct spa_pod_object_body), struct spa_pod); \
spa_pod_is_inside(body, size, iter); \
(iter) = spa_pod_next(iter))
#define SPA_POD_OBJECT_FOREACH(obj, iter) \
SPA_POD_OBJECT_BODY_FOREACH(&obj->body, SPA_POD_BODY_SIZE(obj), iter)
#define SPA_POD_PROP_ALTERNATIVE_FOREACH(body, _size, iter) \
for ((iter) = SPA_MEMBER((body), (body)->value.size + \
sizeof(struct spa_pod_prop_body), __typeof__(*iter)); \
(iter) <= SPA_MEMBER((body), (_size)-(body)->value.size, __typeof__(*iter)); \
(iter) = SPA_MEMBER((iter), (body)->value.size, __typeof__(*iter)))
static inline struct spa_pod_prop *spa_pod_contents_find_prop(const struct spa_pod *pod,
uint32_t offset, uint32_t key)
{
struct spa_pod *res;
SPA_POD_CONTENTS_FOREACH(pod, offset, res) {
if (res->type == SPA_POD_TYPE_PROP
&& ((struct spa_pod_prop *) res)->body.key == key)
return (struct spa_pod_prop *) res;
}
return NULL;
}
static inline struct spa_pod_prop *spa_pod_object_find_prop(const struct spa_pod_object *obj,
uint32_t key)
{
return spa_pod_contents_find_prop(&obj->pod, sizeof(struct spa_pod_object), key);
}
static inline struct spa_pod_prop *spa_pod_struct_find_prop(const struct spa_pod_struct *obj,
uint32_t key)
{
return spa_pod_contents_find_prop(&obj->pod, sizeof(struct spa_pod_struct), key);
}
static inline int spa_pod_object_fixate(struct spa_pod_object *obj)
{
struct spa_pod *res;
SPA_POD_OBJECT_FOREACH(obj, res) {
if (res->type == SPA_POD_TYPE_PROP)
((struct spa_pod_prop *) res)->body.flags &= ~SPA_POD_PROP_FLAG_UNSET;
}
return SPA_RESULT_OK;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_POD_H__ */

View file

@ -0,0 +1,325 @@
/* Spa
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_POD_PARSER_H__
#define __SPA_POD_PARSER_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <spa/pod/iter.h>
struct spa_pod_parser {
int depth;
struct spa_pod_iter iter[SPA_POD_MAX_DEPTH];
};
static inline void spa_pod_parser_init(struct spa_pod_parser *parser,
const void *data, uint32_t size, uint32_t offset)
{
parser->depth = 0;
spa_pod_iter_init(&parser->iter[0], data, size, offset);
}
static inline void spa_pod_parser_pod(struct spa_pod_parser *parser,
const struct spa_pod *pod)
{
spa_pod_parser_init(parser, pod, SPA_POD_SIZE(pod), 0);
}
static inline bool spa_pod_parser_can_collect(struct spa_pod *pod, char type)
{
if (type == 'P')
return true;
switch (SPA_POD_TYPE(pod)) {
case SPA_POD_TYPE_NONE:
return type == 'T' || type == 'O' || type == 'V' || type == 's';
case SPA_POD_TYPE_BOOL:
return type == 'b';
case SPA_POD_TYPE_ID:
return type == 'I';
case SPA_POD_TYPE_INT:
return type == 'i';
case SPA_POD_TYPE_LONG:
return type == 'l';
case SPA_POD_TYPE_FLOAT:
return type == 'f';
case SPA_POD_TYPE_DOUBLE:
return type == 'd';
case SPA_POD_TYPE_STRING:
return type == 's' || type == 'S';
case SPA_POD_TYPE_BYTES:
return type == 'z';
case SPA_POD_TYPE_RECTANGLE:
return type == 'R';
case SPA_POD_TYPE_FRACTION:
return type == 'F';
case SPA_POD_TYPE_BITMAP:
return type == 'B';
case SPA_POD_TYPE_ARRAY:
return type == 'a';
case SPA_POD_TYPE_STRUCT:
return type == 'T';
case SPA_POD_TYPE_OBJECT:
return type == 'O';
case SPA_POD_TYPE_POINTER:
return type == 'p';
case SPA_POD_TYPE_FD:
return type == 'h';
case SPA_POD_TYPE_PROP:
return type == 'V';
default:
return false;
}
}
#define SPA_POD_PARSER_COLLECT(pod,type,args) \
do { \
switch (type) { \
case 'b': \
*va_arg(args, int*) = SPA_POD_VALUE(struct spa_pod_bool, pod); \
break; \
case 'I': \
case 'i': \
*va_arg(args, int32_t*) = SPA_POD_VALUE(struct spa_pod_int, pod); \
break; \
case 'l': \
*va_arg(args, int64_t*) = SPA_POD_VALUE(struct spa_pod_long, pod); \
break; \
case 'f': \
*va_arg(args, float*) = SPA_POD_VALUE(struct spa_pod_float, pod); \
break; \
case 'd': \
*va_arg(args, double*) = SPA_POD_VALUE(struct spa_pod_double, pod); \
break; \
case 's': \
*va_arg(args, char**) = \
(pod == NULL || (SPA_POD_TYPE(pod) == SPA_POD_TYPE_NONE) \
? NULL : SPA_POD_CONTENTS(struct spa_pod_string, pod)); \
break; \
case 'S': \
{ \
char *dest = va_arg(args, char*); \
uint32_t maxlen = va_arg(args, uint32_t); \
strncpy(dest, SPA_POD_CONTENTS(struct spa_pod_string, pod), maxlen-1); \
break; \
} \
case 'z': \
*(va_arg(args, void **)) = SPA_POD_CONTENTS(struct spa_pod_bytes, pod); \
*(va_arg(args, uint32_t *)) = SPA_POD_BODY_SIZE(pod); \
break; \
case 'R': \
*va_arg(args, struct spa_rectangle*) = \
SPA_POD_VALUE(struct spa_pod_rectangle, pod); \
break; \
case 'F': \
*va_arg(args, struct spa_fraction*) = \
SPA_POD_VALUE(struct spa_pod_fraction, pod); \
break; \
case 'B': \
*va_arg(args, uint32_t **) = \
SPA_POD_CONTENTS(struct spa_pod_bitmap, pod); \
break; \
case 'p': \
{ \
struct spa_pod_pointer_body *b = SPA_POD_BODY(pod); \
*(va_arg(args, void **)) = b->value; \
break; \
} \
case 'h': \
*va_arg(args, int*) = SPA_POD_VALUE(struct spa_pod_fd, pod); \
break; \
case 'V': \
case 'P': \
case 'O': \
case 'T': \
*va_arg(args, struct spa_pod**) = \
(pod == NULL || (SPA_POD_TYPE(pod) == SPA_POD_TYPE_NONE) \
? NULL : pod); \
break; \
default: \
break; \
} \
} while(false)
#define SPA_POD_PARSER_SKIP(type,args) \
do { \
switch (type) { \
case 'S': \
va_arg(args, void*); \
va_arg(args, uint32_t); \
break; \
case 'z': \
va_arg(args, void**); \
case 'b': \
case 'I': \
case 'i': \
case 'l': \
case 'f': \
case 'd': \
case 's': \
case 'R': \
case 'F': \
case 'B': \
case 'p': \
case 'h': \
case 'V': \
case 'P': \
case 'T': \
case 'O': \
va_arg(args, void*); \
break; \
} \
} while(false)
static inline int spa_pod_parser_getv(struct spa_pod_parser *parser,
const char *format, va_list args)
{
struct spa_pod *pod = NULL, *current;
struct spa_pod_prop *prop = NULL;
bool required = true, suppress = false, skip = false;
struct spa_pod_iter *it = &parser->iter[parser->depth];
current = pod = spa_pod_iter_current(it);
while (format) {
switch (*format) {
case '<':
if (pod == NULL || SPA_POD_TYPE(pod) != SPA_POD_TYPE_OBJECT)
return SPA_RESULT_ERROR;
if (++parser->depth >= SPA_POD_MAX_DEPTH)
return SPA_RESULT_INVALID_ARGUMENTS;
it = &parser->iter[parser->depth];
spa_pod_iter_init(it, pod, SPA_POD_SIZE(pod), sizeof(struct spa_pod_object));
goto read_pod;
case '[':
if (pod == NULL || SPA_POD_TYPE(pod) != SPA_POD_TYPE_STRUCT)
return SPA_RESULT_ERROR;
if (++parser->depth >= SPA_POD_MAX_DEPTH)
return SPA_RESULT_INVALID_ARGUMENTS;
it = &parser->iter[parser->depth];
spa_pod_iter_init(it, pod, SPA_POD_SIZE(pod), sizeof(struct spa_pod_struct));
goto read_pod;
case ']': case '>':
if (current != NULL)
return SPA_RESULT_ERROR;
if (--parser->depth < 0)
return SPA_RESULT_INVALID_ARGUMENTS;
it = &parser->iter[parser->depth];
current = spa_pod_iter_current(it);
spa_pod_iter_advance(it, current);
goto read_pod;
case '\0':
format = va_arg(args, char *);
continue;
case ' ': case '\n': case '\t': case '\r':
break;
case '?':
required = false;
break;
case '*':
suppress = true;
break;
case ':':
{
uint32_t key = va_arg(args, uint32_t);
const struct spa_pod *obj = parser->iter[parser->depth].data;
if (SPA_POD_TYPE(obj) == SPA_POD_TYPE_OBJECT)
prop = spa_pod_object_find_prop((struct spa_pod_object*)obj, key);
else if (SPA_POD_TYPE(obj) == SPA_POD_TYPE_STRUCT)
prop = spa_pod_struct_find_prop((struct spa_pod_struct*)obj, key);
else
prop = NULL;
if (prop != NULL && (prop->body.flags & SPA_POD_PROP_FLAG_UNSET) == 0)
pod = &prop->body.value;
else
pod = NULL;
it->offset = it->size;
current = NULL;
required = true;
break;
}
case 'V':
pod = (struct spa_pod *) prop;
if (pod == NULL && required)
return SPA_RESULT_NOT_FOUND;
goto collect;
default:
if (pod == NULL || !spa_pod_parser_can_collect(pod, *format)) {
if (required)
return SPA_RESULT_NOT_FOUND;
skip = true;
}
collect:
if (suppress)
suppress = false;
else if (skip)
SPA_POD_PARSER_SKIP(*format, args);
else
SPA_POD_PARSER_COLLECT(pod, *format, args);
spa_pod_iter_advance(it, current);
required = true;
skip = false;
read_pod:
pod = current = spa_pod_iter_current(it);
break;
}
format++;
}
return 0;
}
static inline int spa_pod_parser_get(struct spa_pod_parser *parser,
const char *format, ...)
{
int res;
va_list args;
va_start(args, format);
res = spa_pod_parser_getv(parser, format, args);
va_end(args);
return res;
}
#define spa_pod_object_parse(object,...) \
({ \
struct spa_pod_parser __p; \
const struct spa_pod_object *__obj = object; \
spa_pod_parser_pod(&__p, &__obj->pod); \
spa_pod_parser_get(&__p, "<", ##__VA_ARGS__, NULL); \
})
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_POD_PARSER_H__ */

230
spa/include/spa/pod/pod.h Normal file
View file

@ -0,0 +1,230 @@
/* Simple Plugin API
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_POD_H__
#define __SPA_POD_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <spa/utils/defs.h>
#define SPA_TYPE__POD SPA_TYPE_BASE "POD"
#define SPA_TYPE_POD_BASE SPA_TYPE__POD ":"
#define SPA_TYPE_POD__Object SPA_TYPE_POD_BASE "Object"
#define SPA_TYPE_POD_OBJECT_BASE SPA_TYPE_POD__Object ":"
#define SPA_TYPE_POD__Struct SPA_TYPE_POD_BASE "Struct"
#define SPA_TYPE_POD_STRUCT_BASE SPA_TYPE_POD__Struct ":"
#ifndef SPA_POD_MAX_DEPTH
#define SPA_POD_MAX_DEPTH 16
#endif
/**
* spa_pod_type:
*/
enum spa_pod_type {
SPA_POD_TYPE_INVALID = 0,
SPA_POD_TYPE_NONE = 1,
SPA_POD_TYPE_BOOL,
SPA_POD_TYPE_ID,
SPA_POD_TYPE_INT,
SPA_POD_TYPE_LONG,
SPA_POD_TYPE_FLOAT,
SPA_POD_TYPE_DOUBLE,
SPA_POD_TYPE_STRING,
SPA_POD_TYPE_BYTES,
SPA_POD_TYPE_RECTANGLE,
SPA_POD_TYPE_FRACTION,
SPA_POD_TYPE_BITMAP,
SPA_POD_TYPE_ARRAY,
SPA_POD_TYPE_STRUCT,
SPA_POD_TYPE_OBJECT,
SPA_POD_TYPE_POINTER,
SPA_POD_TYPE_FD,
SPA_POD_TYPE_PROP,
SPA_POD_TYPE_POD,
SPA_POD_TYPE_CUSTOM_START = 64,
};
#define SPA_POD_BODY_SIZE(pod) (((struct spa_pod*)(pod))->size)
#define SPA_POD_TYPE(pod) (((struct spa_pod*)(pod))->type)
#define SPA_POD_SIZE(pod) (sizeof(struct spa_pod) + SPA_POD_BODY_SIZE(pod))
#define SPA_POD_CONTENTS_SIZE(type,pod) (SPA_POD_SIZE(pod)-sizeof(type))
#define SPA_POD_CONTENTS(type,pod) SPA_MEMBER((pod),sizeof(type),void)
#define SPA_POD_CONTENTS_CONST(type,pod) SPA_MEMBER((pod),sizeof(type),const void)
#define SPA_POD_BODY(pod) SPA_MEMBER((pod),sizeof(struct spa_pod),void)
#define SPA_POD_BODY_CONST(pod) SPA_MEMBER((pod),sizeof(struct spa_pod),const void)
struct spa_pod {
uint32_t size;
uint32_t type; /* one of spa_pod_type */
};
#define SPA_POD_VALUE(type,pod) (((type*)pod)->value)
struct spa_pod_bool {
struct spa_pod pod;
int32_t value;
int32_t __padding;
};
struct spa_pod_id {
struct spa_pod pod;
int32_t value;
int32_t __padding;
};
struct spa_pod_int {
struct spa_pod pod;
int32_t value;
int32_t __padding;
};
struct spa_pod_long {
struct spa_pod pod;
int64_t value;
};
struct spa_pod_float {
struct spa_pod pod;
float value;
};
struct spa_pod_double {
struct spa_pod pod;
double value;
};
struct spa_pod_string {
struct spa_pod pod;
/* value here */
};
struct spa_pod_bytes {
struct spa_pod pod;
/* value here */
};
struct spa_pod_rectangle {
struct spa_pod pod;
struct spa_rectangle value;
};
struct spa_pod_fraction {
struct spa_pod pod;
struct spa_fraction value;
};
struct spa_pod_bitmap {
struct spa_pod pod;
/* array of uint8_t follows with the bitmap */
};
struct spa_pod_array_body {
struct spa_pod child;
/* array with elements of child.size follows */
};
struct spa_pod_array {
struct spa_pod pod;
struct spa_pod_array_body body;
};
struct spa_pod_struct {
struct spa_pod pod;
/* one or more spa_pod follow */
};
struct spa_pod_object_body {
uint32_t id;
uint32_t type;
/* contents follow, series of spa_pod */
};
struct spa_pod_object {
struct spa_pod pod;
struct spa_pod_object_body body;
};
static inline bool spa_pod_is_object_type(struct spa_pod *pod, uint32_t type)
{
return (pod->type == SPA_POD_TYPE_OBJECT
&& ((struct spa_pod_object *) pod)->body.type == type);
}
struct spa_pod_pointer_body {
uint32_t type;
void *value;
};
struct spa_pod_pointer {
struct spa_pod pod;
struct spa_pod_pointer_body body;
};
struct spa_pod_fd {
struct spa_pod pod;
int value;
};
#define SPA_POD_PROP_N_VALUES(prop) (((prop)->pod.size - sizeof(struct spa_pod_prop_body)) / (prop)->body.value.size)
struct spa_pod_prop_body {
uint32_t key;
#define SPA_POD_PROP_RANGE_NONE 0
#define SPA_POD_PROP_RANGE_MIN_MAX 1
#define SPA_POD_PROP_RANGE_STEP 2
#define SPA_POD_PROP_RANGE_ENUM 3
#define SPA_POD_PROP_RANGE_FLAGS 4
#define SPA_POD_PROP_RANGE_MASK 0xf
#define SPA_POD_PROP_FLAG_UNSET (1 << 4)
#define SPA_POD_PROP_FLAG_OPTIONAL (1 << 5)
#define SPA_POD_PROP_FLAG_READONLY (1 << 6)
#define SPA_POD_PROP_FLAG_DEPRECATED (1 << 7)
uint32_t flags;
struct spa_pod value;
/* array with elements of value.size follows,
* first element is value/default, rest are alternatives */
};
struct spa_pod_prop {
struct spa_pod pod;
struct spa_pod_prop_body body;
};
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_POD_H__ */