source: roaraudio/libroar/vio.c @ 1505:06a3687a4ce8

Last change on this file since 1505:06a3687a4ce8 was 1505:06a3687a4ce8, checked in by phi, 15 years ago

added more support for vio_ctl, added ROAR_VIO_CTL_GET_*FH and ROAR_VIO_CTL_SELECT

File size: 13.9 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37#ifdef ROAR_HAVE_IO_POSIX
38#define _CAN_OPERATE
39#endif
40
41int roar_vio_init_calls (struct roar_vio_calls * calls) {
42#ifdef _CAN_OPERATE
43 if ( calls == NULL )
44  return -1;
45
46 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
47
48/*
49 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
50 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
51 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
52*/
53
54 calls->read     = roar_vio_basic_read;
55 calls->write    = roar_vio_basic_write;
56 calls->lseek    = roar_vio_basic_lseek;
57 calls->nonblock = roar_vio_basic_nonblock;
58 calls->sync     = roar_vio_basic_sync;
59 calls->ctl      = roar_vio_basic_ctl;
60 calls->close    = roar_vio_basic_close;
61
62 return 0;
63#else
64 return -1;
65#endif
66}
67
68int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
69 if ( vio == NULL )
70  return -1;
71
72 vio->inst = inst;
73
74 return 0;
75}
76
77int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
78 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
79}
80
81int roar_vio_get_fh   (struct roar_vio_calls * vio) {
82 if ( vio == NULL )
83  return -1;
84
85 return ((int)(ROAR_INSTINT)vio->inst) - 1;
86}
87
88
89ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
90 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
91
92 if ( vio == NULL )
93  return -1;
94
95 if ( vio->read == NULL )
96  return -1;
97
98 return vio->read(vio, buf, count);
99}
100
101ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
102 if ( vio == NULL )
103  return -1;
104
105 if ( vio->write == NULL )
106  return -1;
107
108 return vio->write(vio, buf, count);
109}
110
111off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
112 if ( vio == NULL )
113  return -1;
114
115 if ( vio->lseek == NULL )
116  return -1;
117
118 return vio->lseek(vio, offset, whence);
119}
120
121int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
122 if ( vio == NULL )
123  return -1;
124
125 if ( vio->nonblock == NULL )
126  return -1;
127
128 return vio->nonblock(vio, state);
129}
130
131int     roar_vio_sync    (struct roar_vio_calls * vio) {
132 if ( vio == NULL )
133  return -1;
134
135 if ( vio->sync == NULL )
136  return -1;
137
138 return vio->sync(vio);
139}
140
141int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
142 if ( vio == NULL )
143  return -1;
144
145 if ( vio->ctl == NULL )
146  return -1;
147
148 return vio->ctl(vio, cmd, data);
149}
150
151int     roar_vio_close    (struct roar_vio_calls * vio) {
152 if ( vio == NULL )
153  return -1;
154
155 if ( vio->close == NULL )
156  return -1;
157
158 return vio->close(vio);
159}
160
161int     roar_vio_putc    (struct roar_vio_calls * vio, char c) {
162 return roar_vio_write(vio, &c, 1);
163}
164
165int     roar_vio_getc    (struct roar_vio_calls * vio) {
166 unsigned char c;
167
168 if ( roar_vio_read(vio, &c, 1) != 1 )
169  return EOF;
170
171 return c;
172}
173
174int     roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) {
175 va_list ap;
176 int ret;
177 char buf[8192];
178
179 va_start(ap, format);
180 ret = vsnprintf(buf, 8192, format, ap);
181 va_end(ap);
182
183 return roar_vio_write(vio, buf, ret);
184}
185
186// converters:
187int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
188#ifdef _CAN_OPERATE
189 int fh;
190
191 if ( calls == NULL || filename == NULL )
192  return -1;
193
194 if ( (fh = open(filename, flags, mode)) == -1 )
195  return -1;
196
197 if ( roar_vio_open_fh(calls, fh) == -1 ) {
198  close(fh);
199  return -1;
200 }
201
202 return 0;
203#else
204 return -1;
205#endif
206}
207
208int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
209 if ( calls == NULL )
210  return -1;
211
212 if ( roar_vio_init_calls(calls) == -1 )
213  return -1;
214
215 return roar_vio_set_fh(calls, fh);
216}
217
218int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
219 if ( calls == NULL )
220  return -1;
221
222 return roar_vio_open_fh(calls, fh);
223}
224
225int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
226 int fh;
227
228 if ( calls == NULL )
229  return -1;
230
231 if ( (fh = roar_socket_connect(host, port)) == -1 )
232  return -1;
233
234 return roar_vio_open_fh_socket(calls, fh);
235}
236
237int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
238 int fh;
239
240 if ( calls == NULL )
241  return -1;
242
243 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
244  return -1;
245
246 return roar_vio_open_fh_socket(calls, fh);
247}
248
249int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
250                                                               char * server, int dir, char * name) {
251 int fh;
252
253 if ( calls == NULL )
254  return -1;
255
256 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
257  return -1;
258
259 return roar_vio_open_fh_socket(calls, fh);
260}
261
262
263int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
264#ifndef ROAR_WITHOUT_VIO_STDIO
265 if ( calls == NULL || dst == NULL )
266  return -1;
267
268 memset(calls, 0, sizeof(struct roar_vio_calls));
269
270 calls->read  = roar_vio_stdio_read;
271 calls->write = roar_vio_stdio_write;
272 calls->lseek = roar_vio_stdio_lseek;
273 calls->sync  = roar_vio_stdio_sync;
274 calls->close = roar_vio_stdio_close;
275
276 calls->inst  = dst;
277
278 return 0;
279#else
280 return -1;
281#endif
282}
283
284FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
285#ifdef ROAR_HAVE_FOPENCOOKIE
286 cookie_io_functions_t foc_funcs;
287#endif
288
289 if ( calls == NULL )
290  return NULL;
291
292#if defined(ROAR_HAVE_FOPENCOOKIE)
293 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
294
295 foc_funcs.close = roar_vio_to_stdio_close;
296 foc_funcs.read  = roar_vio_to_stdio_read;
297 foc_funcs.write = roar_vio_to_stdio_write;
298
299 return fopencookie((void*) calls, "rw", foc_funcs);
300#elif defined(ROAR_HAVE_FUNOPEN)
301 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
302                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
303#else
304 return NULL;
305#endif
306}
307
308#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
309int roar_vio_to_stdio_close (void *__cookie) {
310 return roar_vio_close((struct roar_vio_calls *) __cookie);
311}
312
313#if defined(ROAR_HAVE_FOPENCOOKIE)
314__ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
315#elif defined(ROAR_HAVE_FUNOPEN)
316int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
317#endif
318 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
319}
320
321#if defined(ROAR_HAVE_FOPENCOOKIE)
322__ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
323#elif defined(ROAR_HAVE_FUNOPEN)
324int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
325#endif
326 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
327}
328
329#if defined(ROAR_HAVE_FOPENCOOKIE)
330int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
331#elif defined(ROAR_HAVE_FUNOPEN)
332fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
333 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
334}
335#endif
336#endif
337
338// VIOs:
339
340// basic
341ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
342#ifdef _CAN_OPERATE
343 return read(roar_vio_get_fh(vio), buf, count);
344#else
345 return -1;
346#endif
347}
348
349ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
350#ifdef _CAN_OPERATE
351 return write(roar_vio_get_fh(vio), buf, count);
352#else
353 return -1;
354#endif
355}
356
357off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
358#ifdef _CAN_OPERATE
359 return lseek(roar_vio_get_fh(vio), offset, whence);
360#else
361 return -1;
362#endif
363}
364
365int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
366 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
367  return -1;
368
369 if ( state == ROAR_SOCKET_NONBLOCK )
370  return 0;
371
372 roar_vio_sync(vio);
373
374 return 0;
375}
376
377int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
378#ifdef ROAR_FDATASYNC
379 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
380#else
381 return 0;
382#endif
383}
384
385int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
386
387 if ( vio == NULL || cmd == -1 )
388  return -1;
389
390 switch (cmd) {
391  case ROAR_VIO_CTL_GET_FH:
392  case ROAR_VIO_CTL_GET_READ_FH:
393  case ROAR_VIO_CTL_GET_WRITE_FH:
394    *(int*)data = roar_vio_get_fh(vio);
395    return 0;
396   break;
397 }
398
399 return -1;
400}
401
402int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
403#ifdef _CAN_OPERATE
404 if ( roar_vio_get_fh(vio) != -1 )
405  return close(roar_vio_get_fh(vio));
406
407 return 0;
408#else
409 return -1;
410#endif
411}
412
413// null
414ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
415 if ( vio == NULL || buf == NULL )
416  return -1;
417
418 return 0;
419}
420
421// pass
422int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
423 if ( calls == NULL )
424  return -1;
425
426 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
427
428 calls->read     = roar_vio_pass_read;
429 calls->write    = roar_vio_pass_write;
430 calls->lseek    = roar_vio_pass_lseek;
431 calls->nonblock = roar_vio_pass_nonblock;
432 calls->sync     = roar_vio_pass_sync;
433 calls->close    = roar_vio_pass_close;
434
435 calls->inst     = dst;
436
437 return 0;
438}
439
440ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
441 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
442}
443
444ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
445 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
446}
447
448off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
449 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
450}
451
452int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
453 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
454}
455
456int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
457 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
458}
459
460int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
461 if (vio == NULL || cmd == -1)
462  return -1;
463
464 switch (cmd) {
465  case ROAR_VIO_CTL_GET_NEXT:
466    *(struct roar_vio_calls **)data = vio->inst;
467    return 0;
468   break;
469  case ROAR_VIO_CTL_SET_NEXT:
470    vio->inst = *(struct roar_vio_calls **)data;
471    return 0;
472   break;
473 }
474
475 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
476}
477
478int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
479 return roar_vio_close((struct roar_vio_calls *) vio->inst);
480}
481
482
483// re
484int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
485 if ( roar_vio_open_pass(calls, dst) == -1 )
486  return -1;
487
488 calls->read  = roar_vio_re_read;
489 calls->write = roar_vio_re_write;
490 calls->lseek = roar_vio_re_lseek;
491
492 return 0;
493}
494ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
495  size_t len =  0;
496 ssize_t r   = -1;
497
498 if ( vio == NULL )
499  return -1;
500
501 if ( vio->inst == NULL )
502  return -1;
503
504 errno = 0;
505
506 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
507  len   += r;
508  buf   += r;
509  count -= r;
510  if ( count == 0 )
511   break;
512 }
513
514 if ( len == 0 && r == -1 )
515  return -1;
516
517 return len;
518}
519
520ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
521  size_t len =  0;
522 ssize_t r   = -1;
523
524 if ( vio == NULL )
525  return -1;
526
527 if ( vio->inst == NULL )
528  return -1;
529
530 errno = 0;
531
532 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
533  len   += r;
534  buf   += r;
535  count -= r;
536  if ( count == 0 )
537   break;
538 }
539
540 if ( len == 0 && r == -1 )
541  return -1;
542
543 return len;
544}
545
546// TODO: we should do a some more intelgent thing here.
547off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
548 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
549}
550
551// stdio:
552#ifndef ROAR_WITHOUT_VIO_STDIO
553ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
554 return fread(buf, 1, count, (FILE*)(vio->inst));
555}
556
557ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
558 return fwrite(buf, 1, count, (FILE*)(vio->inst));
559}
560
561off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
562 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
563  return -1;
564
565 return ftell((FILE*)(vio->inst));
566}
567
568int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
569 return fflush((FILE*)(vio->inst));
570}
571
572int     roar_vio_stdio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
573
574 if ( vio == NULL || cmd == -1 )
575  return -1;
576
577 switch (cmd) {
578  case ROAR_VIO_CTL_GET_FH:
579  case ROAR_VIO_CTL_GET_READ_FH:
580  case ROAR_VIO_CTL_GET_WRITE_FH:
581   *(int*)data = fileno((FILE*)(vio->inst));
582    return 0;
583   break;
584 }
585
586 return -1;
587}
588
589int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
590 return fclose((FILE*)(vio->inst));
591}
592#endif
593
594//ll
Note: See TracBrowser for help on using the repository browser.