source: roaraudio/libroar/vio.c @ 2059:916b1d6f2b91

Last change on this file since 2059:916b1d6f2b91 was 2059:916b1d6f2b91, checked in by phi, 15 years ago

moved vio interface to stdio out of the main vio code

File size: 12.4 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 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
146
147 if ( vio->ctl == NULL )
148  return -1;
149
150 return vio->ctl(vio, cmd, data);
151}
152
153int     roar_vio_close    (struct roar_vio_calls * vio) {
154 if ( vio == NULL )
155  return -1;
156
157 if ( vio->close == NULL )
158  return -1;
159
160 return vio->close(vio);
161}
162
163int     roar_vio_putc    (struct roar_vio_calls * vio, char c) {
164 return roar_vio_write(vio, &c, 1);
165}
166
167int     roar_vio_getc    (struct roar_vio_calls * vio) {
168 unsigned char c;
169
170 if ( roar_vio_read(vio, &c, 1) != 1 )
171  return EOF;
172
173 return c;
174}
175
176int     roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) {
177 va_list ap;
178 int ret;
179 char buf[8192];
180
181 va_start(ap, format);
182 ret = vsnprintf(buf, 8192, format, ap);
183 va_end(ap);
184
185 return roar_vio_write(vio, buf, ret);
186}
187
188// converters:
189int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
190#ifdef _CAN_OPERATE
191 int fh;
192
193 if ( calls == NULL || filename == NULL )
194  return -1;
195
196#ifdef ROAR_TARGET_WIN32
197 flags |= O_BINARY;
198#endif
199
200 if ( (fh = open(filename, flags, mode)) == -1 )
201  return -1;
202
203 if ( roar_vio_open_fh(calls, fh) == -1 ) {
204  close(fh);
205  return -1;
206 }
207
208 return 0;
209#else
210 return -1;
211#endif
212}
213
214int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
215 if ( calls == NULL )
216  return -1;
217
218 if ( roar_vio_init_calls(calls) == -1 )
219  return -1;
220
221 return roar_vio_set_fh(calls, fh);
222}
223
224int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
225 if ( calls == NULL )
226  return -1;
227
228 if ( roar_vio_open_fh(calls, fh) == -1 )
229  return -1;
230
231#ifdef ROAR_TARGET_WIN32
232 calls->read     = roar_vio_winsock_read;
233 calls->write    = roar_vio_winsock_write;
234 calls->nonblock = roar_vio_winsock_nonblock;
235 calls->sync     = roar_vio_winsock_sync;
236 calls->ctl      = roar_vio_winsock_ctl;
237 calls->close    = roar_vio_winsock_close;
238#else
239 calls->sync     = roar_vio_null_sync;
240#endif
241
242 return 0;
243}
244
245int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
246 int fh;
247
248 if ( calls == NULL )
249  return -1;
250
251 if ( (fh = roar_socket_connect(host, port)) == -1 )
252  return -1;
253
254 return roar_vio_open_fh_socket(calls, fh);
255}
256
257int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
258 int fh;
259
260 if ( calls == NULL )
261  return -1;
262
263 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
264  return -1;
265
266 return roar_vio_open_fh_socket(calls, fh);
267}
268
269int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
270                                                               char * server, int dir, char * name) {
271 int fh;
272
273 if ( calls == NULL )
274  return -1;
275
276 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
277  return -1;
278
279 return roar_vio_open_fh_socket(calls, fh);
280}
281
282// VIOs:
283
284// basic
285ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
286#ifdef _CAN_OPERATE
287 return read(roar_vio_get_fh(vio), buf, count);
288#else
289 return -1;
290#endif
291}
292
293ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
294#ifdef _CAN_OPERATE
295 return write(roar_vio_get_fh(vio), buf, count);
296#else
297 return -1;
298#endif
299}
300
301off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
302#ifdef _CAN_OPERATE
303 return lseek(roar_vio_get_fh(vio), offset, whence);
304#else
305 return -1;
306#endif
307}
308
309int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
310 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
311  return -1;
312
313 if ( state == ROAR_SOCKET_NONBLOCK )
314  return 0;
315
316 roar_vio_sync(vio);
317
318 return 0;
319}
320
321int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
322#ifdef ROAR_FDATASYNC
323 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
324#else
325 return 0;
326#endif
327}
328
329int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
330
331 if ( vio == NULL || cmd == -1 )
332  return -1;
333
334 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
335
336 switch (cmd) {
337  case ROAR_VIO_CTL_GET_FH:
338  case ROAR_VIO_CTL_GET_READ_FH:
339  case ROAR_VIO_CTL_GET_WRITE_FH:
340    ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_*FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, roar_vio_get_fh(vio));
341    *(int*)data = roar_vio_get_fh(vio);
342    return 0;
343   break;
344  case ROAR_VIO_CTL_SET_NOSYNC:
345    vio->sync = NULL;
346    return 0;
347   break;
348 }
349
350 return -1;
351}
352
353int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
354#ifdef _CAN_OPERATE
355 if ( roar_vio_get_fh(vio) != -1 )
356  return close(roar_vio_get_fh(vio));
357
358 return 0;
359#else
360 return -1;
361#endif
362}
363
364#ifdef ROAR_TARGET_WIN32
365ssize_t roar_vio_winsock_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
366 return recv(roar_vio_get_fh(vio), buf, count, 0);
367}
368
369ssize_t roar_vio_winsock_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
370 return send(roar_vio_get_fh(vio), buf, count, 0);
371}
372
373int     roar_vio_winsock_nonblock(struct roar_vio_calls * vio, int state) {
374 return -1;
375}
376int     roar_vio_winsock_sync    (struct roar_vio_calls * vio) {
377 return 0;
378}
379int     roar_vio_winsock_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
380 return -1;
381}
382int     roar_vio_winsock_close   (struct roar_vio_calls * vio) {
383
384 closesocket(roar_vio_get_fh(vio));
385
386 return 0;
387}
388#endif
389
390
391// null
392ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
393 if ( vio == NULL || buf == NULL )
394  return -1;
395
396 return 0;
397}
398
399int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
400 return 0;
401}
402
403// pass
404int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
405 if ( calls == NULL )
406  return -1;
407
408 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
409
410 calls->read     = roar_vio_pass_read;
411 calls->write    = roar_vio_pass_write;
412 calls->lseek    = roar_vio_pass_lseek;
413 calls->nonblock = roar_vio_pass_nonblock;
414 calls->sync     = roar_vio_pass_sync;
415 calls->ctl      = roar_vio_pass_ctl;
416 calls->close    = roar_vio_pass_close;
417
418 calls->inst     = dst;
419
420 return 0;
421}
422
423ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
424 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
425}
426
427ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
428 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
429}
430
431off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
432 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
433}
434
435int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
436 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
437}
438
439int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
440 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
441}
442
443int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
444 if (vio == NULL || cmd == -1)
445  return -1;
446
447 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
448
449 switch (cmd) {
450  case ROAR_VIO_CTL_GET_NEXT:
451    *(struct roar_vio_calls **)data = vio->inst;
452    return 0;
453   break;
454  case ROAR_VIO_CTL_SET_NEXT:
455    vio->inst = *(struct roar_vio_calls **)data;
456    return 0;
457   break;
458 }
459
460 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
461}
462
463int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
464 return roar_vio_close((struct roar_vio_calls *) vio->inst);
465}
466
467
468// re
469int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
470 if ( roar_vio_open_pass(calls, dst) == -1 )
471  return -1;
472
473 calls->read  = roar_vio_re_read;
474 calls->write = roar_vio_re_write;
475 calls->lseek = roar_vio_re_lseek;
476
477 return 0;
478}
479ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
480  size_t len =  0;
481 ssize_t r   = -1;
482
483 if ( vio == NULL )
484  return -1;
485
486 if ( vio->inst == NULL )
487  return -1;
488
489 errno = 0;
490
491 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
492  len   += r;
493  buf   += r;
494  count -= r;
495  if ( count == 0 )
496   break;
497 }
498
499 if ( len == 0 && r == -1 )
500  return -1;
501
502 return len;
503}
504
505ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
506  size_t len =  0;
507 ssize_t r   = -1;
508
509 if ( vio == NULL )
510  return -1;
511
512 if ( vio->inst == NULL )
513  return -1;
514
515 errno = 0;
516
517 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
518  len   += r;
519  buf   += r;
520  count -= r;
521  if ( count == 0 )
522   break;
523 }
524
525 if ( len == 0 && r == -1 )
526  return -1;
527
528 return len;
529}
530
531// TODO: we should do a some more intelgent thing here.
532off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
533 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
534}
535
536//ll
Note: See TracBrowser for help on using the repository browser.