PipeWire  0.3.61
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_VERSION_MIN(c,v) ((c) && ((v) == 0 || (c)->version > (v)-1))
137 
139 #define SPA_CALLBACK_CHECK(c,m,v) (SPA_CALLBACK_VERSION_MIN(c,v) && (c)->m)
140 
145 #define SPA_CALLBACKS_INIT(_funcs,_data) ((struct spa_callbacks){ (_funcs), (_data), })
146 
149 struct spa_interface {
150  const char *type;
151  uint32_t version;
152  struct spa_callbacks cb;
153 };
154 
168 #define SPA_INTERFACE_INIT(_type,_version,_funcs,_data) \
169  ((struct spa_interface){ (_type), (_version), SPA_CALLBACKS_INIT(_funcs,_data), })
170 
176 #define spa_callbacks_call(callbacks,type,method,vers,...) \
177 ({ \
178  const type *_f = (const type *) (callbacks)->funcs; \
179  bool _res = SPA_CALLBACK_CHECK(_f,method,vers); \
180  if (SPA_LIKELY(_res)) \
181  _f->method((callbacks)->data, ## __VA_ARGS__); \
182  _res; \
183 })
184 
188 #define spa_callback_version_min(callbacks,type,vers) \
189 ({ \
190  const type *_f = (const type *) (callbacks)->funcs; \
191  SPA_CALLBACK_VERSION_MIN(_f,vers); \
192 })
193 
198 #define spa_callback_check(callbacks,type,method,vers) \
199 ({ \
200  const type *_f = (const type *) (callbacks)->funcs; \
201  SPA_CALLBACK_CHECK(_f,method,vers); \
202 })
203 
210 #define spa_callbacks_call_res(callbacks,type,res,method,vers,...) \
211 ({ \
212  const type *_f = (const type *) (callbacks)->funcs; \
213  if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \
214  res = _f->method((callbacks)->data, ## __VA_ARGS__); \
215  res; \
216 })
217 
221 #define spa_interface_callback_version_min(iface,method_type,vers) \
222  spa_callback_version_min(&(iface)->cb, method_type, vers)
223 
228 #define spa_interface_callback_check(iface,method_type,method,vers) \
229  spa_callback_check(&(iface)->cb, method_type, method, vers)
230 
236 #define spa_interface_call(iface,method_type,method,vers,...) \
237  spa_callbacks_call(&(iface)->cb,method_type,method,vers,##__VA_ARGS__)
238 
246 #define spa_interface_call_res(iface,method_type,res,method,vers,...) \
247  spa_callbacks_call_res(&(iface)->cb,method_type,res,method,vers,##__VA_ARGS__)
248 
331 struct spa_hook_list {
332  struct spa_list list;
333 };
334 
335 
342 struct spa_hook {
343  struct spa_list link;
344  struct spa_callbacks cb;
347  void (*removed) (struct spa_hook *hook);
348  void *priv;
349 };
350 
352 static inline void spa_hook_list_init(struct spa_hook_list *list)
353 {
354  spa_list_init(&list->list);
355 }
356 
357 static inline bool spa_hook_list_is_empty(struct spa_hook_list *list)
358 {
359  return spa_list_is_empty(&list->list);
360 }
361 
363 static inline void spa_hook_list_append(struct spa_hook_list *list,
364  struct spa_hook *hook,
365  const void *funcs, void *data)
366 {
367  spa_zero(*hook);
368  hook->cb = SPA_CALLBACKS_INIT(funcs, data);
369  spa_list_append(&list->list, &hook->link);
370 }
371 
373 static inline void spa_hook_list_prepend(struct spa_hook_list *list,
374  struct spa_hook *hook,
375  const void *funcs, void *data)
376 {
377  spa_zero(*hook);
378  hook->cb = SPA_CALLBACKS_INIT(funcs, data);
379  spa_list_prepend(&list->list, &hook->link);
380 }
381 
383 static inline void spa_hook_remove(struct spa_hook *hook)
384 {
385  if (spa_list_is_initialized(&hook->link))
386  spa_list_remove(&hook->link);
387  if (hook->removed)
388  hook->removed(hook);
389 }
390 
392 static inline void spa_hook_list_clean(struct spa_hook_list *list)
393 {
394  struct spa_hook *h;
395  spa_list_consume(h, &list->list, link)
396  spa_hook_remove(h);
397 }
398 
399 static inline void
401  struct spa_hook_list *save,
402  struct spa_hook *hook,
403  const void *funcs, void *data)
404 {
405  /* init save list and move hooks to it */
406  spa_hook_list_init(save);
407  spa_list_insert_list(&save->list, &list->list);
408  /* init hooks and add single hook */
410  spa_hook_list_append(list, hook, funcs, data);
411 }
412 
413 static inline void
414 spa_hook_list_join(struct spa_hook_list *list,
415  struct spa_hook_list *save)
416 {
417  spa_list_insert_list(&list->list, &save->list);
418 }
419 
420 #define spa_hook_list_call_simple(l,type,method,vers,...) \
421 ({ \
422  struct spa_hook_list *_l = l; \
423  struct spa_hook *_h, *_t; \
424  spa_list_for_each_safe(_h, _t, &_l->list, link) \
425  spa_callbacks_call(&_h->cb,type,method,vers, ## __VA_ARGS__); \
426 })
427 
431 #define spa_hook_list_do_call(l,start,type,method,vers,once,...) \
432 ({ \
433  struct spa_hook_list *_list = l; \
434  struct spa_list *_s = start ? (struct spa_list *)start : &_list->list; \
435  struct spa_hook _cursor = { 0 }, *_ci; \
436  int _count = 0; \
437  spa_list_cursor_start(_cursor, _s, link); \
438  spa_list_for_each_cursor(_ci, _cursor, &_list->list, link) { \
439  if (spa_callbacks_call(&_ci->cb,type,method,vers, ## __VA_ARGS__)) { \
440  _count++; \
441  if (once) \
442  break; \
443  } \
444  } \
445  spa_list_cursor_end(_cursor, link); \
446  _count; \
447 })
448 
453 #define spa_hook_list_call(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,false,##__VA_ARGS__)
459 #define spa_hook_list_call_once(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,true,##__VA_ARGS__)
460 
461 #define spa_hook_list_call_start(l,s,t,m,v,...) spa_hook_list_do_call(l,s,t,m,v,false,##__VA_ARGS__)
462 #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__)
463 
468 #ifdef __cplusplus
469 }
470 #endif
471 
472 #endif /* SPA_HOOK_H */
spa/utils/defs.h
static bool spa_hook_list_is_empty(struct spa_hook_list *list)
Definition: hook.h:366
static 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:372
static void spa_hook_list_join(struct spa_hook_list *list, struct spa_hook_list *save)
Definition: hook.h:423
static void spa_hook_list_init(struct spa_hook_list *list)
Initialize a hook list to the empty list.
Definition: hook.h:361
static void spa_hook_remove(struct spa_hook *hook)
Remove a hook.
Definition: hook.h:392
static void spa_hook_list_clean(struct spa_hook_list *list)
Remove all hooks from the list.
Definition: hook.h:401
static 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:382
static 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:409
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition: hook.h:154
static int spa_list_is_initialized(struct spa_list *list)
Definition: list.h:60
#define spa_list_consume(pos, head, member)
Definition: list.h:112
static void spa_list_init(struct spa_list *list)
Definition: list.h:55
static void spa_list_remove(struct spa_list *elem)
Definition: list.h:85
#define spa_list_prepend(list, item)
Definition: list.h:100
#define spa_list_is_empty(l)
Definition: list.h:65
#define spa_list_append(list, item)
Definition: list.h:97
static void spa_list_insert_list(struct spa_list *list, struct spa_list *other)
Definition: list.h:75
#define spa_zero(x)
Definition: defs.h:425
spa/utils/list.h
Callbacks, contains the structure with functions and the data passed to the functions.
Definition: hook.h:136
const void * funcs
Definition: hook.h:137
void * data
Definition: hook.h:138
A list of hooks.
Definition: hook.h:340
struct spa_list list
Definition: hook.h:341
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:351
void(* removed)(struct spa_hook *hook)
callback and data for the hook list, private to the hook_list implementor
Definition: hook.h:356
struct spa_callbacks cb
Definition: hook.h:353
struct spa_list link
Definition: hook.h:352
void * priv
Definition: hook.h:357
Definition: hook.h:158
uint32_t version
Definition: hook.h:160
const char * type
Definition: hook.h:159
struct spa_callbacks cb
Definition: hook.h:161
Definition: list.h:47