| 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 |
| 29 |
extern "C" { |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <spa/utils/defs.h> |
| 33 |
#include <spa/utils/hook.h> |
| 34 |
#include <spa/support/system.h> |
| 35 |
|
| 36 |
#define SPA_TYPE_INTERFACE_Loop SPA_TYPE_INFO_INTERFACE_BASE "Loop" |
| 37 |
#define SPA_TYPE_INTERFACE_DataLoop SPA_TYPE_INFO_INTERFACE_BASE "DataLoop" |
| 38 |
#define SPA_VERSION_LOOP 0 |
| 39 |
struct spa_loop { struct spa_interface iface; }; |
| 40 |
|
| 41 |
#define SPA_TYPE_INTERFACE_LoopControl SPA_TYPE_INFO_INTERFACE_BASE "LoopControl" |
| 42 |
#define SPA_VERSION_LOOP_CONTROL 0 |
| 43 |
struct spa_loop_control { struct spa_interface iface; }; |
| 44 |
|
| 45 |
#define SPA_TYPE_INTERFACE_LoopUtils SPA_TYPE_INFO_INTERFACE_BASE "LoopUtils" |
| 46 |
#define SPA_VERSION_LOOP_UTILS 0 |
| 47 |
struct spa_loop_utils { struct spa_interface iface; }; |
| 48 |
|
| 49 |
struct spa_source; |
| 50 |
|
| 51 |
typedef void (*spa_source_func_t) (struct spa_source *source); |
| 52 |
|
| 53 |
struct spa_source { |
| 54 |
struct spa_loop *loop; |
| 55 |
spa_source_func_t func; |
| 56 |
void *data; |
| 57 |
int fd; |
| 58 |
uint32_t mask; |
| 59 |
uint32_t rmask; |
| 60 |
}; |
| 61 |
|
| 62 |
typedef int (*spa_invoke_func_t) (struct spa_loop *loop, |
| 63 |
bool async, |
| 64 |
uint32_t seq, |
| 65 |
const void *data, |
| 66 |
size_t size, |
| 67 |
void *user_data); |
| 68 |
|
| 69 |
/** |
| 70 |
* Register sources and work items to an event loop |
| 71 |
*/ |
| 72 |
struct spa_loop_methods { |
| 73 |
/* the version of this structure. This can be used to expand this |
| 74 |
* structure in the future */ |
| 75 |
#define SPA_VERSION_LOOP_METHODS 0 |
| 76 |
uint32_t version; |
| 77 |
|
| 78 |
/** add a source to the loop */ |
| 79 |
int (*add_source) (void *object, |
| 80 |
struct spa_source *source); |
| 81 |
|
| 82 |
/** update the source io mask */ |
| 83 |
int (*update_source) (void *object, |
| 84 |
struct spa_source *source); |
| 85 |
|
| 86 |
/** remove a source from the loop */ |
| 87 |
int (*remove_source) (void *object, |
| 88 |
struct spa_source *source); |
| 89 |
|
| 90 |
/** invoke a function in the context of this loop */ |
| 91 |
int (*invoke) (void *object, |
| 92 |
spa_invoke_func_t func, |
| 93 |
uint32_t seq, |
| 94 |
const void *data, |
| 95 |
size_t size, |
| 96 |
bool block, |
| 97 |
void *user_data); |
| 98 |
}; |
| 99 |
|
| 100 |
#define spa_loop_method(o,method,version,...) \ |
| 101 |
({ \ |
| 102 |
int _res = -ENOTSUP; \ |
| 103 |
struct spa_loop *_o = o; \ |
| 104 |
spa_interface_call_res(&_o->iface, \ |
| 105 |
struct spa_loop_methods, _res, \ |
| 106 |
method, version, ##__VA_ARGS__); \ |
| 107 |
_res; \ |
| 108 |
}) |
| 109 |
|
| 110 |
#define spa_loop_add_source(l,...) spa_loop_method(l,add_source,0,##__VA_ARGS__) |
| 111 |
#define spa_loop_update_source(l,...) spa_loop_method(l,update_source,0,##__VA_ARGS__) |
| 112 |
#define spa_loop_remove_source(l,...) spa_loop_method(l,remove_source,0,##__VA_ARGS__) |
| 113 |
#define spa_loop_invoke(l,...) spa_loop_method(l,invoke,0,##__VA_ARGS__) |
| 114 |
|
| 115 |
|
| 116 |
/** Control hooks. These hooks can't be removed from their |
| 117 |
* callbacks and must be removed from a safe place (when the loop |
| 118 |
* is not running or when it is locked). */ |
| 119 |
struct spa_loop_control_hooks { |
| 120 |
#define SPA_VERSION_LOOP_CONTROL_HOOKS 0 |
| 121 |
uint32_t version; |
| 122 |
/** Executed right before waiting for events. It is typically used to |
| 123 |
* release locks. */ |
| 124 |
void (*before) (void *data); |
| 125 |
/** Executed right after waiting for events. It is typically used to |
| 126 |
* reacquire locks. */ |
| 127 |
void (*after) (void *data); |
| 128 |
}; |
| 129 |
|
| 130 |
#define spa_loop_control_hook_before(l) \ |
| 131 |
({ \ |
| 132 |
struct spa_hook_list *_l = l; \ |
| 133 |
struct spa_hook *_h; \ |
| 134 |
spa_list_for_each_reverse(_h, &_l->list, link) \ |
| 135 |
spa_callbacks_call(&_h->cb, struct spa_loop_control_hooks, before, 0); \ |
| 136 |
}) |
| 137 |
|
| 138 |
#define spa_loop_control_hook_after(l) \ |
| 139 |
({ \ |
| 140 |
struct spa_hook_list *_l = l; \ |
| 141 |
struct spa_hook *_h; \ |
| 142 |
spa_list_for_each(_h, &_l->list, link) \ |
| 143 |
spa_callbacks_call(&_h->cb, struct spa_loop_control_hooks, after, 0); \ |
| 144 |
}) |
| 145 |
|
| 146 |
/** |
| 147 |
* Control an event loop |
| 148 |
*/ |
| 149 |
struct spa_loop_control_methods { |
| 150 |
/* the version of this structure. This can be used to expand this |
| 151 |
* structure in the future */ |
| 152 |
#define SPA_VERSION_LOOP_CONTROL_METHODS 0 |
| 153 |
uint32_t version; |
| 154 |
|
| 155 |
int (*get_fd) (void *object); |
| 156 |
|
| 157 |
/** Add a hook |
| 158 |
* \param ctrl the control to change |
| 159 |
* \param hooks the hooks to add |
| 160 |
* |
| 161 |
* Adds hooks to the loop controlled by \a ctrl. |
| 162 |
*/ |
| 163 |
void (*add_hook) (void *object, |
| 164 |
struct spa_hook *hook, |
| 165 |
const struct spa_loop_control_hooks *hooks, |
| 166 |
void *data); |
| 167 |
|
| 168 |
/** Enter a loop |
| 169 |
* \param ctrl the control |
| 170 |
* |
| 171 |
* Start an iteration of the loop. This function should be called |
| 172 |
* before calling iterate and is typically used to capture the thread |
| 173 |
* that this loop will run in. |
| 174 |
*/ |
| 175 |
void (*enter) (void *object); |
| 176 |
/** Leave a loop |
| 177 |
* \param ctrl the control |
| 178 |
* |
| 179 |
* Ends the iteration of a loop. This should be called after calling |
| 180 |
* iterate. |
| 181 |
*/ |
| 182 |
void (*leave) (void *object); |
| 183 |
|
| 184 |
/** Perform one iteration of the loop. |
| 185 |
* \param ctrl the control |
| 186 |
* \param timeout an optional timeout in milliseconds. |
| 187 |
* 0 for no timeout, -1 for infinite timeout. |
| 188 |
* |
| 189 |
* This function will block |
| 190 |
* up to \a timeout milliseconds and then dispatch the fds with activity. |
| 191 |
* The number of dispatched fds is returned. |
| 192 |
*/ |
| 193 |
int (*iterate) (void *object, int timeout); |
| 194 |
}; |
| 195 |
|
| 196 |
#define spa_loop_control_method_v(o,method,version,...) \ |
| 197 |
({ \ |
| 198 |
struct spa_loop_control *_o = o; \ |
| 199 |
spa_interface_call(&_o->iface, \ |
| 200 |
struct spa_loop_control_methods, \ |
| 201 |
method, version, ##__VA_ARGS__); \ |
| 202 |
}) |
| 203 |
|
| 204 |
#define spa_loop_control_method_r(o,method,version,...) \ |
| 205 |
({ \ |
| 206 |
int _res = -ENOTSUP; \ |
| 207 |
struct spa_loop_control *_o = o; \ |
| 208 |
spa_interface_call_res(&_o->iface, \ |
| 209 |
struct spa_loop_control_methods, _res, \ |
| 210 |
method, version, ##__VA_ARGS__); \ |
| 211 |
_res; \ |
| 212 |
}) |
| 213 |
|
| 214 |
#define spa_loop_control_get_fd(l) spa_loop_control_method_r(l,get_fd,0) |
| 215 |
#define spa_loop_control_add_hook(l,...) spa_loop_control_method_v(l,add_hook,0,__VA_ARGS__) |
| 216 |
#define spa_loop_control_enter(l) spa_loop_control_method_v(l,enter,0) |
| 217 |
#define spa_loop_control_leave(l) spa_loop_control_method_v(l,leave,0) |
| 218 |
#define spa_loop_control_iterate(l,...) spa_loop_control_method_r(l,iterate,0,__VA_ARGS__) |
| 219 |
|
| 220 |
typedef void (*spa_source_io_func_t) (void *data, int fd, uint32_t mask); |
| 221 |
typedef void (*spa_source_idle_func_t) (void *data); |
| 222 |
typedef void (*spa_source_event_func_t) (void *data, uint64_t count); |
| 223 |
typedef void (*spa_source_timer_func_t) (void *data, uint64_t expirations); |
| 224 |
typedef void (*spa_source_signal_func_t) (void *data, int signal_number); |
| 225 |
|
| 226 |
/** |
| 227 |
* Create sources for an event loop |
| 228 |
*/ |
| 229 |
struct spa_loop_utils_methods { |
| 230 |
/* the version of this structure. This can be used to expand this |
| 231 |
* structure in the future */ |
| 232 |
#define SPA_VERSION_LOOP_UTILS_METHODS 0 |
| 233 |
uint32_t version; |
| 234 |
|
| 235 |
struct spa_source *(*add_io) (void *object, |
| 236 |
int fd, |
| 237 |
uint32_t mask, |
| 238 |
bool close, |
| 239 |
spa_source_io_func_t func, void *data); |
| 240 |
|
| 241 |
int (*update_io) (void *object, struct spa_source *source, uint32_t mask); |
| 242 |
|
| 243 |
struct spa_source *(*add_idle) (void *object, |
| 244 |
bool enabled, |
| 245 |
spa_source_idle_func_t func, void *data); |
| 246 |
int (*enable_idle) (void *object, struct spa_source *source, bool enabled); |
| 247 |
|
| 248 |
struct spa_source *(*add_event) (void *object, |
| 249 |
spa_source_event_func_t func, void *data); |
| 250 |
int (*signal_event) (void *object, struct spa_source *source); |
| 251 |
|
| 252 |
struct spa_source *(*add_timer) (void *object, |
| 253 |
spa_source_timer_func_t func, void *data); |
| 254 |
int (*update_timer) (void *object, |
| 255 |
struct spa_source *source, |
| 256 |
struct timespec *value, |
| 257 |
struct timespec *interval, |
| 258 |
bool absolute); |
| 259 |
struct spa_source *(*add_signal) (void *object, |
| 260 |
int signal_number, |
| 261 |
spa_source_signal_func_t func, void *data); |
| 262 |
|
| 263 |
/** destroy a source allocated with this interface. This function |
| 264 |
* should only be called when the loop is not running or from the |
| 265 |
* context of the running loop */ |
| 266 |
void (*destroy_source) (void *object, struct spa_source *source); |
| 267 |
}; |
| 268 |
|
| 269 |
#define spa_loop_utils_method_v(o,method,version,...) \ |
| 270 |
({ \ |
| 271 |
struct spa_loop_utils *_o = o; \ |
| 272 |
spa_interface_call(&_o->iface, \ |
| 273 |
struct spa_loop_utils_methods, \ |
| 274 |
method, version, ##__VA_ARGS__); \ |
| 275 |
}) |
| 276 |
|
| 277 |
#define spa_loop_utils_method_r(o,method,version,...) \ |
| 278 |
({ \ |
| 279 |
int _res = -ENOTSUP; \ |
| 280 |
struct spa_loop_utils *_o = o; \ |
| 281 |
spa_interface_call_res(&_o->iface, \ |
| 282 |
struct spa_loop_utils_methods, _res, \ |
| 283 |
method, version, ##__VA_ARGS__); \ |
| 284 |
_res; \ |
| 285 |
}) |
| 286 |
#define spa_loop_utils_method_s(o,method,version,...) \ |
| 287 |
({ \ |
| 288 |
struct spa_source *_res = NULL; \ |
| 289 |
struct spa_loop_utils *_o = o; \ |
| 290 |
spa_interface_call_res(&_o->iface, \ |
| 291 |
struct spa_loop_utils_methods, _res, \ |
| 292 |
method, version, ##__VA_ARGS__); \ |
| 293 |
_res; \ |
| 294 |
}) |
| 295 |
|
| 296 |
|
| 297 |
#define spa_loop_utils_add_io(l,...) spa_loop_utils_method_s(l,add_io,0,__VA_ARGS__) |
| 298 |
#define spa_loop_utils_update_io(l,...) spa_loop_utils_method_r(l,update_io,0,__VA_ARGS__) |
| 299 |
#define spa_loop_utils_add_idle(l,...) spa_loop_utils_method_s(l,add_idle,0,__VA_ARGS__) |
| 300 |
#define spa_loop_utils_enable_idle(l,...) spa_loop_utils_method_r(l,enable_idle,0,__VA_ARGS__) |
| 301 |
#define spa_loop_utils_add_event(l,...) spa_loop_utils_method_s(l,add_event,0,__VA_ARGS__) |
| 302 |
#define spa_loop_utils_signal_event(l,...) spa_loop_utils_method_r(l,signal_event,0,__VA_ARGS__) |
| 303 |
#define spa_loop_utils_add_timer(l,...) spa_loop_utils_method_s(l,add_timer,0,__VA_ARGS__) |
| 304 |
#define spa_loop_utils_update_timer(l,...) spa_loop_utils_method_r(l,update_timer,0,__VA_ARGS__) |
| 305 |
#define spa_loop_utils_add_signal(l,...) spa_loop_utils_method_s(l,add_signal,0,__VA_ARGS__) |
| 306 |
#define spa_loop_utils_destroy_source(l,...) spa_loop_utils_method_v(l,destroy_source,0,__VA_ARGS__) |
| 307 |
|
| 308 |
#ifdef __cplusplus |
| 309 |
} /* extern "C" */ |
| 310 |
#endif |
| 311 |
|
| 312 |
#endif /* SPA_LOOP_H */ |