source: roaraudio/libroar/vio.c @ 3895:12c0c9a574cb

Last change on this file since 3895:12c0c9a574cb was 3895:12c0c9a574cb, checked in by phi, 14 years ago

test for ioctl()

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