source: roaraudio/roard/container_framework.c @ 2672:7c6d666bfd79

Last change on this file since 2672:7c6d666bfd79 was 2672:7c6d666bfd79, checked in by phi, 15 years ago

added new pcb (*new_child)(*), wrote cont_fw_new_child()

File size: 4.4 KB
Line 
1//container_framework.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of roard 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 *  RoarAudio 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 */
24
25#include "roard.h"
26
27#define _DECL()  struct cont_fw_child_vio_inst * inst
28#define _CHECK() if ( vio == NULL ) \
29                 return -1
30#define _CINST() if ( (inst = vio->inst) == NULL ) \
31                  return -1
32#define _PREP()  _CHECK(); \
33                 _CINST()
34#define _BASIC() _DECL();  \
35                 _PREP()
36
37// Parent:
38int     cont_fw_new     (struct cont_fw_parent_inst ** inst) {
39 struct cont_fw_parent_inst * self;
40
41 if ( inst == NULL )
42  return -1;
43
44 if ( (self = malloc(sizeof(struct cont_fw_parent_inst))) == NULL ) {
45  *inst = NULL;
46  return -1;
47 }
48
49 memset(self, 0, sizeof(struct cont_fw_parent_inst));
50
51 *inst = self;
52 return 0;
53}
54
55int     cont_fw_delete  (struct cont_fw_parent_inst  * inst) {
56 int i;
57
58 if ( inst == NULL )
59  return -1;
60
61 // check if there are streams to close...
62 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
63  if ( inst->child[i] != NULL ) {
64   return -1;
65  }
66 }
67
68 if ( inst->pcb.close != NULL )
69  inst->pcb.close(inst);
70
71 free(inst);
72
73 return 0;
74}
75
76int     cont_fw_set_uinst(struct cont_fw_parent_inst  * inst, void  * u_inst) {
77 if ( inst == NULL )
78  return -1;
79
80 inst->u_inst = u_inst;
81
82 return 0;
83}
84
85int     cont_fw_get_uinst(struct cont_fw_parent_inst  * inst, void ** u_inst) {
86 if ( inst == NULL || u_inst == NULL )
87  return -1;
88
89 *u_inst = inst->u_inst;
90
91 return 0;
92}
93
94// VIOs:
95int     cont_fw_new_child(struct cont_fw_parent_inst  * inst, int id) {
96 struct cont_fw_child_vio_inst * self;
97 int i;
98 int cid = -1;
99
100 if ( inst == NULL || id == -1 )
101  return -1;
102
103 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
104  if ( inst->child[i] == NULL ) {
105   cid = i;
106   break;
107  }
108 }
109
110 if ( cid == -1 )
111  return -1;
112
113 if ( (self = malloc(sizeof(struct cont_fw_child_vio_inst))) == NULL )
114  return -1;
115
116 memset(self, 0, sizeof(struct cont_fw_child_vio_inst));
117
118 self->parent = inst;
119 self->child  = id;
120 self->u_inst = NULL;
121
122 inst->child[i] = self;
123
124 if ( inst->pcb.new_child != NULL ) {
125  if ( inst->pcb.new_child(inst, self) == -1 ) {
126   inst->child[i] = NULL;
127   free(self);
128   return -1;
129  }
130 }
131
132 return 0;
133}
134
135int     cont_fw_init_vio(struct roar_vio_calls * vio, void * inst) {
136 if ( vio == NULL )
137  return -1;
138
139 memset(vio, 0, sizeof(struct roar_vio_calls));
140 vio->inst = inst;
141
142 vio->read  = cont_fw_read;
143 vio->write = cont_fw_write;
144 vio->sync  = cont_fw_sync;
145 vio->close = cont_fw_close;
146
147 return 0;
148}
149
150ssize_t cont_fw_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
151 _BASIC();
152
153 if ( inst->parent->ccb.read != NULL )
154  return inst->parent->ccb.read(inst->parent, inst, buf, count);
155
156 return -1;
157}
158
159ssize_t cont_fw_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
160 _BASIC();
161
162 if ( inst->parent->ccb.write != NULL )
163  return inst->parent->ccb.write(inst->parent, inst, buf, count);
164
165 return -1;
166}
167
168off_t   cont_fw_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
169int     cont_fw_nonblock(struct roar_vio_calls * vio, int state);
170
171int     cont_fw_sync    (struct roar_vio_calls * vio) {
172 _BASIC();
173
174 if ( inst->parent->ccb.flush != NULL )
175  return inst->parent->ccb.flush(inst->parent, inst);
176
177 return 0;
178}
179
180int     cont_fw_close   (struct roar_vio_calls * vio) {
181 _DECL();
182 int r = 0;
183 int i;
184
185 _PREP();
186
187 if ( cont_fw_sync(vio) == -1 )
188  r = -1;
189
190 if ( inst->parent->ccb.close != NULL )
191  return inst->parent->ccb.close(inst->parent, inst);
192
193 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
194  if ( inst->parent->child[i] == inst ) {
195   inst->parent->child[i] = NULL;
196  }
197 }
198
199 free(inst);
200
201 return  r;
202}
203
204int     cont_fw_ctl     (struct roar_vio_calls * vio, int cmd, void * data);
205
206//ll
Note: See TracBrowser for help on using the repository browser.