4 This file is part of PulseAudio.
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 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
28 #include <pulse/timeval.h>
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/idxset.h>
32 #include <pulsecore/core-util.h>
34 #include "glib-mainloop.h"
36 /* A mainloop implementation based on GLIB 1.2 */
39 pa_glib_mainloop
*mainloop
;
41 GIOChannel
*io_channel
;
43 GIOCondition io_condition
;
45 void (*callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
47 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, void *userdata
);
48 pa_io_event
*next
, *prev
;
51 struct pa_time_event
{
52 pa_glib_mainloop
*mainloop
;
55 struct timeval timeval
;
56 void (*callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
58 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, void *userdata
);
59 pa_time_event
*next
, *prev
;
62 struct pa_defer_event
{
63 pa_glib_mainloop
*mainloop
;
66 void (*callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
68 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
69 pa_defer_event
*next
, *prev
;
72 struct pa_glib_mainloop
{
75 pa_io_event
*io_events
, *dead_io_events
;
76 pa_time_event
*time_events
, *dead_time_events
;
77 pa_defer_event
*defer_events
, *dead_defer_events
;
80 static void schedule_free_dead_events(pa_glib_mainloop
*g
);
82 static void glib_io_enable(pa_io_event
*e
, pa_io_event_flags_t f
);
84 static pa_io_event
* glib_io_new(pa_mainloop_api
*m
, int fd
, pa_io_event_flags_t f
, void (*callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
), void *userdata
) {
88 assert(m
&& m
->userdata
&& fd
>= 0 && callback
);
91 e
= pa_xmalloc(sizeof(pa_io_event
));
92 e
->mainloop
= m
->userdata
;
95 e
->callback
= callback
;
96 e
->userdata
= userdata
;
97 e
->destroy_callback
= NULL
;
99 e
->io_channel
= g_io_channel_unix_new(e
->fd
);
100 assert(e
->io_channel
);
101 e
->source
= (guint
) -1;
104 glib_io_enable(e
, f
);
106 e
->next
= g
->io_events
;
107 if (e
->next
) e
->next
->prev
= e
;
114 static gboolean
io_cb(GIOChannel
*source
, GIOCondition condition
, gpointer data
) {
115 pa_io_event
*e
= data
;
116 pa_io_event_flags_t f
;
117 assert(source
&& e
&& e
->io_channel
== source
);
119 f
= (condition
& G_IO_IN
? PA_IO_EVENT_INPUT
: 0) |
120 (condition
& G_IO_OUT
? PA_IO_EVENT_OUTPUT
: 0) |
121 (condition
& G_IO_ERR
? PA_IO_EVENT_ERROR
: 0) |
122 (condition
& G_IO_HUP
? PA_IO_EVENT_HANGUP
: 0);
124 e
->callback(&e
->mainloop
->api
, e
, e
->fd
, f
, e
->userdata
);
128 static void glib_io_enable(pa_io_event
*e
, pa_io_event_flags_t f
) {
130 assert(e
&& !e
->dead
);
132 c
= (f
& PA_IO_EVENT_INPUT
? G_IO_IN
: 0) | (f
& PA_IO_EVENT_OUTPUT
? G_IO_OUT
: 0);
134 if (c
== e
->io_condition
)
137 if (e
->source
!= (guint
) -1)
138 g_source_remove(e
->source
);
140 e
->source
= g_io_add_watch_full(e
->io_channel
, G_PRIORITY_DEFAULT
, c
| G_IO_ERR
| G_IO_HUP
, io_cb
, e
, NULL
);
141 assert(e
->source
!= (guint
) -1);
145 static void glib_io_free(pa_io_event
*e
) {
146 assert(e
&& !e
->dead
);
148 if (e
->source
!= (guint
) -1) {
149 g_source_remove(e
->source
);
150 e
->source
= (guint
) -1;
154 e
->prev
->next
= e
->next
;
156 e
->mainloop
->io_events
= e
->next
;
159 e
->next
->prev
= e
->prev
;
161 if ((e
->next
= e
->mainloop
->dead_io_events
))
164 e
->mainloop
->dead_io_events
= e
;
168 schedule_free_dead_events(e
->mainloop
);
171 static void glib_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_io_event
*e
, void *userdata
)) {
173 e
->destroy_callback
= callback
;
178 static void glib_time_restart(pa_time_event
*e
, const struct timeval
*tv
);
180 static pa_time_event
* glib_time_new(pa_mainloop_api
*m
, const struct timeval
*tv
, void (*callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
), void *userdata
) {
184 assert(m
&& m
->userdata
&& tv
&& callback
);
187 e
= pa_xmalloc(sizeof(pa_time_event
));
190 e
->callback
= callback
;
191 e
->userdata
= userdata
;
192 e
->destroy_callback
= NULL
;
193 e
->source
= (guint
) -1;
195 glib_time_restart(e
, tv
);
197 e
->next
= g
->time_events
;
198 if (e
->next
) e
->next
->prev
= e
;
205 static guint
msec_diff(const struct timeval
*a
, const struct timeval
*b
) {
209 if (a
->tv_sec
< b
->tv_sec
)
212 if (a
->tv_sec
== b
->tv_sec
&& a
->tv_sec
<= b
->tv_sec
)
215 r
= (a
->tv_sec
-b
->tv_sec
)*1000;
217 if (a
->tv_usec
>= b
->tv_usec
)
218 r
+= (a
->tv_usec
- b
->tv_usec
) / 1000;
220 r
-= (b
->tv_usec
- a
->tv_usec
) / 1000;
225 static gboolean
time_cb(gpointer data
) {
226 pa_time_event
* e
= data
;
227 assert(e
&& e
->mainloop
&& e
->source
!= (guint
) -1);
229 g_source_remove(e
->source
);
230 e
->source
= (guint
) -1;
232 e
->callback(&e
->mainloop
->api
, e
, &e
->timeval
, e
->userdata
);
236 static void glib_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
238 assert(e
&& e
->mainloop
&& !e
->dead
);
240 pa_gettimeofday(&now
);
241 if (e
->source
!= (guint
) -1)
242 g_source_remove(e
->source
);
246 e
->source
= g_timeout_add_full(G_PRIORITY_DEFAULT
, msec_diff(tv
, &now
), time_cb
, e
, NULL
);
247 assert(e
->source
!= (guint
) -1);
249 e
->source
= (guint
) -1;
252 static void glib_time_free(pa_time_event
*e
) {
253 assert(e
&& e
->mainloop
&& !e
->dead
);
255 if (e
->source
!= (guint
) -1) {
256 g_source_remove(e
->source
);
257 e
->source
= (guint
) -1;
261 e
->prev
->next
= e
->next
;
263 e
->mainloop
->time_events
= e
->next
;
266 e
->next
->prev
= e
->prev
;
268 if ((e
->next
= e
->mainloop
->dead_time_events
))
271 e
->mainloop
->dead_time_events
= e
;
275 schedule_free_dead_events(e
->mainloop
);
278 static void glib_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_time_event
*e
, void *userdata
)) {
280 e
->destroy_callback
= callback
;
283 /* Deferred sources */
285 static void glib_defer_enable(pa_defer_event
*e
, int b
);
287 static pa_defer_event
* glib_defer_new(pa_mainloop_api
*m
, void (*callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
), void *userdata
) {
291 assert(m
&& m
->userdata
&& callback
);
294 e
= pa_xmalloc(sizeof(pa_defer_event
));
297 e
->callback
= callback
;
298 e
->userdata
= userdata
;
299 e
->destroy_callback
= NULL
;
300 e
->source
= (guint
) -1;
302 glib_defer_enable(e
, 1);
304 e
->next
= g
->defer_events
;
305 if (e
->next
) e
->next
->prev
= e
;
311 static gboolean
idle_cb(gpointer data
) {
312 pa_defer_event
* e
= data
;
313 assert(e
&& e
->mainloop
&& e
->source
!= (guint
) -1);
315 e
->callback(&e
->mainloop
->api
, e
, e
->userdata
);
319 static void glib_defer_enable(pa_defer_event
*e
, int b
) {
320 assert(e
&& e
->mainloop
);
322 if (e
->source
!= (guint
) -1 && !b
) {
323 g_source_remove(e
->source
);
324 e
->source
= (guint
) -1;
325 } else if (e
->source
== (guint
) -1 && b
) {
326 e
->source
= g_idle_add_full(G_PRIORITY_HIGH
, idle_cb
, e
, NULL
);
327 assert(e
->source
!= (guint
) -1);
331 static void glib_defer_free(pa_defer_event
*e
) {
332 assert(e
&& e
->mainloop
&& !e
->dead
);
334 if (e
->source
!= (guint
) -1) {
335 g_source_remove(e
->source
);
336 e
->source
= (guint
) -1;
340 e
->prev
->next
= e
->next
;
342 e
->mainloop
->defer_events
= e
->next
;
345 e
->next
->prev
= e
->prev
;
347 if ((e
->next
= e
->mainloop
->dead_defer_events
))
350 e
->mainloop
->dead_defer_events
= e
;
354 schedule_free_dead_events(e
->mainloop
);
357 static void glib_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
)) {
359 e
->destroy_callback
= callback
;
364 static void glib_quit(pa_mainloop_api
*a
, PA_GCC_UNUSED
int retval
) {
366 assert(a
&& a
->userdata
);
372 static const pa_mainloop_api vtable
= {
375 .io_new
= glib_io_new
,
376 .io_enable
= glib_io_enable
,
377 .io_free
= glib_io_free
,
378 .io_set_destroy
= glib_io_set_destroy
,
380 .time_new
= glib_time_new
,
381 .time_restart
= glib_time_restart
,
382 .time_free
= glib_time_free
,
383 .time_set_destroy
= glib_time_set_destroy
,
385 .defer_new
= glib_defer_new
,
386 .defer_enable
= glib_defer_enable
,
387 .defer_free
= glib_defer_free
,
388 .defer_set_destroy
= glib_defer_set_destroy
,
393 pa_glib_mainloop
*pa_glib_mainloop_new(void) {
396 g
= pa_xmalloc(sizeof(pa_glib_mainloop
));
401 g
->io_events
= g
->dead_io_events
= NULL
;
402 g
->time_events
= g
->dead_time_events
= NULL
;
403 g
->defer_events
= g
->dead_defer_events
= NULL
;
405 g
->cleanup_source
= (guint
) -1;
409 static void free_io_events(pa_io_event
*e
) {
414 if (r
->source
!= (guint
) -1)
415 g_source_remove(r
->source
);
418 g_io_channel_unref(r
->io_channel
);
420 if (r
->destroy_callback
)
421 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
427 static void free_time_events(pa_time_event
*e
) {
429 pa_time_event
*r
= e
;
432 if (r
->source
!= (guint
) -1)
433 g_source_remove(r
->source
);
435 if (r
->destroy_callback
)
436 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
442 static void free_defer_events(pa_defer_event
*e
) {
444 pa_defer_event
*r
= e
;
447 if (r
->source
!= (guint
) -1)
448 g_source_remove(r
->source
);
450 if (r
->destroy_callback
)
451 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
457 void pa_glib_mainloop_free(pa_glib_mainloop
* g
) {
460 free_io_events(g
->io_events
);
461 free_io_events(g
->dead_io_events
);
462 free_defer_events(g
->defer_events
);
463 free_defer_events(g
->dead_defer_events
);
464 free_time_events(g
->time_events
);
465 free_time_events(g
->dead_time_events
);
467 if (g
->cleanup_source
!= (guint
) -1)
468 g_source_remove(g
->cleanup_source
);
473 pa_mainloop_api
* pa_glib_mainloop_get_api(pa_glib_mainloop
*g
) {
478 static gboolean
free_dead_events(gpointer p
) {
479 pa_glib_mainloop
*g
= p
;
482 free_io_events(g
->dead_io_events
);
483 free_defer_events(g
->dead_defer_events
);
484 free_time_events(g
->dead_time_events
);
486 g
->dead_io_events
= NULL
;
487 g
->dead_defer_events
= NULL
;
488 g
->dead_time_events
= NULL
;
490 g_source_remove(g
->cleanup_source
);
491 g
->cleanup_source
= (guint
) -1;
496 static void schedule_free_dead_events(pa_glib_mainloop
*g
) {
499 if (g
->cleanup_source
!= (guint
) -1)
502 g
->cleanup_source
= g_idle_add_full(G_PRIORITY_HIGH
, free_dead_events
, g
, NULL
);