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