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 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.
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 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
43 struct pa_mainloop
*mainloop
;
46 enum pa_io_event_flags events
;
47 void (*callback
) (struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
);
48 struct pollfd
*pollfd
;
50 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_io_event
*e
, void *userdata
);
53 struct pa_time_event
{
54 struct pa_mainloop
*mainloop
;
57 struct timeval timeval
;
58 void (*callback
)(struct pa_mainloop_api
*a
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
);
60 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_time_event
*e
, void *userdata
);
63 struct pa_defer_event
{
64 struct pa_mainloop
*mainloop
;
67 void (*callback
)(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
);
69 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
);
73 struct pa_idxset
*io_events
, *time_events
, *defer_events
;
74 int io_events_scan_dead
, defer_events_scan_dead
, time_events_scan_dead
;
76 struct pollfd
*pollfds
;
77 unsigned max_pollfds
, n_pollfds
;
80 int quit
, running
, retval
;
81 struct pa_mainloop_api api
;
87 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
) {
88 struct pa_mainloop
*m
;
89 struct pa_io_event
*e
;
91 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
95 e
= pa_xmalloc(sizeof(struct pa_io_event
));
101 e
->callback
= callback
;
102 e
->userdata
= userdata
;
103 e
->destroy_callback
= NULL
;
106 pa_idxset_put(m
->io_events
, e
, NULL
);
107 m
->rebuild_pollfds
= 1;
111 static void mainloop_io_enable(struct pa_io_event
*e
, enum pa_io_event_flags events
) {
112 assert(e
&& e
->mainloop
);
117 (events
& PA_IO_EVENT_INPUT
? POLLIN
: 0) |
118 (events
& PA_IO_EVENT_OUTPUT
? POLLOUT
: 0) |
123 static void mainloop_io_free(struct pa_io_event
*e
) {
124 assert(e
&& e
->mainloop
);
125 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
128 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
)) {
130 e
->destroy_callback
= callback
;
134 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
) {
135 struct pa_mainloop
*m
;
136 struct pa_defer_event
*e
;
138 assert(a
&& a
->userdata
&& callback
);
140 assert(a
== &m
->api
);
142 e
= pa_xmalloc(sizeof(struct pa_defer_event
));
147 e
->callback
= callback
;
148 e
->userdata
= userdata
;
149 e
->destroy_callback
= NULL
;
151 pa_idxset_put(m
->defer_events
, e
, NULL
);
153 m
->deferred_pending
++;
157 static void mainloop_defer_enable(struct pa_defer_event
*e
, int b
) {
160 if (e
->enabled
&& !b
) {
161 assert(e
->mainloop
->deferred_pending
> 0);
162 e
->mainloop
->deferred_pending
--;
163 } else if (!e
->enabled
&& b
)
164 e
->mainloop
->deferred_pending
++;
169 static void mainloop_defer_free(struct pa_defer_event
*e
) {
171 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
175 assert(e
->mainloop
->deferred_pending
> 0);
176 e
->mainloop
->deferred_pending
--;
180 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
)) {
182 e
->destroy_callback
= callback
;
186 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
) {
187 struct pa_mainloop
*m
;
188 struct pa_time_event
*e
;
190 assert(a
&& a
->userdata
&& callback
);
192 assert(a
== &m
->api
);
194 e
= pa_xmalloc(sizeof(struct pa_time_event
));
202 e
->callback
= callback
;
203 e
->userdata
= userdata
;
204 e
->destroy_callback
= NULL
;
206 pa_idxset_put(m
->time_events
, e
, NULL
);
211 static void mainloop_time_restart(struct pa_time_event
*e
, const struct timeval
*tv
) {
221 static void mainloop_time_free(struct pa_time_event
*e
) {
224 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
227 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
)) {
229 e
->destroy_callback
= callback
;
234 static void mainloop_quit(struct pa_mainloop_api
*a
, int retval
) {
235 struct pa_mainloop
*m
;
236 assert(a
&& a
->userdata
);
238 assert(a
== &m
->api
);
244 static const struct pa_mainloop_api vtable
= {
247 .io_new
= mainloop_io_new
,
248 .io_enable
= mainloop_io_enable
,
249 .io_free
= mainloop_io_free
,
250 .io_set_destroy
= mainloop_io_set_destroy
,
252 .time_new
= mainloop_time_new
,
253 .time_restart
= mainloop_time_restart
,
254 .time_free
= mainloop_time_free
,
255 .time_set_destroy
= mainloop_time_set_destroy
,
257 .defer_new
= mainloop_defer_new
,
258 .defer_enable
= mainloop_defer_enable
,
259 .defer_free
= mainloop_defer_free
,
260 .defer_set_destroy
= mainloop_defer_set_destroy
,
262 .quit
= mainloop_quit
,
265 struct pa_mainloop
*pa_mainloop_new(void) {
266 struct pa_mainloop
*m
;
268 m
= pa_xmalloc(sizeof(struct pa_mainloop
));
270 m
->io_events
= pa_idxset_new(NULL
, NULL
);
271 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
272 m
->time_events
= pa_idxset_new(NULL
, NULL
);
274 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
276 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
279 m
->max_pollfds
= m
->n_pollfds
= m
->rebuild_pollfds
= 0;
281 m
->quit
= m
->running
= m
->retval
= 0;
286 m
->deferred_pending
= 0;
291 static int io_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
292 struct pa_io_event
*e
= p
;
294 assert(e
&& del
&& all
);
296 if (!*all
&& !e
->dead
)
299 if (e
->destroy_callback
)
300 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
306 static int time_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
307 struct pa_time_event
*e
= p
;
309 assert(e
&& del
&& all
);
311 if (!*all
&& !e
->dead
)
314 if (e
->destroy_callback
)
315 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
321 static int defer_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
322 struct pa_defer_event
*e
= p
;
324 assert(e
&& del
&& all
);
326 if (!*all
&& !e
->dead
)
329 if (e
->destroy_callback
)
330 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
336 void pa_mainloop_free(struct pa_mainloop
* m
) {
340 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
341 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
342 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
344 pa_idxset_free(m
->io_events
, NULL
, NULL
);
345 pa_idxset_free(m
->time_events
, NULL
, NULL
);
346 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
348 pa_xfree(m
->pollfds
);
352 static void scan_dead(struct pa_mainloop
*m
) {
356 if (m
->io_events_scan_dead
)
357 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
358 if (m
->time_events_scan_dead
)
359 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
360 if (m
->defer_events_scan_dead
)
361 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
363 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
366 static void rebuild_pollfds(struct pa_mainloop
*m
) {
367 struct pa_io_event
*e
;
369 uint32_t index
= PA_IDXSET_INVALID
;
372 l
= pa_idxset_ncontents(m
->io_events
);
373 if (m
->max_pollfds
< l
) {
374 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
380 for (e
= pa_idxset_first(m
->io_events
, &index
); e
; e
= pa_idxset_next(m
->io_events
, &index
)) {
389 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
390 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
400 static int dispatch_pollfds(struct pa_mainloop
*m
) {
401 uint32_t index
= PA_IDXSET_INVALID
;
402 struct pa_io_event
*e
;
405 for (e
= pa_idxset_first(m
->io_events
, &index
); e
&& !m
->quit
; e
= pa_idxset_next(m
->io_events
, &index
)) {
406 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
409 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
410 e
->callback(&m
->api
, e
, e
->fd
,
411 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
412 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
413 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
414 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
416 e
->pollfd
->revents
= 0;
423 static int dispatch_defer(struct pa_mainloop
*m
) {
425 struct pa_defer_event
*e
;
428 for (e
= pa_idxset_first(m
->defer_events
, &index
); e
&& !m
->quit
; e
= pa_idxset_next(m
->defer_events
, &index
)) {
429 if (e
->dead
|| !e
->enabled
)
433 e
->callback(&m
->api
, e
, e
->userdata
);
440 static int calc_next_timeout(struct pa_mainloop
*m
) {
442 struct pa_time_event
*e
;
447 if (pa_idxset_isempty(m
->time_events
))
450 for (e
= pa_idxset_first(m
->time_events
, &index
); e
; e
= pa_idxset_next(m
->time_events
, &index
)) {
453 if (e
->dead
|| !e
->enabled
)
456 /* Let's save a system call */
458 gettimeofday(&now
, NULL
);
462 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
465 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
467 if (e
->timeval
.tv_usec
> now
.tv_usec
)
468 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
470 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
474 else if (t
== -1 || tmp
< t
)
481 static int dispatch_timeout(struct pa_mainloop
*m
) {
483 struct pa_time_event
*e
;
489 if (pa_idxset_isempty(m
->time_events
))
492 for (e
= pa_idxset_first(m
->time_events
, &index
); e
&& !m
->quit
; e
= pa_idxset_next(m
->time_events
, &index
)) {
494 if (e
->dead
|| !e
->enabled
)
497 /* Let's save a system call */
499 gettimeofday(&now
, NULL
);
503 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
507 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
516 int pa_mainloop_iterate(struct pa_mainloop
*m
, int block
, int *retval
) {
517 int r
, t
, dispatched
= 0;
518 assert(m
&& !m
->running
);
526 dispatched
+= dispatch_defer(m
);
531 if (m
->rebuild_pollfds
) {
533 m
->rebuild_pollfds
= 0;
536 t
= block
? calc_next_timeout(m
) : 0;
537 r
= poll(m
->pollfds
, m
->n_pollfds
, t
);
543 pa_log(__FILE__
": select(): %s\n", strerror(errno
));
545 dispatched
+= dispatch_timeout(m
);
551 dispatched
+= dispatch_pollfds(m
);
560 /* pa_log("dispatched: %i\n", dispatched); */
562 return r
< 0 ? -1 : dispatched
;
574 int pa_mainloop_run(struct pa_mainloop
*m
, int *retval
) {
576 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) >= 0);
586 void pa_mainloop_quit(struct pa_mainloop
*m
, int r
) {
591 struct pa_mainloop_api
* pa_mainloop_get_api(struct pa_mainloop
*m
) {
596 int pa_mainloop_deferred_pending(struct pa_mainloop
*m
) {
598 return m
->deferred_pending
> 0;