+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 546 lines · 16.4 KB · SPDX: MIT
main
1 /* Simple Plugin API */
2 /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4  
5 #ifndef SPA_HOOK_H
6 #define SPA_HOOK_H
7  
8 #include <spa/utils/defs.h>
9 #include <spa/utils/list.h>
10  
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14  
15 #ifndef SPA_API_HOOK
16 #ifdef SPA_API_IMPL
17 #define SPA_API_HOOK SPA_API_IMPL
18 #else
19 #define SPA_API_HOOK static inline
20 #endif
21 #endif
22  
23 /** \defgroup spa_interfaces Interfaces
24 *
25 * \brief Generic implementation of implementation-independent interfaces
26 *
27 * A SPA Interface is a generic struct that, together with a few macros,
28 * provides a generic way of invoking methods on objects without knowing the
29 * details of the implementation.
30 *
31 * The primary interaction with interfaces is through macros that expand into
32 * the right method call. For the implementation of an interface, we need two
33 * structs and a macro to invoke the `bar` method:
34 *
35 * \code{.c}
36 * // this struct must be public and defines the interface to a
37 * // struct foo
38 * struct foo_methods {
39 * uint32_t version;
40 * void (*bar)(void *object, const char *msg);
41 * };
42 *
43 * // this struct does not need to be public
44 * struct foo {
45 * struct spa_interface iface; // must be first element, see foo_bar()
46 * int some_other_field;
47 * ...
48 * };
49 *
50 * // if struct foo is private, we need to cast to a
51 * // generic spa_interface object
52 * #define foo_bar(obj, ...) ({ \
53 * struct foo *f = obj;
54 * spa_interface_call((struct spa_interface *)f, // pointer to spa_interface in foo
55 * struct foo_methods, // type of callbacks
56 * bar, // name of methods
57 * 0, // hardcoded version to match foo_methods->version
58 * __VA_ARGS__ // pass rest of args through
59 * );/
60 * })
61 * \endcode
62 *
63 * The `struct foo_methods` and the invocation macro `foo_bar()` must be
64 * available to the caller. The implementation of `struct foo` can be private.
65 *
66 * \code{.c}
67 * void main(void) {
68 * struct foo *myfoo = get_foo_from_somewhere();
69 * foo_bar(myfoo, "Invoking bar() on myfoo");
70 * }
71 * \endcode
72 * The expansion of `foo_bar()` resolves roughly into this code:
73 * \code{.c}
74 * void main(void) {
75 * struct foo *myfoo = get_foo_from_somewhere();
76 * // foo_bar(myfoo, "Invoking bar() on myfoo");
77 * const struct foo_methods *methods = ((struct spa_interface*)myfoo)->cb;
78 * if (0 >= methods->version && // version check
79 * methods->bar) // compile error if this function does not exist,
80 * methods->bar(myfoo, "Invoking bar() on myfoo");
81 * }
82 * \endcode
83 *
84 * The typecast used in `foo_bar()` allows `struct foo` to be opaque to the
85 * caller. The implementation may assign the callback methods at object
86 * instantiation, and the caller will transparently invoke the method on the
87 * given object. For example, the following code assigns a different `bar()` method on
88 * Mondays - the caller does not need to know this.
89 * \code{.c}
90 *
91 * static void bar_stdout(struct foo *f, const char *msg) {
92 * printf(msg);
93 * }
94 * static void bar_stderr(struct foo *f, const char *msg) {
95 * fprintf(stderr, msg);
96 * }
97 *
98 * struct foo* get_foo_from_somewhere() {
99 * struct foo *f = calloc(sizeof struct foo);
100 * // illustrative only, use SPA_INTERFACE_INIT()
101 * f->iface->cb = (struct foo_methods*) { .bar = bar_stdout };
102 * if (today_is_monday)
103 * f->iface->cb = (struct foo_methods*) { .bar = bar_stderr };
104 * return f;
105 * }
106 * \endcode
107 */
108  
109 /**
110 * \addtogroup spa_interfaces
111 * \{
112 */
113  
114 /** \struct spa_callbacks
115 * Callbacks, contains the structure with functions and the data passed
116 * to the functions. The structure should also contain a version field that
117 * is checked. */
118 struct spa_callbacks {
119 const void *funcs;
120 void *data;
121 };
122  
123 /** Check if a callback \a c is of at least version \a v */
124 #define SPA_CALLBACK_VERSION_MIN(c,v) ((c) && ((v) == 0 || (c)->version > (v)-1))
125  
126 /** Check if a callback \a c has method \a m of version \a v */
127 #define SPA_CALLBACK_CHECK(c,m,v) (SPA_CALLBACK_VERSION_MIN(c,v) && (c)->m)
128  
129 /**
130 * Initialize the set of functions \a funcs as a \ref spa_callbacks, together
131 * with \a _data.
132 */
133 #define SPA_CALLBACKS_INIT(_funcs,_data) ((struct spa_callbacks){ (_funcs), (_data), })
134  
135 /** \struct spa_interface
136 */
137 struct spa_interface {
138 const char *type;
139 uint32_t version;
140 struct spa_callbacks cb;
141 };
142  
143 /**
144 * Initialize a \ref spa_interface.
145 *
146 * \code{.c}
147 * const static struct foo_methods foo_funcs = {
148 * .bar = some_bar_implementation,
149 * };
150 *
151 * struct foo *f = malloc(...);
152 * f->iface = SPA_INTERFACE_INIT("foo type", 0, foo_funcs, NULL);
153 * \endcode
154 *
155 */
156 #define SPA_INTERFACE_INIT(_type,_version,_funcs,_data) \
157 ((struct spa_interface){ (_type), (_version), SPA_CALLBACKS_INIT(_funcs,_data), })
158  
159 /**
160 * Invoke method named \a method in the \a callbacks.
161 * The \a method_type defines the type of the method struct.
162 * Returns true if the method could be called, false otherwise.
163 */
164 #define spa_callbacks_call(callbacks,type,method,vers,...) \
165 ({ \
166 const type *_f = (const type *) (callbacks)->funcs; \
167 bool _res = SPA_CALLBACK_CHECK(_f,method,vers); \
168 if (SPA_LIKELY(_res)) \
169 (_f->method)((callbacks)->data, ## __VA_ARGS__); \
170 _res; \
171 })
172  
173 #define spa_callbacks_call_fast(callbacks,type,method,vers,...) \
174 ({ \
175 const type *_f = (const type *) (callbacks)->funcs; \
176 (_f->method)((callbacks)->data, ## __VA_ARGS__); \
177 true; \
178 })
179  
180  
181 /**
182 * True if the \a callbacks are of version \a vers, false otherwise
183 */
184 #define spa_callback_version_min(callbacks,type,vers) \
185 ({ \
186 const type *_f = (const type *) (callbacks)->funcs; \
187 SPA_CALLBACK_VERSION_MIN(_f,vers); \
188 })
189  
190 /**
191 * True if the \a callbacks contains \a method of version
192 * \a vers, false otherwise
193 */
194 #define spa_callback_check(callbacks,type,method,vers) \
195 ({ \
196 const type *_f = (const type *) (callbacks)->funcs; \
197 SPA_CALLBACK_CHECK(_f,method,vers); \
198 })
199  
200 /**
201 * Invoke method named \a method in the \a callbacks.
202 * The \a method_type defines the type of the method struct.
203 *
204 * The return value is stored in \a res.
205 */
206 #define spa_callbacks_call_res(callbacks,type,res,method,vers,...) \
207 ({ \
208 const type *_f = (const type *) (callbacks)->funcs; \
209 if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \
210 res = (_f->method)((callbacks)->data, ## __VA_ARGS__); \
211 res; \
212 })
213 #define spa_callbacks_call_fast_res(callbacks,type,res,method,vers,...) \
214 ({ \
215 const type *_f = (const type *) (callbacks)->funcs; \
216 res = (_f->method)((callbacks)->data, ## __VA_ARGS__); \
217 })
218  
219 /**
220 * True if the \a iface's callbacks are of version \a vers, false otherwise
221 */
222 #define spa_interface_callback_version_min(iface,method_type,vers) \
223 spa_callback_version_min(&(iface)->cb, method_type, vers)
224  
225 /**
226 * True if the \a iface's callback \a method is of version \a vers
227 * and exists, false otherwise
228 */
229 #define spa_interface_callback_check(iface,method_type,method,vers) \
230 spa_callback_check(&(iface)->cb, method_type, method, vers)
231  
232 /**
233 * Invoke method named \a method in the callbacks on the given interface object.
234 * The \a method_type defines the type of the method struct, not the interface
235 * itself.
236 */
237 #define spa_interface_call(iface,method_type,method,vers,...) \
238 spa_callbacks_call(&(iface)->cb,method_type,method,vers,##__VA_ARGS__)
239  
240 #define spa_interface_call_fast(iface,method_type,method,vers,...) \
241 spa_callbacks_call_fast(&(iface)->cb,method_type,method,vers,##__VA_ARGS__)
242  
243 /**
244 * Invoke method named \a method in the callbacks on the given interface object.
245 * The \a method_type defines the type of the method struct, not the interface
246 * itself.
247 *
248 * The return value is stored in \a res.
249 */
250 #define spa_interface_call_res(iface,method_type,res,method,vers,...) \
251 spa_callbacks_call_res(&(iface)->cb,method_type,res,method,vers,##__VA_ARGS__)
252  
253 #define spa_interface_call_fast_res(iface,method_type,res,method,vers,...) \
254 spa_callbacks_call_fast_res(&(iface)->cb,method_type,res,method,vers,##__VA_ARGS__)
255  
256  
257 #define spa_api_func_v(o,method,version,...) \
258 ({ \
259 if (SPA_LIKELY(SPA_CALLBACK_CHECK(o,method,version))) \
260 ((o)->method)(o, ##__VA_ARGS__); \
261 })
262 #define spa_api_func_r(rtype,def,o,method,version,...) \
263 ({ \
264 rtype _res = def; \
265 if (SPA_LIKELY(SPA_CALLBACK_CHECK(o,method,version))) \
266 _res = ((o)->method)(o, ##__VA_ARGS__); \
267 _res; \
268 })
269 #define spa_api_func_fast(o,method,...) \
270 ({ \
271 ((o)->method)(o, ##__VA_ARGS__); \
272 })
273  
274 #define spa_api_method_v(type,o,method,version,...) \
275 ({ \
276 struct spa_interface *_i = o; \
277 spa_interface_call(_i, struct type ##_methods, \
278 method, version, ##__VA_ARGS__); \
279 })
280 #define spa_api_method_r(rtype,def,type,o,method,version,...) \
281 ({ \
282 rtype _res = def; \
283 struct spa_interface *_i = o; \
284 spa_interface_call_res(_i, struct type ##_methods, \
285 _res, method, version, ##__VA_ARGS__); \
286 _res; \
287 })
288 #define spa_api_method_null_v(type,co,o,method,version,...) \
289 ({ \
290 struct type *_co = co; \
291 if (SPA_LIKELY(_co != NULL)) { \
292 struct spa_interface *_i = o; \
293 spa_interface_call(_i, struct type ##_methods, \
294 method, version, ##__VA_ARGS__); \
295 } \
296 })
297 #define spa_api_method_null_r(rtype,def,type,co,o,method,version,...) \
298 ({ \
299 rtype _res = def; \
300 struct type *_co = co; \
301 if (SPA_LIKELY(_co != NULL)) { \
302 struct spa_interface *_i = o; \
303 spa_interface_call_res(_i, struct type ##_methods, \
304 _res, method, version, ##__VA_ARGS__); \
305 } \
306 _res; \
307 })
308 #define spa_api_method_fast_v(type,o,method,version,...) \
309 ({ \
310 struct spa_interface *_i = o; \
311 spa_interface_call_fast(_i, struct type ##_methods, \
312 method, version, ##__VA_ARGS__); \
313 })
314 #define spa_api_method_fast_r(rtype,def,type,o,method,version,...) \
315 ({ \
316 rtype _res = def; \
317 struct spa_interface *_i = o; \
318 spa_interface_call_fast_res(_i, struct type ##_methods, \
319 _res, method, version, ##__VA_ARGS__); \
320 _res; \
321 })
322  
323 /**
324 * \}
325 */
326  
327 /** \defgroup spa_hooks Hooks
328 *
329 * A SPA Hook is a data structure to keep track of callbacks. It is similar to
330 * the \ref spa_interfaces and typically used where an implementation allows
331 * for multiple external callback functions. For example, an implementation may
332 * use a hook list to implement signals with each caller using a hook to
333 * register callbacks to be invoked on those signals.
334 *
335 * The below (pseudo)code is a minimal example outlining the use of hooks:
336 * \code{.c}
337 * // the public interface
338 * #define VERSION_BAR_EVENTS 0 // version of the vtable
339 * struct bar_events {
340 * uint32_t version; // NOTE: an integral member named `version`
341 * // must be present in the vtable
342 * void (*boom)(void *data, const char *msg);
343 * };
344 *
345 * // private implementation
346 * struct party {
347 * struct spa_hook_list bar_list;
348 * };
349 *
350 * void party_add_event_listener(struct party *p, struct spa_hook *listener,
351 * const struct bar_events *events, void *data)
352 * {
353 * spa_hook_list_append(&p->bar_list, listener, events, data);
354 * }
355 *
356 * static void party_on(struct party *p)
357 * {
358 * // NOTE: this is a macro, it evaluates to an integer,
359 * // which is the number of hooks called
360 * spa_hook_list_call(&p->list,
361 * struct bar_events, // vtable type
362 * boom, // function name
363 * 0, // hardcoded version,
364 * // usually the version in which `boom`
365 * // has been added to the vtable
366 * "party on, wayne" // function argument(s)
367 * );
368 * }
369 * \endcode
370 *
371 * In the caller, the hooks can be used like this:
372 * \code{.c}
373 * static void boom_cb(void *data, const char *msg) {
374 * // data is userdata from main()
375 * printf("%s", msg);
376 * }
377 *
378 * static const struct bar_events events = {
379 * .version = VERSION_BAR_EVENTS, // version of the implemented interface
380 * .boom = boom_cb,
381 * };
382 *
383 * void main(void) {
384 * void *userdata = whatever;
385 * struct spa_hook hook;
386 * struct party *p = start_the_party();
387 *
388 * party_add_event_listener(p, &hook, &events, userdata);
389 *
390 * mainloop();
391 * return 0;
392 * }
393 *
394 * \endcode
395 */
396  
397 /**
398 * \addtogroup spa_hooks
399 * \{
400 */
401  
402 /** \struct spa_hook_list
403 * A list of hooks. This struct is primarily used by
404 * implementation that use multiple caller-provided \ref spa_hook. */
405 struct spa_hook_list {
406 struct spa_list list;
407 };
408  
409  
410 /** \struct spa_hook
411 * A hook, contains the structure with functions and the data passed
412 * to the functions.
413 *
414 * A hook should be treated as opaque by the caller.
415 */
416 struct spa_hook {
417 struct spa_list link;
418 struct spa_callbacks cb;
419 /** callback and data for the hook list, private to the
420 * hook_list implementor */
421 void (*removed) (struct spa_hook *hook);
422 void *priv;
423 };
424  
425 /** Initialize a hook list to the empty list*/
426 SPA_API_HOOK void spa_hook_list_init(struct spa_hook_list *list)
427 {
428 spa_list_init(&list->list);
429 }
430  
431 SPA_API_HOOK bool spa_hook_list_is_empty(struct spa_hook_list *list)
432 {
433 return spa_list_is_empty(&list->list);
434 }
435  
436 /** Append a hook. */
437 SPA_API_HOOK void spa_hook_list_append(struct spa_hook_list *list,
438 struct spa_hook *hook,
439 const void *funcs, void *data)
440 {
441 spa_zero(*hook);
442 hook->cb = SPA_CALLBACKS_INIT(funcs, data);
443 spa_list_append(&list->list, &hook->link);
444 }
445  
446 /** Prepend a hook */
447 SPA_API_HOOK void spa_hook_list_prepend(struct spa_hook_list *list,
448 struct spa_hook *hook,
449 const void *funcs, void *data)
450 {
451 spa_zero(*hook);
452 hook->cb = SPA_CALLBACKS_INIT(funcs, data);
453 spa_list_prepend(&list->list, &hook->link);
454 }
455  
456 /** Remove a hook */
457 SPA_API_HOOK void spa_hook_remove(struct spa_hook *hook)
458 {
459 if (spa_list_is_initialized(&hook->link))
460 spa_list_remove(&hook->link);
461 if (hook->removed)
462 hook->removed(hook);
463 }
464  
465 /** Remove all hooks from the list */
466 SPA_API_HOOK void spa_hook_list_clean(struct spa_hook_list *list)
467 {
468 struct spa_hook *h;
469 spa_list_consume(h, &list->list, link)
470 spa_hook_remove(h);
471 }
472  
473 SPA_API_HOOK void
474 spa_hook_list_isolate(struct spa_hook_list *list,
475 struct spa_hook_list *save,
476 struct spa_hook *hook,
477 const void *funcs, void *data)
478 {
479 /* init save list and move hooks to it */
480 spa_hook_list_init(save);
481 spa_list_insert_list(&save->list, &list->list);
482 /* init hooks and add single hook */
483 spa_hook_list_init(list);
484 spa_hook_list_append(list, hook, funcs, data);
485 }
486  
487 SPA_API_HOOK void
488 spa_hook_list_join(struct spa_hook_list *list,
489 struct spa_hook_list *save)
490 {
491 spa_list_insert_list(&list->list, &save->list);
492 }
493  
494 #define spa_hook_list_call_simple(l,type,method,vers,...) \
495 ({ \
496 struct spa_hook_list *_l = l; \
497 struct spa_hook *_h, *_t; \
498 spa_list_for_each_safe(_h, _t, &_l->list, link) \
499 spa_callbacks_call(&_h->cb,type,method,vers, ## __VA_ARGS__); \
500 })
501  
502 /** Call all hooks in a list, starting from the given one and optionally stopping
503 * after calling the first non-NULL function, returns the number of methods
504 * called */
505 #define spa_hook_list_do_call(l,start,type,method,vers,once,...) \
506 ({ \
507 struct spa_hook_list *_list = l; \
508 struct spa_list *_s = start ? (struct spa_list *)start : &_list->list; \
509 struct spa_hook _cursor = { 0 }, *_ci; \
510 int _count = 0; \
511 spa_list_cursor_start(_cursor, _s, link); \
512 spa_list_for_each_cursor(_ci, _cursor, &_list->list, link) { \
513 if (spa_callbacks_call(&_ci->cb,type,method,vers, ## __VA_ARGS__)) { \
514 _count++; \
515 if (once) \
516 break; \
517 } \
518 } \
519 spa_list_cursor_end(_cursor, link); \
520 _count; \
521 })
522  
523 /**
524 * Call the method named \a m for each element in list \a l.
525 * \a t specifies the type of the callback struct.
526 */
527 #define spa_hook_list_call(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,false,##__VA_ARGS__)
528 /**
529 * Call the method named \a m for each element in list \a l, stopping after
530 * the first invocation.
531 * \a t specifies the type of the callback struct.
532 */
533 #define spa_hook_list_call_once(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,true,##__VA_ARGS__)
534  
535 #define spa_hook_list_call_start(l,s,t,m,v,...) spa_hook_list_do_call(l,s,t,m,v,false,##__VA_ARGS__)
536 #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__)
537  
538 /**
539 * \}
540 */
541  
542 #ifdef __cplusplus
543 }
544 #endif
545  
546 #endif /* SPA_HOOK_H */