source: roaraudio/libroar/vio.c @ 3795:6d3ee2c1c22f

Last change on this file since 3795:6d3ee2c1c22f was 3795:6d3ee2c1c22f, checked in by phi, 14 years ago

support ROAR_VIO_CTL_GET_NAME

File size: 13.0 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
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
184// converters:
185int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
186#ifdef _CAN_OPERATE
187 int fh;
188
189 if ( calls == NULL || filename == NULL )
190  return -1;
191
192#ifdef ROAR_TARGET_WIN32
193 flags |= O_BINARY;
194#endif
195
196 if ( (fh = open(filename, flags, mode)) == -1 )
197  return -1;
198
199 if ( roar_vio_open_fh(calls, fh) == -1 ) {
200  close(fh);
201  return -1;
202 }
203
204 return 0;
205#else
206 return -1;
207#endif
208}
209
210int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
211 if ( calls == NULL )
212  return -1;
213
214 if ( roar_vio_init_calls(calls) == -1 )
215  return -1;
216
217 return roar_vio_set_fh(calls, fh);
218}
219
220int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
221 if ( calls == NULL )
222  return -1;
223
224 if ( roar_vio_open_fh(calls, fh) == -1 )
225  return -1;
226
227#ifdef ROAR_TARGET_WIN32
228 calls->read     = roar_vio_winsock_read;
229 calls->write    = roar_vio_winsock_write;
230 calls->nonblock = roar_vio_winsock_nonblock;
231 calls->sync     = roar_vio_winsock_sync;
232 calls->ctl      = roar_vio_winsock_ctl;
233 calls->close    = roar_vio_winsock_close;
234#else
235 calls->sync     = roar_vio_null_sync;
236#endif
237
238 return 0;
239}
240
241int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
242 int fh;
243
244 if ( calls == NULL )
245  return -1;
246
247 if ( (fh = roar_socket_connect(host, port)) == -1 )
248  return -1;
249
250 return roar_vio_open_fh_socket(calls, fh);
251}
252
253int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
254 int fh;
255
256 if ( calls == NULL )
257  return -1;
258
259 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
260  return -1;
261
262 return roar_vio_open_fh_socket(calls, fh);
263}
264
265int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
266                                                               char * server, int dir, char * name) {
267 int fh;
268
269 if ( calls == NULL )
270  return -1;
271
272 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
273  return -1;
274
275 return roar_vio_open_fh_socket(calls, fh);
276}
277
278int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
279                                        struct roar_connection * con,
280                                        struct roar_stream * s,
281                                        int rate, int channels, int bits, int codec, int dir) {
282 int fh;
283
284 if ( calls == NULL )
285  return -1;
286
287 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 )
288  return -1;
289
290 return roar_vio_open_fh_socket(calls, fh);
291}
292
293// VIOs:
294
295// basic
296ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
297#ifdef _CAN_OPERATE
298 return read(roar_vio_get_fh(vio), buf, count);
299#else
300 return -1;
301#endif
302}
303
304ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
305#ifdef _CAN_OPERATE
306 return write(roar_vio_get_fh(vio), buf, count);
307#else
308 return -1;
309#endif
310}
311
312off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
313#ifdef _CAN_OPERATE
314 return lseek(roar_vio_get_fh(vio), offset, whence);
315#else
316 return -1;
317#endif
318}
319
320int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
321 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
322  return -1;
323
324 if ( state == ROAR_SOCKET_NONBLOCK )
325  return 0;
326
327 roar_vio_sync(vio);
328
329 return 0;
330}
331
332int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
333#ifdef ROAR_FDATASYNC
334 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
335#else
336 return 0;
337#endif
338}
339
340int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
341 int fh;
342
343 if ( vio == NULL || cmd == -1 )
344  return -1;
345
346 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
347
348 switch (cmd) {
349  case ROAR_VIO_CTL_GET_NAME:
350    if ( data == NULL )
351     return -1;
352
353    *(char**)data = "basic";
354    return 0;
355   break;
356  case ROAR_VIO_CTL_GET_FH:
357  case ROAR_VIO_CTL_GET_READ_FH:
358  case ROAR_VIO_CTL_GET_WRITE_FH:
359  case ROAR_VIO_CTL_GET_SELECT_FH:
360  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
361  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
362    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));
363    *(int*)data = roar_vio_get_fh(vio);
364    return 0;
365   break;
366  case ROAR_VIO_CTL_SET_NOSYNC:
367    vio->sync = NULL;
368    return 0;
369   break;
370  case ROAR_VIO_CTL_ACCEPT:
371    fh = accept(roar_vio_get_fh(vio), NULL, 0);
372    if ( fh == -1 )
373     return -1;
374
375    // most proably a socket.
376    if ( roar_vio_open_fh_socket(data, fh) == -1 ) {
377#ifdef ROAR_TARGET_WIN32
378     closesocket(fh);
379#else
380     close(fh);
381#endif
382     return -1;
383    }
384
385    return 0;
386   break;
387 }
388
389 return -1;
390}
391
392int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
393#ifdef _CAN_OPERATE
394 if ( roar_vio_get_fh(vio) != -1 )
395  return close(roar_vio_get_fh(vio));
396
397 return 0;
398#else
399 return -1;
400#endif
401}
402
403// null
404ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
405 if ( vio == NULL || buf == NULL )
406  return -1;
407
408 return 0;
409}
410
411int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
412 return 0;
413}
414
415// pass
416int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
417 if ( calls == NULL )
418  return -1;
419
420 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
421
422 calls->read     = roar_vio_pass_read;
423 calls->write    = roar_vio_pass_write;
424 calls->lseek    = roar_vio_pass_lseek;
425 calls->nonblock = roar_vio_pass_nonblock;
426 calls->sync     = roar_vio_pass_sync;
427 calls->ctl      = roar_vio_pass_ctl;
428 calls->close    = roar_vio_pass_close;
429
430 calls->inst     = dst;
431
432 return 0;
433}
434
435ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
436 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
437}
438
439ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
440 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
441}
442
443off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
444 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
445}
446
447int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
448 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
449}
450
451int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
452 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
453}
454
455int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
456 if (vio == NULL || cmd == -1)
457  return -1;
458
459 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
460
461 switch (cmd) {
462  case ROAR_VIO_CTL_GET_NAME:
463    if ( data == NULL )
464     return -1;
465
466    *(char**)data = "pass";
467    return 0;
468   break;
469  case ROAR_VIO_CTL_GET_NEXT:
470    *(struct roar_vio_calls **)data = vio->inst;
471    return 0;
472   break;
473  case ROAR_VIO_CTL_SET_NEXT:
474    vio->inst = *(struct roar_vio_calls **)data;
475    return 0;
476   break;
477 }
478
479 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
480}
481
482int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
483 return roar_vio_close((struct roar_vio_calls *) vio->inst);
484}
485
486
487// re
488int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
489 if ( roar_vio_open_pass(calls, dst) == -1 )
490  return -1;
491
492 calls->read  = roar_vio_re_read;
493 calls->write = roar_vio_re_write;
494 calls->lseek = roar_vio_re_lseek;
495
496 return 0;
497}
498ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
499  size_t len =  0;
500 ssize_t r   = -1;
501
502 if ( vio == NULL )
503  return -1;
504
505 if ( vio->inst == NULL )
506  return -1;
507
508 errno = 0;
509
510 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
511  len   += r;
512  buf   += r;
513  count -= r;
514  if ( count == 0 )
515   break;
516 }
517
518 if ( len == 0 && r == -1 )
519  return -1;
520
521 return len;
522}
523
524ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
525  size_t len =  0;
526 ssize_t r   = -1;
527
528 if ( vio == NULL )
529  return -1;
530
531 if ( vio->inst == NULL )
532  return -1;
533
534 errno = 0;
535
536 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
537  len   += r;
538  buf   += r;
539  count -= r;
540  if ( count == 0 )
541   break;
542 }
543
544 if ( len == 0 && r == -1 )
545  return -1;
546
547 return len;
548}
549
550// TODO: we should do a some more intelgent thing here.
551off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
552 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
553}
554
555//ll
Note: See TracBrowser for help on using the repository browser.