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
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
;
85 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
) {
86 struct pa_mainloop
*m
;
87 struct pa_io_event
*e
;
89 assert(a
&& a
->userdata
&& fd
>= 0 && callback
);
93 e
= pa_xmalloc(sizeof(struct pa_io_event
));
99 e
->callback
= callback
;
100 e
->userdata
= userdata
;
101 e
->destroy_callback
= NULL
;
104 pa_idxset_put(m
->io_events
, e
, NULL
);
105 m
->rebuild_pollfds
= 1;
109 static void mainloop_io_enable(struct pa_io_event
*e
, enum pa_io_event_flags events
) {
110 assert(e
&& e
->mainloop
);
115 (events
& PA_IO_EVENT_INPUT
? POLLIN
: 0) |
116 (events
& PA_IO_EVENT_OUTPUT
? POLLOUT
: 0) |
121 static void mainloop_io_free(struct pa_io_event
*e
) {
122 assert(e
&& e
->mainloop
);
123 e
->dead
= e
->mainloop
->io_events_scan_dead
= e
->mainloop
->rebuild_pollfds
= 1;
126 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
)) {
128 e
->destroy_callback
= callback
;
132 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
) {
133 struct pa_mainloop
*m
;
134 struct pa_defer_event
*e
;
136 assert(a
&& a
->userdata
&& callback
);
138 assert(a
== &m
->api
);
140 e
= pa_xmalloc(sizeof(struct pa_defer_event
));
145 e
->callback
= callback
;
146 e
->userdata
= userdata
;
147 e
->destroy_callback
= NULL
;
149 pa_idxset_put(m
->defer_events
, e
, NULL
);
153 static void mainloop_defer_enable(struct pa_defer_event
*e
, int b
) {
158 static void mainloop_defer_free(struct pa_defer_event
*e
) {
160 e
->dead
= e
->mainloop
->defer_events_scan_dead
= 1;
163 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
)) {
165 e
->destroy_callback
= callback
;
169 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
) {
170 struct pa_mainloop
*m
;
171 struct pa_time_event
*e
;
173 assert(a
&& a
->userdata
&& callback
);
175 assert(a
== &m
->api
);
177 e
= pa_xmalloc(sizeof(struct pa_time_event
));
185 e
->callback
= callback
;
186 e
->userdata
= userdata
;
187 e
->destroy_callback
= NULL
;
189 pa_idxset_put(m
->time_events
, e
, NULL
);
194 static void mainloop_time_restart(struct pa_time_event
*e
, const struct timeval
*tv
) {
204 static void mainloop_time_free(struct pa_time_event
*e
) {
207 e
->dead
= e
->mainloop
->time_events_scan_dead
= 1;
210 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
)) {
212 e
->destroy_callback
= callback
;
217 static void mainloop_quit(struct pa_mainloop_api
*a
, int retval
) {
218 struct pa_mainloop
*m
;
219 assert(a
&& a
->userdata
);
221 assert(a
== &m
->api
);
227 static const struct pa_mainloop_api vtable
= {
230 .io_new
= mainloop_io_new
,
231 .io_enable
= mainloop_io_enable
,
232 .io_free
= mainloop_io_free
,
233 .io_set_destroy
= mainloop_io_set_destroy
,
235 .time_new
= mainloop_time_new
,
236 .time_restart
= mainloop_time_restart
,
237 .time_free
= mainloop_time_free
,
238 .time_set_destroy
= mainloop_time_set_destroy
,
240 .defer_new
= mainloop_defer_new
,
241 .defer_enable
= mainloop_defer_enable
,
242 .defer_free
= mainloop_defer_free
,
243 .defer_set_destroy
= mainloop_defer_set_destroy
,
245 .quit
= mainloop_quit
,
248 struct pa_mainloop
*pa_mainloop_new(void) {
249 struct pa_mainloop
*m
;
251 m
= pa_xmalloc(sizeof(struct pa_mainloop
));
253 m
->io_events
= pa_idxset_new(NULL
, NULL
);
254 m
->defer_events
= pa_idxset_new(NULL
, NULL
);
255 m
->time_events
= pa_idxset_new(NULL
, NULL
);
257 assert(m
->io_events
&& m
->defer_events
&& m
->time_events
);
259 m
->io_events_scan_dead
= m
->defer_events_scan_dead
= m
->time_events_scan_dead
= 0;
262 m
->max_pollfds
= m
->n_pollfds
= m
->rebuild_pollfds
= 0;
264 m
->quit
= m
->running
= m
->retval
= 0;
272 static int io_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
273 struct pa_io_event
*e
= p
;
275 assert(e
&& del
&& all
);
277 if (!*all
&& !e
->dead
)
280 if (e
->destroy_callback
)
281 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
287 static int time_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
288 struct pa_time_event
*e
= p
;
290 assert(e
&& del
&& all
);
292 if (!*all
&& !e
->dead
)
295 if (e
->destroy_callback
)
296 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
302 static int defer_foreach(void *p
, uint32_t index
, int *del
, void*userdata
) {
303 struct pa_defer_event
*e
= p
;
305 assert(e
&& del
&& all
);
307 if (!*all
&& !e
->dead
)
310 if (e
->destroy_callback
)
311 e
->destroy_callback(&e
->mainloop
->api
, e
, e
->userdata
);
317 void pa_mainloop_free(struct pa_mainloop
* m
) {
321 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
322 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
323 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
325 pa_idxset_free(m
->io_events
, NULL
, NULL
);
326 pa_idxset_free(m
->time_events
, NULL
, NULL
);
327 pa_idxset_free(m
->defer_events
, NULL
, NULL
);
329 pa_xfree(m
->pollfds
);
333 static void scan_dead(struct pa_mainloop
*m
) {
337 if (m
->io_events_scan_dead
)
338 pa_idxset_foreach(m
->io_events
, io_foreach
, &all
);
339 if (m
->time_events_scan_dead
)
340 pa_idxset_foreach(m
->time_events
, time_foreach
, &all
);
341 if (m
->defer_events_scan_dead
)
342 pa_idxset_foreach(m
->defer_events
, defer_foreach
, &all
);
344 m
->io_events_scan_dead
= m
->time_events_scan_dead
= m
->defer_events_scan_dead
= 0;
347 static void rebuild_pollfds(struct pa_mainloop
*m
) {
348 struct pa_io_event
*e
;
350 uint32_t index
= PA_IDXSET_INVALID
;
353 l
= pa_idxset_ncontents(m
->io_events
);
354 if (m
->max_pollfds
< l
) {
355 m
->pollfds
= pa_xrealloc(m
->pollfds
, sizeof(struct pollfd
)*l
);
361 for (e
= pa_idxset_first(m
->io_events
, &index
); e
; e
= pa_idxset_next(m
->io_events
, &index
)) {
370 ((e
->events
& PA_IO_EVENT_INPUT
) ? POLLIN
: 0) |
371 ((e
->events
& PA_IO_EVENT_OUTPUT
) ? POLLOUT
: 0) |
381 static void dispatch_pollfds(struct pa_mainloop
*m
) {
382 uint32_t index
= PA_IDXSET_INVALID
;
383 struct pa_io_event
*e
;
385 for (e
= pa_idxset_first(m
->io_events
, &index
); e
; e
= pa_idxset_next(m
->io_events
, &index
)) {
386 if (e
->dead
|| !e
->pollfd
|| !e
->pollfd
->revents
)
389 assert(e
->pollfd
->fd
== e
->fd
&& e
->callback
);
390 e
->callback(&m
->api
, e
, e
->fd
,
391 (e
->pollfd
->revents
& POLLHUP
? PA_IO_EVENT_HANGUP
: 0) |
392 (e
->pollfd
->revents
& POLLIN
? PA_IO_EVENT_INPUT
: 0) |
393 (e
->pollfd
->revents
& POLLOUT
? PA_IO_EVENT_OUTPUT
: 0) |
394 (e
->pollfd
->revents
& POLLERR
? PA_IO_EVENT_ERROR
: 0),
396 e
->pollfd
->revents
= 0;
400 static void dispatch_defer(struct pa_mainloop
*m
) {
402 struct pa_defer_event
*e
;
404 for (e
= pa_idxset_first(m
->defer_events
, &index
); e
; e
= pa_idxset_next(m
->defer_events
, &index
)) {
405 if (e
->dead
|| !e
->enabled
)
409 e
->callback(&m
->api
, e
, e
->userdata
);
413 static int calc_next_timeout(struct pa_mainloop
*m
) {
415 struct pa_time_event
*e
;
420 if (pa_idxset_isempty(m
->time_events
))
423 for (e
= pa_idxset_first(m
->time_events
, &index
); e
; e
= pa_idxset_next(m
->time_events
, &index
)) {
426 if (e
->dead
|| !e
->enabled
)
429 /* Let's save a system call */
431 gettimeofday(&now
, NULL
);
435 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
))
438 tmp
= (e
->timeval
.tv_sec
- now
.tv_sec
)*1000;
440 if (e
->timeval
.tv_usec
> now
.tv_usec
)
441 tmp
+= (e
->timeval
.tv_usec
- now
.tv_usec
)/1000;
443 tmp
-= (now
.tv_usec
- e
->timeval
.tv_usec
)/1000;
447 else if (t
== -1 || tmp
< t
)
454 static void dispatch_timeout(struct pa_mainloop
*m
) {
456 struct pa_time_event
*e
;
461 if (pa_idxset_isempty(m
->time_events
))
464 for (e
= pa_idxset_first(m
->time_events
, &index
); e
; e
= pa_idxset_next(m
->time_events
, &index
)) {
466 if (e
->dead
|| !e
->enabled
)
469 /* Let's save a system call */
471 gettimeofday(&now
, NULL
);
475 if (e
->timeval
.tv_sec
< now
.tv_sec
|| (e
->timeval
.tv_sec
== now
.tv_sec
&& e
->timeval
.tv_usec
<= now
.tv_usec
)) {
479 e
->callback(&m
->api
, e
, &e
->timeval
, e
->userdata
);
484 int pa_mainloop_iterate(struct pa_mainloop
*m
, int block
, int *retval
) {
486 assert(m
&& !m
->running
);
499 if (m
->rebuild_pollfds
) {
501 m
->rebuild_pollfds
= 0;
505 int t
= block
? calc_next_timeout(m
) : 0;
506 /*pa_log(__FILE__": %u\n", t);*/
507 r
= poll(m
->pollfds
, m
->n_pollfds
, t
);
508 } while (r
< 0 && errno
== EINTR
);
515 pa_log(__FILE__
": select(): %s\n", strerror(errno
));
518 return r
< 0 ? -1 : 0;
521 int pa_mainloop_run(struct pa_mainloop
*m
, int *retval
) {
523 while ((r
= pa_mainloop_iterate(m
, 1, retval
)) == 0);
527 void pa_mainloop_quit(struct pa_mainloop
*m
, int r
) {
532 struct pa_mainloop_api
* pa_mainloop_get_api(struct pa_mainloop
*m
) {