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 <polypcore/idxset.h>
29 #include <polypcore/xmalloc.h>
30 #include <polypcore/util.h>
32 #include "glib-mainloop.h"
34 /* A mainloop implementation based on GLIB 1.2 */
37 pa_glib_mainloop
*mainloop
;
39 GIOChannel
*io_channel
;
41 GIOCondition io_condition
;
43 void (*callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
);
45 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_io_event
*e
, void *userdata
);
46 pa_io_event
*next
, *prev
;
49 struct pa_time_event
{
50 pa_glib_mainloop
*mainloop
;
53 struct timeval timeval
;
54 void (*callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
56 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_time_event
*e
, void *userdata
);
57 pa_time_event
*next
, *prev
;
60 struct pa_defer_event
{
61 pa_glib_mainloop
*mainloop
;
64 void (*callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
66 void (*destroy_callback
) (pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
67 pa_defer_event
*next
, *prev
;
70 struct pa_glib_mainloop
{
73 pa_io_event
*io_events
, *dead_io_events
;
74 pa_time_event
*time_events
, *dead_time_events
;
75 pa_defer_event
*defer_events
, *dead_defer_events
;
78 static void schedule_free_dead_events(pa_glib_mainloop
*g
);
80 static void glib_io_enable(pa_io_event
*e
, pa_io_event_flags_t f
);
82 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
) {
86 assert(m
&& m
->userdata
&& fd
>= 0 && callback
);
89 e
= pa_xmalloc(sizeof(pa_io_event
));
90 e
->mainloop
= m
->userdata
;
93 e
->callback
= callback
;
94 e
->userdata
= userdata
;
95 e
->destroy_callback
= NULL
;
97 e
->io_channel
= g_io_channel_unix_new(e
->fd
);
98 assert(e
->io_channel
);
99 e
->source
= (guint
) -1;
102 glib_io_enable(e
, f
);
104 e
->next
= g
->io_events
;
105 if (e
->next
) e
->next
->prev
= e
;
112 static gboolean
io_cb(GIOChannel
*source
, GIOCondition condition
, gpointer data
) {
113 pa_io_event
*e
= data
;
114 pa_io_event_flags_t f
;
115 assert(source
&& e
&& e
->io_channel
== source
);
117 f
= (condition
& G_IO_IN
? PA_IO_EVENT_INPUT
: 0) |
118 (condition
& G_IO_OUT
? PA_IO_EVENT_OUTPUT
: 0) |
119 (condition
& G_IO_ERR
? PA_IO_EVENT_ERROR
: 0) |
120 (condition
& G_IO_HUP
? PA_IO_EVENT_HANGUP
: 0);
122 e
->callback(&e
->mainloop
->api
, e
, e
->fd
, f
, e
->userdata
);
126 static void glib_io_enable(pa_io_event
*e
, pa_io_event_flags_t f
) {
128 assert(e
&& !e
->dead
);
130 c
= (f
& PA_IO_EVENT_INPUT
? G_IO_IN
: 0) | (f
& PA_IO_EVENT_OUTPUT
? G_IO_OUT
: 0);
132 if (c
== e
->io_condition
)
135 if (e
->source
!= (guint
) -1)
136 g_source_remove(e
->source
);
138 e
->source
= g_io_add_watch_full(e
->io_channel
, G_PRIORITY_DEFAULT
, c
| G_IO_ERR
| G_IO_HUP
, io_cb
, e
, NULL
);
139 assert(e
->source
!= (guint
) -1);
143 static void glib_io_free(pa_io_event
*e
) {
144 assert(e
&& !e
->dead
);
146 if (e
->source
!= (guint
) -1) {
147 g_source_remove(e
->source
);
148 e
->source
= (guint
) -1;
152 e
->prev
->next
= e
->next
;
154 e
->mainloop
->io_events
= e
->next
;
157 e
->next
->prev
= e
->prev
;
159 if ((e
->next
= e
->mainloop
->dead_io_events
))
162 e
->mainloop
->dead_io_events
= e
;
166 schedule_free_dead_events(e
->mainloop
);
169 static void glib_io_set_destroy(pa_io_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_io_event
*e
, void *userdata
)) {
171 e
->destroy_callback
= callback
;
176 static void glib_time_restart(pa_time_event
*e
, const struct timeval
*tv
);
178 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
) {
182 assert(m
&& m
->userdata
&& tv
&& callback
);
185 e
= pa_xmalloc(sizeof(pa_time_event
));
188 e
->callback
= callback
;
189 e
->userdata
= userdata
;
190 e
->destroy_callback
= NULL
;
191 e
->source
= (guint
) -1;
193 glib_time_restart(e
, tv
);
195 e
->next
= g
->time_events
;
196 if (e
->next
) e
->next
->prev
= e
;
203 static guint
msec_diff(const struct timeval
*a
, const struct timeval
*b
) {
207 if (a
->tv_sec
< b
->tv_sec
)
210 if (a
->tv_sec
== b
->tv_sec
&& a
->tv_sec
<= b
->tv_sec
)
213 r
= (a
->tv_sec
-b
->tv_sec
)*1000;
215 if (a
->tv_usec
>= b
->tv_usec
)
216 r
+= (a
->tv_usec
- b
->tv_usec
) / 1000;
218 r
-= (b
->tv_usec
- a
->tv_usec
) / 1000;
223 static gboolean
time_cb(gpointer data
) {
224 pa_time_event
* e
= data
;
225 assert(e
&& e
->mainloop
&& e
->source
!= (guint
) -1);
227 g_source_remove(e
->source
);
228 e
->source
= (guint
) -1;
230 e
->callback(&e
->mainloop
->api
, e
, &e
->timeval
, e
->userdata
);
234 static void glib_time_restart(pa_time_event
*e
, const struct timeval
*tv
) {
236 assert(e
&& e
->mainloop
&& !e
->dead
);
238 pa_gettimeofday(&now
);
239 if (e
->source
!= (guint
) -1)
240 g_source_remove(e
->source
);
244 e
->source
= g_timeout_add_full(G_PRIORITY_DEFAULT
, msec_diff(tv
, &now
), time_cb
, e
, NULL
);
245 assert(e
->source
!= (guint
) -1);
247 e
->source
= (guint
) -1;
250 static void glib_time_free(pa_time_event
*e
) {
251 assert(e
&& e
->mainloop
&& !e
->dead
);
253 if (e
->source
!= (guint
) -1) {
254 g_source_remove(e
->source
);
255 e
->source
= (guint
) -1;
259 e
->prev
->next
= e
->next
;
261 e
->mainloop
->time_events
= e
->next
;
264 e
->next
->prev
= e
->prev
;
266 if ((e
->next
= e
->mainloop
->dead_time_events
))
269 e
->mainloop
->dead_time_events
= e
;
273 schedule_free_dead_events(e
->mainloop
);
276 static void glib_time_set_destroy(pa_time_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_time_event
*e
, void *userdata
)) {
278 e
->destroy_callback
= callback
;
281 /* Deferred sources */
283 static void glib_defer_enable(pa_defer_event
*e
, int b
);
285 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
) {
289 assert(m
&& m
->userdata
&& callback
);
292 e
= pa_xmalloc(sizeof(pa_defer_event
));
295 e
->callback
= callback
;
296 e
->userdata
= userdata
;
297 e
->destroy_callback
= NULL
;
298 e
->source
= (guint
) -1;
300 glib_defer_enable(e
, 1);
302 e
->next
= g
->defer_events
;
303 if (e
->next
) e
->next
->prev
= e
;
309 static gboolean
idle_cb(gpointer data
) {
310 pa_defer_event
* e
= data
;
311 assert(e
&& e
->mainloop
&& e
->source
!= (guint
) -1);
313 e
->callback(&e
->mainloop
->api
, e
, e
->userdata
);
317 static void glib_defer_enable(pa_defer_event
*e
, int b
) {
318 assert(e
&& e
->mainloop
);
320 if (e
->source
!= (guint
) -1 && !b
) {
321 g_source_remove(e
->source
);
322 e
->source
= (guint
) -1;
323 } else if (e
->source
== (guint
) -1 && b
) {
324 e
->source
= g_idle_add_full(G_PRIORITY_HIGH
, idle_cb
, e
, NULL
);
325 assert(e
->source
!= (guint
) -1);
329 static void glib_defer_free(pa_defer_event
*e
) {
330 assert(e
&& e
->mainloop
&& !e
->dead
);
332 if (e
->source
!= (guint
) -1) {
333 g_source_remove(e
->source
);
334 e
->source
= (guint
) -1;
338 e
->prev
->next
= e
->next
;
340 e
->mainloop
->defer_events
= e
->next
;
343 e
->next
->prev
= e
->prev
;
345 if ((e
->next
= e
->mainloop
->dead_defer_events
))
348 e
->mainloop
->dead_defer_events
= e
;
352 schedule_free_dead_events(e
->mainloop
);
355 static void glib_defer_set_destroy(pa_defer_event
*e
, void (*callback
)(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
)) {
357 e
->destroy_callback
= callback
;
362 static void glib_quit(pa_mainloop_api
*a
, PA_GCC_UNUSED
int retval
) {
364 assert(a
&& a
->userdata
);
370 static const pa_mainloop_api vtable
= {
373 .io_new
= glib_io_new
,
374 .io_enable
= glib_io_enable
,
375 .io_free
= glib_io_free
,
376 .io_set_destroy
= glib_io_set_destroy
,
378 .time_new
= glib_time_new
,
379 .time_restart
= glib_time_restart
,
380 .time_free
= glib_time_free
,
381 .time_set_destroy
= glib_time_set_destroy
,
383 .defer_new
= glib_defer_new
,
384 .defer_enable
= glib_defer_enable
,
385 .defer_free
= glib_defer_free
,
386 .defer_set_destroy
= glib_defer_set_destroy
,
391 pa_glib_mainloop
*pa_glib_mainloop_new(void) {
394 g
= pa_xmalloc(sizeof(pa_glib_mainloop
));
399 g
->io_events
= g
->dead_io_events
= NULL
;
400 g
->time_events
= g
->dead_time_events
= NULL
;
401 g
->defer_events
= g
->dead_defer_events
= NULL
;
403 g
->cleanup_source
= (guint
) -1;
407 static void free_io_events(pa_io_event
*e
) {
412 if (r
->source
!= (guint
) -1)
413 g_source_remove(r
->source
);
416 g_io_channel_unref(r
->io_channel
);
418 if (r
->destroy_callback
)
419 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
425 static void free_time_events(pa_time_event
*e
) {
427 pa_time_event
*r
= e
;
430 if (r
->source
!= (guint
) -1)
431 g_source_remove(r
->source
);
433 if (r
->destroy_callback
)
434 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
440 static void free_defer_events(pa_defer_event
*e
) {
442 pa_defer_event
*r
= e
;
445 if (r
->source
!= (guint
) -1)
446 g_source_remove(r
->source
);
448 if (r
->destroy_callback
)
449 r
->destroy_callback(&r
->mainloop
->api
, r
, r
->userdata
);
455 void pa_glib_mainloop_free(pa_glib_mainloop
* g
) {
458 free_io_events(g
->io_events
);
459 free_io_events(g
->dead_io_events
);
460 free_defer_events(g
->defer_events
);
461 free_defer_events(g
->dead_defer_events
);
462 free_time_events(g
->time_events
);
463 free_time_events(g
->dead_time_events
);
465 if (g
->cleanup_source
!= (guint
) -1)
466 g_source_remove(g
->cleanup_source
);
471 pa_mainloop_api
* pa_glib_mainloop_get_api(pa_glib_mainloop
*g
) {
476 static gboolean
free_dead_events(gpointer p
) {
477 pa_glib_mainloop
*g
= p
;
480 free_io_events(g
->dead_io_events
);
481 free_defer_events(g
->dead_defer_events
);
482 free_time_events(g
->dead_time_events
);
484 g
->dead_io_events
= NULL
;
485 g
->dead_defer_events
= NULL
;
486 g
->dead_time_events
= NULL
;
488 g_source_remove(g
->cleanup_source
);
489 g
->cleanup_source
= (guint
) -1;
494 static void schedule_free_dead_events(pa_glib_mainloop
*g
) {
497 if (g
->cleanup_source
!= (guint
) -1)
500 g
->cleanup_source
= g_idle_add_full(G_PRIORITY_HIGH
, free_dead_events
, g
, NULL
);