source: roaraudio/libroar/vio_stack.c @ 5278:b3e0dd3f3141

Last change on this file since 5278:b3e0dd3f3141 was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

File size: 5.0 KB
Line 
1//vio_stack.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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
38int     roar_vio_open_stack    (struct roar_vio_calls * calls) {
39 return roar_vio_open_stack2(calls, free);
40}
41
42int     roar_vio_open_stack2   (struct roar_vio_calls * calls, void (*func)(void*)) {
43 struct roar_vio_stack * self;
44
45 if ( calls == NULL ) {
46  roar_err_set(ROAR_ERROR_FAULT);
47  return -1;
48 }
49
50 if ( func == NULL )
51  func = roar_mm_free_retvoid;
52
53 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_stack))) == NULL )
54  return -1;
55
56 memset(self,  0, sizeof(struct roar_vio_stack));
57 memset(calls, 0, sizeof(struct roar_vio_calls));
58
59 self->free      = func;
60
61 calls->inst     = self;
62 calls->close    = roar_vio_stack_close;
63 calls->ctl      = roar_vio_stack_ctl;
64 calls->read     = roar_vio_stack_read;
65 calls->write    = roar_vio_stack_write;
66 calls->lseek    = roar_vio_stack_lseek;
67 calls->sync     = roar_vio_stack_sync;
68
69 return 0;
70}
71
72int     roar_vio_stack_add     (struct roar_vio_calls * calls, struct roar_vio_calls * vio) {
73 struct roar_vio_stack * self;
74
75 if ( calls == NULL || vio == NULL ) {
76  roar_err_set(ROAR_ERROR_FAULT);
77  return -1;
78 }
79
80 if ( (self = calls->inst) == NULL ) {
81  roar_err_set(ROAR_ERROR_FAULT);
82  return -1;
83 }
84
85 if ( self->next == ROAR_VIO_STACK_MAX )
86  return -1;
87
88 self->cur = self->calls[self->next++] = vio;
89
90 return 0;
91}
92
93int     roar_vio_stack_close   (struct roar_vio_calls * vio) {
94 struct roar_vio_stack * self;
95 int i;
96
97 if ( vio == NULL ) {
98  roar_err_set(ROAR_ERROR_FAULT);
99  return -1;
100 }
101
102 if ( (self = vio->inst) == NULL )
103  return -1;
104
105 if ( self->cur != NULL ) {
106  if ( roar_vio_close(self->cur) == -1 )
107   return -1;
108
109  for (i = 0; i < self->next; i++)
110   self->free(self->calls[i]);
111 }
112
113 roar_mm_free(self);
114
115 return 0;
116}
117
118int     roar_vio_stack_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
119 if (vio == NULL || cmd == -1)
120  return -1;
121
122 switch (cmd) {
123  case ROAR_VIO_CTL_GET_NAME:
124    if ( data == NULL )
125     return -1;
126
127    *(char**)data = "stack";
128    return 0;
129   break;
130  case ROAR_VIO_CTL_GET_FH:
131  case ROAR_VIO_CTL_GET_READ_FH:
132  case ROAR_VIO_CTL_GET_WRITE_FH:
133  case ROAR_VIO_CTL_SELECT:
134    return roar_vio_ctl(((struct roar_vio_stack*)(vio->inst))->cur, cmd, data);
135   break;
136  case ROAR_VIO_CTL_GET_NEXT:
137    *(struct roar_vio_calls **)data = ((struct roar_vio_stack*)(vio->inst))->cur;
138    return 0;
139   break;
140 }
141
142 return roar_vio_ctl(((struct roar_vio_stack*)(vio->inst))->cur, cmd, data);
143}
144
145
146ssize_t roar_vio_stack_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
147 if ( vio == NULL )
148  return -1;
149
150 if ( vio->inst == NULL )
151  return -1;
152
153 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
154  return -1;
155
156 return roar_vio_read(((struct roar_vio_stack*)(vio->inst))->cur, buf, count);
157}
158
159ssize_t roar_vio_stack_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
160 if ( vio == NULL )
161  return -1;
162
163 if ( vio->inst == NULL )
164  return -1;
165
166 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
167  return -1;
168
169 return roar_vio_write(((struct roar_vio_stack*)(vio->inst))->cur, buf, count);
170}
171
172roar_off_t   roar_vio_stack_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
173 if ( vio == NULL )
174  return -1;
175
176 if ( vio->inst == NULL )
177  return -1;
178
179 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
180  return -1;
181
182 return roar_vio_lseek(((struct roar_vio_stack*)(vio->inst))->cur, offset, whence);
183}
184
185int     roar_vio_stack_sync    (struct roar_vio_calls * vio) {
186 if ( vio == NULL )
187  return -1;
188
189 if ( vio->inst == NULL )
190  return -1;
191
192 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
193  return -1;
194
195 return roar_vio_sync(((struct roar_vio_stack*)(vio->inst))->cur);
196}
197
198//ll
Note: See TracBrowser for help on using the repository browser.