source: roaraudio/libroar/vio_stack.c @ 5388:e5cc8e03a3e1

Last change on this file since 5388:e5cc8e03a3e1 was 5388:e5cc8e03a3e1, checked in by phi, 10 years ago

Added support for refcount based VIOs as well as dynamically allocated VIOs (non-stack or global VIOs) (Closes: #127)

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