[3138] | 1 | //libroaross.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
| 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, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "roaraudio.h" |
---|
| 36 | |
---|
| 37 | #if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS) |
---|
| 38 | #if defined(__OpenBSD__) || defined(__NetBSD__) |
---|
| 39 | #include <soundcard.h> |
---|
| 40 | #else |
---|
| 41 | #include <sys/soundcard.h> |
---|
| 42 | #endif |
---|
| 43 | #include <sys/ioctl.h> |
---|
| 44 | |
---|
| 45 | #ifdef ROAR_HAVE_H_SYS_TYPES |
---|
| 46 | #include <sys/types.h> |
---|
| 47 | #endif |
---|
| 48 | |
---|
| 49 | #ifdef ROAR_HAVE_H_FCNTL |
---|
| 50 | #include <fcntl.h> |
---|
| 51 | #endif |
---|
| 52 | |
---|
| 53 | #ifdef ROAR_HAVE_H_UNISTD |
---|
| 54 | #include <unistd.h> |
---|
| 55 | #endif |
---|
| 56 | |
---|
| 57 | #include <sys/stat.h> |
---|
[3139] | 58 | #include <dlfcn.h> |
---|
| 59 | |
---|
| 60 | #if defined(RTLD_NEXT) |
---|
| 61 | #define REAL_LIBC RTLD_NEXT |
---|
| 62 | #else |
---|
| 63 | #define REAL_LIBC ((void *) -1L) |
---|
| 64 | #endif |
---|
[3138] | 65 | |
---|
| 66 | #define _MAX_POINTER 8 |
---|
| 67 | |
---|
[3140] | 68 | // handle type: |
---|
| 69 | #define HT_NONE 0 |
---|
| 70 | #define HT_STREAM 1 |
---|
| 71 | #define HT_MIXER 2 |
---|
| 72 | |
---|
[3138] | 73 | struct session { |
---|
| 74 | int refc; |
---|
| 75 | struct roar_connection con; |
---|
| 76 | }; |
---|
| 77 | |
---|
| 78 | static struct session _session = {.refc = 0}; |
---|
| 79 | |
---|
| 80 | struct handle { |
---|
| 81 | int refc; // refrence counter |
---|
| 82 | struct session * session; |
---|
[3140] | 83 | int type; |
---|
| 84 | struct roar_stream stream; |
---|
| 85 | struct roar_vio_calls stream_vio; |
---|
| 86 | int stream_opened; |
---|
[3138] | 87 | }; |
---|
| 88 | |
---|
| 89 | static struct { |
---|
| 90 | int (*open)(const char *pathname, int flags, mode_t mode); |
---|
| 91 | int (*close)(int fd); |
---|
| 92 | ssize_t (*write)(int fd, const void *buf, size_t count); |
---|
| 93 | ssize_t (*read)(int fd, void *buf, size_t count); |
---|
| 94 | } _os; |
---|
| 95 | |
---|
[3140] | 96 | static struct pointer { |
---|
[3138] | 97 | int fh; |
---|
| 98 | struct handle * handle; |
---|
| 99 | } _ptr[_MAX_POINTER]; |
---|
| 100 | |
---|
| 101 | static void _init_os (void) { |
---|
| 102 | memset(&_os, 0, sizeof(_os)); |
---|
[3139] | 103 | |
---|
| 104 | _os.open = dlsym(REAL_LIBC, "open"); |
---|
| 105 | _os.close = dlsym(REAL_LIBC, "close"); |
---|
| 106 | _os.write = dlsym(REAL_LIBC, "write"); |
---|
| 107 | _os.read = dlsym(REAL_LIBC, "read"); |
---|
[3138] | 108 | } |
---|
| 109 | |
---|
| 110 | static void _init_ptr (void) { |
---|
| 111 | int i; |
---|
| 112 | |
---|
| 113 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
| 114 | _ptr[i].fh = -1; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | static void _init (void) { |
---|
| 119 | static int inited = 0; |
---|
| 120 | |
---|
| 121 | if ( !inited ) { |
---|
| 122 | _init_os(); |
---|
| 123 | _init_ptr(); |
---|
| 124 | inited++; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | static int _open_dummy (void) { |
---|
| 129 | int p[2]; |
---|
| 130 | |
---|
| 131 | if ( pipe(p) == -1 ) |
---|
| 132 | return -1; |
---|
| 133 | |
---|
| 134 | close(p[1]); |
---|
| 135 | |
---|
| 136 | return p[0]; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | static struct session * _open_session (char * server, char * name) { |
---|
| 140 | if ( _session.refc == 0 ) { |
---|
| 141 | |
---|
| 142 | if ( name == NULL ) |
---|
| 143 | name = "libroaross client"; |
---|
| 144 | |
---|
| 145 | if ( roar_simple_connect(&(_session.con), server, name) == -1 ) |
---|
| 146 | return NULL; |
---|
| 147 | |
---|
| 148 | _session.refc++; |
---|
| 149 | } else { |
---|
| 150 | _session.refc++; |
---|
| 151 | return &_session; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | static void _close_session(struct session * session) { |
---|
| 156 | if ( session == NULL ) |
---|
| 157 | return; |
---|
| 158 | |
---|
| 159 | session->refc--; |
---|
| 160 | |
---|
| 161 | if ( session->refc == 0 ) { |
---|
| 162 | roar_disconnect(&(session->con)); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
[3140] | 166 | static struct handle * _open_handle(struct session * session) { |
---|
| 167 | struct handle * handle; |
---|
| 168 | |
---|
| 169 | if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL ) |
---|
| 170 | return NULL; |
---|
| 171 | |
---|
| 172 | memset(handle, 0, sizeof(struct handle)); |
---|
| 173 | |
---|
| 174 | handle->refc = 1; |
---|
| 175 | handle->session = session; |
---|
| 176 | session->refc++; // TODO: better warp this |
---|
| 177 | handle->type = HT_NONE; |
---|
| 178 | roar_stream_new_empty(&(handle->stream)); |
---|
| 179 | |
---|
| 180 | return handle; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | static void _close_handle(struct handle * handle) { |
---|
| 184 | if (handle == NULL) |
---|
| 185 | return; |
---|
| 186 | |
---|
| 187 | handle->refc--; |
---|
| 188 | |
---|
| 189 | if ( handle->refc == 0 ) { |
---|
| 190 | _close_session(handle->session); |
---|
| 191 | |
---|
| 192 | if ( handle->stream_opened ) |
---|
| 193 | roar_vio_close(&(handle->stream_vio)); |
---|
| 194 | |
---|
| 195 | roar_mm_free(handle); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | static int _open_stream (struct handle * handle) { |
---|
| 200 | return -1; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | static struct pointer * _get_pointer_by_fh (int fh) { |
---|
| 204 | int i; |
---|
| 205 | |
---|
| 206 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
| 207 | if ( _ptr[i].fh == fh ) |
---|
| 208 | return &(_ptr[i]); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | return NULL; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | static struct pointer * _open_pointer(struct handle * handle) { |
---|
| 215 | struct pointer * ret = _get_pointer_by_fh(-1); |
---|
| 216 | |
---|
| 217 | if ( ret == NULL ) |
---|
| 218 | return NULL; |
---|
| 219 | |
---|
| 220 | if ( (ret->fh = _open_dummy()) == -1 ) |
---|
| 221 | return NULL; |
---|
| 222 | |
---|
| 223 | ret->handle = handle; |
---|
| 224 | |
---|
| 225 | return ret; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | static void _close_pointer(struct pointer * pointer) { |
---|
| 229 | if ( pointer == NULL ) |
---|
| 230 | return; |
---|
| 231 | |
---|
| 232 | _os.close(pointer->fh); |
---|
| 233 | |
---|
| 234 | pointer->fh = -1; |
---|
| 235 | |
---|
| 236 | _close_handle(pointer->handle); |
---|
| 237 | } |
---|
[3138] | 238 | |
---|
| 239 | // ------------------------------------- |
---|
| 240 | // emulated functions follow: |
---|
| 241 | // ------------------------------------- |
---|
| 242 | |
---|
| 243 | int open(const char *pathname, int flags, ...) { |
---|
| 244 | mode_t mode = 0; |
---|
| 245 | va_list args; |
---|
| 246 | |
---|
| 247 | _init(); |
---|
| 248 | |
---|
| 249 | if (flags & O_CREAT) { |
---|
| 250 | va_start(args, flags); |
---|
| 251 | mode = va_arg(args, mode_t); |
---|
| 252 | va_end(args); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | return _os.open(pathname, flags, mode); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | int close(int fd) { |
---|
[3140] | 259 | struct pointer * pointer; |
---|
[3138] | 260 | _init(); |
---|
| 261 | |
---|
[3140] | 262 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 263 | _close_pointer(pointer); |
---|
| 264 | return 0; |
---|
| 265 | } |
---|
| 266 | |
---|
[3138] | 267 | return _os.close(fd); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | ssize_t write(int fd, const void *buf, size_t count) { |
---|
[3140] | 271 | struct pointer * pointer; |
---|
| 272 | |
---|
[3138] | 273 | _init(); |
---|
| 274 | |
---|
[3140] | 275 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 276 | if ( pointer->handle->type == HT_STREAM ) { |
---|
| 277 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 278 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 279 | errno = EIO; |
---|
| 280 | return -1; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | return roar_vio_write(&(pointer->handle->stream_vio), buf, count); |
---|
| 284 | } else { |
---|
| 285 | errno = EINVAL; |
---|
| 286 | return -1; |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
[3138] | 290 | return _os.write(fd, buf, count); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | ssize_t read(int fd, void *buf, size_t count) { |
---|
[3140] | 294 | struct pointer * pointer; |
---|
| 295 | |
---|
[3138] | 296 | _init(); |
---|
| 297 | |
---|
[3140] | 298 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 299 | if ( pointer->handle->type == HT_STREAM ) { |
---|
| 300 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 301 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 302 | errno = EIO; |
---|
| 303 | return -1; |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | return roar_vio_read(&(pointer->handle->stream_vio), buf, count); |
---|
| 307 | } else { |
---|
| 308 | errno = EINVAL; |
---|
| 309 | return -1; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
[3138] | 313 | return _os.read(fd, buf, count); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | #endif |
---|
| 317 | |
---|
| 318 | //ll |
---|