hooks: use hook to implement the callbacks

This way we get the version check implemented and save some
code.
This commit is contained in:
Wim Taymans 2019-05-15 11:19:23 +02:00
parent a78617c6a8
commit 6ee192dff5
26 changed files with 121 additions and 111 deletions

View file

@ -31,6 +31,7 @@ extern "C" {
#include <spa/utils/defs.h>
#include <spa/utils/list.h>
#include <spa/utils/hook.h>
#include <spa/node/node.h>
#include <spa/node/io.h>
@ -92,8 +93,6 @@ struct spa_graph_node_callbacks {
int (*reuse_buffer) (void *data, struct spa_graph_node *node,
uint32_t port_id, uint32_t buffer_id);
};
#define spa_graph_node_process(n) ((n)->callbacks->process((n)->callbacks_data,(n)))
#define spa_graph_node_reuse_buffer(n,p,i) ((n)->callbacks->reuse_buffer((n)->callbacks_data,(n),(p),(i)))
struct spa_graph_node {
struct spa_list link; /**< link in graph nodes list */
@ -104,11 +103,21 @@ struct spa_graph_node {
struct spa_graph_state *state; /**< state of the node */
struct spa_graph_link graph_link; /**< link in graph */
struct spa_graph *subgraph; /**< subgraph or NULL */
const struct spa_graph_node_callbacks *callbacks;
void *callbacks_data;
struct spa_hook callbacks;
struct spa_list sched_link; /**< link for scheduler */
};
#define spa_graph_node_call(n,method,version,...) \
({ \
int __res = 0; \
spa_hook_call_res(&(n)->callbacks, \
struct spa_graph_node_callbacks, __res, \
method, version, ##__VA_ARGS__); \
__res; \
})
#define spa_graph_node_process(n) spa_graph_node_call(n, process, 0, n)
#define spa_graph_node_reuse_buffer(n,p,i) spa_graph_node_call(n, reuse_buffer, 0, n, p, i)
struct spa_graph_port {
struct spa_list link; /**< link in node port list */
@ -239,10 +248,9 @@ static inline void spa_graph_node_set_subgraph(struct spa_graph_node *node,
static inline void
spa_graph_node_set_callbacks(struct spa_graph_node *node,
const struct spa_graph_node_callbacks *callbacks,
void *callbacks_data)
void *data)
{
node->callbacks = callbacks;
node->callbacks_data = callbacks_data;
node->callbacks = SPA_HOOK_INIT(callbacks, data);
}
static inline void

View file

@ -86,6 +86,17 @@ struct spa_monitor_callbacks {
int (*event) (void *data, struct spa_event *event);
};
#define spa_monitor_call(hook,method,version,...) \
({ \
int __res = 0; \
spa_hook_call_res(hook, struct spa_monitor_callbacks, __res, \
method, version, ##__VA_ARGS__); \
__res; \
})
#define spa_monitor_call_info(hook,i) spa_monitor_call(hook, info, 0, i)
#define spa_monitor_call_event(hook,e) spa_monitor_call(hook, event, 0, e)
/**
* spa_monitor:
*

View file

@ -182,6 +182,7 @@ struct spa_node_events {
*/
struct spa_node_callbacks {
#define SPA_VERSION_NODE_CALLBACKS 0
uint32_t version;
/**
* \param node a spa_node
*
@ -207,6 +208,18 @@ struct spa_node_callbacks {
uint32_t buffer_id);
};
#define spa_node_call(hook,method,version,...) \
({ \
int __res = 0; \
spa_hook_call_res(hook, struct spa_node_callbacks, __res, \
method, version, ##__VA_ARGS__); \
__res; \
})
#define spa_node_call_ready(hook,s) spa_node_call(hook, ready, 0, s)
#define spa_node_call_reuse_buffer(hook,p,b) spa_node_call(hook, reuse_buffer, 0, p, b)
/** flags that can be passed to set_param and port_set_param functions */
#define SPA_NODE_PARAM_FLAG_TEST_ONLY (1 << 0) /* just check if the param is accepted */
#define SPA_NODE_PARAM_FLAG_FIXATE (1 << 1) /* fixate the non-optional unset fields */

View file

@ -31,6 +31,7 @@ extern "C" {
#include <stdarg.h>
#include <spa/utils/hook.h>
#include <spa/pod/iter.h>
#include <spa/pod/vararg.h>
@ -47,7 +48,8 @@ struct spa_pod_builder;
struct spa_pod_builder_callbacks {
#define SPA_VERSION_POD_BUILDER_CALLBACKS 0
uint32_t version;
int (*overflow) (void *callbacks_data, uint32_t size);
int (*overflow) (void *data, uint32_t size);
};
struct spa_pod_builder {
@ -55,8 +57,7 @@ struct spa_pod_builder {
uint32_t size;
uint32_t _padding;
struct spa_pod_builder_state state;
const struct spa_pod_builder_callbacks *callbacks;
void *callbacks_data;
struct spa_hook callbacks;
};
#define SPA_POD_BUILDER_INIT(buffer,size) (struct spa_pod_builder){ buffer, size, }
@ -67,6 +68,13 @@ spa_pod_builder_get_state(struct spa_pod_builder *builder, struct spa_pod_builde
*state = builder->state;
}
static inline void
spa_pod_builder_set_callbacks(struct spa_pod_builder *builder,
const struct spa_pod_builder_callbacks *callbacks, void *data)
{
builder->callbacks = SPA_HOOK_INIT(callbacks, data);
}
static inline void
spa_pod_builder_reset(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
{
@ -118,10 +126,9 @@ static inline int spa_pod_builder_raw(struct spa_pod_builder *builder, const voi
uint32_t offset = builder->state.offset;
if (offset + size > builder->size) {
if (builder->callbacks && builder->callbacks->overflow)
res = builder->callbacks->overflow(builder->callbacks_data, offset + size);
else
res = -ENOSPC;
res = -ENOSPC;
spa_hook_call_res(&builder->callbacks, struct spa_pod_builder_callbacks, res,
overflow, 0, offset + size);
}
if (res == 0)
memcpy(SPA_MEMBER(builder->data, offset, void), data, size);

View file

@ -112,14 +112,14 @@ spa_hook_list_join(struct spa_hook_list *list,
#define spa_hook_call(hook,type,method,vers,...) \
({ \
const type *cb = (hook)->funcs; \
const type *cb = (const type *) (hook)->funcs; \
if (cb && cb->version >= vers && cb->method) \
cb->method((hook)->data, ## __VA_ARGS__); \
})
#define spa_hook_call_res(hook,type,res,method,vers,...) \
({ \
const type *cb = (hook)->funcs; \
const type *cb = (const type *) (hook)->funcs; \
if (cb && cb->version >= vers && cb->method) \
res = cb->method((hook)->data, ## __VA_ARGS__); \
res; \
@ -144,7 +144,7 @@ spa_hook_list_join(struct spa_hook_list *list,
int count = 0; \
spa_list_cursor_start(cursor, s, link); \
spa_list_for_each_cursor(ci, cursor, &list->list, link) { \
const type *cb = ci->funcs; \
const type *cb = (const type *)ci->funcs; \
if (cb && cb->version >= vers && cb->method) { \
cb->method(ci->data, ## __VA_ARGS__); \
count++; \