Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulse / glib-mainloop.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <assert.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulse/timeval.h>
32
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/llist.h>
37
38 #include <glib.h>
39 #include "glib-mainloop.h"
40
41 struct pa_io_event  {
42     pa_glib_mainloop *mainloop;
43     int dead;
44
45     GPollFD poll_fd;
46     int poll_fd_added;
47
48     pa_io_event_cb_t callback;
49     void *userdata;
50     pa_io_event_destroy_cb_t destroy_callback;
51
52     PA_LLIST_FIELDS(pa_io_event);
53 };
54
55 struct pa_time_event {
56     pa_glib_mainloop *mainloop;
57     int dead;
58
59     int enabled;
60     struct timeval timeval;
61
62     pa_time_event_cb_t callback;
63     void *userdata;
64     pa_time_event_destroy_cb_t destroy_callback;
65
66     PA_LLIST_FIELDS(pa_time_event);
67 };
68
69 struct pa_defer_event {
70     pa_glib_mainloop *mainloop;
71     int dead;
72
73     int enabled;
74
75     pa_defer_event_cb_t callback;
76     void *userdata;
77     pa_defer_event_destroy_cb_t destroy_callback;
78
79     PA_LLIST_FIELDS(pa_defer_event);
80 };
81
82 struct pa_glib_mainloop {
83     GSource source;
84
85     pa_mainloop_api api;
86     GMainContext *context;
87
88     PA_LLIST_HEAD(pa_io_event, io_events);
89     PA_LLIST_HEAD(pa_time_event, time_events);
90     PA_LLIST_HEAD(pa_defer_event, defer_events);
91
92     int n_enabled_defer_events, n_enabled_time_events;
93     int io_events_please_scan, time_events_please_scan, defer_events_please_scan;
94
95     pa_time_event *cached_next_time_event;
96 };
97
98 static void cleanup_io_events(pa_glib_mainloop *g, int force) {
99     pa_io_event *e;
100
101     e = g->io_events;
102     while (e) {
103         pa_io_event *n = e->next;
104
105         if (!force && g->io_events_please_scan <= 0)
106             break;
107
108         if (force || e->dead) {
109             PA_LLIST_REMOVE(pa_io_event, g->io_events, e);
110
111             if (e->dead) {
112                 g_assert(g->io_events_please_scan > 0);
113                 g->io_events_please_scan--;
114             }
115
116             if (e->poll_fd_added)
117                 g_source_remove_poll(&g->source, &e->poll_fd);
118
119             if (e->destroy_callback)
120                 e->destroy_callback(&g->api, e, e->userdata);
121
122             pa_xfree(e);
123         }
124
125         e = n;
126     }
127
128     g_assert(g->io_events_please_scan == 0);
129 }
130
131 static void cleanup_time_events(pa_glib_mainloop *g, int force) {
132     pa_time_event *e;
133
134     e = g->time_events;
135     while (e) {
136         pa_time_event *n = e->next;
137
138         if (!force && g->time_events_please_scan <= 0)
139             break;
140
141         if (force || e->dead) {
142             PA_LLIST_REMOVE(pa_time_event, g->time_events, e);
143
144             if (e->dead) {
145                 g_assert(g->time_events_please_scan > 0);
146                 g->time_events_please_scan--;
147             }
148
149             if (!e->dead && e->enabled) {
150                 g_assert(g->n_enabled_time_events > 0);
151                 g->n_enabled_time_events--;
152             }
153
154             if (e->destroy_callback)
155                 e->destroy_callback(&g->api, e, e->userdata);
156
157             pa_xfree(e);
158         }
159
160         e = n;
161     }
162
163     g_assert(g->time_events_please_scan == 0);
164 }
165
166 static void cleanup_defer_events(pa_glib_mainloop *g, int force) {
167     pa_defer_event *e;
168
169     e = g->defer_events;
170     while (e) {
171         pa_defer_event *n = e->next;
172
173         if (!force && g->defer_events_please_scan <= 0)
174             break;
175
176         if (force || e->dead) {
177             PA_LLIST_REMOVE(pa_defer_event, g->defer_events, e);
178
179             if (e->dead) {
180                 g_assert(g->defer_events_please_scan > 0);
181                 g->defer_events_please_scan--;
182             }
183
184             if (!e->dead && e->enabled) {
185                 g_assert(g->n_enabled_defer_events > 0);
186                 g->n_enabled_defer_events--;
187             }
188
189             if (e->destroy_callback)
190                 e->destroy_callback(&g->api, e, e->userdata);
191
192             pa_xfree(e);
193         }
194
195         e = n;
196     }
197
198     g_assert(g->defer_events_please_scan == 0);
199 }
200
201 static gushort map_flags_to_glib(pa_io_event_flags_t flags) {
202     return
203         (flags & PA_IO_EVENT_INPUT ? G_IO_IN : 0) |
204         (flags & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0) |
205         (flags & PA_IO_EVENT_ERROR ? G_IO_ERR : 0) |
206         (flags & PA_IO_EVENT_HANGUP ? G_IO_HUP : 0);
207 }
208
209 static pa_io_event_flags_t map_flags_from_glib(gushort flags) {
210     return
211         (flags & G_IO_IN ? PA_IO_EVENT_INPUT : 0) |
212         (flags & G_IO_OUT ? PA_IO_EVENT_OUTPUT : 0) |
213         (flags & G_IO_ERR ? PA_IO_EVENT_ERROR : 0) |
214         (flags & G_IO_HUP ? PA_IO_EVENT_HANGUP : 0);
215 }
216
217 static pa_io_event* glib_io_new(
218         pa_mainloop_api*m,
219         int fd,
220         pa_io_event_flags_t f,
221         pa_io_event_cb_t cb,
222         void *userdata) {
223
224     pa_io_event *e;
225     pa_glib_mainloop *g;
226
227     g_assert(m);
228     g_assert(m->userdata);
229     g_assert(fd >= 0);
230     g_assert(cb);
231
232     g = m->userdata;
233
234     e = pa_xnew(pa_io_event, 1);
235     e->mainloop = g;
236     e->dead = 0;
237
238     e->poll_fd.fd = fd;
239     e->poll_fd.events = map_flags_to_glib(f);
240     e->poll_fd.revents = 0;
241
242     e->callback = cb;
243     e->userdata = userdata;
244     e->destroy_callback = NULL;
245
246     PA_LLIST_PREPEND(pa_io_event, g->io_events, e);
247
248     g_source_add_poll(&g->source, &e->poll_fd);
249     e->poll_fd_added = 1;
250
251     return e;
252 }
253
254 static void glib_io_enable(pa_io_event*e, pa_io_event_flags_t f) {
255     g_assert(e);
256     g_assert(!e->dead);
257
258     e->poll_fd.events = map_flags_to_glib(f);
259 }
260
261 static void glib_io_free(pa_io_event*e) {
262     g_assert(e);
263     g_assert(!e->dead);
264
265     e->dead = 1;
266     e->mainloop->io_events_please_scan++;
267
268     if (e->poll_fd_added) {
269         g_source_remove_poll(&e->mainloop->source, &e->poll_fd);
270         e->poll_fd_added = 0;
271     }
272 }
273
274 static void glib_io_set_destroy(pa_io_event*e, pa_io_event_destroy_cb_t cb) {
275     g_assert(e);
276     g_assert(!e->dead);
277
278     e->destroy_callback = cb;
279 }
280
281 /* Time sources */
282
283 static pa_time_event* glib_time_new(
284         pa_mainloop_api*m,
285         const struct timeval *tv,
286         pa_time_event_cb_t cb,
287         void *userdata) {
288
289     pa_glib_mainloop *g;
290     pa_time_event *e;
291
292     g_assert(m);
293     g_assert(m->userdata);
294     g_assert(cb);
295
296     g = m->userdata;
297
298     e = pa_xnew(pa_time_event, 1);
299     e->mainloop = g;
300     e->dead = 0;
301
302     if ((e->enabled = !!tv)) {
303         e->timeval = *tv;
304         g->n_enabled_time_events++;
305
306         if (g->cached_next_time_event) {
307             g_assert(g->cached_next_time_event->enabled);
308
309             if (pa_timeval_cmp(tv, &g->cached_next_time_event->timeval) < 0)
310                 g->cached_next_time_event = e;
311         }
312     }
313
314     e->callback = cb;
315     e->userdata = userdata;
316     e->destroy_callback = NULL;
317
318     PA_LLIST_PREPEND(pa_time_event, g->time_events, e);
319
320     return e;
321 }
322
323 static void glib_time_restart(pa_time_event*e, const struct timeval *tv) {
324     g_assert(e);
325     g_assert(!e->dead);
326
327     if (e->enabled && !tv) {
328         g_assert(e->mainloop->n_enabled_time_events > 0);
329         e->mainloop->n_enabled_time_events--;
330     } else if (!e->enabled && tv)
331         e->mainloop->n_enabled_time_events++;
332
333     if ((e->enabled = !!tv))
334         e->timeval = *tv;
335
336     if (e->mainloop->cached_next_time_event && e->enabled) {
337         g_assert(e->mainloop->cached_next_time_event->enabled);
338
339         if (pa_timeval_cmp(tv, &e->mainloop->cached_next_time_event->timeval) < 0)
340             e->mainloop->cached_next_time_event = e;
341     } else if (e->mainloop->cached_next_time_event == e)
342         e->mainloop->cached_next_time_event = NULL;
343  }
344
345 static void glib_time_free(pa_time_event *e) {
346     g_assert(e);
347     g_assert(!e->dead);
348
349     e->dead = 1;
350     e->mainloop->time_events_please_scan++;
351
352     if (e->enabled)
353         e->mainloop->n_enabled_time_events--;
354
355     if (e->mainloop->cached_next_time_event == e)
356         e->mainloop->cached_next_time_event = NULL;
357 }
358
359 static void glib_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t cb) {
360     g_assert(e);
361     g_assert(!e->dead);
362
363     e->destroy_callback = cb;
364 }
365
366 /* Deferred sources */
367
368 static pa_defer_event* glib_defer_new(
369         pa_mainloop_api*m,
370         pa_defer_event_cb_t cb,
371         void *userdata) {
372
373     pa_defer_event *e;
374     pa_glib_mainloop *g;
375
376     g_assert(m);
377     g_assert(m->userdata);
378     g_assert(cb);
379
380     g = m->userdata;
381
382     e = pa_xnew(pa_defer_event, 1);
383     e->mainloop = g;
384     e->dead = 0;
385
386     e->enabled = 1;
387     g->n_enabled_defer_events++;
388
389     e->callback = cb;
390     e->userdata = userdata;
391     e->destroy_callback = NULL;
392
393     PA_LLIST_PREPEND(pa_defer_event, g->defer_events, e);
394     return e;
395 }
396
397 static void glib_defer_enable(pa_defer_event *e, int b) {
398     g_assert(e);
399     g_assert(!e->dead);
400
401     if (e->enabled && !b) {
402         g_assert(e->mainloop->n_enabled_defer_events > 0);
403         e->mainloop->n_enabled_defer_events--;
404     } else if (!e->enabled && b)
405         e->mainloop->n_enabled_defer_events++;
406
407     e->enabled = b;
408 }
409
410 static void glib_defer_free(pa_defer_event *e) {
411     g_assert(e);
412     g_assert(!e->dead);
413
414     e->dead = 1;
415     e->mainloop->defer_events_please_scan++;
416
417     if (e->enabled) {
418         g_assert(e->mainloop->n_enabled_defer_events > 0);
419         e->mainloop->n_enabled_defer_events--;
420     }
421 }
422
423 static void glib_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t cb) {
424     g_assert(e);
425     g_assert(!e->dead);
426
427     e->destroy_callback = cb;
428 }
429
430 /* quit() */
431
432 static void glib_quit(pa_mainloop_api*a, PA_GCC_UNUSED int retval) {
433
434     g_warning("quit() ignored");
435
436     /* NOOP */
437 }
438
439 static pa_time_event* find_next_time_event(pa_glib_mainloop *g) {
440     pa_time_event *t, *n = NULL;
441     g_assert(g);
442
443     if (g->cached_next_time_event)
444         return g->cached_next_time_event;
445
446     for (t = g->time_events; t; t = t->next) {
447
448         if (t->dead || !t->enabled)
449             continue;
450
451         if (!n || pa_timeval_cmp(&t->timeval, &n->timeval) < 0) {
452             n = t;
453
454             /* Shortcut for tv = { 0, 0 } */
455             if (n->timeval.tv_sec <= 0)
456                 break;
457         }
458     }
459
460     g->cached_next_time_event = n;
461     return n;
462 }
463
464 static void scan_dead(pa_glib_mainloop *g) {
465     g_assert(g);
466
467     if (g->io_events_please_scan)
468         cleanup_io_events(g, 0);
469
470     if (g->time_events_please_scan)
471         cleanup_time_events(g, 0);
472
473     if (g->defer_events_please_scan)
474         cleanup_defer_events(g, 0);
475 }
476
477 static gboolean prepare_func(GSource *source, gint *timeout) {
478     pa_glib_mainloop *g = (pa_glib_mainloop*) source;
479
480     g_assert(g);
481     g_assert(timeout);
482
483     scan_dead(g);
484
485     if (g->n_enabled_defer_events) {
486         *timeout = 0;
487         return TRUE;
488     } else if (g->n_enabled_time_events) {
489         pa_time_event *t;
490         GTimeVal now;
491         struct timeval tvnow;
492         pa_usec_t usec;
493
494         t = find_next_time_event(g);
495         g_assert(t);
496
497         g_source_get_current_time(source, &now);
498         tvnow.tv_sec = now.tv_sec;
499         tvnow.tv_usec = now.tv_usec;
500
501         if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0) {
502             *timeout = 0;
503             return TRUE;
504         }
505         usec = pa_timeval_diff(&t->timeval, &tvnow);
506         *timeout = (gint) (usec / 1000);
507     } else
508         *timeout = -1;
509
510     return FALSE;
511 }
512 static gboolean check_func(GSource *source) {
513     pa_glib_mainloop *g = (pa_glib_mainloop*) source;
514     pa_io_event *e;
515
516     g_assert(g);
517
518     if (g->n_enabled_defer_events)
519         return TRUE;
520     else if (g->n_enabled_time_events) {
521         pa_time_event *t;
522         GTimeVal now;
523         struct timeval tvnow;
524
525         t = find_next_time_event(g);
526         g_assert(t);
527
528         g_source_get_current_time(source, &now);
529         tvnow.tv_sec = now.tv_sec;
530         tvnow.tv_usec = now.tv_usec;
531
532         if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0)
533             return TRUE;
534     }
535
536     for (e = g->io_events; e; e = e->next)
537         if (!e->dead && e->poll_fd.revents != 0)
538             return TRUE;
539
540     return FALSE;
541 }
542
543 static gboolean dispatch_func(GSource *source, PA_GCC_UNUSED GSourceFunc callback, PA_GCC_UNUSED gpointer userdata) {
544     pa_glib_mainloop *g = (pa_glib_mainloop*) source;
545     pa_io_event *e;
546
547     g_assert(g);
548
549     if (g->n_enabled_defer_events) {
550         pa_defer_event *d;
551
552         for (d = g->defer_events; d; d = d->next) {
553             if (d->dead || !d->enabled)
554                 continue;
555
556             break;
557         }
558
559         g_assert(d);
560
561         d->callback(&g->api, d, d->userdata);
562         return TRUE;
563     }
564
565     if (g->n_enabled_time_events) {
566         GTimeVal now;
567         struct timeval tvnow;
568         pa_time_event *t;
569
570         t = find_next_time_event(g);
571         g_assert(t);
572
573         g_source_get_current_time(source, &now);
574         tvnow.tv_sec = now.tv_sec;
575         tvnow.tv_usec = now.tv_usec;
576
577         if (pa_timeval_cmp(&t->timeval, &tvnow) <= 0) {
578
579             /* Disable time event */
580             glib_time_restart(t, NULL);
581
582             t->callback(&g->api, t, &t->timeval, t->userdata);
583             return TRUE;
584         }
585     }
586
587     for (e = g->io_events; e; e = e->next)
588         if (!e->dead && e->poll_fd.revents != 0) {
589             e->callback(&g->api, e, e->poll_fd.fd, map_flags_from_glib(e->poll_fd.revents), e->userdata);
590             e->poll_fd.revents = 0;
591             return TRUE;
592         }
593
594     return FALSE;
595 }
596
597 static const pa_mainloop_api vtable = {
598     .userdata = NULL,
599
600     .io_new = glib_io_new,
601     .io_enable = glib_io_enable,
602     .io_free = glib_io_free,
603     .io_set_destroy= glib_io_set_destroy,
604
605     .time_new = glib_time_new,
606     .time_restart = glib_time_restart,
607     .time_free = glib_time_free,
608     .time_set_destroy = glib_time_set_destroy,
609
610     .defer_new = glib_defer_new,
611     .defer_enable = glib_defer_enable,
612     .defer_free = glib_defer_free,
613     .defer_set_destroy = glib_defer_set_destroy,
614
615     .quit = glib_quit,
616 };
617
618 pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c) {
619     pa_glib_mainloop *g;
620
621     static GSourceFuncs source_funcs = {
622         prepare_func,
623         check_func,
624         dispatch_func,
625         NULL,
626         NULL,
627         NULL
628     };
629
630     g = (pa_glib_mainloop*) g_source_new(&source_funcs, sizeof(pa_glib_mainloop));
631     g_main_context_ref(g->context = c ? c : g_main_context_default());
632
633     g->api = vtable;
634     g->api.userdata = g;
635
636     PA_LLIST_HEAD_INIT(pa_io_event, g->io_events);
637     PA_LLIST_HEAD_INIT(pa_time_event, g->time_events);
638     PA_LLIST_HEAD_INIT(pa_defer_event, g->defer_events);
639
640     g->n_enabled_defer_events = g->n_enabled_time_events = 0;
641     g->io_events_please_scan = g->time_events_please_scan = g->defer_events_please_scan = 0;
642
643     g->cached_next_time_event = NULL;
644
645     g_source_attach(&g->source, g->context);
646     g_source_set_can_recurse(&g->source, FALSE);
647
648     return g;
649 }
650
651 void pa_glib_mainloop_free(pa_glib_mainloop* g) {
652     g_assert(g);
653
654     cleanup_io_events(g, 1);
655     cleanup_defer_events(g, 1);
656     cleanup_time_events(g, 1);
657
658     g_main_context_unref(g->context);
659     g_source_destroy(&g->source);
660     g_source_unref(&g->source);
661 }
662
663 pa_mainloop_api* pa_glib_mainloop_get_api(pa_glib_mainloop *g) {
664     g_assert(g);
665
666     return &g->api;
667 }