source: roaraudio/libroar/vio.c @ 3852:adfa773b39a8

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

added support vor (sysio) ioctl() to VIOs

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