PipeWire  0.3.29
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 
48 struct spa_hook_list {
49  struct spa_list list;
50 };
51 
55 struct spa_callbacks {
56  const void *funcs;
57  void *data;
58 };
59 
61 #define SPA_CALLBACK_CHECK(c,m,v) ((c) && ((v) == 0 || (c)->version > (v)-1) && (c)->m)
62 
63 #define SPA_CALLBACKS_INIT(_funcs,_data) (struct spa_callbacks){ _funcs, _data, }
64 
65 struct spa_interface {
66  const char *type;
67  uint32_t version;
68  struct spa_callbacks cb;
69 };
70 
71 #define SPA_INTERFACE_INIT(_type,_version,_funcs,_data) \
72  (struct spa_interface){ _type, _version, SPA_CALLBACKS_INIT(_funcs,_data), }
73 
76 struct spa_hook {
77  struct spa_list link;
78  struct spa_callbacks cb;
81  void (*removed) (struct spa_hook *hook);
82  void *priv;
83 };
84 
86 /* static */ inline void spa_hook_list_init(struct spa_hook_list *list)
87 {
88  spa_list_init(&list->list);
89 }
90 
91 /* static */ inline bool spa_hook_list_is_empty(struct spa_hook_list *list)
92 {
93  return spa_list_is_empty(&list->list);
94 }
95 
97 /* static */ inline void spa_hook_list_append(struct spa_hook_list *list,
98  struct spa_hook *hook,
99  const void *funcs, void *data)
100 {
101  spa_zero(*hook);
102  hook->cb = SPA_CALLBACKS_INIT(funcs, data);
103  spa_list_append(&list->list, &hook->link);
104 }
105 
107 /* static */ inline void spa_hook_list_prepend(struct spa_hook_list *list,
108  struct spa_hook *hook,
109  const void *funcs, void *data)
110 {
111  spa_zero(*hook);
112  hook->cb = SPA_CALLBACKS_INIT(funcs, data);
113  spa_list_prepend(&list->list, &hook->link);
114 }
115 
117 /* static */ inline void spa_hook_remove(struct spa_hook *hook)
118 {
119  spa_list_remove(&hook->link);
120  if (hook->removed)
121  hook->removed(hook);
122 }
123 
124 /* static */ inline void spa_hook_list_clean(struct spa_hook_list *list)
125 {
126  struct spa_hook *h;
127  spa_list_consume(h, &list->list, link)
128  spa_hook_remove(h);
129 }
130 
131 /* static */ inline void
133  struct spa_hook_list *save,
134  struct spa_hook *hook,
135  const void *funcs, void *data)
136 {
137  /* init save list and move hooks to it */
138  spa_hook_list_init(save);
139  spa_list_insert_list(&save->list, &list->list);
140  /* init hooks and add single hook */
141  spa_hook_list_init(list);
142  spa_hook_list_append(list, hook, funcs, data);
143 }
144 
145 /* static */ inline void
147  struct spa_hook_list *save)
148 {
149  spa_list_insert_list(&list->list, &save->list);
150 }
151 
152 #define spa_callbacks_call(callbacks,type,method,vers,...) \
153 ({ \
154  const type *_f = (const type *) (callbacks)->funcs; \
155  if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \
156  _f->method((callbacks)->data, ## __VA_ARGS__); \
157 })
158 
159 #define spa_callbacks_call_res(callbacks,type,res,method,vers,...) \
160 ({ \
161  const type *_f = (const type *) (callbacks)->funcs; \
162  if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) \
163  res = _f->method((callbacks)->data, ## __VA_ARGS__); \
164  res; \
165 })
166 
167 #define spa_interface_call(iface,type,method,vers,...) \
168  spa_callbacks_call(&(iface)->cb,type,method,vers,##__VA_ARGS__)
169 
170 #define spa_interface_call_res(iface,type,res,method,vers,...) \
171  spa_callbacks_call_res(&(iface)->cb,type,res,method,vers,##__VA_ARGS__)
172 
173 #define spa_hook_list_call_simple(l,type,method,vers,...) \
174 ({ \
175  struct spa_hook_list *_l = l; \
176  struct spa_hook *_h, *_t; \
177  spa_list_for_each_safe(_h, _t, &_l->list, link) \
178  spa_callbacks_call(&_h->cb,type,method,vers, ## __VA_ARGS__); \
179 })
180 
184 #define spa_hook_list_do_call(l,start,type,method,vers,once,...) \
185 ({ \
186  struct spa_hook_list *list = l; \
187  struct spa_list *s = start ? (struct spa_list *)start : &list->list; \
188  struct spa_hook cursor = { 0 }, *ci; \
189  int count = 0; \
190  spa_list_cursor_start(cursor, s, link); \
191  spa_list_for_each_cursor(ci, cursor, &list->list, link) { \
192  const type *_f = (const type *)ci->cb.funcs; \
193  if (SPA_LIKELY(SPA_CALLBACK_CHECK(_f,method,vers))) { \
194  _f->method(ci->cb.data, ## __VA_ARGS__); \
195  count++; \
196  if (once) \
197  break; \
198  } \
199  } \
200  spa_list_cursor_end(cursor, link); \
201  count; \
202 })
203 
204 #define spa_hook_list_call(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,false,##__VA_ARGS__)
205 #define spa_hook_list_call_once(l,t,m,v,...) spa_hook_list_do_call(l,NULL,t,m,v,true,##__VA_ARGS__)
206 
207 #define spa_hook_list_call_start(l,s,t,m,v,...) spa_hook_list_do_call(l,s,t,m,v,false,##__VA_ARGS__)
208 #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__)
209 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif /* SPA_HOOK_H */
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:132
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:107
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:97
void spa_hook_list_init(struct spa_hook_list *list)
Initialize a hook list.
Definition: hook.h:86
#define SPA_CALLBACKS_INIT(_funcs, _data)
Definition: hook.h:63
bool spa_hook_list_is_empty(struct spa_hook_list *list)
Definition: hook.h:91
void spa_hook_remove(struct spa_hook *hook)
Remove a hook.
Definition: hook.h:117
void spa_hook_list_join(struct spa_hook_list *list, struct spa_hook_list *save)
Definition: hook.h:146
void spa_hook_list_clean(struct spa_hook_list *list)
Definition: hook.h:124
void spa_list_init(struct spa_list *list)
Definition: list.h:44
#define spa_zero(x)
Definition: defs.h:288
void spa_list_insert_list(struct spa_list *list, struct spa_list *other)
Definition: list.h:59
#define spa_list_consume(pos, head, member)
Definition: list.h:96
void spa_list_remove(struct spa_list *elem)
Definition: list.h:69
#define spa_list_prepend(list, item)
Definition: list.h:84
#define spa_list_is_empty(l)
Definition: list.h:49
#define spa_list_append(list, item)
Definition: list.h:81
Definition: filter.c:75
Callbacks, contains the structure with functions and the data passed to the functions.
Definition: hook.h:55
const void * funcs
Definition: hook.h:56
void * data
Definition: hook.h:57
A list of hooks.
Definition: hook.h:48
struct spa_list list
Definition: hook.h:49
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:76
void(* removed)(struct spa_hook *hook)
callback and data for the hook list, private to the hook_list implementor
Definition: hook.h:81
struct spa_callbacks cb
Definition: hook.h:78
struct spa_list link
Definition: hook.h:77
void * priv
Definition: hook.h:82
Definition: hook.h:65
uint32_t version
Definition: hook.h:67
const char * type
Definition: hook.h:66
struct spa_callbacks cb
Definition: hook.h:68
Definition: list.h:37