2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulse/xmalloc.h>
27 #include <pulse/timeval.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/llist.h>
34 #include "glib-mainloop.h"
37 pa_glib_mainloop *mainloop;
43 pa_io_event_cb_t callback;
45 pa_io_event_destroy_cb_t destroy_callback;
47 PA_LLIST_FIELDS(pa_io_event);
50 struct pa_time_event {
51 pa_glib_mainloop *mainloop;
55 struct timeval timeval;
57 pa_time_event_cb_t callback;
59 pa_time_event_destroy_cb_t destroy_callback;
61 PA_LLIST_FIELDS(pa_time_event);
64 struct pa_defer_event {
65 pa_glib_mainloop *mainloop;
70 pa_defer_event_cb_t callback;
72 pa_defer_event_destroy_cb_t destroy_callback;
74 PA_LLIST_FIELDS(pa_defer_event);
77 struct pa_glib_mainloop {
81 GMainContext *context;
83 PA_LLIST_HEAD(pa_io_event, io_events);
84 PA_LLIST_HEAD(pa_time_event, time_events);
85 PA_LLIST_HEAD(pa_defer_event, defer_events);
87 int n_enabled_defer_events, n_enabled_time_events;
88 int io_events_please_scan, time_events_please_scan, defer_events_please_scan;
90 pa_time_event *cached_next_time_event;
93 static void cleanup_io_events(pa_glib_mainloop *g, int force) {
98 pa_io_event *n = e->next;
100 if (!force && g->io_events_please_scan <= 0)
103 if (force || e->dead) {
104 PA_LLIST_REMOVE(pa_io_event, g->io_events, e);
107 g_assert(g->io_events_please_scan > 0);
108 g->io_events_please_scan--;
111 if (e->poll_fd_added)
112 g_source_remove_poll(&g->source, &e->poll_fd);
114 if (e->destroy_callback)
115 e->destroy_callback(&g->api, e, e->userdata);
123 g_assert(g->io_events_please_scan == 0);
126 static void cleanup_time_events(pa_glib_mainloop *g, int force) {
131 pa_time_event *n = e->next;
133 if (!force && g->time_events_please_scan <= 0)
136 if (force || e->dead) {
137 PA_LLIST_REMOVE(pa_time_event, g->time_events, e);
140 g_assert(g->time_events_please_scan > 0);
141 g->time_events_please_scan--;
144 if (!e->dead && e->enabled) {
145 g_assert(g->n_enabled_time_events > 0);
146 g->n_enabled_time_events--;
149 if (e->destroy_callback)
150 e->destroy_callback(&g->api, e, e->userdata);
158 g_assert(g->time_events_please_scan == 0);
161 static void cleanup_defer_events(pa_glib_mainloop *g, int force) {
166 pa_defer_event *n = e->next;
168 if (!force && g->defer_events_please_scan <= 0)
171 if (force || e->dead) {
172 PA_LLIST_REMOVE(pa_defer_event, g->defer_events, e);
175 g_assert(g->defer_events_please_scan > 0);
176 g->defer_events_please_scan--;
179 if (!e->dead && e->enabled) {
180 g_assert(g->n_enabled_defer_events > 0);
181 g->n_enabled_defer_events--;
184 if (e->destroy_callback)
185 e->destroy_callback(&g->api, e, e->userdata);
193 g_assert(g->defer_events_please_scan == 0);
196 static gushort map_flags_to_glib(pa_io_event_flags_t flags) {
198 ((flags & PA_IO_EVENT_INPUT ? G_IO_IN : 0) |
199 (flags & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0) |
200 (flags & PA_IO_EVENT_ERROR ? G_IO_ERR : 0) |
201 (flags & PA_IO_EVENT_HANGUP ? G_IO_HUP : 0));
204 static pa_io_event_flags_t map_flags_from_glib(gushort flags) {
206 (flags & G_IO_IN ? PA_IO_EVENT_INPUT : 0) |
207 (flags & G_IO_OUT ? PA_IO_EVENT_OUTPUT : 0) |
208 (flags & G_IO_ERR ? PA_IO_EVENT_ERROR : 0) |
209 (flags & G_IO_HUP ? PA_IO_EVENT_HANGUP : 0);
212 static pa_io_event* glib_io_new(
215 pa_io_event_flags_t f,
223 g_assert(m->userdata);
229 e = pa_xnew(pa_io_event, 1);
234 e->poll_fd.events = map_flags_to_glib(f);
235 e->poll_fd.revents = 0;
238 e->userdata = userdata;
239 e->destroy_callback = NULL;
241 PA_LLIST_PREPEND(pa_io_event, g->io_events, e);
243 g_source_add_poll(&g->source, &e->poll_fd);
244 e->poll_fd_added = 1;
249 static void glib_io_enable(pa_io_event*e, pa_io_event_flags_t f) {
253 e->poll_fd.events = map_flags_to_glib(f);
256 static void glib_io_free(pa_io_event*e) {
261 e->mainloop->io_events_please_scan++;
263 if (e->poll_fd_added) {
264 g_source_remove_poll(&e->mainloop->source, &e->poll_fd);
265 e->poll_fd_added = 0;
269 static void glib_io_set_destroy(pa_io_event*e, pa_io_event_destroy_cb_t cb) {
273 e->destroy_callback = cb;
278 static pa_time_event* glib_time_new(
280 const struct timeval *tv,
281 pa_time_event_cb_t cb,
288 g_assert(m->userdata);
293 e = pa_xnew(pa_time_event, 1);
297 if ((e->enabled = !!tv)) {
299 g->n_enabled_time_events++;
301 if (g->cached_next_time_event) {
302 g_assert(g->cached_next_time_event->enabled);
304 if (pa_timeval_cmp(tv, &g->cached_next_time_event->timeval) < 0)
305 g->cached_next_time_event = e;
310 e->userdata = userdata;
311 e->destroy_callback = NULL;
313 PA_LLIST_PREPEND(pa_time_event, g->time_events, e);
318 static void glib_time_restart(pa_time_event*e, const struct timeval *tv) {
322 if (e->enabled && !tv) {
323 g_assert(e->mainloop->n_enabled_time_events > 0);
324 e->mainloop->n_enabled_time_events--;
325 } else if (!e->enabled && tv)
326 e->mainloop->n_enabled_time_events++;
328 if ((e->enabled = !!tv))
331 if (e->mainloop->cached_next_time_event == e)
332 e->mainloop->cached_next_time_event = NULL;
334 if (e->mainloop->cached_next_time_event && e->enabled) {
335 g_assert(e->mainloop->cached_next_time_event->enabled);
337 if (pa_timeval_cmp(tv, &e->mainloop->cached_next_time_event->timeval) < 0)
338 e->mainloop->cached_next_time_event = e;
342 static void glib_time_free(pa_time_event *e) {
347 e->mainloop->time_events_please_scan++;
350 e->mainloop->n_enabled_time_events--;
352 if (e->mainloop->cached_next_time_event == e)
353 e->mainloop->cached_next_time_event = NULL;
356 static void glib_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t cb) {
360 e->destroy_callback = cb;
363 /* Deferred sources */
365 static pa_defer_event* glib_defer_new(
367 pa_defer_event_cb_t cb,
374 g_assert(m->userdata);
379 e = pa_xnew(pa_defer_event, 1);
384 g->n_enabled_defer_events++;
387 e->userdata = userdata;
388 e->destroy_callback = NULL;
390 PA_LLIST_PREPEND(pa_defer_event, g->defer_events, e);
394 static void glib_defer_enable(pa_defer_event *e, int b) {
398 if (e->enabled && !b) {
399 g_assert(e->mainloop->n_enabled_defer_events > 0);
400 e->mainloop->n_enabled_defer_events--;
401 } else if (!e->enabled && b)
402 e->mainloop->n_enabled_defer_events++;
407 static void glib_defer_free(pa_defer_event *e) {
412 e->mainloop->defer_events_please_scan++;
415 g_assert(e->mainloop->n_enabled_defer_events > 0);
416 e->mainloop->n_enabled_defer_events--;
420 static void glib_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t cb) {
424 e->destroy_callback = cb;
429 static void glib_quit(pa_mainloop_api*a, int retval) {
431 g_warning("quit() ignored");
436 static pa_time_event* find_next_time_event(pa_glib_mainloop *g) {
437 pa_time_event *t, *n = NULL;
440 if (g->cached_next_time_event)
441 return g->cached_next_time_event;
443 for (t = g->time_events; t; t = t->next) {
445 if (t->dead || !t->enabled)
448 if (!n || pa_timeval_cmp(&t->timeval, &n->timeval) < 0) {
451 /* Shortcut for tv = { 0, 0 } */
452 if (n->timeval.tv_sec <= 0)
457 g->cached_next_time_event = n;
461 static void scan_dead(pa_glib_mainloop *g) {
464 if (g->io_events_please_scan)
465 cleanup_io_events(g, 0);
467 if (g->time_events_please_scan)
468 cleanup_time_events(g, 0);
470 if (g->defer_events_please_scan)
471 cleanup_defer_events(g, 0);
474 static gboolean prepare_func(GSource *source, gint *timeout) {
475 pa_glib_mainloop *g = (pa_glib_mainloop*) source;
482 if (g->n_enabled_defer_events) {
485 } else if (g->n_enabled_time_events) {
488 struct timeval tvnow;
491 t = find_next_time_event(g);
494 g_get_current_time(&now);
495 tvnow.tv_sec = now.tv_sec;
496 tvnow.tv_usec = now.tv_usec;
498 if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0) {
502 usec = pa_timeval_diff(&t->timeval, &tvnow);
503 *timeout = (gint) (usec / 1000);
509 static gboolean check_func(GSource *source) {
510 pa_glib_mainloop *g = (pa_glib_mainloop*) source;
515 if (g->n_enabled_defer_events)
517 else if (g->n_enabled_time_events) {
520 struct timeval tvnow;
522 t = find_next_time_event(g);
525 g_get_current_time(&now);
526 tvnow.tv_sec = now.tv_sec;
527 tvnow.tv_usec = now.tv_usec;
529 if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0)
533 for (e = g->io_events; e; e = e->next)
534 if (!e->dead && e->poll_fd.revents != 0)
540 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer userdata) {
541 pa_glib_mainloop *g = (pa_glib_mainloop*) source;
546 if (g->n_enabled_defer_events) {
549 for (d = g->defer_events; d; d = d->next) {
550 if (d->dead || !d->enabled)
558 d->callback(&g->api, d, d->userdata);
562 if (g->n_enabled_time_events) {
564 struct timeval tvnow;
567 t = find_next_time_event(g);
570 g_get_current_time(&now);
571 tvnow.tv_sec = now.tv_sec;
572 tvnow.tv_usec = now.tv_usec;
574 if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0) {
576 /* Disable time event */
577 glib_time_restart(t, NULL);
579 t->callback(&g->api, t, &t->timeval, t->userdata);
584 for (e = g->io_events; e; e = e->next)
585 if (!e->dead && e->poll_fd.revents != 0) {
586 e->callback(&g->api, e, e->poll_fd.fd, map_flags_from_glib(e->poll_fd.revents), e->userdata);
587 e->poll_fd.revents = 0;
594 static const pa_mainloop_api vtable = {
597 .io_new = glib_io_new,
598 .io_enable = glib_io_enable,
599 .io_free = glib_io_free,
600 .io_set_destroy= glib_io_set_destroy,
602 .time_new = glib_time_new,
603 .time_restart = glib_time_restart,
604 .time_free = glib_time_free,
605 .time_set_destroy = glib_time_set_destroy,
607 .defer_new = glib_defer_new,
608 .defer_enable = glib_defer_enable,
609 .defer_free = glib_defer_free,
610 .defer_set_destroy = glib_defer_set_destroy,
615 pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c) {
618 static GSourceFuncs source_funcs = {
627 g = (pa_glib_mainloop*) g_source_new(&source_funcs, sizeof(pa_glib_mainloop));
628 g_main_context_ref(g->context = c ? c : g_main_context_default());
633 PA_LLIST_HEAD_INIT(pa_io_event, g->io_events);
634 PA_LLIST_HEAD_INIT(pa_time_event, g->time_events);
635 PA_LLIST_HEAD_INIT(pa_defer_event, g->defer_events);
637 g->n_enabled_defer_events = g->n_enabled_time_events = 0;
638 g->io_events_please_scan = g->time_events_please_scan = g->defer_events_please_scan = 0;
640 g->cached_next_time_event = NULL;
642 g_source_attach(&g->source, g->context);
643 g_source_set_can_recurse(&g->source, FALSE);
648 void pa_glib_mainloop_free(pa_glib_mainloop* g) {
651 cleanup_io_events(g, 1);
652 cleanup_defer_events(g, 1);
653 cleanup_time_events(g, 1);
655 g_main_context_unref(g->context);
656 g_source_destroy(&g->source);
657 g_source_unref(&g->source);
660 pa_mainloop_api* pa_glib_mainloop_get_api(pa_glib_mainloop *g) {