* new tool pacmd
[profile/ivi/pulseaudio-panda.git] / polyp / ioline.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 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.
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 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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "ioline.h"
33 #include "xmalloc.h"
34 #include "log.h"
35
36 #define BUFFER_LIMIT (64*1024)
37 #define READ_SIZE (1024)
38
39 struct pa_ioline {
40     struct pa_iochannel *io;
41     struct pa_defer_event *defer_event;
42     struct pa_mainloop_api *mainloop;
43     int ref;
44     int dead;
45
46     char *wbuf;
47     size_t wbuf_length, wbuf_index, wbuf_valid_length;
48
49     char *rbuf;
50     size_t rbuf_length, rbuf_index, rbuf_valid_length;
51
52     void (*callback)(struct pa_ioline*io, const char *s, void *userdata);
53     void *userdata;
54 };
55
56 static void io_callback(struct pa_iochannel*io, void *userdata);
57 static void defer_callback(struct pa_mainloop_api*m, struct pa_defer_event*e, void *userdata);
58
59 struct pa_ioline* pa_ioline_new(struct pa_iochannel *io) {
60     struct pa_ioline *l;
61     assert(io);
62     
63     l = pa_xmalloc(sizeof(struct pa_ioline));
64     l->io = io;
65     l->dead = 0;
66
67     l->wbuf = NULL;
68     l->wbuf_length = l->wbuf_index = l->wbuf_valid_length = 0;
69
70     l->rbuf = NULL;
71     l->rbuf_length = l->rbuf_index = l->rbuf_valid_length = 0;
72
73     l->callback = NULL;
74     l->userdata = NULL;
75     l->ref = 1;
76
77     l->mainloop = pa_iochannel_get_mainloop_api(io);
78
79     l->defer_event = l->mainloop->defer_new(l->mainloop, defer_callback, l);
80     l->mainloop->defer_enable(l->defer_event, 0);
81     
82     pa_iochannel_set_callback(io, io_callback, l);
83     
84     return l;
85 }
86
87 static void ioline_free(struct pa_ioline *l) {
88     assert(l);
89
90     if (l->io)
91         pa_iochannel_free(l->io);
92
93     if (l->defer_event)
94         l->mainloop->defer_free(l->defer_event);
95
96     pa_xfree(l->wbuf);
97     pa_xfree(l->rbuf);
98     pa_xfree(l);
99 }
100
101 void pa_ioline_unref(struct pa_ioline *l) {
102     assert(l && l->ref >= 1);
103
104     if ((--l->ref) <= 0)
105         ioline_free(l);
106 }
107
108 struct pa_ioline* pa_ioline_ref(struct pa_ioline *l) {
109     assert(l && l->ref >= 1);
110
111     l->ref++;
112     return l;
113 }
114
115 void pa_ioline_close(struct pa_ioline *l) {
116     assert(l && l->ref >= 1);
117
118     l->dead = 1;
119     if (l->io) {
120         pa_iochannel_free(l->io);
121         l->io = NULL;
122     }
123
124     if (l->defer_event) {
125         l->mainloop->defer_free(l->defer_event);
126         l->defer_event = NULL;
127     }
128 }
129
130 void pa_ioline_puts(struct pa_ioline *l, const char *c) {
131     size_t len;
132     assert(l && c && l->ref >= 1 && !l->dead);
133
134     pa_ioline_ref(l);
135     
136     len = strlen(c);
137     if (len > BUFFER_LIMIT - l->wbuf_valid_length)
138         len = BUFFER_LIMIT - l->wbuf_valid_length;
139
140     if (len) {
141         assert(l->wbuf_length >= l->wbuf_valid_length);
142         
143         /* In case the allocated buffer is too small, enlarge it. */
144         if (l->wbuf_valid_length + len > l->wbuf_length) {
145             size_t n = l->wbuf_valid_length+len;
146             char *new = pa_xmalloc(n);
147             if (l->wbuf) {
148                 memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
149                 pa_xfree(l->wbuf);
150             }
151             l->wbuf = new;
152             l->wbuf_length = n;
153             l->wbuf_index = 0;
154         } else if (l->wbuf_index + l->wbuf_valid_length + len > l->wbuf_length) {
155             
156             /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
157             memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
158             l->wbuf_index = 0;
159         }
160         
161         assert(l->wbuf_index + l->wbuf_valid_length + len <= l->wbuf_length);
162         
163         /* Append the new string */
164         memcpy(l->wbuf + l->wbuf_index + l->wbuf_valid_length, c, len);
165         l->wbuf_valid_length += len;
166
167         l->mainloop->defer_enable(l->defer_event, 1);
168     }
169
170     pa_ioline_unref(l);
171 }
172
173 void pa_ioline_set_callback(struct pa_ioline*l, void (*callback)(struct pa_ioline*io, const char *s, void *userdata), void *userdata) {
174     assert(l && l->ref >= 1);
175     l->callback = callback;
176     l->userdata = userdata;
177 }
178
179 static void failure(struct pa_ioline *l) {
180     assert(l && l->ref >= 1 && !l->dead);
181
182     pa_ioline_close(l);
183
184     if (l->callback)
185         l->callback(l, NULL, l->userdata);
186 }
187
188 static void scan_for_lines(struct pa_ioline *l, size_t skip) {
189     assert(l && l->ref >= 1 && skip < l->rbuf_valid_length);
190
191     while (!l->dead && l->rbuf_valid_length > skip) {
192         char *e, *p;
193         size_t m;
194         
195         if (!(e = memchr(l->rbuf + l->rbuf_index + skip, '\n', l->rbuf_valid_length - skip)))
196             break;
197
198         *e = 0;
199     
200         p = l->rbuf + l->rbuf_index;
201         m = strlen(p);
202
203         l->rbuf_index += m+1;
204         l->rbuf_valid_length -= m+1;
205
206         /* A shortcut for the next time */
207         if (l->rbuf_valid_length == 0)
208             l->rbuf_index = 0;
209
210         if (l->callback)
211             l->callback(l, p, l->userdata);
212
213         skip = 0;
214     }
215
216     /* If the buffer became too large and still no newline was found, drop it. */
217     if (l->rbuf_valid_length >= BUFFER_LIMIT)
218         l->rbuf_index = l->rbuf_valid_length = 0;
219 }
220
221 static int do_read(struct pa_ioline *l) {
222     assert(l && l->ref >= 1);
223
224     while (!l->dead && pa_iochannel_is_readable(l->io)) {
225         ssize_t r;
226         size_t len;
227
228         len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
229         
230         /* Check if we have to enlarge the read buffer */
231         if (len < READ_SIZE) {
232             size_t n = l->rbuf_valid_length+READ_SIZE;
233             
234             if (n >= BUFFER_LIMIT)
235                 n = BUFFER_LIMIT;
236             
237             if (l->rbuf_length >= n) {
238                 /* The current buffer is large enough, let's just move the data to the front */
239                 if (l->rbuf_valid_length)
240                     memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
241             } else {
242                 /* Enlarge the buffer */
243                 char *new = pa_xmalloc(n);
244                 if (l->rbuf_valid_length)
245                     memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
246                 pa_xfree(l->rbuf);
247                 l->rbuf = new;
248                 l->rbuf_length = n;
249             }
250             
251             l->rbuf_index = 0;
252         }
253         
254         len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
255         
256         assert(len >= READ_SIZE);
257         
258         /* Read some data */
259         if ((r = pa_iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len)) <= 0) {
260             pa_log(__FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
261             failure(l);
262             return -1;
263         }
264         
265         l->rbuf_valid_length += r;
266         
267         /* Look if a line has been terminated in the newly read data */
268         scan_for_lines(l, l->rbuf_valid_length - r);
269     }
270         
271     return 0;
272 }
273
274 /* Try to flush the buffer */
275 static int do_write(struct pa_ioline *l) {
276     ssize_t r;
277     assert(l && l->ref >= 1);
278
279     while (!l->dead && pa_iochannel_is_writable(l->io) && l->wbuf_valid_length) {
280         
281         if ((r = pa_iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) < 0) {
282             pa_log(__FILE__": write() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
283             failure(l);
284             return -1;
285         }
286         
287         l->wbuf_index += r;
288         l->wbuf_valid_length -= r;
289         
290         /* A shortcut for the next time */
291         if (l->wbuf_valid_length == 0)
292             l->wbuf_index = 0;
293     }
294         
295     return 0;
296 }
297
298 /* Try to flush read/write data */
299 static void do_work(struct pa_ioline *l) {
300     assert(l && l->ref >= 1);
301
302     pa_ioline_ref(l);
303
304     l->mainloop->defer_enable(l->defer_event, 0);
305     
306     if (!l->dead)
307         do_write(l);
308
309     if (!l->dead)
310         do_read(l);
311
312     pa_ioline_unref(l);
313 }
314
315 static void io_callback(struct pa_iochannel*io, void *userdata) {
316     struct pa_ioline *l = userdata;
317     assert(io && l && l->ref >= 1);
318
319     do_work(l);
320 }
321
322 static void defer_callback(struct pa_mainloop_api*m, struct pa_defer_event*e, void *userdata) {
323     struct pa_ioline *l = userdata;
324     assert(l && l->ref >= 1 && l->mainloop == m && l->defer_event == e);
325
326     do_work(l);
327 }