rename configuration file
[profile/ivi/pulseaudio-panda.git] / polyp / 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 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 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 <stdio.h>
27 #include <signal.h>
28 #include <unistd.h>
29 #include <sys/poll.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <fcntl.h>
34 #include <errno.h>
35
36 #include "mainloop.h"
37 #include "util.h"
38 #include "idxset.h"
39
40 struct mainloop_source_header {
41     struct pa_mainloop *mainloop;
42     int dead;
43 };
44     
45 struct mainloop_source_io {
46     struct mainloop_source_header header;
47     
48     int fd;
49     enum pa_mainloop_api_io_events events;
50     void (*callback) (struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata);
51     void *userdata;
52
53     struct pollfd *pollfd;
54 };
55
56 struct mainloop_source_fixed_or_idle {
57     struct mainloop_source_header header;
58     int enabled;
59
60     void (*callback)(struct pa_mainloop_api*a, void *id, void *userdata);
61     void *userdata;
62 };
63
64 struct mainloop_source_time {
65     struct mainloop_source_header header;
66     int enabled;
67     
68     struct timeval timeval;
69     void (*callback)(struct pa_mainloop_api*a, void *id, const struct timeval*tv, void *userdata);
70     void *userdata;
71 };
72
73 struct pa_mainloop {
74     struct pa_idxset *io_sources, *fixed_sources, *idle_sources, *time_sources;
75     int io_sources_scan_dead, fixed_sources_scan_dead, idle_sources_scan_dead, time_sources_scan_dead;
76
77     struct pollfd *pollfds;
78     unsigned max_pollfds, n_pollfds;
79     int rebuild_pollfds;
80
81     int quit, running, retval;
82     struct pa_mainloop_api api;
83 };
84
85 static void setup_api(struct pa_mainloop *m);
86
87 struct pa_mainloop *pa_mainloop_new(void) {
88     struct pa_mainloop *m;
89
90     m = malloc(sizeof(struct pa_mainloop));
91     assert(m);
92
93     m->io_sources = pa_idxset_new(NULL, NULL);
94     m->fixed_sources = pa_idxset_new(NULL, NULL);
95     m->idle_sources = pa_idxset_new(NULL, NULL);
96     m->time_sources = pa_idxset_new(NULL, NULL);
97
98     assert(m->io_sources && m->fixed_sources && m->idle_sources && m->time_sources);
99
100     m->io_sources_scan_dead = m->fixed_sources_scan_dead = m->idle_sources_scan_dead = m->time_sources_scan_dead = 0;
101     
102     m->pollfds = NULL;
103     m->max_pollfds = m->n_pollfds = m->rebuild_pollfds = 0;
104
105     m->quit = m->running = m->retval = 0;
106
107     setup_api(m);
108     
109     return m;
110 }
111
112 static int foreach(void *p, uint32_t index, int *del, void*userdata) {
113     struct mainloop_source_header *h = p;
114     int *all = userdata;
115     assert(p && del && all);
116
117     if (*all || h->dead) {
118         free(h);
119         *del = 1;
120     }
121
122     return 0;
123 };
124
125 void pa_mainloop_free(struct pa_mainloop* m) {
126     int all = 1;
127     assert(m);
128     pa_idxset_foreach(m->io_sources, foreach, &all);
129     pa_idxset_foreach(m->fixed_sources, foreach, &all);
130     pa_idxset_foreach(m->idle_sources, foreach, &all);
131     pa_idxset_foreach(m->time_sources, foreach, &all);
132
133     pa_idxset_free(m->io_sources, NULL, NULL);
134     pa_idxset_free(m->fixed_sources, NULL, NULL);
135     pa_idxset_free(m->idle_sources, NULL, NULL);
136     pa_idxset_free(m->time_sources, NULL, NULL);
137
138     free(m->pollfds);
139     free(m);
140 }
141
142 static void scan_dead(struct pa_mainloop *m) {
143     int all = 0;
144     assert(m);
145     if (m->io_sources_scan_dead)
146         pa_idxset_foreach(m->io_sources, foreach, &all);
147     if (m->fixed_sources_scan_dead)
148         pa_idxset_foreach(m->fixed_sources, foreach, &all);
149     if (m->idle_sources_scan_dead)
150         pa_idxset_foreach(m->idle_sources, foreach, &all);
151     if (m->time_sources_scan_dead)
152         pa_idxset_foreach(m->time_sources, foreach, &all);
153 }
154
155 static void rebuild_pollfds(struct pa_mainloop *m) {
156     struct mainloop_source_io*s;
157     struct pollfd *p;
158     uint32_t index = PA_IDXSET_INVALID;
159     unsigned l;
160
161     l = pa_idxset_ncontents(m->io_sources);
162     if (m->max_pollfds < l) {
163         m->pollfds = realloc(m->pollfds, sizeof(struct pollfd)*l);
164         m->max_pollfds = l;
165     }
166
167     m->n_pollfds = 0;
168     p = m->pollfds;
169     for (s = pa_idxset_first(m->io_sources, &index); s; s = pa_idxset_next(m->io_sources, &index)) {
170         if (s->header.dead) {
171             s->pollfd = NULL;
172             continue;
173         }
174
175         s->pollfd = p;
176         p->fd = s->fd;
177         p->events = ((s->events & PA_MAINLOOP_API_IO_EVENT_INPUT) ? POLLIN : 0) | ((s->events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) ? POLLOUT : 0);
178         p->revents = 0;
179
180         p++;
181         m->n_pollfds++;
182     }
183 }
184
185 static void dispatch_pollfds(struct pa_mainloop *m) {
186     uint32_t index = PA_IDXSET_INVALID;
187     struct mainloop_source_io *s;
188
189     for (s = pa_idxset_first(m->io_sources, &index); s; s = pa_idxset_next(m->io_sources, &index)) {
190         if (s->header.dead || !s->pollfd || !s->pollfd->revents)
191             continue;
192         
193         assert(s->pollfd->fd == s->fd && s->callback);
194         s->callback(&m->api, s, s->fd,
195                     ((s->pollfd->revents & POLLHUP) ? PA_MAINLOOP_API_IO_EVENT_HUP : 0) |
196                     ((s->pollfd->revents & POLLIN) ? PA_MAINLOOP_API_IO_EVENT_INPUT : 0) |
197                     ((s->pollfd->revents & POLLOUT) ? PA_MAINLOOP_API_IO_EVENT_OUTPUT : 0), s->userdata);
198         s->pollfd->revents = 0;
199     }
200 }
201
202 static void run_fixed_or_idle(struct pa_mainloop *m, struct pa_idxset *i) {
203     uint32_t index = PA_IDXSET_INVALID;
204     struct mainloop_source_fixed_or_idle *s;
205
206     for (s = pa_idxset_first(i, &index); s; s = pa_idxset_next(i, &index)) {
207         if (s->header.dead || !s->enabled)
208             continue;
209
210         assert(s->callback);
211         s->callback(&m->api, s, s->userdata);
212     }
213 }
214
215 static int calc_next_timeout(struct pa_mainloop *m) {
216     uint32_t index = PA_IDXSET_INVALID;
217     struct mainloop_source_time *s;
218     struct timeval now;
219     int t = -1;
220
221     if (pa_idxset_isempty(m->time_sources))
222         return -1;
223
224     gettimeofday(&now, NULL);
225     
226     for (s = pa_idxset_first(m->time_sources, &index); s; s = pa_idxset_next(m->time_sources, &index)) {
227         int tmp;
228         
229         if (s->header.dead || !s->enabled)
230             continue;
231
232         if (s->timeval.tv_sec < now.tv_sec || (s->timeval.tv_sec == now.tv_sec && s->timeval.tv_usec <= now.tv_usec)) 
233             return 0;
234
235         tmp = (s->timeval.tv_sec - now.tv_sec)*1000;
236             
237         if (s->timeval.tv_usec > now.tv_usec)
238             tmp += (s->timeval.tv_usec - now.tv_usec)/1000;
239         else
240             tmp -= (now.tv_usec - s->timeval.tv_usec)/1000;
241
242         if (tmp == 0)
243             return 0;
244         else if (t == -1 || tmp < t)
245             t = tmp;
246     }
247
248     return t;
249 }
250
251 static void dispatch_timeout(struct pa_mainloop *m) {
252     uint32_t index = PA_IDXSET_INVALID;
253     struct mainloop_source_time *s;
254     struct timeval now;
255     assert(m);
256
257     if (pa_idxset_isempty(m->time_sources))
258         return;
259
260     gettimeofday(&now, NULL);
261     for (s = pa_idxset_first(m->time_sources, &index); s; s = pa_idxset_next(m->time_sources, &index)) {
262         
263         if (s->header.dead || !s->enabled)
264             continue;
265
266         if (s->timeval.tv_sec < now.tv_sec || (s->timeval.tv_sec == now.tv_sec && s->timeval.tv_usec <= now.tv_usec)) {
267             assert(s->callback);
268
269             s->enabled = 0;
270             s->callback(&m->api, s, &s->timeval, s->userdata);
271         }
272     }
273 }
274
275 static int any_idle_sources(struct pa_mainloop *m) {
276     struct mainloop_source_fixed_or_idle *s;
277     uint32_t index;
278     assert(m);
279     
280     for (s = pa_idxset_first(m->idle_sources, &index); s; s = pa_idxset_next(m->idle_sources, &index))
281         if (!s->header.dead && s->enabled)
282             return 1;
283
284     return 0;
285 }
286
287 int pa_mainloop_iterate(struct pa_mainloop *m, int block, int *retval) {
288     int r, idle;
289     assert(m && !m->running);
290     
291     if(m->quit) {
292         if (retval)
293             *retval = m->retval;
294         return 1;
295     }
296
297     m->running = 1;
298
299     scan_dead(m);
300     run_fixed_or_idle(m, m->fixed_sources);
301
302     if (m->rebuild_pollfds) {
303         rebuild_pollfds(m);
304         m->rebuild_pollfds = 0;
305     }
306
307     idle = any_idle_sources(m);
308
309     do {
310         int t;
311
312         if (!block || idle)
313             t = 0;
314         else 
315             t = calc_next_timeout(m);
316             
317         r = poll(m->pollfds, m->n_pollfds, t);
318     } while (r < 0 && errno == EINTR);
319
320     dispatch_timeout(m);
321     
322     if (r > 0)
323         dispatch_pollfds(m);
324     else if (r == 0 && idle)
325         run_fixed_or_idle(m, m->idle_sources);
326     else if (r < 0)
327         fprintf(stderr, "select(): %s\n", strerror(errno));
328     
329     m->running = 0;
330     return r < 0 ? -1 : 0;
331 }
332
333 int pa_mainloop_run(struct pa_mainloop *m, int *retval) {
334     int r;
335     while ((r = pa_mainloop_iterate(m, 1, retval)) == 0);
336     return r;
337 }
338
339 void pa_mainloop_quit(struct pa_mainloop *m, int r) {
340     assert(m);
341     m->quit = r;
342 }
343
344 /* IO sources */
345 static void* mainloop_source_io(struct pa_mainloop_api*a, int fd, enum pa_mainloop_api_io_events events, void (*callback) (struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata), void *userdata) {
346     struct pa_mainloop *m;
347     struct mainloop_source_io *s;
348     assert(a && a->userdata && fd >= 0 && callback);
349     m = a->userdata;
350     assert(a == &m->api);
351
352     s = malloc(sizeof(struct mainloop_source_io));
353     assert(s);
354     s->header.mainloop = m;
355     s->header.dead = 0;
356
357     s->fd = fd;
358     s->events = events;
359     s->callback = callback;
360     s->userdata = userdata;
361     s->pollfd = NULL;
362
363     pa_idxset_put(m->io_sources, s, NULL);
364     m->rebuild_pollfds = 1;
365     return s;
366 }
367
368 static void mainloop_enable_io(struct pa_mainloop_api*a, void* id, enum pa_mainloop_api_io_events events) {
369     struct pa_mainloop *m;
370     struct mainloop_source_io *s = id;
371     assert(a && a->userdata && s && !s->header.dead);
372     m = a->userdata;
373     assert(a == &m->api && s->header.mainloop == m);
374
375     s->events = events;
376     if (s->pollfd)
377         s->pollfd->events = ((s->events & PA_MAINLOOP_API_IO_EVENT_INPUT) ? POLLIN : 0) | ((s->events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) ? POLLOUT : 0);
378 }
379
380 static void mainloop_cancel_io(struct pa_mainloop_api*a, void* id) {
381     struct pa_mainloop *m;
382     struct mainloop_source_io *s = id;
383     assert(a && a->userdata && s && !s->header.dead);
384     m = a->userdata;
385     assert(a == &m->api && s->header.mainloop == m);
386
387     s->header.dead = 1;
388     m->io_sources_scan_dead = 1;
389     m->rebuild_pollfds = 1;
390 }
391
392 /* Fixed sources */
393 static void* mainloop_source_fixed(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, void *id, void *userdata), void *userdata) {
394     struct pa_mainloop *m;
395     struct mainloop_source_fixed_or_idle *s;
396     assert(a && a->userdata && callback);
397     m = a->userdata;
398     assert(a == &m->api);
399
400     s = malloc(sizeof(struct mainloop_source_fixed_or_idle));
401     assert(s);
402     s->header.mainloop = m;
403     s->header.dead = 0;
404
405     s->enabled = 1;
406     s->callback = callback;
407     s->userdata = userdata;
408
409     pa_idxset_put(m->fixed_sources, s, NULL);
410     return s;
411 }
412
413 static void mainloop_enable_fixed(struct pa_mainloop_api*a, void* id, int b) {
414     struct pa_mainloop *m;
415     struct mainloop_source_fixed_or_idle *s = id;
416     assert(a && a->userdata && s && !s->header.dead);
417     m = a->userdata;
418     assert(a == &m->api);
419
420     s->enabled = b;
421 }
422
423 static void mainloop_cancel_fixed(struct pa_mainloop_api*a, void* id) {
424     struct pa_mainloop *m;
425     struct mainloop_source_fixed_or_idle *s = id;
426     assert(a && a->userdata && s && !s->header.dead);
427     m = a->userdata;
428     assert(a == &m->api);
429
430     s->header.dead = 1;
431     m->fixed_sources_scan_dead = 1;
432 }
433
434 /* Idle sources */
435 static void* mainloop_source_idle(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, void *id, void *userdata), void *userdata) {
436     struct pa_mainloop *m;
437     struct mainloop_source_fixed_or_idle *s;
438     assert(a && a->userdata && callback);
439     m = a->userdata;
440     assert(a == &m->api);
441
442     s = malloc(sizeof(struct mainloop_source_fixed_or_idle));
443     assert(s);
444     s->header.mainloop = m;
445     s->header.dead = 0;
446
447     s->enabled = 1;
448     s->callback = callback;
449     s->userdata = userdata;
450
451     pa_idxset_put(m->idle_sources, s, NULL);
452     return s;
453 }
454
455 static void mainloop_cancel_idle(struct pa_mainloop_api*a, void* id) {
456     struct pa_mainloop *m;
457     struct mainloop_source_fixed_or_idle *s = id;
458     assert(a && a->userdata && s && !s->header.dead);
459     m = a->userdata;
460     assert(a == &m->api);
461
462     s->header.dead = 1;
463     m->idle_sources_scan_dead = 1;
464 }
465
466 /* Time sources */
467 static void* mainloop_source_time(struct pa_mainloop_api*a, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*a, void *id, const struct timeval *tv, void *userdata), void *userdata) {
468     struct pa_mainloop *m;
469     struct mainloop_source_time *s;
470     assert(a && a->userdata && callback);
471     m = a->userdata;
472     assert(a == &m->api);
473
474     s = malloc(sizeof(struct mainloop_source_time));
475     assert(s);
476     s->header.mainloop = m;
477     s->header.dead = 0;
478
479     s->enabled = !!tv;
480     if (tv)
481         s->timeval = *tv;
482
483     s->callback = callback;
484     s->userdata = userdata;
485
486     pa_idxset_put(m->time_sources, s, NULL);
487     return s;
488 }
489
490 static void mainloop_enable_time(struct pa_mainloop_api*a, void *id, const struct timeval *tv) {
491     struct pa_mainloop *m;
492     struct mainloop_source_time *s = id;
493     assert(a && a->userdata && s && !s->header.dead);
494     m = a->userdata;
495     assert(a == &m->api);
496
497     if (tv) {
498         s->enabled = 1;
499         s->timeval = *tv;
500     } else
501         s->enabled = 0;
502 }
503
504 static void mainloop_cancel_time(struct pa_mainloop_api*a, void* id) {
505     struct pa_mainloop *m;
506     struct mainloop_source_time *s = id;
507     assert(a && a->userdata && s && !s->header.dead);
508     m = a->userdata;
509     assert(a == &m->api);
510
511     s->header.dead = 1;
512     m->time_sources_scan_dead = 1;
513
514 }
515
516 static void mainloop_quit(struct pa_mainloop_api*a, int retval) {
517     struct pa_mainloop *m;
518     assert(a && a->userdata);
519     m = a->userdata;
520     assert(a == &m->api);
521
522     m->quit = 1;
523     m->retval = retval;
524 }
525     
526 static void setup_api(struct pa_mainloop *m) {
527     assert(m);
528     
529     m->api.userdata = m;
530     m->api.source_io = mainloop_source_io;
531     m->api.enable_io = mainloop_enable_io;
532     m->api.cancel_io = mainloop_cancel_io;
533
534     m->api.source_fixed = mainloop_source_fixed;
535     m->api.enable_fixed = mainloop_enable_fixed;
536     m->api.cancel_fixed = mainloop_cancel_fixed;
537
538     m->api.source_idle = mainloop_source_idle;
539     m->api.enable_idle = mainloop_enable_fixed; /* (!) */
540     m->api.cancel_idle = mainloop_cancel_idle;
541     
542     m->api.source_time = mainloop_source_time;
543     m->api.enable_time = mainloop_enable_time;
544     m->api.cancel_time = mainloop_cancel_time;
545
546     m->api.quit = mainloop_quit;
547 }
548
549 struct pa_mainloop_api* pa_mainloop_get_api(struct pa_mainloop*m) {
550     assert(m);
551     return &m->api;
552 }
553