4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
41 struct pa_base_event {
45 struct pa_mainloop *mainloop;
48 enum pa_io_event_flags events;
49 void (*callback) (struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata);
50 struct pollfd *pollfd;
52 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_io_event *e, void *userdata);
55 struct pa_time_event {
56 struct pa_mainloop *mainloop;
59 struct timeval timeval;
60 void (*callback)(struct pa_mainloop_api*a, struct pa_time_event *e, const struct timeval*tv, void *userdata);
62 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_time_event *e, void *userdata);
65 struct pa_defer_event {
66 struct pa_mainloop *mainloop;
69 void (*callback)(struct pa_mainloop_api*a, struct pa_defer_event*e, void *userdata);
71 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata);
75 struct pa_idxset *io_events, *time_events, *defer_events;
76 int io_events_scan_dead, defer_events_scan_dead, time_events_scan_dead;
78 struct pollfd *pollfds;
79 unsigned max_pollfds, n_pollfds;
82 int quit, running, retval;
83 struct pa_mainloop_api api;
87 static struct pa_io_event* mainloop_io_new(struct pa_mainloop_api*a, int fd, enum pa_io_event_flags events, void (*callback) (struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags events, void *userdata), void *userdata) {
88 struct pa_mainloop *m;
89 struct pa_io_event *e;
91 assert(a && a->userdata && fd >= 0 && callback);
95 e = pa_xmalloc(sizeof(struct pa_io_event));
101 e->callback = callback;
102 e->userdata = userdata;
103 e->destroy_callback = NULL;
106 pa_idxset_put(m->io_events, e, NULL);
107 m->rebuild_pollfds = 1;
111 static void mainloop_io_enable(struct pa_io_event *e, enum pa_io_event_flags events) {
112 assert(e && e->mainloop);
117 (events & PA_IO_EVENT_INPUT ? POLLIN : 0) |
118 (events & PA_IO_EVENT_OUTPUT ? POLLOUT : 0) |
123 static void mainloop_io_free(struct pa_io_event *e) {
124 assert(e && e->mainloop);
125 e->dead = e->mainloop->io_events_scan_dead = e->mainloop->rebuild_pollfds = 1;
128 static void mainloop_io_set_destroy(struct pa_io_event *e, void (*callback)(struct pa_mainloop_api*a, struct pa_io_event *e, void *userdata)) {
130 e->destroy_callback = callback;
134 struct pa_defer_event* mainloop_defer_new(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata), void *userdata) {
135 struct pa_mainloop *m;
136 struct pa_defer_event *e;
138 assert(a && a->userdata && callback);
140 assert(a == &m->api);
142 e = pa_xmalloc(sizeof(struct pa_defer_event));
147 e->callback = callback;
148 e->userdata = userdata;
149 e->destroy_callback = NULL;
151 pa_idxset_put(m->defer_events, e, NULL);
155 static void mainloop_defer_enable(struct pa_defer_event *e, int b) {
160 static void mainloop_defer_free(struct pa_defer_event *e) {
162 e->dead = e->mainloop->defer_events_scan_dead = 1;
165 static void mainloop_defer_set_destroy(struct pa_defer_event *e, void (*callback)(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata)) {
167 e->destroy_callback = callback;
171 static struct pa_time_event* mainloop_time_new(struct pa_mainloop_api*a, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*a, struct pa_time_event*e, const struct timeval *tv, void *userdata), void *userdata) {
172 struct pa_mainloop *m;
173 struct pa_time_event *e;
175 assert(a && a->userdata && callback);
177 assert(a == &m->api);
179 e = pa_xmalloc(sizeof(struct pa_time_event));
187 e->callback = callback;
188 e->userdata = userdata;
189 e->destroy_callback = NULL;
191 pa_idxset_put(m->time_events, e, NULL);
195 static void mainloop_time_restart(struct pa_time_event *e, const struct timeval *tv) {
205 static void mainloop_time_free(struct pa_time_event *e) {
207 e->dead = e->mainloop->time_events_scan_dead = 1;
210 static void mainloop_time_set_destroy(struct pa_time_event *e, void (*callback)(struct pa_mainloop_api*a, struct pa_time_event *e, void *userdata)) {
212 e->destroy_callback = callback;
217 static void mainloop_quit(struct pa_mainloop_api*a, int retval) {
218 struct pa_mainloop *m;
219 assert(a && a->userdata);
221 assert(a == &m->api);
227 static const struct pa_mainloop_api vtable = {
230 io_new: mainloop_io_new,
231 io_enable: mainloop_io_enable,
232 io_free: mainloop_io_free,
233 io_set_destroy: mainloop_io_set_destroy,
235 time_new : mainloop_time_new,
236 time_restart : mainloop_time_restart,
237 time_free : mainloop_time_free,
238 time_set_destroy : mainloop_time_set_destroy,
240 defer_new : mainloop_defer_new,
241 defer_enable : mainloop_defer_enable,
242 defer_free : mainloop_defer_free,
243 defer_set_destroy : mainloop_defer_set_destroy,
245 quit : mainloop_quit,
248 struct pa_mainloop *pa_mainloop_new(void) {
249 struct pa_mainloop *m;
251 m = pa_xmalloc(sizeof(struct pa_mainloop));
253 m->io_events = pa_idxset_new(NULL, NULL);
254 m->defer_events = pa_idxset_new(NULL, NULL);
255 m->time_events = pa_idxset_new(NULL, NULL);
257 assert(m->io_events && m->defer_events && m->time_events);
259 m->io_events_scan_dead = m->defer_events_scan_dead = m->time_events_scan_dead = 0;
262 m->max_pollfds = m->n_pollfds = m->rebuild_pollfds = 0;
264 m->quit = m->running = m->retval = 0;
272 static int io_foreach(void *p, uint32_t index, int *del, void*userdata) {
273 struct pa_io_event *e = p;
275 assert(e && del && all);
277 if (!*all || !e->dead)
280 if (e->destroy_callback)
281 e->destroy_callback(&e->mainloop->api, e, e->userdata);
287 static int time_foreach(void *p, uint32_t index, int *del, void*userdata) {
288 struct pa_time_event *e = p;
290 assert(e && del && all);
292 if (!*all || !e->dead)
295 if (e->destroy_callback)
296 e->destroy_callback(&e->mainloop->api, e, e->userdata);
302 static int defer_foreach(void *p, uint32_t index, int *del, void*userdata) {
303 struct pa_defer_event *e = p;
305 assert(e && del && all);
307 if (!*all || !e->dead)
310 if (e->destroy_callback)
311 e->destroy_callback(&e->mainloop->api, e, e->userdata);
317 void pa_mainloop_free(struct pa_mainloop* m) {
321 pa_idxset_foreach(m->io_events, io_foreach, &all);
322 pa_idxset_foreach(m->time_events, time_foreach, &all);
323 pa_idxset_foreach(m->defer_events, defer_foreach, &all);
325 pa_idxset_free(m->io_events, NULL, NULL);
326 pa_idxset_free(m->time_events, NULL, NULL);
327 pa_idxset_free(m->defer_events, NULL, NULL);
329 pa_xfree(m->pollfds);
333 static void scan_dead(struct pa_mainloop *m) {
336 if (m->io_events_scan_dead)
337 pa_idxset_foreach(m->io_events, io_foreach, &all);
338 if (m->time_events_scan_dead)
339 pa_idxset_foreach(m->time_events, time_foreach, &all);
340 if (m->defer_events_scan_dead)
341 pa_idxset_foreach(m->defer_events, defer_foreach, &all);
344 static void rebuild_pollfds(struct pa_mainloop *m) {
345 struct pa_io_event*e;
347 uint32_t index = PA_IDXSET_INVALID;
350 l = pa_idxset_ncontents(m->io_events);
351 if (m->max_pollfds < l) {
352 m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
358 for (e = pa_idxset_first(m->io_events, &index); e; e = pa_idxset_next(m->io_events, &index)) {
367 ((e->events & PA_IO_EVENT_INPUT) ? POLLIN : 0) |
368 ((e->events & PA_IO_EVENT_OUTPUT) ? POLLOUT : 0) |
378 static void dispatch_pollfds(struct pa_mainloop *m) {
379 uint32_t index = PA_IDXSET_INVALID;
380 struct pa_io_event *e;
382 for (e = pa_idxset_first(m->io_events, &index); e; e = pa_idxset_next(m->io_events, &index)) {
383 if (e->dead || !e->pollfd || !e->pollfd->revents)
386 assert(e->pollfd->fd == e->fd && e->callback);
387 e->callback(&m->api, e, e->fd,
388 (e->pollfd->revents & POLLHUP ? PA_IO_EVENT_HANGUP : 0) |
389 (e->pollfd->revents & POLLIN ? PA_IO_EVENT_INPUT : 0) |
390 (e->pollfd->revents & POLLOUT ? PA_IO_EVENT_OUTPUT : 0) |
391 (e->pollfd->revents & POLLERR ? PA_IO_EVENT_ERROR : 0),
393 e->pollfd->revents = 0;
397 static void dispatch_defer(struct pa_mainloop *m) {
399 struct pa_defer_event *e;
401 for (e = pa_idxset_first(m->defer_events, &index); e; e = pa_idxset_next(m->defer_events, &index)) {
402 if (e->dead || !e->enabled)
406 e->callback(&m->api, e, e->userdata);
410 static int calc_next_timeout(struct pa_mainloop *m) {
412 struct pa_time_event *e;
416 if (pa_idxset_isempty(m->time_events))
419 gettimeofday(&now, NULL);
421 for (e = pa_idxset_first(m->time_events, &index); e; e = pa_idxset_next(m->time_events, &index)) {
424 if (e->dead || !e->enabled)
427 if (e->timeval.tv_sec < now.tv_sec || (e->timeval.tv_sec == now.tv_sec && e->timeval.tv_usec <= now.tv_usec))
430 tmp = (e->timeval.tv_sec - now.tv_sec)*1000;
432 if (e->timeval.tv_usec > now.tv_usec)
433 tmp += (e->timeval.tv_usec - now.tv_usec)/1000;
435 tmp -= (now.tv_usec - e->timeval.tv_usec)/1000;
439 else if (t == -1 || tmp < t)
446 static void dispatch_timeout(struct pa_mainloop *m) {
448 struct pa_time_event *e;
452 if (pa_idxset_isempty(m->time_events))
455 gettimeofday(&now, NULL);
456 for (e = pa_idxset_first(m->time_events, &index); e; e = pa_idxset_next(m->time_events, &index)) {
458 if (e->dead || !e->enabled)
461 if (e->timeval.tv_sec < now.tv_sec || (e->timeval.tv_sec == now.tv_sec && e->timeval.tv_usec <= now.tv_usec)) {
465 e->callback(&m->api, e, &e->timeval, e->userdata);
470 int pa_mainloop_iterate(struct pa_mainloop *m, int block, int *retval) {
472 assert(m && !m->running);
485 if (m->rebuild_pollfds) {
487 m->rebuild_pollfds = 0;
491 int t = block ? calc_next_timeout(m) : 0;
492 /*fprintf(stderr, "%u\n", t);*/
493 r = poll(m->pollfds, m->n_pollfds, t);
494 } while (r < 0 && errno == EINTR);
501 fprintf(stderr, "select(): %s\n", strerror(errno));
504 return r < 0 ? -1 : 0;
507 int pa_mainloop_run(struct pa_mainloop *m, int *retval) {
509 while ((r = pa_mainloop_iterate(m, 1, retval)) == 0);
513 void pa_mainloop_quit(struct pa_mainloop *m, int r) {
518 struct pa_mainloop_api* pa_mainloop_get_api(struct pa_mainloop*m) {