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 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 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 Lesser 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
28 #include <polyp/glib-mainloop.h>
29 #include <polypcore/idxset.h>
30 #include <polypcore/xmalloc.h>
31 #include <polypcore/util.h>
33 /* A mainloop implementation based on GLIB 1.2 */
36 pa_glib_mainloop
*mainloop
;
38 GIOChannel
*io_channel
;
40 GIOCondition io_condition
;
42 void (*callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
44 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, void *userdata
);
45 pa_io_event
*next
, *prev
;
48 struct pa_time_event
{
49 pa_glib_mainloop
*mainloop
;
52 struct timeval timeval
;
53 void (*callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
55 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, void *userdata
);
56 pa_time_event
*next
, *prev
;
59 struct pa_defer_event
{
60 pa_glib_mainloop
*mainloop
;
63 void (*callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
65 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
66 pa_defer_event
*next
, *prev
;
69 struct pa_glib_mainloop
{
72 pa_io_event
*io_events
, *dead_io_events
;
73 pa_time_event
*time_events
, *dead_time_events
;
74 pa_defer_event
*defer_events
, *dead_defer_events
;
77 static void schedule_free_dead_events(pa_glib_mainloop
*g
);
79 static void glib_io_enable(pa_io_event
*e
, pa_io_event_flags_t f
);
81 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
) {
85 assert(m
&& m
->userdata
&& fd
>= 0 && callback
);
88 e
= pa_xmalloc(sizeof(pa_io_event
));
89 e
->mainloop
= m
->userdata
;
92 e
->callback
= callback
;
93 e
->userdata
= userdata
;
94 e
->destroy_callback
= NULL
;
96 e
->io_channel
= g_io_channel_unix_new(e
->fd
);
97 assert(e
->io_channel
);
98 e
->source
= (guint
) -1;
101 glib_io_enable(e
, f
);
103 e
->next
= g
->io_events
;
104 if (e
->next
) e
->next
->prev
= e
;
111 static gboolean
io_cb(GIOChannel
*source
, GIOCondition condition
, gpointer data
) {
112 pa_io_event
*e
= data
;
113 pa_io_event_flags_t f
;
114 assert(source
&& e
&& e
->io_channel
== source
);
116 f
= (condition
& G_IO_IN
? PA_IO_EVENT_INPUT
: 0) |
117 (condition
& G_IO_OUT
? PA_IO_EVENT_OUTPUT
: 0) |
118 (condition
& G_IO_ERR
? PA_IO_EVENT_ERROR
: 0) |
119 (condition
& G_IO_HUP
? PA_IO_EVENT_HANGUP
: 0);
121 e
->callback(&e
->mainloop
->api
, e
, e
->fd
, f
, e
->userdata
);
125 static void glib_io_enable(pa_io_event
*e
, pa_io_event_flags_t f
) {
127 assert(e
&& !e
->dead
);
129 c
= (f
& PA_IO_EVENT_INPUT
? G_IO_IN
: 0) | (f
& PA_IO_EVENT_OUTPUT
? G_IO_OUT
: 0);
131 if (c
== e
->io_condition
)
134 if (e
->source
!= (guint
) -1)
135 g_source_remove(e
->source
);
137 e
->source
= g_io_add_watch_full(e
->io_channel
, G_PRIORITY_DEFAULT
, c
| G_IO_ERR
| G_IO_HUP
, io_cb
, e
, NULL
);
138 assert(e
->source
!= (guint
) -1);
142 static void glib_io_free(pa_io_event
*e
) {
143 assert(e
&& !e
->dead
);
145 if (e
->source
!= (guint
) -1) {
146 g_source_remove(e
->source
);
147 e
->source
= (guint
) -1;
151 e
->prev
->next
= e
->next
;
153 e
->mainloop
->io_events
= e
->next
;
156 e
->next
->prev
= e
->prev
;
158 if ((e
->next
= e
->mainloop
->dead_io_events
))
161 e
->mainloop
->dead_io_events
= e
;
165 schedule_free_dead_events(e
->mainloop
);
168 static void glib_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_io_event
*e
, void *userdata
)) {
170 e
->destroy_callback
= callback
;
175 static void glib_time_restart(pa_time_event
*e
, const struct timeval
*tv
);
177 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
) {
181 assert(m
&& m
->userdata
&& tv
&& callback
);
184 e
= pa_xmalloc(sizeof(pa_time_event
));
187 e
->callback
= callback
;
188 e
->userdata
= userdata
;
189 e
->destroy_callback
= NULL
;
190 e
->source
= (guint
) -1;
192 glib_time_restart(e
, tv
);
194 e
->next
= g
->time_events
;
195 if (e
->next
) e
->next
->prev
= e
;
202 static guint
msec_diff(const struct timeval
*a
, const struct timeval
*b
) {
206 if (a
->tv_sec
< b
->tv_sec
)
209 if (a
->tv_sec
== b
->tv_sec
&& a
->tv_sec
<= b
->tv_sec
)
212 r
= (a
->tv_sec
-b
->tv_sec
)*1000;
214 if (a
->tv_usec
>= b
->tv_usec
)
215 r
+= (a
->tv_usec
- b
->tv_usec
) / 1000;
217 r
-= (b
->tv_usec
- a
->tv_usec
) / 1000;
222 static gboolean
time_cb(gpointer data
) {
223 pa_time_event
* e
= data
;
224 assert(e
&& e
->mainloop
&& e
->source
!= (guint
) -1);
226 g_source_remove(e
->source
);
227 e
->source
= (guint
) -1;
229 e
->callback(&e
->mainloop
->api
, e
, &e
->timeval
, e
->userdata
);
233 static void glib_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
235 assert(e
&& e
->mainloop
&& !e
->dead
);
237 pa_gettimeofday(&now
);
238 if (e
->source
!= (guint
) -1)
239 g_source_remove(e
->source
);
243 e
->source
= g_timeout_add_full(G_PRIORITY_DEFAULT
, msec_diff(tv
, &now
), time_cb
, e
, NULL
);
244 assert(e
->source
!= (guint
) -1);
246 e
->source
= (guint
) -1;
249 static void glib_time_free(pa_time_event
*e
) {
250 assert(e
&& e
->mainloop
&& !e
->dead
);
252 if (e
->source
!= (guint
) -1) {
253 g_source_remove(e
->source
);
254 e
->source
= (guint
) -1;
258 e
->prev
->next
= e
->next
;
260 e
->mainloop
->time_events
= e
->next
;
263 e
->next
->prev
= e
->prev
;
265 if ((e
->next
= e
->mainloop
->dead_time_events
))
268 e
->mainloop
->dead_time_events
= e
;
272 schedule_free_dead_events(e
->mainloop
);
275 static void glib_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_time_event
*e
, void *userdata
)) {
277 e
->destroy_callback
= callback
;
280 /* Deferred sources */
282 static void glib_defer_enable(pa_defer_event
*e
, int b
);
284 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
) {
288 assert(m
&& m
->userdata
&& callback
);
291 e
= pa_xmalloc(sizeof(pa_defer_event
));
294 e
->callback
= callback
;
295 e
->userdata
= userdata
;
296 e
->destroy_callback
= NULL
;
297 e
->source
= (guint
) -1;
299 glib_defer_enable(e
, 1);
301 e
->next
= g
->defer_events
;
302 if (e
->next
) e
->next
->prev
= e
;
308 static gboolean
idle_cb(gpointer data
) {
309 pa_defer_event
* e
= data
;
310 assert(e
&& e
->mainloop
&& e
->source
!= (guint
) -1);
312 e
->callback(&e
->mainloop
->api
, e
, e
->userdata
);
316 static void glib_defer_enable(pa_defer_event
*e
, int b
) {
317 assert(e
&& e
->mainloop
);
319 if (e
->source
!= (guint
) -1 && !b
) {
320 g_source_remove(e
->source
);
321 e
->source
= (guint
) -1;
322 } else if (e
->source
== (guint
) -1 && b
) {
323 e
->source
= g_idle_add_full(G_PRIORITY_HIGH
, idle_cb
, e
, NULL
);
324 assert(e
->source
!= (guint
) -1);
328 static void glib_defer_free(pa_defer_event
*e
) {
329 assert(e
&& e
->mainloop
&& !e
->dead
);
331 if (e
->source
!= (guint
) -1) {
332 g_source_remove(e
->source
);
333 e
->source
= (guint
) -1;
337 e
->prev
->next
= e
->next
;
339 e
->mainloop
->defer_events
= e
->next
;
342 e
->next
->prev
= e
->prev
;
344 if ((e
->next
= e
->mainloop
->dead_defer_events
))
347 e
->mainloop
->dead_defer_events
= e
;
351 schedule_free_dead_events(e
->mainloop
);
354 static void glib_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
)) {
356 e
->destroy_callback
= callback
;
361 static void glib_quit(pa_mainloop_api
*a
, PA_GCC_UNUSED
int retval
) {
363 assert(a
&& a
->userdata
);
369 static const pa_mainloop_api vtable
= {
372 .io_new
= glib_io_new
,
373 .io_enable
= glib_io_enable
,
374 .io_free
= glib_io_free
,
375 .io_set_destroy
= glib_io_set_destroy
,
377 .time_new
= glib_time_new
,
378 .time_restart
= glib_time_restart
,
379 .time_free
= glib_time_free
,
380 .time_set_destroy
= glib_time_set_destroy
,
382 .defer_new
= glib_defer_new
,
383 .defer_enable
= glib_defer_enable
,
384 .defer_free
= glib_defer_free
,
385 .defer_set_destroy
= glib_defer_set_destroy
,
390 pa_glib_mainloop
*pa_glib_mainloop_new(void) {
393 g
= pa_xmalloc(sizeof(pa_glib_mainloop
));
398 g
->io_events
= g
->dead_io_events
= NULL
;
399 g
->time_events
= g
->dead_time_events
= NULL
;
400 g
->defer_events
= g
->dead_defer_events
= NULL
;
402 g
->cleanup_source
= (guint
) -1;
406 static void free_io_events(pa_io_event
*e
) {
411 if (r
->source
!= (guint
) -1)
412 g_source_remove(r
->source
);
415 g_io_channel_unref(r
->io_channel
);
417 if (r
->destroy_callback
)
418 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
424 static void free_time_events(pa_time_event
*e
) {
426 pa_time_event
*r
= e
;
429 if (r
->source
!= (guint
) -1)
430 g_source_remove(r
->source
);
432 if (r
->destroy_callback
)
433 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
439 static void free_defer_events(pa_defer_event
*e
) {
441 pa_defer_event
*r
= e
;
444 if (r
->source
!= (guint
) -1)
445 g_source_remove(r
->source
);
447 if (r
->destroy_callback
)
448 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
454 void pa_glib_mainloop_free(pa_glib_mainloop
* g
) {
457 free_io_events(g
->io_events
);
458 free_io_events(g
->dead_io_events
);
459 free_defer_events(g
->defer_events
);
460 free_defer_events(g
->dead_defer_events
);
461 free_time_events(g
->time_events
);
462 free_time_events(g
->dead_time_events
);
464 if (g
->cleanup_source
!= (guint
) -1)
465 g_source_remove(g
->cleanup_source
);
470 pa_mainloop_api
* pa_glib_mainloop_get_api(pa_glib_mainloop
*g
) {
475 static gboolean
free_dead_events(gpointer p
) {
476 pa_glib_mainloop
*g
= p
;
479 free_io_events(g
->dead_io_events
);
480 free_defer_events(g
->dead_defer_events
);
481 free_time_events(g
->dead_time_events
);
483 g
->dead_io_events
= NULL
;
484 g
->dead_defer_events
= NULL
;
485 g
->dead_time_events
= NULL
;
487 g_source_remove(g
->cleanup_source
);
488 g
->cleanup_source
= (guint
) -1;
493 static void schedule_free_dead_events(pa_glib_mainloop
*g
) {
496 if (g
->cleanup_source
!= (guint
) -1)
499 g
->cleanup_source
= g_idle_add_full(G_PRIORITY_HIGH
, free_dead_events
, g
, NULL
);