PipeWire: hook.h Source File

PipeWire 0.3.34

hook.h
Go to the documentation of this file.
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
130 struct spa_callbacks {
131 const void *funcs;
132 void *data;
133 };
134
136 #define SPA_CALLBACK_CHECK(c,m,v) ((c) && ((v) == 0 || (c)->version > (v)-1) && (c)->m)
137
142 #define SPA_CALLBACKS_INIT(_funcs,_data) (struct spa_callbacks){ _funcs, _data, }
143
146 struct spa_interface {
147 const char *type;
148 uint32_t version;
149 struct spa_callbacks cb;
150 };
151
165 #define SPA_INTERFACE_INIT(_type,_version,_funcs,_data) \
166 (struct spa_interface){ _type, _version, SPA_CALLBACKS_INIT(_funcs,_data), }
167
172 #define spa_callbacks_call(callbacks,type,method,vers,...) \
173 ({ \
174 const type *_f = (const type *) (callbacks)->funcs; \
175 if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \
176 _f->method((callbacks)->data, ## __VA_ARGS__); \
177 })
178
185 #define spa_callbacks_call_res(callbacks,type,res,method,vers,...) \
186 ({ \
187 const type *_f = (const type *) (callbacks)->funcs; \
188 if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \
189 res = _f->method((callbacks)->data, ## __VA_ARGS__); \
190 res; \
191 })
192
198 #define spa_interface_call(iface,method_type,method,vers,...) \
199 spa_callbacks_call(&(iface)->cb,method_type,method,vers,##__VA_ARGS__)
200
208 #define spa_interface_call_res(iface,method_type,res,method,vers,...) \
209 spa_callbacks_call_res(&(iface)->cb,method_type,res,method,vers,##__VA_ARGS__)
210
284 struct spa_hook_list {
285 struct spa_list list;
286 };
287
288
295 struct spa_hook {
296 struct spa_list link;
297 struct spa_callbacks cb;
300 void (*removed) (struct spa_hook *hook);
301 void *priv;
302 };
303
305 /* static */ inline void spa_hook_list_init(struct spa_hook_list *list)
306 {
307 spa_list_init(&list->list);
308 }
309
310 /* static */ inline bool spa_hook_list_is_empty(struct spa_hook_list *list)
311 {
312 return spa_list_is_empty(&list->list);
313 }
314
316 /* static */ inline void spa_hook_list_append(struct spa_hook_list *list,
317 struct spa_hook *hook,
318 const void *funcs, void *data)
319 {
320 spa_zero(*hook);
321 hook->cb = SPA_CALLBACKS_INIT(funcs, data);
322 spa_list_append(&list->list, &hook->link);
323 }
324
326 /* static */ inline void spa_hook_list_prepend(struct spa_hook_list *list,
327 struct spa_hook *hook,
328 const void *funcs, void *data)
329 {
330 spa_zero(*hook);
331 hook->cb = SPA_CALLBACKS_INIT(funcs, data);
332 spa_list_prepend(&list->list, &hook->link);
333 }
334
336 /* static */ inline void spa_hook_remove(struct spa_hook *hook)
337 {
338 spa_list_remove(&hook->link);
339 if (hook->removed)
340 hook->removed(hook);
341 }
342
344 /* static */ inline void spa_hook_list_clean(struct spa_hook_list *list)
345 {
346 struct spa_hook *h;
347 spa_list_consume(h, &list->list, link)
348 spa_hook_remove(h);
349 }
350
351 /* static */ inline void
352 spa_hook_list_isolate(struct spa_hook_list *list,
353 struct spa_hook_list *save,
354 struct spa_hook *hook,
355 const void *funcs, void *data)
356 {
357 /* init save list and move hooks to it */
358 spa_hook_list_init(save);
359 spa_list_insert_list(&save->list, &list->list);
360 /* init hooks and add single hook */
361 spa_hook_list_init(list);
362 spa_hook_list_append(list, hook, funcs, data);
363 }
364
365 /* static */ inline void
366 spa_hook_list_join(struct spa_hook_list *list,
367 struct spa_hook_list *save)
368 {
369 spa_list_insert_list(&list->list, &save->list);
370 }
371
372 #define spa_hook_list_call_simple(l,type,method,vers,...) \
373 ({ \
374 struct spa_hook_list *_l = l; \
375 struct spa_hook *_h, *_t; \
376 spa_list_for_each_safe(_h, _t, &_l->list, link) \
377 spa_callbacks_call(&_h->cb,type,method,vers, ## __VA_ARGS__); \
378 })
379
383 #define spa_hook_list_do_call(l,start,type,method,vers,once,...) \
384 ({ \
385 struct spa_hook_list *list = l; \
386 struct spa_list *s = start ? (struct spa_list *)start : &list->list; \
387 struct spa_hook cursor = { 0 }, *ci; \
388 int count = 0; \
389 spa_list_cursor_start(cursor, s, link); \
390 spa_list_for_each_cursor(ci, cursor, &list->list, link) { \
391 const type *_f = (const type *)ci->cb.funcs; \
392 if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) { \
393 _f->method(ci->cb.data, ## __VA_ARGS__); \
394 count++; \
395 if (once) \
396 break; \
397 } \
398 } \
399 spa_list_cursor_end(cursor, link); \
400 count; \
401 })
402
407 #define spa_hook_list_call(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,false,##__VA_ARGS__)
413 #define spa_hook_list_call_once(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,true,##__VA_ARGS__)
414
415 #define spa_hook_list_call_start(l,s,t,m,v,...) spa_hook_list_do_call(l,s,t,m,v,false,##__VA_ARGS__)
416 #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__)
417
422 #ifdef __cplusplus
423 }
424 #endif
425
426 #endif /* SPA_HOOK_H */
defs.h
spa_hook_list_isolate
void spa_hook_list_isolate(struct spa_hook_list *list, struct spa_hook_list *save, struct spa_hook *hook, const void *funcs, void *data)
Definition: hook.h:352
spa_hook_list_prepend
void spa_hook_list_prepend(struct spa_hook_list *list, struct spa_hook *hook, const void *funcs, void *data)
Prepend a hook.
Definition: hook.h:326
spa_hook_list_append
void spa_hook_list_append(struct spa_hook_list *list, struct spa_hook *hook, const void *funcs, void *data)
Append a hook.
Definition: hook.h:316
spa_hook_list_init
void spa_hook_list_init(struct spa_hook_list *list)
Initialize a hook list to the empty list.
Definition: hook.h:305
spa_hook_list_is_empty
bool spa_hook_list_is_empty(struct spa_hook_list *list)
Definition: hook.h:310
spa_hook_remove
void spa_hook_remove(struct spa_hook *hook)
Remove a hook.
Definition: hook.h:336
spa_hook_list_join
void spa_hook_list_join(struct spa_hook_list *list, struct spa_hook_list *save)
Definition: hook.h:366
spa_hook_list_clean
void spa_hook_list_clean(struct spa_hook_list *list)
Remove all hooks from the list.
Definition: hook.h:344
SPA_CALLBACKS_INIT
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition: hook.h:142
spa_list_init
void spa_list_init(struct spa_list *list)
Definition: list.h:44
spa_zero
#define spa_zero(x)
Definition: defs.h:303
spa_list_insert_list
void spa_list_insert_list(struct spa_list *list, struct spa_list *other)
Definition: list.h:59
spa_list_consume
#define spa_list_consume(pos, head, member)
Definition: list.h:96
spa_list_remove
void spa_list_remove(struct spa_list *elem)
Definition: list.h:69
spa_list_prepend
#define spa_list_prepend(list, item)
Definition: list.h:84
spa_list_is_empty
#define spa_list_is_empty(l)
Definition: list.h:49
spa_list_append
#define spa_list_append(list, item)
Definition: list.h:81
list.h
data
user data to add to an object
Definition: filter.c:75
link
Definition: module-filter-chain.c:180
spa_callbacks
Callbacks, contains the structure with functions and the data passed to the functions.
Definition: hook.h:130
spa_callbacks::funcs
const void * funcs
Definition: hook.h:131
spa_callbacks::data
void * data
Definition: hook.h:132
spa_hook_list
A list of hooks.
Definition: hook.h:284
spa_hook_list::list
struct spa_list list
Definition: hook.h:285
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:295
spa_hook::removed
void(* removed)(struct spa_hook *hook)
callback and data for the hook list, private to the hook_list implementor
Definition: hook.h:300
spa_hook::cb
struct spa_callbacks cb
Definition: hook.h:297
spa_hook::link
struct spa_list link
Definition: hook.h:296
spa_hook::priv
void * priv
Definition: hook.h:301
spa_interface
Definition: hook.h:146
spa_interface::version
uint32_t version
Definition: hook.h:148
spa_interface::type
const char * type
Definition: hook.h:147
spa_interface::cb
struct spa_callbacks cb
Definition: hook.h:149
spa_list
Definition: list.h:37