source: roaraudio/libroar/vio.c @ 3860:96075c123fec

Last change on this file since 3860:96075c123fec was 3860:96075c123fec, checked in by phi, 14 years ago

use roar_libroar_*warn()

File size: 14.1 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 roar_libroar_nowarn();
278 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 ) {
279  roar_libroar_warn();
280  return -1;
281 }
282 roar_libroar_warn();
283
284 return roar_vio_open_fh_socket(calls, fh);
285}
286
287int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
288                                        struct roar_connection * con,
289                                        struct roar_stream * s,
290                                        int rate, int channels, int bits, int codec, int dir) {
291 struct roar_stream stream;
292 int fh;
293
294 if ( calls == NULL )
295  return -1;
296
297 if ( s == NULL )
298  s = &stream;
299
300 roar_libroar_nowarn();
301 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 ) {
302  roar_libroar_warn();
303  return -1;
304 }
305 roar_libroar_warn();
306
307 return roar_vio_open_fh_socket(calls, fh);
308}
309
310// VIOs:
311
312// basic
313ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
314#ifdef _CAN_OPERATE
315 return read(roar_vio_get_fh(vio), buf, count);
316#else
317 return -1;
318#endif
319}
320
321ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
322#ifdef _CAN_OPERATE
323 return write(roar_vio_get_fh(vio), buf, count);
324#else
325 return -1;
326#endif
327}
328
329off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
330#ifdef _CAN_OPERATE
331 return lseek(roar_vio_get_fh(vio), offset, whence);
332#else
333 return -1;
334#endif
335}
336
337int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
338 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
339  return -1;
340
341 if ( state == ROAR_SOCKET_NONBLOCK )
342  return 0;
343
344 roar_vio_sync(vio);
345
346 return 0;
347}
348
349int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
350#ifdef ROAR_FDATASYNC
351 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
352#else
353 return 0;
354#endif
355}
356
357int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
358 struct roar_vio_sysio_ioctl * sysioctl;
359 int tmp;
360 int s_r = 0, s_w = 0;
361
362 if ( vio == NULL || cmd == -1 )
363  return -1;
364
365 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
366
367 switch (cmd) {
368  case ROAR_VIO_CTL_GET_NAME:
369    if ( data == NULL )
370     return -1;
371
372    *(char**)data = "basic";
373    return 0;
374   break;
375  case ROAR_VIO_CTL_GET_FH:
376  case ROAR_VIO_CTL_GET_READ_FH:
377  case ROAR_VIO_CTL_GET_WRITE_FH:
378  case ROAR_VIO_CTL_GET_SELECT_FH:
379  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
380  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
381    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));
382    *(int*)data = roar_vio_get_fh(vio);
383    return 0;
384   break;
385  case ROAR_VIO_CTL_SET_NOSYNC:
386    vio->sync = NULL;
387    return 0;
388   break;
389  case ROAR_VIO_CTL_ACCEPT:
390    tmp = accept(roar_vio_get_fh(vio), NULL, 0);
391    if ( tmp == -1 )
392     return -1;
393
394    // most proably a socket.
395    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
396#ifdef ROAR_TARGET_WIN32
397     closesocket(tmp);
398#else
399     close(tmp);
400#endif
401     return -1;
402    }
403
404    return 0;
405   break;
406  case ROAR_VIO_CTL_SHUTDOWN:
407    tmp = *(int*)data;
408
409    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
410     s_r = 1;
411     tmp -= ROAR_VIO_SHUTDOWN_READ;
412    }
413
414    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
415     s_w = 1;
416     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
417    }
418
419    if ( tmp != 0 ) /* we currently only support R and W shutdowns */
420     return -1;
421
422    if ( s_r && s_w ) {
423     tmp = SHUT_RDWR;
424    } else if ( s_r ) {
425     tmp = SHUT_RD;
426    } else if ( s_w ) {
427     tmp = SHUT_WR;
428    } else {
429     return 0; // nothing to do.
430    }
431
432    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
433   break;
434  case ROAR_VIO_CTL_SYSIO_IOCTL:
435    sysioctl = data;
436    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
437   break;
438 }
439
440 return -1;
441}
442
443int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
444#ifdef _CAN_OPERATE
445 if ( roar_vio_get_fh(vio) != -1 )
446  return close(roar_vio_get_fh(vio));
447
448 return 0;
449#else
450 return -1;
451#endif
452}
453
454// null
455ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
456 if ( vio == NULL || buf == NULL )
457  return -1;
458
459 return 0;
460}
461
462int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
463 return 0;
464}
465
466// pass
467int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
468 if ( calls == NULL )
469  return -1;
470
471 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
472
473 calls->read     = roar_vio_pass_read;
474 calls->write    = roar_vio_pass_write;
475 calls->lseek    = roar_vio_pass_lseek;
476 calls->nonblock = roar_vio_pass_nonblock;
477 calls->sync     = roar_vio_pass_sync;
478 calls->ctl      = roar_vio_pass_ctl;
479 calls->close    = roar_vio_pass_close;
480
481 calls->inst     = dst;
482
483 return 0;
484}
485
486ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
487 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
488}
489
490ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
491 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
492}
493
494off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
495 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
496}
497
498int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
499 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
500}
501
502int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
503 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
504}
505
506int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
507 if (vio == NULL || cmd == -1)
508  return -1;
509
510 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
511
512 switch (cmd) {
513  case ROAR_VIO_CTL_GET_NAME:
514    if ( data == NULL )
515     return -1;
516
517    *(char**)data = "pass";
518    return 0;
519   break;
520  case ROAR_VIO_CTL_GET_NEXT:
521    *(struct roar_vio_calls **)data = vio->inst;
522    return 0;
523   break;
524  case ROAR_VIO_CTL_SET_NEXT:
525    vio->inst = *(struct roar_vio_calls **)data;
526    return 0;
527   break;
528 }
529
530 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
531}
532
533int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
534 return roar_vio_close((struct roar_vio_calls *) vio->inst);
535}
536
537
538// re
539int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
540 if ( roar_vio_open_pass(calls, dst) == -1 )
541  return -1;
542
543 calls->read  = roar_vio_re_read;
544 calls->write = roar_vio_re_write;
545 calls->lseek = roar_vio_re_lseek;
546
547 return 0;
548}
549ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
550  size_t len =  0;
551 ssize_t r   = -1;
552
553 if ( vio == NULL )
554  return -1;
555
556 if ( vio->inst == NULL )
557  return -1;
558
559 errno = 0;
560
561 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
562  len   += r;
563  buf   += r;
564  count -= r;
565  if ( count == 0 )
566   break;
567 }
568
569 if ( len == 0 && r == -1 )
570  return -1;
571
572 return len;
573}
574
575ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
576  size_t len =  0;
577 ssize_t r   = -1;
578
579 if ( vio == NULL )
580  return -1;
581
582 if ( vio->inst == NULL )
583  return -1;
584
585 errno = 0;
586
587 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
588  len   += r;
589  buf   += r;
590  count -= r;
591  if ( count == 0 )
592   break;
593 }
594
595 if ( len == 0 && r == -1 )
596  return -1;
597
598 return len;
599}
600
601// TODO: we should do a some more intelgent thing here.
602off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
603 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
604}
605
606//ll
Note: See TracBrowser for help on using the repository browser.