PipeWire: spa/support/loop.h Source File

PipeWire 0.3.51

loop.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_LOOP_H
26#define SPA_LOOP_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include <spa/utils/defs.h>
33#include <spa/utils/hook.h>
34#include <spa/support/system.h>
35
45#define SPA_TYPE_INTERFACE_Loop SPA_TYPE_INFO_INTERFACE_BASE "Loop"
46#define SPA_TYPE_INTERFACE_DataLoop SPA_TYPE_INFO_INTERFACE_BASE "DataLoop"
47#define SPA_VERSION_LOOP 0
48struct spa_loop { struct spa_interface iface; };
49
50#define SPA_TYPE_INTERFACE_LoopControl SPA_TYPE_INFO_INTERFACE_BASE "LoopControl"
51#define SPA_VERSION_LOOP_CONTROL 0
52struct spa_loop_control { struct spa_interface iface; };
53
54#define SPA_TYPE_INTERFACE_LoopUtils SPA_TYPE_INFO_INTERFACE_BASE "LoopUtils"
55#define SPA_VERSION_LOOP_UTILS 0
56 struct spa_loop_utils { struct spa_interface iface; };
57
58struct spa_source;
59
60typedef void (*spa_source_func_t) (struct spa_source *source);
61
62 struct spa_source {
63 struct spa_loop *loop;
64 spa_source_func_t func;
65 void *data;
66 int fd;
67 uint32_t mask;
68 uint32_t rmask;
69 /* private data for the loop implementer */
70 void *priv;
71};
72
73 typedef int (*spa_invoke_func_t) (struct spa_loop *loop,
74 bool async,
75 uint32_t seq,
76 const void *data,
77 size_t size,
78 void *user_data);
79
83 struct spa_loop_methods {
84 /* the version of this structure. This can be used to expand this
85 * structure in the future */
86#define SPA_VERSION_LOOP_METHODS 0
87 uint32_t version;
88
90 int (*add_source) (void *object,
91 struct spa_source *source);
92
94 int (*update_source) (void *object,
95 struct spa_source *source);
96
98 int (*remove_source) (void *object,
99 struct spa_source *source);
100
102 int (*invoke) (void *object,
103 spa_invoke_func_t func,
104 uint32_t seq,
105 const void *data,
106 size_t size,
107 bool block,
108 void *user_data);
109};
110
111#define spa_loop_method(o,method,version,...) \
112({ \
113 int _res = -ENOTSUP; \
114 struct spa_loop *_o = o; \
115 spa_interface_call_res(&_o->iface, \
116 struct spa_loop_methods, _res, \
117 method, version, ##__VA_ARGS__); \
118 _res; \
119})
120
121#define spa_loop_add_source(l,...) spa_loop_method(l,add_source,0,##__VA_ARGS__)
122#define spa_loop_update_source(l,...) spa_loop_method(l,update_source,0,##__VA_ARGS__)
123#define spa_loop_remove_source(l,...) spa_loop_method(l,remove_source,0,##__VA_ARGS__)
124#define spa_loop_invoke(l,...) spa_loop_method(l,invoke,0,##__VA_ARGS__)
125
126
130struct spa_loop_control_hooks {
131#define SPA_VERSION_LOOP_CONTROL_HOOKS 0
132 uint32_t version;
135 void (*before) (void *data);
138 void (*after) (void *data);
139};
140
141#define spa_loop_control_hook_before(l) \
142({ \
143 struct spa_hook_list *_l = l; \
144 struct spa_hook *_h; \
145 spa_list_for_each_reverse(_h, &_l->list, link) \
146 spa_callbacks_call(&_h->cb, struct spa_loop_control_hooks, before, 0); \
147})
148
149#define spa_loop_control_hook_after(l) \
150({ \
151 struct spa_hook_list *_l = l; \
152 struct spa_hook *_h; \
153 spa_list_for_each(_h, &_l->list, link) \
154 spa_callbacks_call(&_h->cb, struct spa_loop_control_hooks, after, 0); \
155})
156
160struct spa_loop_control_methods {
161 /* the version of this structure. This can be used to expand this
162 * structure in the future */
163#define SPA_VERSION_LOOP_CONTROL_METHODS 0
164 uint32_t version;
165
166 int (*get_fd) (void *object);
167
174 void (*add_hook) (void *object,
175 struct spa_hook *hook,
176 const struct spa_loop_control_hooks *hooks,
177 void *data);
178
186 void (*enter) (void *object);
193 void (*leave) (void *object);
194
204 int (*iterate) (void *object, int timeout);
205};
206
207#define spa_loop_control_method_v(o,method,version,...) \
208({ \
209 struct spa_loop_control *_o = o; \
210 spa_interface_call(&_o->iface, \
211 struct spa_loop_control_methods, \
212 method, version, ##__VA_ARGS__); \
213})
214
215#define spa_loop_control_method_r(o,method,version,...) \
216({ \
217 int _res = -ENOTSUP; \
218 struct spa_loop_control *_o = o; \
219 spa_interface_call_res(&_o->iface, \
220 struct spa_loop_control_methods, _res, \
221 method, version, ##__VA_ARGS__); \
222 _res; \
223})
224
225#define spa_loop_control_get_fd(l) spa_loop_control_method_r(l,get_fd,0)
226#define spa_loop_control_add_hook(l,...) spa_loop_control_method_v(l,add_hook,0,__VA_ARGS__)
227#define spa_loop_control_enter(l) spa_loop_control_method_v(l,enter,0)
228#define spa_loop_control_leave(l) spa_loop_control_method_v(l,leave,0)
229#define spa_loop_control_iterate(l,...) spa_loop_control_method_r(l,iterate,0,__VA_ARGS__)
230
231 typedef void (*spa_source_io_func_t) (void *data, int fd, uint32_t mask);
232typedef void (*spa_source_idle_func_t) (void *data);
233typedef void (*spa_source_event_func_t) (void *data, uint64_t count);
234typedef void (*spa_source_timer_func_t) (void *data, uint64_t expirations);
235typedef void (*spa_source_signal_func_t) (void *data, int signal_number);
236
240struct spa_loop_utils_methods {
241 /* the version of this structure. This can be used to expand this
242 * structure in the future */
243#define SPA_VERSION_LOOP_UTILS_METHODS 0
244 uint32_t version;
245
246 struct spa_source *(*add_io) (void *object,
247 int fd,
248 uint32_t mask,
249 bool close,
250 spa_source_io_func_t func, void *data);
251
252 int (*update_io) (void *object, struct spa_source *source, uint32_t mask);
253
254 struct spa_source *(*add_idle) (void *object,
255 bool enabled,
256 spa_source_idle_func_t func, void *data);
257 int (*enable_idle) (void *object, struct spa_source *source, bool enabled);
258
259 struct spa_source *(*add_event) (void *object,
260 spa_source_event_func_t func, void *data);
261 int (*signal_event) (void *object, struct spa_source *source);
262
263 struct spa_source *(*add_timer) (void *object,
264 spa_source_timer_func_t func, void *data);
265 int (*update_timer) (void *object,
266 struct spa_source *source,
267 struct timespec *value,
268 struct timespec *interval,
269 bool absolute);
270 struct spa_source *(*add_signal) (void *object,
271 int signal_number,
272 spa_source_signal_func_t func, void *data);
273
277 void (*destroy_source) (void *object, struct spa_source *source);
278};
279
280#define spa_loop_utils_method_v(o,method,version,...) \
281({ \
282 struct spa_loop_utils *_o = o; \
283 spa_interface_call(&_o->iface, \
284 struct spa_loop_utils_methods, \
285 method, version, ##__VA_ARGS__); \
286})
287
288#define spa_loop_utils_method_r(o,method,version,...) \
289({ \
290 int _res = -ENOTSUP; \
291 struct spa_loop_utils *_o = o; \
292 spa_interface_call_res(&_o->iface, \
293 struct spa_loop_utils_methods, _res, \
294 method, version, ##__VA_ARGS__); \
295 _res; \
296})
297#define spa_loop_utils_method_s(o,method,version,...) \
298({ \
299 struct spa_source *_res = NULL; \
300 struct spa_loop_utils *_o = o; \
301 spa_interface_call_res(&_o->iface, \
302 struct spa_loop_utils_methods, _res, \
303 method, version, ##__VA_ARGS__); \
304 _res; \
305})
306
307
308#define spa_loop_utils_add_io(l,...) spa_loop_utils_method_s(l,add_io,0,__VA_ARGS__)
309#define spa_loop_utils_update_io(l,...) spa_loop_utils_method_r(l,update_io,0,__VA_ARGS__)
310#define spa_loop_utils_add_idle(l,...) spa_loop_utils_method_s(l,add_idle,0,__VA_ARGS__)
311#define spa_loop_utils_enable_idle(l,...) spa_loop_utils_method_r(l,enable_idle,0,__VA_ARGS__)
312#define spa_loop_utils_add_event(l,...) spa_loop_utils_method_s(l,add_event,0,__VA_ARGS__)
313#define spa_loop_utils_signal_event(l,...) spa_loop_utils_method_r(l,signal_event,0,__VA_ARGS__)
314#define spa_loop_utils_add_timer(l,...) spa_loop_utils_method_s(l,add_timer,0,__VA_ARGS__)
315#define spa_loop_utils_update_timer(l,...) spa_loop_utils_method_r(l,update_timer,0,__VA_ARGS__)
316#define spa_loop_utils_add_signal(l,...) spa_loop_utils_method_s(l,add_signal,0,__VA_ARGS__)
317#define spa_loop_utils_destroy_source(l,...) spa_loop_utils_method_v(l,destroy_source,0,__VA_ARGS__)
318
323#ifdef __cplusplus
324} /* extern "C" */
325#endif
326
327#endif /* SPA_LOOP_H */
defs.h
spa/utils/defs.h
spa_source_timer_func_t
void(* spa_source_timer_func_t)(void *data, uint64_t expirations)
Definition: loop.h:268
spa_source_event_func_t
void(* spa_source_event_func_t)(void *data, uint64_t count)
Definition: loop.h:267
spa_source_signal_func_t
void(* spa_source_signal_func_t)(void *data, int signal_number)
Definition: loop.h:269
spa_source_idle_func_t
void(* spa_source_idle_func_t)(void *data)
Definition: loop.h:266
spa_source_func_t
void(* spa_source_func_t)(struct spa_source *source)
Definition: loop.h:73
spa_invoke_func_t
int(* spa_invoke_func_t)(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
Definition: loop.h:86
spa_source_io_func_t
void(* spa_source_io_func_t)(void *data, int fd, uint32_t mask)
Definition: loop.h:265
hook.h
spa/utils/hook.h
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:351
spa_interface
Definition: hook.h:158
spa_loop_control_hooks
Control hooks.
Definition: loop.h:152
spa_loop_control_hooks::before
void(* before)(void *data)
Executed right before waiting for events.
Definition: loop.h:158
spa_loop_control_hooks::version
uint32_t version
Definition: loop.h:155
spa_loop_control_hooks::after
void(* after)(void *data)
Executed right after waiting for events.
Definition: loop.h:161
spa_loop_control_methods
Control an event loop.
Definition: loop.h:183
spa_loop_control_methods::get_fd
int(* get_fd)(void *object)
Definition: loop.h:190
spa_loop_control_methods::iterate
int(* iterate)(void *object, int timeout)
Perform one iteration of the loop.
Definition: loop.h:228
spa_loop_control_methods::enter
void(* enter)(void *object)
Enter a loop.
Definition: loop.h:210
spa_loop_control_methods::add_hook
void(* add_hook)(void *object, struct spa_hook *hook, const struct spa_loop_control_hooks *hooks, void *data)
Add a hook.
Definition: loop.h:198
spa_loop_control_methods::leave
void(* leave)(void *object)
Leave a loop.
Definition: loop.h:217
spa_loop_control_methods::version
uint32_t version
Definition: loop.h:188
spa_loop_control
Definition: loop.h:62
spa_loop_control::iface
struct spa_interface iface
Definition: loop.h:62
spa_loop_methods
Register sources and work items to an event loop.
Definition: loop.h:96
spa_loop_methods::add_source
int(* add_source)(void *object, struct spa_source *source)
add a source to the loop
Definition: loop.h:104
spa_loop_methods::remove_source
int(* remove_source)(void *object, struct spa_source *source)
remove a source from the loop
Definition: loop.h:112
spa_loop_methods::invoke
int(* invoke)(void *object, spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
invoke a function in the context of this loop
Definition: loop.h:116
spa_loop_methods::update_source
int(* update_source)(void *object, struct spa_source *source)
update the source io mask
Definition: loop.h:108
spa_loop_methods::version
uint32_t version
Definition: loop.h:101
spa_loop_utils_methods
Create sources for an event loop.
Definition: loop.h:274
spa_loop_utils_methods::update_io
int(* update_io)(void *object, struct spa_source *source, uint32_t mask)
Definition: loop.h:287
spa_loop_utils_methods::enable_idle
int(* enable_idle)(void *object, struct spa_source *source, bool enabled)
Definition: loop.h:292
spa_loop_utils_methods::signal_event
int(* signal_event)(void *object, struct spa_source *source)
Definition: loop.h:296
spa_loop_utils_methods::update_timer
int(* update_timer)(void *object, struct spa_source *source, struct timespec *value, struct timespec *interval, bool absolute)
Definition: loop.h:300
spa_loop_utils_methods::destroy_source
void(* destroy_source)(void *object, struct spa_source *source)
destroy a source allocated with this interface.
Definition: loop.h:312
spa_loop_utils_methods::version
uint32_t version
Definition: loop.h:279
spa_loop_utils
Definition: loop.h:68
spa_loop_utils::iface
struct spa_interface iface
Definition: loop.h:68
spa_loop
Definition: loop.h:56
spa_loop::iface
struct spa_interface iface
Definition: loop.h:56
spa_source
Definition: loop.h:75
spa_source::rmask
uint32_t rmask
Definition: loop.h:81
spa_source::data
void * data
Definition: loop.h:78
spa_source::priv
void * priv
Definition: loop.h:83
spa_source::mask
uint32_t mask
Definition: loop.h:80
spa_source::func
spa_source_func_t func
Definition: loop.h:77
spa_source::fd
int fd
Definition: loop.h:79
spa_source::loop
struct spa_loop * loop
Definition: loop.h:76
system.h
spa/support/system.h