| 1 |
/* Simple Plugin API |
| 2 |
* |
| 3 |
* Copyright © 2018 Wim Taymans |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef SPA_HOOK_H |
| 26 |
#define SPA_HOOK_H |
| 27 |
|
| 28 |
#ifdef __cplusplus |
| 29 |
extern "C" { |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <spa/utils/defs.h> |
| 33 |
#include <spa/utils/list.h> |
| 34 |
|
| 35 |
/** \class spa_hook |
| 36 |
* |
| 37 |
* \brief a list of hooks |
| 38 |
* |
| 39 |
* The hook list provides a way to keep track of hooks. |
| 40 |
*/ |
| 41 |
/** A list of hooks */ |
| 42 |
struct spa_hook_list { |
| 43 |
struct spa_list list; |
| 44 |
}; |
| 45 |
|
| 46 |
/** Callbacks, contains the structure with functions and the data passed |
| 47 |
* to the functions. The structure should also contain a version field that |
| 48 |
* is checked. */ |
| 49 |
struct spa_callbacks { |
| 50 |
const void *funcs; |
| 51 |
void *data; |
| 52 |
}; |
| 53 |
|
| 54 |
/** Check if a callback \c has method \m of version \v */ |
| 55 |
#define SPA_CALLBACK_CHECK(c,m,v) ((c) && ((v) == 0 || (c)->version > (v)-1) && (c)->m) |
| 56 |
|
| 57 |
#define SPA_CALLBACKS_INIT(_funcs,_data) (struct spa_callbacks){ _funcs, _data, } |
| 58 |
|
| 59 |
struct spa_interface { |
| 60 |
const char *type; |
| 61 |
uint32_t version; |
| 62 |
struct spa_callbacks cb; |
| 63 |
}; |
| 64 |
|
| 65 |
#define SPA_INTERFACE_INIT(_type,_version,_funcs,_data) \ |
| 66 |
(struct spa_interface){ _type, _version, SPA_CALLBACKS_INIT(_funcs,_data), } |
| 67 |
|
| 68 |
/** A hook, contains the structure with functions and the data passed |
| 69 |
* to the functions. */ |
| 70 |
struct spa_hook { |
| 71 |
struct spa_list link; |
| 72 |
struct spa_callbacks cb; |
| 73 |
/** callback and data for the hook list, private to the |
| 74 |
* hook_list implementor */ |
| 75 |
void (*removed) (struct spa_hook *hook); |
| 76 |
void *priv; |
| 77 |
}; |
| 78 |
|
| 79 |
/** Initialize a hook list */ |
| 80 |
static inline void spa_hook_list_init(struct spa_hook_list *list) |
| 81 |
{ |
| 82 |
spa_list_init(&list->list); |
| 83 |
} |
| 84 |
|
| 85 |
static inline bool spa_hook_list_is_empty(struct spa_hook_list *list) |
| 86 |
{ |
| 87 |
return spa_list_is_empty(&list->list); |
| 88 |
} |
| 89 |
|
| 90 |
/** Append a hook \memberof spa_hook. */ |
| 91 |
static inline void spa_hook_list_append(struct spa_hook_list *list, |
| 92 |
struct spa_hook *hook, |
| 93 |
const void *funcs, void *data) |
| 94 |
{ |
| 95 |
spa_zero(*hook); |
| 96 |
hook->cb = SPA_CALLBACKS_INIT(funcs, data); |
| 97 |
spa_list_append(&list->list, &hook->link); |
| 98 |
} |
| 99 |
|
| 100 |
/** Prepend a hook \memberof spa_hook */ |
| 101 |
static inline void spa_hook_list_prepend(struct spa_hook_list *list, |
| 102 |
struct spa_hook *hook, |
| 103 |
const void *funcs, void *data) |
| 104 |
{ |
| 105 |
spa_zero(*hook); |
| 106 |
hook->cb = SPA_CALLBACKS_INIT(funcs, data); |
| 107 |
spa_list_prepend(&list->list, &hook->link); |
| 108 |
} |
| 109 |
|
| 110 |
/** Remove a hook \memberof spa_hook */ |
| 111 |
static inline void spa_hook_remove(struct spa_hook *hook) |
| 112 |
{ |
| 113 |
spa_list_remove(&hook->link); |
| 114 |
if (hook->removed) |
| 115 |
hook->removed(hook); |
| 116 |
} |
| 117 |
|
| 118 |
static inline void spa_hook_list_clean(struct spa_hook_list *list) |
| 119 |
{ |
| 120 |
struct spa_hook *h; |
| 121 |
spa_list_consume(h, &list->list, link) |
| 122 |
spa_hook_remove(h); |
| 123 |
} |
| 124 |
|
| 125 |
static inline void |
| 126 |
spa_hook_list_isolate(struct spa_hook_list *list, |
| 127 |
struct spa_hook_list *save, |
| 128 |
struct spa_hook *hook, |
| 129 |
const void *funcs, void *data) |
| 130 |
{ |
| 131 |
/* init save list and move hooks to it */ |
| 132 |
spa_hook_list_init(save); |
| 133 |
spa_list_insert_list(&save->list, &list->list); |
| 134 |
/* init hooks and add single hook */ |
| 135 |
spa_hook_list_init(list); |
| 136 |
spa_hook_list_append(list, hook, funcs, data); |
| 137 |
} |
| 138 |
|
| 139 |
static inline void |
| 140 |
spa_hook_list_join(struct spa_hook_list *list, |
| 141 |
struct spa_hook_list *save) |
| 142 |
{ |
| 143 |
spa_list_insert_list(&list->list, &save->list); |
| 144 |
} |
| 145 |
|
| 146 |
#define spa_callbacks_call(callbacks,type,method,vers,...) \ |
| 147 |
({ \ |
| 148 |
const type *_f = (const type *) (callbacks)->funcs; \ |
| 149 |
if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \ |
| 150 |
_f->method((callbacks)->data, ## __VA_ARGS__); \ |
| 151 |
}) |
| 152 |
|
| 153 |
#define spa_callbacks_call_res(callbacks,type,res,method,vers,...) \ |
| 154 |
({ \ |
| 155 |
const type *_f = (const type *) (callbacks)->funcs; \ |
| 156 |
if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \ |
| 157 |
res = _f->method((callbacks)->data, ## __VA_ARGS__); \ |
| 158 |
res; \ |
| 159 |
}) |
| 160 |
|
| 161 |
#define spa_interface_call(iface,type,method,vers,...) \ |
| 162 |
spa_callbacks_call(&(iface)->cb,type,method,vers,##__VA_ARGS__) |
| 163 |
|
| 164 |
#define spa_interface_call_res(iface,type,res,method,vers,...) \ |
| 165 |
spa_callbacks_call_res(&(iface)->cb,type,res,method,vers,##__VA_ARGS__) |
| 166 |
|
| 167 |
#define spa_hook_list_call_simple(l,type,method,vers,...) \ |
| 168 |
({ \ |
| 169 |
struct spa_hook_list *_l = l; \ |
| 170 |
struct spa_hook *_h, *_t; \ |
| 171 |
spa_list_for_each_safe(_h, _t, &_l->list, link) \ |
| 172 |
spa_callbacks_call(&_h->cb,type,method,vers, ## __VA_ARGS__); \ |
| 173 |
}) |
| 174 |
|
| 175 |
/** Call all hooks in a list, starting from the given one and optionally stopping |
| 176 |
* after calling the first non-NULL function, returns the number of methods |
| 177 |
* called */ |
| 178 |
#define spa_hook_list_do_call(l,start,type,method,vers,once,...) \ |
| 179 |
({ \ |
| 180 |
struct spa_hook_list *list = l; \ |
| 181 |
struct spa_list *s = start ? (struct spa_list *)start : &list->list; \ |
| 182 |
struct spa_hook cursor = { 0 }, *ci; \ |
| 183 |
int count = 0; \ |
| 184 |
spa_list_cursor_start(cursor, s, link); \ |
| 185 |
spa_list_for_each_cursor(ci, cursor, &list->list, link) { \ |
| 186 |
const type *_f = (const type *)ci->cb.funcs; \ |
| 187 |
if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) { \ |
| 188 |
_f->method(ci->cb.data, ## __VA_ARGS__); \ |
| 189 |
count++; \ |
| 190 |
if (once) \ |
| 191 |
break; \ |
| 192 |
} \ |
| 193 |
} \ |
| 194 |
spa_list_cursor_end(cursor, link); \ |
| 195 |
count; \ |
| 196 |
}) |
| 197 |
|
| 198 |
#define spa_hook_list_call(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,false,##__VA_ARGS__) |
| 199 |
#define spa_hook_list_call_once(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,true,##__VA_ARGS__) |
| 200 |
|
| 201 |
#define spa_hook_list_call_start(l,s,t,m,v,...) spa_hook_list_do_call(l,s,t,m,v,false,##__VA_ARGS__) |
| 202 |
#define spa_hook_list_call_once_start(l,s,t,m,v,...) spa_hook_list_do_call(l,s,t,m,v,true,##__VA_ARGS__) |
| 203 |
|
| 204 |
#ifdef __cplusplus |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
#endif /* SPA_HOOK_H */ |