default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / ioline.c
bloba18188df3cb04eb3b823c28adce96aca17672346
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/socket.h>
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/refcnt.h>
40 #include "ioline.h"
42 #define BUFFER_LIMIT (64*1024)
43 #define READ_SIZE (1024)
45 struct pa_ioline {
46 PA_REFCNT_DECLARE;
48 pa_iochannel *io;
49 pa_defer_event *defer_event;
50 pa_mainloop_api *mainloop;
52 char *wbuf;
53 size_t wbuf_length, wbuf_index, wbuf_valid_length;
55 char *rbuf;
56 size_t rbuf_length, rbuf_index, rbuf_valid_length;
58 pa_ioline_cb_t callback;
59 void *userdata;
61 pa_ioline_drain_cb_t drain_callback;
62 void *drain_userdata;
64 pa_bool_t dead:1;
65 pa_bool_t defer_close:1;
68 static void io_callback(pa_iochannel*io, void *userdata);
69 static void defer_callback(pa_mainloop_api*m, pa_defer_event*e, void *userdata);
71 pa_ioline* pa_ioline_new(pa_iochannel *io) {
72 pa_ioline *l;
73 pa_assert(io);
75 l = pa_xnew(pa_ioline, 1);
76 PA_REFCNT_INIT(l);
77 l->io = io;
79 l->wbuf = NULL;
80 l->wbuf_length = l->wbuf_index = l->wbuf_valid_length = 0;
82 l->rbuf = NULL;
83 l->rbuf_length = l->rbuf_index = l->rbuf_valid_length = 0;
85 l->callback = NULL;
86 l->userdata = NULL;
88 l->drain_callback = NULL;
89 l->drain_userdata = NULL;
91 l->mainloop = pa_iochannel_get_mainloop_api(io);
93 l->defer_event = l->mainloop->defer_new(l->mainloop, defer_callback, l);
94 l->mainloop->defer_enable(l->defer_event, 0);
96 l->dead = FALSE;
97 l->defer_close = FALSE;
99 pa_iochannel_set_callback(io, io_callback, l);
101 return l;
104 static void ioline_free(pa_ioline *l) {
105 pa_assert(l);
107 if (l->io)
108 pa_iochannel_free(l->io);
110 if (l->defer_event)
111 l->mainloop->defer_free(l->defer_event);
113 pa_xfree(l->wbuf);
114 pa_xfree(l->rbuf);
115 pa_xfree(l);
118 void pa_ioline_unref(pa_ioline *l) {
119 pa_assert(l);
120 pa_assert(PA_REFCNT_VALUE(l) >= 1);
122 if (PA_REFCNT_DEC(l) <= 0)
123 ioline_free(l);
126 pa_ioline* pa_ioline_ref(pa_ioline *l) {
127 pa_assert(l);
128 pa_assert(PA_REFCNT_VALUE(l) >= 1);
130 PA_REFCNT_INC(l);
131 return l;
134 void pa_ioline_close(pa_ioline *l) {
135 pa_assert(l);
136 pa_assert(PA_REFCNT_VALUE(l) >= 1);
138 l->dead = TRUE;
140 if (l->io) {
141 pa_iochannel_free(l->io);
142 l->io = NULL;
145 if (l->defer_event) {
146 l->mainloop->defer_free(l->defer_event);
147 l->defer_event = NULL;
150 if (l->callback)
151 l->callback = NULL;
154 void pa_ioline_puts(pa_ioline *l, const char *c) {
155 size_t len;
157 pa_assert(l);
158 pa_assert(PA_REFCNT_VALUE(l) >= 1);
159 pa_assert(c);
161 if (l->dead)
162 return;
164 len = strlen(c);
165 if (len > BUFFER_LIMIT - l->wbuf_valid_length)
166 len = BUFFER_LIMIT - l->wbuf_valid_length;
168 if (len) {
169 pa_assert(l->wbuf_length >= l->wbuf_valid_length);
171 /* In case the allocated buffer is too small, enlarge it. */
172 if (l->wbuf_valid_length + len > l->wbuf_length) {
173 size_t n = l->wbuf_valid_length+len;
174 char *new = pa_xnew(char, (unsigned) n);
176 if (l->wbuf) {
177 memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
178 pa_xfree(l->wbuf);
181 l->wbuf = new;
182 l->wbuf_length = n;
183 l->wbuf_index = 0;
184 } else if (l->wbuf_index + l->wbuf_valid_length + len > l->wbuf_length) {
186 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
187 memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
188 l->wbuf_index = 0;
191 pa_assert(l->wbuf_index + l->wbuf_valid_length + len <= l->wbuf_length);
193 /* Append the new string */
194 memcpy(l->wbuf + l->wbuf_index + l->wbuf_valid_length, c, len);
195 l->wbuf_valid_length += len;
197 l->mainloop->defer_enable(l->defer_event, 1);
201 void pa_ioline_set_callback(pa_ioline*l, pa_ioline_cb_t callback, void *userdata) {
202 pa_assert(l);
203 pa_assert(PA_REFCNT_VALUE(l) >= 1);
205 if (l->dead)
206 return;
208 l->callback = callback;
209 l->userdata = userdata;
212 void pa_ioline_set_drain_callback(pa_ioline*l, pa_ioline_drain_cb_t callback, void *userdata) {
213 pa_assert(l);
214 pa_assert(PA_REFCNT_VALUE(l) >= 1);
216 if (l->dead)
217 return;
219 l->drain_callback = callback;
220 l->drain_userdata = userdata;
223 static void failure(pa_ioline *l, pa_bool_t process_leftover) {
224 pa_assert(l);
225 pa_assert(PA_REFCNT_VALUE(l) >= 1);
226 pa_assert(!l->dead);
228 if (process_leftover && l->rbuf_valid_length > 0) {
229 /* Pass the last missing bit to the client */
231 if (l->callback) {
232 char *p = pa_xstrndup(l->rbuf+l->rbuf_index, l->rbuf_valid_length);
233 l->callback(l, p, l->userdata);
234 pa_xfree(p);
238 if (l->callback) {
239 l->callback(l, NULL, l->userdata);
240 l->callback = NULL;
243 pa_ioline_close(l);
246 static void scan_for_lines(pa_ioline *l, size_t skip) {
247 pa_assert(l);
248 pa_assert(PA_REFCNT_VALUE(l) >= 1);
249 pa_assert(skip < l->rbuf_valid_length);
251 while (!l->dead && l->rbuf_valid_length > skip) {
252 char *e, *p;
253 size_t m;
255 if (!(e = memchr(l->rbuf + l->rbuf_index + skip, '\n', l->rbuf_valid_length - skip)))
256 break;
258 *e = 0;
260 p = l->rbuf + l->rbuf_index;
261 m = strlen(p);
263 l->rbuf_index += m+1;
264 l->rbuf_valid_length -= m+1;
266 /* A shortcut for the next time */
267 if (l->rbuf_valid_length == 0)
268 l->rbuf_index = 0;
270 if (l->callback)
271 l->callback(l, pa_strip_nl(p), l->userdata);
273 skip = 0;
276 /* If the buffer became too large and still no newline was found, drop it. */
277 if (l->rbuf_valid_length >= BUFFER_LIMIT)
278 l->rbuf_index = l->rbuf_valid_length = 0;
281 static int do_write(pa_ioline *l);
283 static int do_read(pa_ioline *l) {
284 pa_assert(l);
285 pa_assert(PA_REFCNT_VALUE(l) >= 1);
287 while (l->io && !l->dead && pa_iochannel_is_readable(l->io)) {
288 ssize_t r;
289 size_t len;
291 len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
293 /* Check if we have to enlarge the read buffer */
294 if (len < READ_SIZE) {
295 size_t n = l->rbuf_valid_length+READ_SIZE;
297 if (n >= BUFFER_LIMIT)
298 n = BUFFER_LIMIT;
300 if (l->rbuf_length >= n) {
301 /* The current buffer is large enough, let's just move the data to the front */
302 if (l->rbuf_valid_length)
303 memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
304 } else {
305 /* Enlarge the buffer */
306 char *new = pa_xnew(char, (unsigned) n);
307 if (l->rbuf_valid_length)
308 memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
309 pa_xfree(l->rbuf);
310 l->rbuf = new;
311 l->rbuf_length = n;
314 l->rbuf_index = 0;
317 len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
319 pa_assert(len >= READ_SIZE);
321 /* Read some data */
322 if ((r = pa_iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len)) <= 0) {
324 if (r < 0 && errno == EAGAIN)
325 return 0;
327 if (r < 0 && errno != ECONNRESET) {
328 pa_log("read(): %s", pa_cstrerror(errno));
329 failure(l, FALSE);
330 } else
331 failure(l, TRUE);
333 return -1;
336 l->rbuf_valid_length += (size_t) r;
338 /* Look if a line has been terminated in the newly read data */
339 scan_for_lines(l, l->rbuf_valid_length - (size_t) r);
342 return 0;
345 /* Try to flush the buffer */
346 static int do_write(pa_ioline *l) {
347 ssize_t r;
349 pa_assert(l);
350 pa_assert(PA_REFCNT_VALUE(l) >= 1);
352 while (l->io && !l->dead && pa_iochannel_is_writable(l->io) && l->wbuf_valid_length > 0) {
354 if ((r = pa_iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) <= 0) {
356 if (r < 0 && errno == EAGAIN)
357 break;
359 if (r < 0 && errno != EPIPE)
360 pa_log("write(): %s", pa_cstrerror(errno));
362 failure(l, FALSE);
364 return -1;
367 l->wbuf_index += (size_t) r;
368 l->wbuf_valid_length -= (size_t) r;
370 /* A shortcut for the next time */
371 if (l->wbuf_valid_length == 0)
372 l->wbuf_index = 0;
375 if (l->wbuf_valid_length <= 0 && l->drain_callback)
376 l->drain_callback(l, l->drain_userdata);
378 return 0;
381 /* Try to flush read/write data */
382 static void do_work(pa_ioline *l) {
383 pa_assert(l);
384 pa_assert(PA_REFCNT_VALUE(l) >= 1);
386 pa_ioline_ref(l);
388 l->mainloop->defer_enable(l->defer_event, 0);
390 if (!l->dead)
391 do_read(l);
393 if (!l->dead)
394 do_write(l);
396 if (l->defer_close && !l->wbuf_valid_length)
397 failure(l, TRUE);
399 pa_ioline_unref(l);
402 static void io_callback(pa_iochannel*io, void *userdata) {
403 pa_ioline *l = userdata;
405 pa_assert(io);
406 pa_assert(l);
407 pa_assert(PA_REFCNT_VALUE(l) >= 1);
409 do_work(l);
412 static void defer_callback(pa_mainloop_api*m, pa_defer_event*e, void *userdata) {
413 pa_ioline *l = userdata;
415 pa_assert(l);
416 pa_assert(PA_REFCNT_VALUE(l) >= 1);
417 pa_assert(l->mainloop == m);
418 pa_assert(l->defer_event == e);
420 do_work(l);
423 void pa_ioline_defer_close(pa_ioline *l) {
424 pa_assert(l);
425 pa_assert(PA_REFCNT_VALUE(l) >= 1);
427 l->defer_close = TRUE;
429 if (!l->wbuf_valid_length)
430 l->mainloop->defer_enable(l->defer_event, 1);
433 void pa_ioline_printf(pa_ioline *l, const char *format, ...) {
434 char *t;
435 va_list ap;
437 pa_assert(l);
438 pa_assert(PA_REFCNT_VALUE(l) >= 1);
440 va_start(ap, format);
441 t = pa_vsprintf_malloc(format, ap);
442 va_end(ap);
444 pa_ioline_puts(l, t);
445 pa_xfree(t);
448 pa_iochannel* pa_ioline_detach_iochannel(pa_ioline *l) {
449 pa_iochannel *r;
451 pa_assert(l);
453 if (!l->io)
454 return NULL;
456 r = l->io;
457 l->io = NULL;
459 pa_iochannel_set_callback(r, NULL, NULL);
461 return r;
464 pa_bool_t pa_ioline_is_drained(pa_ioline *l) {
465 pa_assert(l);
467 return l->wbuf_valid_length <= 0;