source: roaraudio/libroar/vio.c @ 3812:acdaec327ece

Last change on this file since 3812:acdaec327ece was 3812:acdaec327ece, checked in by phi, 14 years ago

fixed some copyright statements

File size: 13.7 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
38#ifdef ROAR_HAVE_IO_POSIX
39#define _CAN_OPERATE
40#endif
41
42int roar_vio_init_calls (struct roar_vio_calls * calls) {
43#ifdef _CAN_OPERATE
44 if ( calls == NULL )
45  return -1;
46
47 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
48
49/*
50 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
51 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
52 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
53*/
54
55 calls->read     = roar_vio_basic_read;
56 calls->write    = roar_vio_basic_write;
57 calls->lseek    = roar_vio_basic_lseek;
58 calls->nonblock = roar_vio_basic_nonblock;
59 calls->sync     = roar_vio_basic_sync;
60 calls->ctl      = roar_vio_basic_ctl;
61 calls->close    = roar_vio_basic_close;
62
63 return 0;
64#else
65 return -1;
66#endif
67}
68
69int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
70 if ( vio == NULL )
71  return -1;
72
73 vio->inst = inst;
74
75 return 0;
76}
77
78int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
79 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
80}
81
82int roar_vio_get_fh   (struct roar_vio_calls * vio) {
83 if ( vio == NULL )
84  return -1;
85
86 return ((int)(ROAR_INSTINT)vio->inst) - 1;
87}
88
89
90ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
91 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
92
93 if ( vio == NULL )
94  return -1;
95
96 if ( vio->read == NULL )
97  return -1;
98
99 return vio->read(vio, buf, count);
100}
101
102ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
103 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
104
105 if ( vio == NULL )
106  return -1;
107
108 if ( vio->write == NULL )
109  return -1;
110
111 return vio->write(vio, buf, count);
112}
113
114off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
115 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%u, whence=%i) = ?", vio, (unsigned int)offset, whence);
116
117 if ( vio == NULL )
118  return -1;
119
120 if ( vio->lseek == NULL )
121  return -1;
122
123 return vio->lseek(vio, offset, whence);
124}
125
126int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
127 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
128
129 if ( vio == NULL )
130  return -1;
131
132 if ( vio->nonblock == NULL )
133  return -1;
134
135 return vio->nonblock(vio, state);
136}
137
138int     roar_vio_sync    (struct roar_vio_calls * vio) {
139 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
140
141 if ( vio == NULL )
142  return -1;
143
144 if ( vio->sync == NULL )
145  return -1;
146
147 return vio->sync(vio);
148}
149
150int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
151 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
152
153 if ( vio == NULL )
154  return -1;
155
156 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
157
158 if ( vio->ctl == NULL )
159  return -1;
160
161 return vio->ctl(vio, cmd, data);
162}
163
164int     roar_vio_close    (struct roar_vio_calls * vio) {
165 ROAR_DBG("roar_vio_close(vio=%p) = ?", vio);
166
167 if ( vio == NULL )
168  return -1;
169
170 if ( vio->close == NULL )
171  return -1;
172
173 return vio->close(vio);
174}
175
176// specal commands:
177int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
178 if (dst == NULL || calls == NULL)
179  return -1;
180
181 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
182}
183
184int     roar_vio_shutdown(struct roar_vio_calls * vio,   int how) {
185 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
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
282int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
283                                        struct roar_connection * con,
284                                        struct roar_stream * s,
285                                        int rate, int channels, int bits, int codec, int dir) {
286 int fh;
287
288 if ( calls == NULL )
289  return -1;
290
291 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 )
292  return -1;
293
294 return roar_vio_open_fh_socket(calls, fh);
295}
296
297// VIOs:
298
299// basic
300ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
301#ifdef _CAN_OPERATE
302 return read(roar_vio_get_fh(vio), buf, count);
303#else
304 return -1;
305#endif
306}
307
308ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
309#ifdef _CAN_OPERATE
310 return write(roar_vio_get_fh(vio), buf, count);
311#else
312 return -1;
313#endif
314}
315
316off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
317#ifdef _CAN_OPERATE
318 return lseek(roar_vio_get_fh(vio), offset, whence);
319#else
320 return -1;
321#endif
322}
323
324int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
325 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
326  return -1;
327
328 if ( state == ROAR_SOCKET_NONBLOCK )
329  return 0;
330
331 roar_vio_sync(vio);
332
333 return 0;
334}
335
336int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
337#ifdef ROAR_FDATASYNC
338 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
339#else
340 return 0;
341#endif
342}
343
344int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
345 int tmp;
346 int s_r = 0, s_w = 0;
347
348 if ( vio == NULL || cmd == -1 )
349  return -1;
350
351 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
352
353 switch (cmd) {
354  case ROAR_VIO_CTL_GET_NAME:
355    if ( data == NULL )
356     return -1;
357
358    *(char**)data = "basic";
359    return 0;
360   break;
361  case ROAR_VIO_CTL_GET_FH:
362  case ROAR_VIO_CTL_GET_READ_FH:
363  case ROAR_VIO_CTL_GET_WRITE_FH:
364  case ROAR_VIO_CTL_GET_SELECT_FH:
365  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
366  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
367    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));
368    *(int*)data = roar_vio_get_fh(vio);
369    return 0;
370   break;
371  case ROAR_VIO_CTL_SET_NOSYNC:
372    vio->sync = NULL;
373    return 0;
374   break;
375  case ROAR_VIO_CTL_ACCEPT:
376    tmp = accept(roar_vio_get_fh(vio), NULL, 0);
377    if ( tmp == -1 )
378     return -1;
379
380    // most proably a socket.
381    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
382#ifdef ROAR_TARGET_WIN32
383     closesocket(tmp);
384#else
385     close(tmp);
386#endif
387     return -1;
388    }
389
390    return 0;
391   break;
392  case ROAR_VIO_CTL_SHUTDOWN:
393    tmp = *(int*)data;
394
395    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
396     s_r = 1;
397     tmp -= ROAR_VIO_SHUTDOWN_READ;
398    }
399
400    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
401     s_w = 1;
402     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
403    }
404
405    if ( tmp != 0 ) /* we currently only support R and W shutdowns */
406     return -1;
407
408    if ( s_r && s_w ) {
409     tmp = SHUT_RDWR;
410    } else if ( s_r ) {
411     tmp = SHUT_RD;
412    } else if ( s_w ) {
413     tmp = SHUT_WR;
414    } else {
415     return 0; // nothing to do.
416    }
417
418    return shutdown(roar_vio_get_fh(vio), tmp);
419   break;
420 }
421
422 return -1;
423}
424
425int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
426#ifdef _CAN_OPERATE
427 if ( roar_vio_get_fh(vio) != -1 )
428  return close(roar_vio_get_fh(vio));
429
430 return 0;
431#else
432 return -1;
433#endif
434}
435
436// null
437ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
438 if ( vio == NULL || buf == NULL )
439  return -1;
440
441 return 0;
442}
443
444int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
445 return 0;
446}
447
448// pass
449int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
450 if ( calls == NULL )
451  return -1;
452
453 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
454
455 calls->read     = roar_vio_pass_read;
456 calls->write    = roar_vio_pass_write;
457 calls->lseek    = roar_vio_pass_lseek;
458 calls->nonblock = roar_vio_pass_nonblock;
459 calls->sync     = roar_vio_pass_sync;
460 calls->ctl      = roar_vio_pass_ctl;
461 calls->close    = roar_vio_pass_close;
462
463 calls->inst     = dst;
464
465 return 0;
466}
467
468ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
469 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
470}
471
472ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
473 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
474}
475
476off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
477 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
478}
479
480int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
481 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
482}
483
484int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
485 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
486}
487
488int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
489 if (vio == NULL || cmd == -1)
490  return -1;
491
492 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
493
494 switch (cmd) {
495  case ROAR_VIO_CTL_GET_NAME:
496    if ( data == NULL )
497     return -1;
498
499    *(char**)data = "pass";
500    return 0;
501   break;
502  case ROAR_VIO_CTL_GET_NEXT:
503    *(struct roar_vio_calls **)data = vio->inst;
504    return 0;
505   break;
506  case ROAR_VIO_CTL_SET_NEXT:
507    vio->inst = *(struct roar_vio_calls **)data;
508    return 0;
509   break;
510 }
511
512 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
513}
514
515int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
516 return roar_vio_close((struct roar_vio_calls *) vio->inst);
517}
518
519
520// re
521int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
522 if ( roar_vio_open_pass(calls, dst) == -1 )
523  return -1;
524
525 calls->read  = roar_vio_re_read;
526 calls->write = roar_vio_re_write;
527 calls->lseek = roar_vio_re_lseek;
528
529 return 0;
530}
531ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
532  size_t len =  0;
533 ssize_t r   = -1;
534
535 if ( vio == NULL )
536  return -1;
537
538 if ( vio->inst == NULL )
539  return -1;
540
541 errno = 0;
542
543 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
544  len   += r;
545  buf   += r;
546  count -= r;
547  if ( count == 0 )
548   break;
549 }
550
551 if ( len == 0 && r == -1 )
552  return -1;
553
554 return len;
555}
556
557ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
558  size_t len =  0;
559 ssize_t r   = -1;
560
561 if ( vio == NULL )
562  return -1;
563
564 if ( vio->inst == NULL )
565  return -1;
566
567 errno = 0;
568
569 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
570  len   += r;
571  buf   += r;
572  count -= r;
573  if ( count == 0 )
574   break;
575 }
576
577 if ( len == 0 && r == -1 )
578  return -1;
579
580 return len;
581}
582
583// TODO: we should do a some more intelgent thing here.
584off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
585 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
586}
587
588//ll
Note: See TracBrowser for help on using the repository browser.