[1326] | 1 | //vio_stack.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
[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)); |
---|
| 58 | |
---|
[4975] | 59 | self->free = func; |
---|
| 60 | |
---|
[1327] | 61 | calls->inst = self; |
---|
| 62 | calls->close = roar_vio_stack_close; |
---|
[1505] | 63 | calls->ctl = roar_vio_stack_ctl; |
---|
[1327] | 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 | |
---|
| 72 | int roar_vio_stack_add (struct roar_vio_calls * calls, struct roar_vio_calls * vio) { |
---|
| 73 | struct roar_vio_stack * self; |
---|
| 74 | |
---|
[4975] | 75 | if ( calls == NULL || vio == NULL ) { |
---|
| 76 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 77 | return -1; |
---|
[4975] | 78 | } |
---|
[1327] | 79 | |
---|
[4975] | 80 | if ( (self = calls->inst) == NULL ) { |
---|
| 81 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 82 | return -1; |
---|
[4975] | 83 | } |
---|
[1327] | 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 | |
---|
| 93 | int roar_vio_stack_close (struct roar_vio_calls * vio) { |
---|
| 94 | struct roar_vio_stack * self; |
---|
| 95 | int i; |
---|
| 96 | |
---|
[4975] | 97 | if ( vio == NULL ) { |
---|
| 98 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1327] | 99 | return -1; |
---|
[4975] | 100 | } |
---|
[1327] | 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++) |
---|
[4975] | 110 | self->free(self->calls[i]); |
---|
[1327] | 111 | } |
---|
| 112 | |
---|
[3063] | 113 | roar_mm_free(self); |
---|
[1327] | 114 | |
---|
| 115 | return 0; |
---|
| 116 | } |
---|
| 117 | |
---|
[5278] | 118 | int roar_vio_stack_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[1505] | 119 | if (vio == NULL || cmd == -1) |
---|
| 120 | return -1; |
---|
| 121 | |
---|
| 122 | switch (cmd) { |
---|
[3795] | 123 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 124 | if ( data == NULL ) |
---|
| 125 | return -1; |
---|
| 126 | |
---|
| 127 | *(char**)data = "stack"; |
---|
| 128 | return 0; |
---|
| 129 | break; |
---|
[1505] | 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; |
---|
[4527] | 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; |
---|
[1505] | 140 | } |
---|
| 141 | |
---|
[3282] | 142 | return roar_vio_ctl(((struct roar_vio_stack*)(vio->inst))->cur, cmd, data); |
---|
[1505] | 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
[1327] | 146 | ssize_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 | |
---|
| 159 | ssize_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 | |
---|
[5278] | 172 | roar_off_t roar_vio_stack_lseek (struct roar_vio_calls * vio, roar_off_t offset, int whence) { |
---|
[1327] | 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 | |
---|
| 185 | int 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 | |
---|
[1326] | 198 | //ll |
---|