]> code.delx.au - pulseaudio/blob - src/polyp/glib-mainloop.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[pulseaudio] / src / polyp / glib-mainloop.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27
28 #include <polyp/glib-mainloop.h>
29 #include <polypcore/idxset.h>
30 #include <polypcore/xmalloc.h>
31 #include "glib.h"
32 #include <polypcore/util.h>
33
34 struct pa_io_event {
35 pa_glib_mainloop *mainloop;
36 int dead;
37 GIOChannel *io_channel;
38 GSource *source;
39 GIOCondition io_condition;
40 int fd;
41 void (*callback) (pa_mainloop_api*m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata);
42 void *userdata;
43 void (*destroy_callback) (pa_mainloop_api *m, pa_io_event *e, void *userdata);
44 pa_io_event *next, *prev;
45 };
46
47 struct pa_time_event {
48 pa_glib_mainloop *mainloop;
49 int dead;
50 GSource *source;
51 struct timeval timeval;
52 void (*callback) (pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata);
53 void *userdata;
54 void (*destroy_callback) (pa_mainloop_api *m, pa_time_event*e, void *userdata);
55 pa_time_event *next, *prev;
56 };
57
58 struct pa_defer_event {
59 pa_glib_mainloop *mainloop;
60 int dead;
61 GSource *source;
62 void (*callback) (pa_mainloop_api*m, pa_defer_event *e, void *userdata);
63 void *userdata;
64 void (*destroy_callback) (pa_mainloop_api *m, pa_defer_event*e, void *userdata);
65 pa_defer_event *next, *prev;
66 };
67
68 struct pa_glib_mainloop {
69 GMainContext *glib_main_context;
70 pa_mainloop_api api;
71 GSource *cleanup_source;
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;
75 };
76
77 static void schedule_free_dead_events(pa_glib_mainloop *g);
78
79 static void glib_io_enable(pa_io_event*e, pa_io_event_flags_t f);
80
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) {
82 pa_io_event *e;
83 pa_glib_mainloop *g;
84
85 assert(m && m->userdata && fd >= 0 && callback);
86 g = m->userdata;
87
88 e = pa_xmalloc(sizeof(pa_io_event));
89 e->mainloop = m->userdata;
90 e->dead = 0;
91 e->fd = fd;
92 e->callback = callback;
93 e->userdata = userdata;
94 e->destroy_callback = NULL;
95
96 e->io_channel = g_io_channel_unix_new(e->fd);
97 assert(e->io_channel);
98 e->source = NULL;
99 e->io_condition = 0;
100
101 glib_io_enable(e, f);
102
103 e->next = g->io_events;
104 if (e->next) e->next->prev = e;
105 g->io_events = e;
106 e->prev = NULL;
107
108 return e;
109 }
110
111 /* The callback GLIB calls whenever an IO condition is met */
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);
116
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);
121
122 e->callback(&e->mainloop->api, e, e->fd, f, e->userdata);
123 return TRUE;
124 }
125
126 static void glib_io_enable(pa_io_event*e, pa_io_event_flags_t f) {
127 GIOCondition c;
128 assert(e && !e->dead);
129
130 c = (f & PA_IO_EVENT_INPUT ? G_IO_IN : 0) | (f & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0);
131
132 if (c == e->io_condition)
133 return;
134
135 if (e->source) {
136 g_source_destroy(e->source);
137 g_source_unref(e->source);
138 }
139
140 e->source = g_io_create_watch(e->io_channel, c | G_IO_ERR | G_IO_HUP);
141 assert(e->source);
142
143 g_source_set_callback(e->source, (GSourceFunc) io_cb, e, NULL);
144 g_source_attach(e->source, e->mainloop->glib_main_context);
145 g_source_set_priority(e->source, G_PRIORITY_DEFAULT);
146
147 e->io_condition = c;
148 }
149
150 static void glib_io_free(pa_io_event*e) {
151 assert(e && !e->dead);
152
153 if (e->source) {
154 g_source_destroy(e->source);
155 g_source_unref(e->source);
156 e->source = NULL;
157 }
158
159 if (e->prev)
160 e->prev->next = e->next;
161 else
162 e->mainloop->io_events = e->next;
163
164 if (e->next)
165 e->next->prev = e->prev;
166
167 if ((e->next = e->mainloop->dead_io_events))
168 e->next->prev = e;
169
170 e->mainloop->dead_io_events = e;
171 e->prev = NULL;
172
173 e->dead = 1;
174 schedule_free_dead_events(e->mainloop);
175 }
176
177 static void glib_io_set_destroy(pa_io_event*e, void (*callback)(pa_mainloop_api*m, pa_io_event *e, void *userdata)) {
178 assert(e);
179 e->destroy_callback = callback;
180 }
181
182 /* Time sources */
183
184 static void glib_time_restart(pa_time_event*e, const struct timeval *tv);
185
186 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) {
187 pa_glib_mainloop *g;
188 pa_time_event *e;
189
190 assert(m && m->userdata && tv && callback);
191 g = m->userdata;
192
193 e = pa_xmalloc(sizeof(pa_time_event));
194 e->mainloop = g;
195 e->dead = 0;
196 e->callback = callback;
197 e->userdata = userdata;
198 e->destroy_callback = NULL;
199 e->source = NULL;
200
201 glib_time_restart(e, tv);
202
203 e->next = g->time_events;
204 if (e->next) e->next->prev = e;
205 g->time_events = e;
206 e->prev = NULL;
207
208 return e;
209 }
210
211 static guint msec_diff(const struct timeval *a, const struct timeval *b) {
212 guint r;
213 assert(a && b);
214
215 if (a->tv_sec < b->tv_sec)
216 return 0;
217
218 if (a->tv_sec == b->tv_sec && a->tv_sec <= b->tv_sec)
219 return 0;
220
221 r = (a->tv_sec-b->tv_sec)*1000;
222
223 if (a->tv_usec >= b->tv_usec)
224 r += (a->tv_usec - b->tv_usec) / 1000;
225 else
226 r -= (b->tv_usec - a->tv_usec) / 1000;
227
228 return r;
229 }
230
231 static gboolean time_cb(gpointer data) {
232 pa_time_event* e = data;
233 assert(e && e->mainloop && e->source);
234
235 g_source_unref(e->source);
236 e->source = NULL;
237
238 e->callback(&e->mainloop->api, e, &e->timeval, e->userdata);
239 return FALSE;
240 }
241
242 static void glib_time_restart(pa_time_event*e, const struct timeval *tv) {
243 struct timeval now;
244 assert(e && e->mainloop && !e->dead);
245
246 pa_gettimeofday(&now);
247 if (e->source) {
248 g_source_destroy(e->source);
249 g_source_unref(e->source);
250 }
251
252 if (tv) {
253 e->timeval = *tv;
254 e->source = g_timeout_source_new(msec_diff(tv, &now));
255 assert(e->source);
256 g_source_set_callback(e->source, time_cb, e, NULL);
257 g_source_set_priority(e->source, G_PRIORITY_DEFAULT);
258 g_source_attach(e->source, e->mainloop->glib_main_context);
259 } else
260 e->source = NULL;
261 }
262
263 static void glib_time_free(pa_time_event *e) {
264 assert(e && e->mainloop && !e->dead);
265
266 if (e->source) {
267 g_source_destroy(e->source);
268 g_source_unref(e->source);
269 e->source = NULL;
270 }
271
272 if (e->prev)
273 e->prev->next = e->next;
274 else
275 e->mainloop->time_events = e->next;
276
277 if (e->next)
278 e->next->prev = e->prev;
279
280 if ((e->next = e->mainloop->dead_time_events))
281 e->next->prev = e;
282
283 e->mainloop->dead_time_events = e;
284 e->prev = NULL;
285
286 e->dead = 1;
287 schedule_free_dead_events(e->mainloop);
288 }
289
290 static void glib_time_set_destroy(pa_time_event *e, void (*callback)(pa_mainloop_api*m, pa_time_event*e, void *userdata)) {
291 assert(e);
292 e->destroy_callback = callback;
293 }
294
295 /* Deferred sources */
296
297 static void glib_defer_enable(pa_defer_event *e, int b);
298
299 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) {
300 pa_defer_event *e;
301 pa_glib_mainloop *g;
302
303 assert(m && m->userdata && callback);
304 g = m->userdata;
305
306 e = pa_xmalloc(sizeof(pa_defer_event));
307 e->mainloop = g;
308 e->dead = 0;
309 e->callback = callback;
310 e->userdata = userdata;
311 e->destroy_callback = NULL;
312 e->source = NULL;
313
314 glib_defer_enable(e, 1);
315
316 e->next = g->defer_events;
317 if (e->next) e->next->prev = e;
318 g->defer_events = e;
319 e->prev = NULL;
320 return e;
321 }
322
323 static gboolean idle_cb(gpointer data) {
324 pa_defer_event* e = data;
325 assert(e && e->mainloop && e->source);
326
327 e->callback(&e->mainloop->api, e, e->userdata);
328 return TRUE;
329 }
330
331 static void glib_defer_enable(pa_defer_event *e, int b) {
332 assert(e && e->mainloop);
333
334 if (e->source && !b) {
335 g_source_destroy(e->source);
336 g_source_unref(e->source);
337 e->source = NULL;
338 } else if (!e->source && b) {
339 e->source = g_idle_source_new();
340 assert(e->source);
341 g_source_set_callback(e->source, idle_cb, e, NULL);
342 g_source_attach(e->source, e->mainloop->glib_main_context);
343 g_source_set_priority(e->source, G_PRIORITY_HIGH);
344 }
345 }
346
347 static void glib_defer_free(pa_defer_event *e) {
348 assert(e && e->mainloop && !e->dead);
349
350 if (e->source) {
351 g_source_destroy(e->source);
352 g_source_unref(e->source);
353 e->source = NULL;
354 }
355
356 if (e->prev)
357 e->prev->next = e->next;
358 else
359 e->mainloop->defer_events = e->next;
360
361 if (e->next)
362 e->next->prev = e->prev;
363
364 if ((e->next = e->mainloop->dead_defer_events))
365 e->next->prev = e;
366
367 e->mainloop->dead_defer_events = e;
368 e->prev = NULL;
369
370 e->dead = 1;
371 schedule_free_dead_events(e->mainloop);
372 }
373
374 static void glib_defer_set_destroy(pa_defer_event *e, void (*callback)(pa_mainloop_api *m, pa_defer_event *e, void *userdata)) {
375 assert(e);
376 e->destroy_callback = callback;
377 }
378
379 /* quit() */
380
381 static void glib_quit(pa_mainloop_api*a, PA_GCC_UNUSED int retval) {
382 pa_glib_mainloop *g;
383 assert(a && a->userdata);
384 g = a->userdata;
385
386 /* NOOP */
387 }
388
389 static const pa_mainloop_api vtable = {
390 .userdata = NULL,
391
392 .io_new = glib_io_new,
393 .io_enable = glib_io_enable,
394 .io_free = glib_io_free,
395 .io_set_destroy= glib_io_set_destroy,
396
397 .time_new = glib_time_new,
398 .time_restart = glib_time_restart,
399 .time_free = glib_time_free,
400 .time_set_destroy = glib_time_set_destroy,
401
402 .defer_new = glib_defer_new,
403 .defer_enable = glib_defer_enable,
404 .defer_free = glib_defer_free,
405 .defer_set_destroy = glib_defer_set_destroy,
406
407 .quit = glib_quit,
408 };
409
410 pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c) {
411 pa_glib_mainloop *g;
412
413 g = pa_xmalloc(sizeof(pa_glib_mainloop));
414 if (c) {
415 g->glib_main_context = c;
416 g_main_context_ref(c);
417 } else
418 g->glib_main_context = g_main_context_default();
419
420 g->api = vtable;
421 g->api.userdata = g;
422
423 g->io_events = g->dead_io_events = NULL;
424 g->time_events = g->dead_time_events = NULL;
425 g->defer_events = g->dead_defer_events = NULL;
426
427 g->cleanup_source = NULL;
428 return g;
429 }
430
431 static void free_io_events(pa_io_event *e) {
432 while (e) {
433 pa_io_event *r = e;
434 e = r->next;
435
436 if (r->source) {
437 g_source_destroy(r->source);
438 g_source_unref(r->source);
439 }
440
441 if (r->io_channel)
442 g_io_channel_unref(r->io_channel);
443
444 if (r->destroy_callback)
445 r->destroy_callback(&r->mainloop->api, r, r->userdata);
446
447 pa_xfree(r);
448 }
449 }
450
451 static void free_time_events(pa_time_event *e) {
452 while (e) {
453 pa_time_event *r = e;
454 e = r->next;
455
456 if (r->source) {
457 g_source_destroy(r->source);
458 g_source_unref(r->source);
459 }
460
461 if (r->destroy_callback)
462 r->destroy_callback(&r->mainloop->api, r, r->userdata);
463
464 pa_xfree(r);
465 }
466 }
467
468 static void free_defer_events(pa_defer_event *e) {
469 while (e) {
470 pa_defer_event *r = e;
471 e = r->next;
472
473 if (r->source) {
474 g_source_destroy(r->source);
475 g_source_unref(r->source);
476 }
477
478 if (r->destroy_callback)
479 r->destroy_callback(&r->mainloop->api, r, r->userdata);
480
481 pa_xfree(r);
482 }
483 }
484
485 void pa_glib_mainloop_free(pa_glib_mainloop* g) {
486 assert(g);
487
488 free_io_events(g->io_events);
489 free_io_events(g->dead_io_events);
490 free_defer_events(g->defer_events);
491 free_defer_events(g->dead_defer_events);
492 free_time_events(g->time_events);
493 free_time_events(g->dead_time_events);
494
495 if (g->cleanup_source) {
496 g_source_destroy(g->cleanup_source);
497 g_source_unref(g->cleanup_source);
498 }
499
500 g_main_context_unref(g->glib_main_context);
501 pa_xfree(g);
502 }
503
504 pa_mainloop_api* pa_glib_mainloop_get_api(pa_glib_mainloop *g) {
505 assert(g);
506 return &g->api;
507 }
508
509 static gboolean free_dead_events(gpointer p) {
510 pa_glib_mainloop *g = p;
511 assert(g);
512
513 free_io_events(g->dead_io_events);
514 free_defer_events(g->dead_defer_events);
515 free_time_events(g->dead_time_events);
516
517 g->dead_io_events = NULL;
518 g->dead_defer_events = NULL;
519 g->dead_time_events = NULL;
520
521 g_source_destroy(g->cleanup_source);
522 g_source_unref(g->cleanup_source);
523 g->cleanup_source = NULL;
524
525 return FALSE;
526 }
527
528 static void schedule_free_dead_events(pa_glib_mainloop *g) {
529 assert(g && g->glib_main_context);
530
531 if (g->cleanup_source)
532 return;
533
534 g->cleanup_source = g_idle_source_new();
535 assert(g->cleanup_source);
536 g_source_set_callback(g->cleanup_source, free_dead_events, g, NULL);
537 g_source_attach(g->cleanup_source, g->glib_main_context);
538 }