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