[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; |
---|
[3144] | 86 | int stream_dir; |
---|
[3140] | 87 | int stream_opened; |
---|
[3138] | 88 | }; |
---|
| 89 | |
---|
| 90 | static struct { |
---|
| 91 | int (*open)(const char *pathname, int flags, mode_t mode); |
---|
| 92 | int (*close)(int fd); |
---|
| 93 | ssize_t (*write)(int fd, const void *buf, size_t count); |
---|
| 94 | ssize_t (*read)(int fd, void *buf, size_t count); |
---|
[3146] | 95 | int (*ioctl)(int d, int request, ...); |
---|
[3138] | 96 | } _os; |
---|
| 97 | |
---|
[3140] | 98 | static struct pointer { |
---|
[3138] | 99 | int fh; |
---|
| 100 | struct handle * handle; |
---|
| 101 | } _ptr[_MAX_POINTER]; |
---|
| 102 | |
---|
| 103 | static void _init_os (void) { |
---|
| 104 | memset(&_os, 0, sizeof(_os)); |
---|
[3139] | 105 | |
---|
| 106 | _os.open = dlsym(REAL_LIBC, "open"); |
---|
| 107 | _os.close = dlsym(REAL_LIBC, "close"); |
---|
| 108 | _os.write = dlsym(REAL_LIBC, "write"); |
---|
| 109 | _os.read = dlsym(REAL_LIBC, "read"); |
---|
[3146] | 110 | _os.ioctl = dlsym(REAL_LIBC, "ioctl"); |
---|
[3138] | 111 | } |
---|
| 112 | |
---|
| 113 | static void _init_ptr (void) { |
---|
| 114 | int i; |
---|
| 115 | |
---|
| 116 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
| 117 | _ptr[i].fh = -1; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | static void _init (void) { |
---|
| 122 | static int inited = 0; |
---|
| 123 | |
---|
| 124 | if ( !inited ) { |
---|
| 125 | _init_os(); |
---|
| 126 | _init_ptr(); |
---|
| 127 | inited++; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | static int _open_dummy (void) { |
---|
| 132 | int p[2]; |
---|
| 133 | |
---|
| 134 | if ( pipe(p) == -1 ) |
---|
| 135 | return -1; |
---|
| 136 | |
---|
| 137 | close(p[1]); |
---|
| 138 | |
---|
| 139 | return p[0]; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | static struct session * _open_session (char * server, char * name) { |
---|
| 143 | if ( _session.refc == 0 ) { |
---|
| 144 | |
---|
| 145 | if ( name == NULL ) |
---|
| 146 | name = "libroaross client"; |
---|
| 147 | |
---|
| 148 | if ( roar_simple_connect(&(_session.con), server, name) == -1 ) |
---|
| 149 | return NULL; |
---|
[3143] | 150 | } |
---|
[3138] | 151 | |
---|
[3143] | 152 | _session.refc++; |
---|
| 153 | return &_session; |
---|
[3138] | 154 | } |
---|
| 155 | |
---|
| 156 | static void _close_session(struct session * session) { |
---|
| 157 | if ( session == NULL ) |
---|
| 158 | return; |
---|
| 159 | |
---|
| 160 | session->refc--; |
---|
| 161 | |
---|
[3144] | 162 | ROAR_DBG("_close_session(session=%p): session->refc=%i", session, session->refc); |
---|
| 163 | |
---|
[3138] | 164 | if ( session->refc == 0 ) { |
---|
| 165 | roar_disconnect(&(session->con)); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[3140] | 169 | static struct handle * _open_handle(struct session * session) { |
---|
| 170 | struct handle * handle; |
---|
| 171 | |
---|
| 172 | if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL ) |
---|
| 173 | return NULL; |
---|
| 174 | |
---|
| 175 | memset(handle, 0, sizeof(struct handle)); |
---|
| 176 | |
---|
| 177 | handle->refc = 1; |
---|
| 178 | handle->session = session; |
---|
| 179 | session->refc++; // TODO: better warp this |
---|
| 180 | handle->type = HT_NONE; |
---|
[3144] | 181 | handle->stream_dir = ROAR_DIR_PLAY; |
---|
| 182 | roar_stream_new(&(handle->stream), ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, ROAR_CODEC_DEFAULT); |
---|
[3140] | 183 | |
---|
| 184 | return handle; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | static void _close_handle(struct handle * handle) { |
---|
| 188 | if (handle == NULL) |
---|
| 189 | return; |
---|
| 190 | |
---|
| 191 | handle->refc--; |
---|
| 192 | |
---|
[3144] | 193 | ROAR_DBG("_close_handle(handle=%p): handle->refc=%i", handle, handle->refc); |
---|
| 194 | |
---|
[3140] | 195 | if ( handle->refc == 0 ) { |
---|
| 196 | if ( handle->stream_opened ) |
---|
| 197 | roar_vio_close(&(handle->stream_vio)); |
---|
| 198 | |
---|
[3144] | 199 | handle->session->refc--; |
---|
| 200 | |
---|
| 201 | _close_session(handle->session); |
---|
| 202 | |
---|
[3140] | 203 | roar_mm_free(handle); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | static struct pointer * _get_pointer_by_fh (int fh) { |
---|
| 208 | int i; |
---|
| 209 | |
---|
| 210 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
| 211 | if ( _ptr[i].fh == fh ) |
---|
| 212 | return &(_ptr[i]); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | return NULL; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | static struct pointer * _open_pointer(struct handle * handle) { |
---|
| 219 | struct pointer * ret = _get_pointer_by_fh(-1); |
---|
| 220 | |
---|
| 221 | if ( ret == NULL ) |
---|
| 222 | return NULL; |
---|
| 223 | |
---|
| 224 | if ( (ret->fh = _open_dummy()) == -1 ) |
---|
| 225 | return NULL; |
---|
| 226 | |
---|
| 227 | ret->handle = handle; |
---|
| 228 | |
---|
| 229 | return ret; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | static void _close_pointer(struct pointer * pointer) { |
---|
| 233 | if ( pointer == NULL ) |
---|
| 234 | return; |
---|
| 235 | |
---|
| 236 | _os.close(pointer->fh); |
---|
| 237 | |
---|
| 238 | pointer->fh = -1; |
---|
| 239 | |
---|
| 240 | _close_handle(pointer->handle); |
---|
| 241 | } |
---|
[3138] | 242 | |
---|
| 243 | // ------------------------------------- |
---|
[3142] | 244 | // central open function: |
---|
| 245 | // ------------------------------------- |
---|
| 246 | |
---|
| 247 | static int _open_file (const char *pathname, int flags) { |
---|
[3143] | 248 | struct session * session; |
---|
| 249 | struct handle * handle; |
---|
| 250 | struct pointer * pointer; |
---|
| 251 | struct { |
---|
| 252 | char * prefix; |
---|
| 253 | int type; |
---|
| 254 | } * ptr = NULL, p[] = { |
---|
| 255 | {"/dev/dsp", HT_STREAM}, |
---|
| 256 | {NULL, HT_NONE}, |
---|
| 257 | }; |
---|
| 258 | int i; |
---|
| 259 | |
---|
| 260 | for (i = 0; p[i].prefix != NULL; i++) { |
---|
| 261 | if ( !strcmp(pathname, p[i].prefix) ) { |
---|
| 262 | ptr = &(p[i]); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | if ( ptr == NULL ) |
---|
| 267 | return -2; |
---|
| 268 | |
---|
| 269 | if ( (session = _open_session(NULL, NULL)) == NULL ) { |
---|
| 270 | return -1; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | if ( (handle = _open_handle(session)) == NULL ) { |
---|
| 274 | _close_session(session); |
---|
| 275 | return -1; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | handle->type = ptr->type; |
---|
| 279 | |
---|
[3145] | 280 | switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) { |
---|
| 281 | case O_RDONLY: |
---|
| 282 | handle->stream_dir = ROAR_DIR_MONITOR; |
---|
| 283 | break; |
---|
| 284 | case O_WRONLY: |
---|
| 285 | handle->stream_dir = ROAR_DIR_PLAY; |
---|
| 286 | break; |
---|
| 287 | case O_RDWR: |
---|
| 288 | handle->stream_dir = ROAR_DIR_BIDIR; |
---|
| 289 | break; |
---|
| 290 | } |
---|
| 291 | |
---|
[3143] | 292 | if ( (pointer = _open_pointer(handle)) == NULL ) { |
---|
| 293 | _close_handle(handle); |
---|
| 294 | return -1; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | return pointer->fh; |
---|
[3142] | 298 | } |
---|
| 299 | |
---|
| 300 | // ------------------------------------- |
---|
[3144] | 301 | // open function for streams: |
---|
| 302 | // ------------------------------------- |
---|
| 303 | |
---|
| 304 | static int _open_stream (struct handle * handle) { |
---|
| 305 | // FIXME: this should be re-written much more cleanly: |
---|
| 306 | |
---|
| 307 | if ( handle == NULL ) |
---|
| 308 | return -1; |
---|
| 309 | |
---|
| 310 | if ( roar_vio_simple_new_stream_obj(&(handle->stream_vio), |
---|
| 311 | &(handle->session->con), &(handle->stream), |
---|
| 312 | handle->stream.info.rate, |
---|
| 313 | handle->stream.info.channels, |
---|
| 314 | handle->stream.info.bits, |
---|
| 315 | handle->stream.info.codec, |
---|
| 316 | handle->stream_dir |
---|
| 317 | ) == -1 ) |
---|
| 318 | return -1; |
---|
| 319 | |
---|
| 320 | handle->stream_opened++; |
---|
| 321 | |
---|
| 322 | return 0; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | // ------------------------------------- |
---|
[3138] | 326 | // emulated functions follow: |
---|
| 327 | // ------------------------------------- |
---|
| 328 | |
---|
| 329 | int open(const char *pathname, int flags, ...) { |
---|
[3142] | 330 | int ret; |
---|
| 331 | mode_t mode = 0; |
---|
[3138] | 332 | va_list args; |
---|
| 333 | |
---|
| 334 | _init(); |
---|
| 335 | |
---|
[3142] | 336 | ret = _open_file(pathname, flags); |
---|
| 337 | |
---|
| 338 | switch (ret) { |
---|
| 339 | case -2: // continue as normal, use _op.open() |
---|
| 340 | break; |
---|
| 341 | case -1: // pass error to caller |
---|
| 342 | return -1; |
---|
| 343 | break; |
---|
| 344 | default: // return successfully opened pointer to caller |
---|
| 345 | return ret; |
---|
| 346 | break; |
---|
| 347 | } |
---|
| 348 | |
---|
[3138] | 349 | if (flags & O_CREAT) { |
---|
| 350 | va_start(args, flags); |
---|
| 351 | mode = va_arg(args, mode_t); |
---|
| 352 | va_end(args); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | return _os.open(pathname, flags, mode); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | int close(int fd) { |
---|
[3140] | 359 | struct pointer * pointer; |
---|
[3138] | 360 | _init(); |
---|
| 361 | |
---|
[3140] | 362 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 363 | _close_pointer(pointer); |
---|
| 364 | return 0; |
---|
| 365 | } |
---|
| 366 | |
---|
[3138] | 367 | return _os.close(fd); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | ssize_t write(int fd, const void *buf, size_t count) { |
---|
[3140] | 371 | struct pointer * pointer; |
---|
| 372 | |
---|
[3138] | 373 | _init(); |
---|
| 374 | |
---|
[3140] | 375 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 376 | if ( pointer->handle->type == HT_STREAM ) { |
---|
| 377 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 378 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 379 | errno = EIO; |
---|
| 380 | return -1; |
---|
| 381 | } |
---|
| 382 | } |
---|
[3141] | 383 | return roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count); |
---|
[3140] | 384 | } else { |
---|
| 385 | errno = EINVAL; |
---|
| 386 | return -1; |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | |
---|
[3138] | 390 | return _os.write(fd, buf, count); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | ssize_t read(int fd, void *buf, size_t count) { |
---|
[3140] | 394 | struct pointer * pointer; |
---|
| 395 | |
---|
[3138] | 396 | _init(); |
---|
| 397 | |
---|
[3140] | 398 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 399 | if ( pointer->handle->type == HT_STREAM ) { |
---|
| 400 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 401 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 402 | errno = EIO; |
---|
| 403 | return -1; |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | return roar_vio_read(&(pointer->handle->stream_vio), buf, count); |
---|
| 407 | } else { |
---|
| 408 | errno = EINVAL; |
---|
| 409 | return -1; |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
[3138] | 413 | return _os.read(fd, buf, count); |
---|
| 414 | } |
---|
| 415 | |
---|
[3146] | 416 | extern int ioctl (int __fd, unsigned long int __request, ...) { |
---|
| 417 | va_list args; |
---|
| 418 | void *argp; |
---|
| 419 | |
---|
| 420 | _init(); |
---|
| 421 | |
---|
| 422 | va_start (args, __request); |
---|
| 423 | argp = va_arg (args, void *); |
---|
| 424 | va_end (args); |
---|
| 425 | |
---|
| 426 | return _os.ioctl(__fd, __request, argp); |
---|
| 427 | } |
---|
| 428 | |
---|
[3138] | 429 | #endif |
---|
| 430 | |
---|
| 431 | //ll |
---|