source: roaraudio/libroar/vio.c @ 3859:692da44cb264

Last change on this file since 3859:692da44cb264 was 3859:692da44cb264, checked in by phi, 14 years ago

support NULL as stream object in roar_vio_simple_new_stream_obj()

File size: 14.0 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37#include <sys/ioctl.h>
38
39#ifdef ROAR_HAVE_IO_POSIX
40#define _CAN_OPERATE
41#endif
42
43int roar_vio_init_calls (struct roar_vio_calls * calls) {
44#ifdef _CAN_OPERATE
45 if ( calls == NULL )
46  return -1;
47
48 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
49
50/*
51 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
52 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
53 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
54*/
55
56 calls->read     = roar_vio_basic_read;
57 calls->write    = roar_vio_basic_write;
58 calls->lseek    = roar_vio_basic_lseek;
59 calls->nonblock = roar_vio_basic_nonblock;
60 calls->sync     = roar_vio_basic_sync;
61 calls->ctl      = roar_vio_basic_ctl;
62 calls->close    = roar_vio_basic_close;
63
64 return 0;
65#else
66 return -1;
67#endif
68}
69
70int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
71 if ( vio == NULL )
72  return -1;
73
74 vio->inst = inst;
75
76 return 0;
77}
78
79int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
80 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
81}
82
83int roar_vio_get_fh   (struct roar_vio_calls * vio) {
84 if ( vio == NULL )
85  return -1;
86
87 return ((int)(ROAR_INSTINT)vio->inst) - 1;
88}
89
90
91ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
92 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
93
94 if ( vio == NULL )
95  return -1;
96
97 if ( vio->read == NULL )
98  return -1;
99
100 return vio->read(vio, buf, count);
101}
102
103ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
104 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
105
106 if ( vio == NULL )
107  return -1;
108
109 if ( vio->write == NULL )
110  return -1;
111
112 return vio->write(vio, buf, count);
113}
114
115off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
116 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%u, whence=%i) = ?", vio, (unsigned int)offset, whence);
117
118 if ( vio == NULL )
119  return -1;
120
121 if ( vio->lseek == NULL )
122  return -1;
123
124 return vio->lseek(vio, offset, whence);
125}
126
127int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
128 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
129
130 if ( vio == NULL )
131  return -1;
132
133 if ( vio->nonblock == NULL )
134  return -1;
135
136 return vio->nonblock(vio, state);
137}
138
139int     roar_vio_sync    (struct roar_vio_calls * vio) {
140 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
141
142 if ( vio == NULL )
143  return -1;
144
145 if ( vio->sync == NULL )
146  return -1;
147
148 return vio->sync(vio);
149}
150
151int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
152 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
153
154 if ( vio == NULL )
155  return -1;
156
157 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
158
159 if ( vio->ctl == NULL )
160  return -1;
161
162 return vio->ctl(vio, cmd, data);
163}
164
165int     roar_vio_close    (struct roar_vio_calls * vio) {
166 ROAR_DBG("roar_vio_close(vio=%p) = ?", vio);
167
168 if ( vio == NULL )
169  return -1;
170
171 if ( vio->close == NULL )
172  return -1;
173
174 return vio->close(vio);
175}
176
177// specal commands:
178int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
179 if (dst == NULL || calls == NULL)
180  return -1;
181
182 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
183}
184
185int     roar_vio_shutdown(struct roar_vio_calls * vio,   int how) {
186 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
187}
188
189// converters:
190int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
191#ifdef _CAN_OPERATE
192 int fh;
193
194 if ( calls == NULL || filename == NULL )
195  return -1;
196
197#ifdef ROAR_TARGET_WIN32
198 flags |= O_BINARY;
199#endif
200
201 if ( (fh = open(filename, flags, mode)) == -1 )
202  return -1;
203
204 if ( roar_vio_open_fh(calls, fh) == -1 ) {
205  close(fh);
206  return -1;
207 }
208
209 return 0;
210#else
211 return -1;
212#endif
213}
214
215int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
216 if ( calls == NULL )
217  return -1;
218
219 if ( roar_vio_init_calls(calls) == -1 )
220  return -1;
221
222 return roar_vio_set_fh(calls, fh);
223}
224
225int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
226 if ( calls == NULL )
227  return -1;
228
229 if ( roar_vio_open_fh(calls, fh) == -1 )
230  return -1;
231
232#ifdef ROAR_TARGET_WIN32
233 calls->read     = roar_vio_winsock_read;
234 calls->write    = roar_vio_winsock_write;
235 calls->nonblock = roar_vio_winsock_nonblock;
236 calls->sync     = roar_vio_winsock_sync;
237 calls->ctl      = roar_vio_winsock_ctl;
238 calls->close    = roar_vio_winsock_close;
239#else
240 calls->sync     = roar_vio_null_sync;
241#endif
242
243 return 0;
244}
245
246int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
247 int fh;
248
249 if ( calls == NULL )
250  return -1;
251
252 if ( (fh = roar_socket_connect(host, port)) == -1 )
253  return -1;
254
255 return roar_vio_open_fh_socket(calls, fh);
256}
257
258int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
259 int fh;
260
261 if ( calls == NULL )
262  return -1;
263
264 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
265  return -1;
266
267 return roar_vio_open_fh_socket(calls, fh);
268}
269
270int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
271                                                               char * server, int dir, char * name) {
272 int fh;
273
274 if ( calls == NULL )
275  return -1;
276
277 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
278  return -1;
279
280 return roar_vio_open_fh_socket(calls, fh);
281}
282
283int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
284                                        struct roar_connection * con,
285                                        struct roar_stream * s,
286                                        int rate, int channels, int bits, int codec, int dir) {
287 struct roar_stream stream;
288 int fh;
289
290 if ( calls == NULL )
291  return -1;
292
293 if ( s == NULL )
294  s = &stream;
295
296 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 )
297  return -1;
298
299 return roar_vio_open_fh_socket(calls, fh);
300}
301
302// VIOs:
303
304// basic
305ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
306#ifdef _CAN_OPERATE
307 return read(roar_vio_get_fh(vio), buf, count);
308#else
309 return -1;
310#endif
311}
312
313ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
314#ifdef _CAN_OPERATE
315 return write(roar_vio_get_fh(vio), buf, count);
316#else
317 return -1;
318#endif
319}
320
321off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
322#ifdef _CAN_OPERATE
323 return lseek(roar_vio_get_fh(vio), offset, whence);
324#else
325 return -1;
326#endif
327}
328
329int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
330 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
331  return -1;
332
333 if ( state == ROAR_SOCKET_NONBLOCK )
334  return 0;
335
336 roar_vio_sync(vio);
337
338 return 0;
339}
340
341int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
342#ifdef ROAR_FDATASYNC
343 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
344#else
345 return 0;
346#endif
347}
348
349int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
350 struct roar_vio_sysio_ioctl * sysioctl;
351 int tmp;
352 int s_r = 0, s_w = 0;
353
354 if ( vio == NULL || cmd == -1 )
355  return -1;
356
357 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
358
359 switch (cmd) {
360  case ROAR_VIO_CTL_GET_NAME:
361    if ( data == NULL )
362     return -1;
363
364    *(char**)data = "basic";
365    return 0;
366   break;
367  case ROAR_VIO_CTL_GET_FH:
368  case ROAR_VIO_CTL_GET_READ_FH:
369  case ROAR_VIO_CTL_GET_WRITE_FH:
370  case ROAR_VIO_CTL_GET_SELECT_FH:
371  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
372  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
373    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));
374    *(int*)data = roar_vio_get_fh(vio);
375    return 0;
376   break;
377  case ROAR_VIO_CTL_SET_NOSYNC:
378    vio->sync = NULL;
379    return 0;
380   break;
381  case ROAR_VIO_CTL_ACCEPT:
382    tmp = accept(roar_vio_get_fh(vio), NULL, 0);
383    if ( tmp == -1 )
384     return -1;
385
386    // most proably a socket.
387    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
388#ifdef ROAR_TARGET_WIN32
389     closesocket(tmp);
390#else
391     close(tmp);
392#endif
393     return -1;
394    }
395
396    return 0;
397   break;
398  case ROAR_VIO_CTL_SHUTDOWN:
399    tmp = *(int*)data;
400
401    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
402     s_r = 1;
403     tmp -= ROAR_VIO_SHUTDOWN_READ;
404    }
405
406    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
407     s_w = 1;
408     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
409    }
410
411    if ( tmp != 0 ) /* we currently only support R and W shutdowns */
412     return -1;
413
414    if ( s_r && s_w ) {
415     tmp = SHUT_RDWR;
416    } else if ( s_r ) {
417     tmp = SHUT_RD;
418    } else if ( s_w ) {
419     tmp = SHUT_WR;
420    } else {
421     return 0; // nothing to do.
422    }
423
424    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
425   break;
426  case ROAR_VIO_CTL_SYSIO_IOCTL:
427    sysioctl = data;
428    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
429   break;
430 }
431
432 return -1;
433}
434
435int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
436#ifdef _CAN_OPERATE
437 if ( roar_vio_get_fh(vio) != -1 )
438  return close(roar_vio_get_fh(vio));
439
440 return 0;
441#else
442 return -1;
443#endif
444}
445
446// null
447ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
448 if ( vio == NULL || buf == NULL )
449  return -1;
450
451 return 0;
452}
453
454int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
455 return 0;
456}
457
458// pass
459int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
460 if ( calls == NULL )
461  return -1;
462
463 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
464
465 calls->read     = roar_vio_pass_read;
466 calls->write    = roar_vio_pass_write;
467 calls->lseek    = roar_vio_pass_lseek;
468 calls->nonblock = roar_vio_pass_nonblock;
469 calls->sync     = roar_vio_pass_sync;
470 calls->ctl      = roar_vio_pass_ctl;
471 calls->close    = roar_vio_pass_close;
472
473 calls->inst     = dst;
474
475 return 0;
476}
477
478ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
479 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
480}
481
482ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
483 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
484}
485
486off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
487 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
488}
489
490int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
491 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
492}
493
494int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
495 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
496}
497
498int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
499 if (vio == NULL || cmd == -1)
500  return -1;
501
502 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
503
504 switch (cmd) {
505  case ROAR_VIO_CTL_GET_NAME:
506    if ( data == NULL )
507     return -1;
508
509    *(char**)data = "pass";
510    return 0;
511   break;
512  case ROAR_VIO_CTL_GET_NEXT:
513    *(struct roar_vio_calls **)data = vio->inst;
514    return 0;
515   break;
516  case ROAR_VIO_CTL_SET_NEXT:
517    vio->inst = *(struct roar_vio_calls **)data;
518    return 0;
519   break;
520 }
521
522 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
523}
524
525int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
526 return roar_vio_close((struct roar_vio_calls *) vio->inst);
527}
528
529
530// re
531int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
532 if ( roar_vio_open_pass(calls, dst) == -1 )
533  return -1;
534
535 calls->read  = roar_vio_re_read;
536 calls->write = roar_vio_re_write;
537 calls->lseek = roar_vio_re_lseek;
538
539 return 0;
540}
541ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
542  size_t len =  0;
543 ssize_t r   = -1;
544
545 if ( vio == NULL )
546  return -1;
547
548 if ( vio->inst == NULL )
549  return -1;
550
551 errno = 0;
552
553 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
554  len   += r;
555  buf   += r;
556  count -= r;
557  if ( count == 0 )
558   break;
559 }
560
561 if ( len == 0 && r == -1 )
562  return -1;
563
564 return len;
565}
566
567ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
568  size_t len =  0;
569 ssize_t r   = -1;
570
571 if ( vio == NULL )
572  return -1;
573
574 if ( vio->inst == NULL )
575  return -1;
576
577 errno = 0;
578
579 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
580  len   += r;
581  buf   += r;
582  count -= r;
583  if ( count == 0 )
584   break;
585 }
586
587 if ( len == 0 && r == -1 )
588  return -1;
589
590 return len;
591}
592
593// TODO: we should do a some more intelgent thing here.
594off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
595 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
596}
597
598//ll
Note: See TracBrowser for help on using the repository browser.