]>
code.delx.au - pulseaudio/blob - src/polypcore/ioline.c
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
32 #include <polyp/xmalloc.h>
34 #include <polypcore/log.h>
38 #define BUFFER_LIMIT (64*1024)
39 #define READ_SIZE (1024)
43 pa_defer_event
*defer_event
;
44 pa_mainloop_api
*mainloop
;
49 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
52 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
54 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
60 static void io_callback(pa_iochannel
*io
, void *userdata
);
61 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
63 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
67 l
= pa_xmalloc(sizeof(pa_ioline
));
72 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
75 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
81 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
83 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
84 l
->mainloop
->defer_enable(l
->defer_event
, 0);
88 pa_iochannel_set_callback(io
, io_callback
, l
);
93 static void ioline_free(pa_ioline
*l
) {
97 pa_iochannel_free(l
->io
);
100 l
->mainloop
->defer_free(l
->defer_event
);
107 void pa_ioline_unref(pa_ioline
*l
) {
108 assert(l
&& l
->ref
>= 1);
114 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
115 assert(l
&& l
->ref
>= 1);
121 void pa_ioline_close(pa_ioline
*l
) {
122 assert(l
&& l
->ref
>= 1);
126 pa_iochannel_free(l
->io
);
130 if (l
->defer_event
) {
131 l
->mainloop
->defer_free(l
->defer_event
);
132 l
->defer_event
= NULL
;
136 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
138 assert(l
&& c
&& l
->ref
>= 1 && !l
->dead
);
143 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
144 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
147 assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
149 /* In case the allocated buffer is too small, enlarge it. */
150 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
151 size_t n
= l
->wbuf_valid_length
+len
;
152 char *new = pa_xmalloc(n
);
154 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
160 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
162 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
163 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
167 assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
169 /* Append the new string */
170 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
171 l
->wbuf_valid_length
+= len
;
173 l
->mainloop
->defer_enable(l
->defer_event
, 1);
179 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
180 assert(l
&& l
->ref
>= 1);
181 l
->callback
= callback
;
182 l
->userdata
= userdata
;
185 static void failure(pa_ioline
*l
) {
186 assert(l
&& l
->ref
>= 1 && !l
->dead
);
191 l
->callback(l
, NULL
, l
->userdata
);
196 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
197 assert(l
&& l
->ref
>= 1 && skip
< l
->rbuf_valid_length
);
199 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
203 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
208 p
= l
->rbuf
+ l
->rbuf_index
;
211 l
->rbuf_index
+= m
+1;
212 l
->rbuf_valid_length
-= m
+1;
214 /* A shortcut for the next time */
215 if (l
->rbuf_valid_length
== 0)
219 l
->callback(l
, p
, l
->userdata
);
224 /* If the buffer became too large and still no newline was found, drop it. */
225 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
226 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
229 static int do_write(pa_ioline
*l
);
231 static int do_read(pa_ioline
*l
) {
232 assert(l
&& l
->ref
>= 1);
234 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
238 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
240 /* Check if we have to enlarge the read buffer */
241 if (len
< READ_SIZE
) {
242 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
244 if (n
>= BUFFER_LIMIT
)
247 if (l
->rbuf_length
>= n
) {
248 /* The current buffer is large enough, let's just move the data to the front */
249 if (l
->rbuf_valid_length
)
250 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
252 /* Enlarge the buffer */
253 char *new = pa_xmalloc(n
);
254 if (l
->rbuf_valid_length
)
255 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
264 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
266 assert(len
>= READ_SIZE
);
269 r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
);
271 /* Got an EOF, so fake an exit command. */
273 snprintf (l
->rbuf
, l
->rbuf_length
, "exit\n");
275 pa_ioline_puts(l
, "\nExiting.\n");
278 pa_log(__FILE__
": read() failed: %s", strerror(errno
));
283 l
->rbuf_valid_length
+= r
;
285 /* Look if a line has been terminated in the newly read data */
286 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
292 /* Try to flush the buffer */
293 static int do_write(pa_ioline
*l
) {
295 assert(l
&& l
->ref
>= 1);
297 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
299 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) < 0) {
300 pa_log(__FILE__
": write() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
306 l
->wbuf_valid_length
-= r
;
308 /* A shortcut for the next time */
309 if (l
->wbuf_valid_length
== 0)
316 /* Try to flush read/write data */
317 static void do_work(pa_ioline
*l
) {
318 assert(l
&& l
->ref
>= 1);
322 l
->mainloop
->defer_enable(l
->defer_event
, 0);
330 if (l
->defer_close
&& !l
->wbuf_valid_length
)
336 static void io_callback(pa_iochannel
*io
, void *userdata
) {
337 pa_ioline
*l
= userdata
;
338 assert(io
&& l
&& l
->ref
>= 1);
343 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
344 pa_ioline
*l
= userdata
;
345 assert(l
&& l
->ref
>= 1 && l
->mainloop
== m
&& l
->defer_event
== e
);
350 void pa_ioline_defer_close(pa_ioline
*l
) {
355 if (!l
->wbuf_valid_length
)
356 l
->mainloop
->defer_enable(l
->defer_event
, 1);
359 void pa_ioline_printf(pa_ioline
*s
, const char *format
, ...) {
364 va_start(ap
, format
);
365 t
= pa_vsprintf_malloc(format
, ap
);
368 pa_ioline_puts(s
, t
);