[1326] | 1 | //vio_stack.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
[1326] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1326] | 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 | |
---|
[1327] | 38 | int roar_vio_open_stack (struct roar_vio_calls * calls) { |
---|
[4975] | 39 | return roar_vio_open_stack2(calls, free); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | int roar_vio_open_stack2 (struct roar_vio_calls * calls, void (*func)(void*)) { |
---|
[1327] | 43 | struct roar_vio_stack * self; |
---|
| 44 | |
---|
[4975] | 45 | if ( calls == NULL ) { |
---|
| 46 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 47 | return -1; |
---|
[4975] | 48 | } |
---|
| 49 | |
---|
| 50 | if ( func == NULL ) |
---|
| 51 | func = roar_mm_free_retvoid; |
---|
[1327] | 52 | |
---|
[3063] | 53 | if ( (self = roar_mm_malloc(sizeof(struct roar_vio_stack))) == NULL ) |
---|
[1327] | 54 | return -1; |
---|
| 55 | |
---|
| 56 | memset(self, 0, sizeof(struct roar_vio_stack)); |
---|
| 57 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
[5388] | 58 | calls->flags = ROAR_VIO_FLAGS_NONE; |
---|
| 59 | calls->refc = 1; |
---|
[1327] | 60 | |
---|
[4975] | 61 | self->free = func; |
---|
| 62 | |
---|
[1327] | 63 | calls->inst = self; |
---|
| 64 | calls->close = roar_vio_stack_close; |
---|
[1505] | 65 | calls->ctl = roar_vio_stack_ctl; |
---|
[1327] | 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 | |
---|
| 74 | int roar_vio_stack_add (struct roar_vio_calls * calls, struct roar_vio_calls * vio) { |
---|
| 75 | struct roar_vio_stack * self; |
---|
| 76 | |
---|
[4975] | 77 | if ( calls == NULL || vio == NULL ) { |
---|
| 78 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 79 | return -1; |
---|
[4975] | 80 | } |
---|
[1327] | 81 | |
---|
[4975] | 82 | if ( (self = calls->inst) == NULL ) { |
---|
| 83 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 84 | return -1; |
---|
[4975] | 85 | } |
---|
[1327] | 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 | |
---|
| 95 | int roar_vio_stack_close (struct roar_vio_calls * vio) { |
---|
| 96 | struct roar_vio_stack * self; |
---|
| 97 | int i; |
---|
| 98 | |
---|
[4975] | 99 | if ( vio == NULL ) { |
---|
| 100 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 101 | return -1; |
---|
[4975] | 102 | } |
---|
[1327] | 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++) |
---|
[4975] | 112 | self->free(self->calls[i]); |
---|
[1327] | 113 | } |
---|
| 114 | |
---|
[3063] | 115 | roar_mm_free(self); |
---|
[1327] | 116 | |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
[5278] | 120 | int roar_vio_stack_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[1505] | 121 | if (vio == NULL || cmd == -1) |
---|
| 122 | return -1; |
---|
| 123 | |
---|
| 124 | switch (cmd) { |
---|
[3795] | 125 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 126 | if ( data == NULL ) |
---|
| 127 | return -1; |
---|
| 128 | |
---|
| 129 | *(char**)data = "stack"; |
---|
| 130 | return 0; |
---|
| 131 | break; |
---|
[1505] | 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; |
---|
[4527] | 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; |
---|
[1505] | 142 | } |
---|
| 143 | |
---|
[3282] | 144 | return roar_vio_ctl(((struct roar_vio_stack*)(vio->inst))->cur, cmd, data); |
---|
[1505] | 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[1327] | 148 | ssize_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 | |
---|
| 161 | ssize_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 | |
---|
[5278] | 174 | roar_off_t roar_vio_stack_lseek (struct roar_vio_calls * vio, roar_off_t offset, int whence) { |
---|
[1327] | 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 | |
---|
| 187 | int 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 | |
---|
[1326] | 200 | //ll |
---|