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