source: roaraudio/libroar/vio.c @ 1125:b4e6078fc699

Last change on this file since 1125:b4e6078fc699 was 1125:b4e6078fc699, checked in by phi, 15 years ago

fixed display of sync flag for driver streams: may not depend on fdatasync() and do not remove it from list after streams_set_sync() is called

File size: 5.8 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_vio_init_calls (struct roar_vio_calls * calls) {
38 if ( calls == NULL )
39  return -1;
40
41 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
42
43/*
44 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
45 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
46 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
47*/
48
49 calls->read     = roar_vio_basic_read;
50 calls->write    = roar_vio_basic_write;
51 calls->lseek    = roar_vio_basic_lseek;
52 calls->nonblock = roar_vio_basic_nonblock;
53 calls->sync     = roar_vio_basic_sync;
54
55 return 0;
56}
57
58int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
59 if ( vio == NULL )
60  return -1;
61
62 vio->inst = inst;
63
64 return 0;
65}
66
67int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
68 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
69}
70
71int roar_vio_get_fh   (struct roar_vio_calls * vio) {
72 if ( vio == NULL )
73  return -1;
74
75 return ((int)(ROAR_INSTINT)vio->inst) - 1;
76}
77
78
79ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
80 if ( vio == NULL )
81  return -1;
82
83 if ( vio->read == NULL )
84  return -1;
85
86 return vio->read(vio, buf, count);
87}
88
89ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
90 if ( vio == NULL )
91  return -1;
92
93 if ( vio->write == NULL )
94  return -1;
95
96 return vio->write(vio, buf, count);
97}
98
99off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
100 if ( vio == NULL )
101  return -1;
102
103 if ( vio->lseek == NULL )
104  return -1;
105
106 return vio->lseek(vio, offset, whence);
107}
108
109int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
110 if ( vio == NULL )
111  return -1;
112
113 if ( vio->nonblock == NULL )
114  return -1;
115
116 return vio->nonblock(vio, state);
117}
118
119int     roar_vio_sync    (struct roar_vio_calls * vio) {
120 if ( vio == NULL )
121  return -1;
122
123 if ( vio->sync == NULL )
124  return -1;
125
126 return vio->sync(vio);
127}
128
129// VIOs:
130
131// basic
132ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
133 return read(roar_vio_get_fh(vio), buf, count);
134}
135
136ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
137 return write(roar_vio_get_fh(vio), buf, count);
138}
139
140off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
141 return lseek(roar_vio_get_fh(vio), offset, whence);
142}
143
144int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
145 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
146  return -1;
147
148 if ( state == ROAR_SOCKET_NONBLOCK )
149  return 0;
150
151 roar_vio_sync(vio);
152
153 return 0;
154}
155
156int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
157 return fdatasync(roar_vio_get_fh(vio));
158}
159
160// null
161ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
162 if ( vio == NULL || buf == NULL )
163  return -1;
164
165 return 0;
166}
167
168// pass
169ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
170 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
171}
172
173ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
174 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
175}
176
177off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
178 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
179}
180
181
182// re
183ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
184  size_t len =  0;
185 ssize_t r   = -1;
186
187 if ( vio == NULL )
188  return -1;
189
190 if ( vio->inst == NULL )
191  return -1;
192
193 errno = 0;
194
195 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
196  len   += r;
197  buf   += r;
198  count -= r;
199  if ( count == 0 )
200   break;
201 }
202
203 if ( len == 0 && r == -1 )
204  return -1;
205
206 return len;
207}
208
209ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
210  size_t len =  0;
211 ssize_t r   = -1;
212
213 if ( vio == NULL )
214  return -1;
215
216 if ( vio->inst == NULL )
217  return -1;
218
219 errno = 0;
220
221 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
222  len   += r;
223  buf   += r;
224  count -= r;
225  if ( count == 0 )
226   break;
227 }
228
229 if ( len == 0 && r == -1 )
230  return -1;
231
232 return len;
233}
234
235// we should do a some more intelgent thing here.
236off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
237 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
238}
239
240
241//ll
Note: See TracBrowser for help on using the repository browser.