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 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 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
42 struct pa_mainloop
*mainloop
;
45 enum pa_io_event_flags events
;
46 void (*callback
) (struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
);
47 struct pollfd
*pollfd
;
49 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_io_event
*e
, void *userdata
);
52 struct pa_time_event
{
53 struct pa_mainloop
*mainloop
;
56 struct timeval timeval
;
57 void (*callback
)(struct pa_mainloop_api
*a
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
59 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_time_event
*e
, void *userdata
);
62 struct pa_defer_event
{
63 struct pa_mainloop
*mainloop
;
66 void (*callback
)(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
);
68 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
);
72 struct pa_idxset
*io_events
, *time_events
, *defer_events
;
73 int io_events_scan_dead
, defer_events_scan_dead
, time_events_scan_dead
;
75 struct pollfd
*pollfds
;
76 unsigned max_pollfds
, n_pollfds
;
79 int quit
, running
, retval
;
80 struct pa_mainloop_api api
;
84 static struct pa_io_event
* mainloop_io_new(struct pa_mainloop_api
*a
, int fd
, enum pa_io_event_flags events
, void (*callback
) (struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags events
, void *userdata
), void *userdata
) {
85 struct pa_mainloop
*m
;
86 struct pa_io_event
*e
;
88 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
92 e
= pa_xmalloc(sizeof(struct pa_io_event
));
98 e
->callback
= callback
;
99 e
->userdata
= userdata
;
100 e
->destroy_callback
= NULL
;
103 pa_idxset_put(m
->io_events
, e
, NULL
);
104 m
->rebuild_pollfds
= 1;
108 static void mainloop_io_enable(struct pa_io_event
*e
, enum pa_io_event_flags events
) {
109 assert(e
&& e
->mainloop
);
114 (events
& PA_IO_EVENT_INPUT
? POLLIN
: 0) |
115 (events
& PA_IO_EVENT_OUTPUT
? POLLOUT
: 0) |
120 static void mainloop_io_free(struct pa_io_event
*e
) {
121 assert(e
&& e
->mainloop
);
122 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
125 static void mainloop_io_set_destroy(struct pa_io_event
*e
, void (*callback
)(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, void *userdata
)) {
127 e
->destroy_callback
= callback
;
131 struct pa_defer_event
* mainloop_defer_new(struct pa_mainloop_api
*a
, void (*callback
) (struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
), void *userdata
) {
132 struct pa_mainloop
*m
;
133 struct pa_defer_event
*e
;
135 assert(a
&& a
->userdata
&& callback
);
137 assert(a
== &m
->api
);
139 e
= pa_xmalloc(sizeof(struct pa_defer_event
));
144 e
->callback
= callback
;
145 e
->userdata
= userdata
;
146 e
->destroy_callback
= NULL
;
148 pa_idxset_put(m
->defer_events
, e
, NULL
);
152 static void mainloop_defer_enable(struct pa_defer_event
*e
, int b
) {
157 static void mainloop_defer_free(struct pa_defer_event
*e
) {
159 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
162 static void mainloop_defer_set_destroy(struct pa_defer_event
*e
, void (*callback
)(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
)) {
164 e
->destroy_callback
= callback
;
168 static struct pa_time_event
* mainloop_time_new(struct pa_mainloop_api
*a
, const struct timeval
*tv
, void (*callback
) (struct pa_mainloop_api
*a
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
), void *userdata
) {
169 struct pa_mainloop
*m
;
170 struct pa_time_event
*e
;
172 assert(a
&& a
->userdata
&& callback
);
174 assert(a
== &m
->api
);
176 e
= pa_xmalloc(sizeof(struct pa_time_event
));
184 e
->callback
= callback
;
185 e
->userdata
= userdata
;
186 e
->destroy_callback
= NULL
;
188 pa_idxset_put(m
->time_events
, e
, NULL
);
193 static void mainloop_time_restart(struct pa_time_event
*e
, const struct timeval
*tv
) {
203 static void mainloop_time_free(struct pa_time_event
*e
) {
206 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
209 static void mainloop_time_set_destroy(struct pa_time_event
*e
, void (*callback
)(struct pa_mainloop_api
*a
, struct pa_time_event
*e
, void *userdata
)) {
211 e
->destroy_callback
= callback
;
216 static void mainloop_quit(struct pa_mainloop_api
*a
, int retval
) {
217 struct pa_mainloop
*m
;
218 assert(a
&& a
->userdata
);
220 assert(a
== &m
->api
);
226 static const struct pa_mainloop_api vtable
= {
229 .io_new
= mainloop_io_new
,
230 .io_enable
= mainloop_io_enable
,
231 .io_free
= mainloop_io_free
,
232 .io_set_destroy
= mainloop_io_set_destroy
,
234 .time_new
= mainloop_time_new
,
235 .time_restart
= mainloop_time_restart
,
236 .time_free
= mainloop_time_free
,
237 .time_set_destroy
= mainloop_time_set_destroy
,
239 .defer_new
= mainloop_defer_new
,
240 .defer_enable
= mainloop_defer_enable
,
241 .defer_free
= mainloop_defer_free
,
242 .defer_set_destroy
= mainloop_defer_set_destroy
,
244 .quit
= mainloop_quit
,
247 struct pa_mainloop
*pa_mainloop_new(void) {
248 struct pa_mainloop
*m
;
250 m
= pa_xmalloc(sizeof(struct pa_mainloop
));
252 m
->io_events
= pa_idxset_new(NULL
, NULL
);
253 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
254 m
->time_events
= pa_idxset_new(NULL
, NULL
);
256 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
258 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
261 m
->max_pollfds
= m
->n_pollfds
= m
->rebuild_pollfds
= 0;
263 m
->quit
= m
->running
= m
->retval
= 0;
271 static int io_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
272 struct pa_io_event
*e
= p
;
274 assert(e
&& del
&& all
);
276 if (!*all
&& !e
->dead
)
279 if (e
->destroy_callback
)
280 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
286 static int time_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
287 struct pa_time_event
*e
= p
;
289 assert(e
&& del
&& all
);
291 if (!*all
&& !e
->dead
)
294 if (e
->destroy_callback
)
295 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
301 static int defer_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
302 struct pa_defer_event
*e
= p
;
304 assert(e
&& del
&& all
);
306 if (!*all
&& !e
->dead
)
309 if (e
->destroy_callback
)
310 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
316 void pa_mainloop_free(struct pa_mainloop
* m
) {
320 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
321 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
322 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
324 pa_idxset_free(m
->io_events
, NULL
, NULL
);
325 pa_idxset_free(m
->time_events
, NULL
, NULL
);
326 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
328 pa_xfree(m
->pollfds
);
332 static void scan_dead(struct pa_mainloop
*m
) {
335 if (m
->io_events_scan_dead
)
336 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
337 if (m
->time_events_scan_dead
)
338 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
339 if (m
->defer_events_scan_dead
)
340 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
342 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
345 static void rebuild_pollfds(struct pa_mainloop
*m
) {
346 struct pa_io_event
*e
;
348 uint32_t index
= PA_IDXSET_INVALID
;
351 l
= pa_idxset_ncontents(m
->io_events
);
352 if (m
->max_pollfds
< l
) {
353 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
359 for (e
= pa_idxset_first(m
->io_events
, &index
); e
; e
= pa_idxset_next(m
->io_events
, &index
)) {
368 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
369 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
379 static void dispatch_pollfds(struct pa_mainloop
*m
) {
380 uint32_t index
= PA_IDXSET_INVALID
;
381 struct pa_io_event
*e
;
383 for (e
= pa_idxset_first(m
->io_events
, &index
); e
; e
= pa_idxset_next(m
->io_events
, &index
)) {
384 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
387 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
388 e
->callback(&m
->api
, e
, e
->fd
,
389 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
390 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
391 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
392 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
394 e
->pollfd
->revents
= 0;
398 static void dispatch_defer(struct pa_mainloop
*m
) {
400 struct pa_defer_event
*e
;
402 for (e
= pa_idxset_first(m
->defer_events
, &index
); e
; e
= pa_idxset_next(m
->defer_events
, &index
)) {
403 if (e
->dead
|| !e
->enabled
)
407 e
->callback(&m
->api
, e
, e
->userdata
);
411 static int calc_next_timeout(struct pa_mainloop
*m
) {
413 struct pa_time_event
*e
;
417 if (pa_idxset_isempty(m
->time_events
))
420 gettimeofday(&now
, NULL
);
422 for (e
= pa_idxset_first(m
->time_events
, &index
); e
; e
= pa_idxset_next(m
->time_events
, &index
)) {
425 if (e
->dead
|| !e
->enabled
)
428 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
431 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
433 if (e
->timeval
.tv_usec
> now
.tv_usec
)
434 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
436 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
440 else if (t
== -1 || tmp
< t
)
447 static void dispatch_timeout(struct pa_mainloop
*m
) {
449 struct pa_time_event
*e
;
453 if (pa_idxset_isempty(m
->time_events
))
456 gettimeofday(&now
, NULL
);
457 for (e
= pa_idxset_first(m
->time_events
, &index
); e
; e
= pa_idxset_next(m
->time_events
, &index
)) {
459 if (e
->dead
|| !e
->enabled
)
462 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
466 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
471 int pa_mainloop_iterate(struct pa_mainloop
*m
, int block
, int *retval
) {
473 assert(m
&& !m
->running
);
486 if (m
->rebuild_pollfds
) {
488 m
->rebuild_pollfds
= 0;
492 int t
= block
? calc_next_timeout(m
) : 0;
493 /*fprintf(stderr, "%u\n", t);*/
494 r
= poll(m
->pollfds
, m
->n_pollfds
, t
);
495 } while (r
< 0 && errno
== EINTR
);
502 fprintf(stderr
, "select(): %s\n", strerror(errno
));
505 return r
< 0 ? -1 : 0;
508 int pa_mainloop_run(struct pa_mainloop
*m
, int *retval
) {
510 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) == 0);
514 void pa_mainloop_quit(struct pa_mainloop
*m
, int r
) {
519 struct pa_mainloop_api
* pa_mainloop_get_api(struct pa_mainloop
*m
) {