[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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[3138] | 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 "roaraudio.h" |
---|
[3261] | 37 | #include "libroarlight/libroarlight.h" |
---|
[3138] | 38 | |
---|
| 39 | #if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS) |
---|
| 40 | #if defined(__OpenBSD__) || defined(__NetBSD__) |
---|
| 41 | #include <soundcard.h> |
---|
| 42 | #else |
---|
| 43 | #include <sys/soundcard.h> |
---|
| 44 | #endif |
---|
| 45 | #include <sys/ioctl.h> |
---|
| 46 | |
---|
| 47 | #ifdef ROAR_HAVE_H_SYS_TYPES |
---|
| 48 | #include <sys/types.h> |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | #ifdef ROAR_HAVE_H_FCNTL |
---|
| 52 | #include <fcntl.h> |
---|
| 53 | #endif |
---|
| 54 | |
---|
| 55 | #ifdef ROAR_HAVE_H_UNISTD |
---|
| 56 | #include <unistd.h> |
---|
| 57 | #endif |
---|
| 58 | |
---|
| 59 | #include <sys/stat.h> |
---|
[3139] | 60 | #include <dlfcn.h> |
---|
[3778] | 61 | #include <stdarg.h> |
---|
[3139] | 62 | |
---|
| 63 | #if defined(RTLD_NEXT) |
---|
| 64 | #define REAL_LIBC RTLD_NEXT |
---|
| 65 | #else |
---|
| 66 | #define REAL_LIBC ((void *) -1L) |
---|
| 67 | #endif |
---|
[3138] | 68 | |
---|
[3164] | 69 | #ifndef ENOTSUP |
---|
| 70 | #define ENOTSUP ENOSYS |
---|
| 71 | #endif |
---|
| 72 | |
---|
[3166] | 73 | #if defined(ROAR_OS_NETBSD) && defined(ioctl) |
---|
| 74 | #define IOCTL_IS_ALIAS |
---|
| 75 | #endif |
---|
| 76 | |
---|
[3167] | 77 | #ifdef ROAR_OS_FREEBSD |
---|
| 78 | #define mode_t int |
---|
| 79 | #endif |
---|
| 80 | |
---|
[3169] | 81 | #ifdef ROAR_OS_NETBSD |
---|
| 82 | #define IOCTL() int _oss_ioctl __P((int fd, unsigned long com, void *argp)) |
---|
[3171] | 83 | #define map_args int __fd = fd; unsigned long int __request = com |
---|
[3180] | 84 | #elif defined(ROAR_TARGET_CYGWIN) |
---|
| 85 | #define IOCTL() int ioctl (int __fd, int __cmd, ...) |
---|
| 86 | #define map_args unsigned long int __request = __cmd; void * argp |
---|
| 87 | #define va_argp |
---|
| 88 | #define ioctl_lastarg __cmd |
---|
[3169] | 89 | #else |
---|
| 90 | #define IOCTL() int ioctl (int __fd, unsigned long int __request, ...) |
---|
[3171] | 91 | #define map_args void * argp |
---|
[3169] | 92 | #define va_argp |
---|
[3180] | 93 | #define ioctl_lastarg __request |
---|
[3169] | 94 | #endif |
---|
| 95 | |
---|
[3153] | 96 | #define OSS_VOLUME_SCALE 100 |
---|
| 97 | |
---|
[3138] | 98 | #define _MAX_POINTER 8 |
---|
| 99 | |
---|
[3140] | 100 | // handle type: |
---|
[3663] | 101 | #define HT_NONE 0 /* Unused object */ |
---|
| 102 | #define HT_STREAM 1 /* Stream with no specal handling needed */ |
---|
| 103 | #define HT_MIXER 2 /* Mixer device */ |
---|
| 104 | #define HT_WAVEFORM 3 /* Waveform device */ |
---|
| 105 | #define HT_MIDI 4 /* MIDI device */ |
---|
| 106 | #define HT_DMX 5 /* DMX512/DMX4Linux device */ |
---|
| 107 | #define HT_VIO 6 /* General VIO object */ |
---|
[3666] | 108 | #define HT_STATIC 7 /* Static file */ |
---|
[3140] | 109 | |
---|
[3138] | 110 | struct session { |
---|
| 111 | int refc; |
---|
| 112 | struct roar_connection con; |
---|
| 113 | }; |
---|
| 114 | |
---|
| 115 | static struct session _session = {.refc = 0}; |
---|
| 116 | |
---|
| 117 | struct handle { |
---|
| 118 | int refc; // refrence counter |
---|
| 119 | struct session * session; |
---|
[3140] | 120 | int type; |
---|
[3780] | 121 | int sysio_flags; |
---|
[3140] | 122 | struct roar_stream stream; |
---|
| 123 | struct roar_vio_calls stream_vio; |
---|
[3144] | 124 | int stream_dir; |
---|
[3140] | 125 | int stream_opened; |
---|
[3178] | 126 | size_t stream_buffersize; |
---|
| 127 | size_t readc, writec; |
---|
[3246] | 128 | size_t pos; |
---|
[3666] | 129 | union { |
---|
| 130 | struct { |
---|
| 131 | char * data; |
---|
| 132 | size_t len; |
---|
| 133 | } sf; |
---|
| 134 | } userdata; |
---|
[3138] | 135 | }; |
---|
| 136 | |
---|
| 137 | static struct { |
---|
| 138 | int (*open)(const char *pathname, int flags, mode_t mode); |
---|
| 139 | int (*close)(int fd); |
---|
| 140 | ssize_t (*write)(int fd, const void *buf, size_t count); |
---|
| 141 | ssize_t (*read)(int fd, void *buf, size_t count); |
---|
[3166] | 142 | #ifndef IOCTL_IS_ALIAS |
---|
[3146] | 143 | int (*ioctl)(int d, int request, ...); |
---|
[3166] | 144 | #endif |
---|
[3246] | 145 | off_t (*lseek)(int fildes, off_t offset, int whence); |
---|
[3264] | 146 | FILE *(*fopen)(const char *path, const char *mode); |
---|
[3657] | 147 | int (*dup)(int oldfd); |
---|
| 148 | int (*dup2)(int oldfd, int newfd); |
---|
[3771] | 149 | int (*select)(int nfds, fd_set *readfds, fd_set *writefds, |
---|
| 150 | fd_set *exceptfds, struct timeval *timeout); |
---|
[3778] | 151 | int (*fcntl)(int fd, int cmd, ...); |
---|
[3782] | 152 | int (*access)(const char *pathname, int mode); |
---|
[3866] | 153 | int (*open64)(const char *__file, int __oflag, ...); |
---|
[3138] | 154 | } _os; |
---|
| 155 | |
---|
[3150] | 156 | static struct { |
---|
| 157 | struct { |
---|
| 158 | int volume; |
---|
| 159 | int pcm; |
---|
| 160 | int line; |
---|
| 161 | int line1; |
---|
| 162 | int line2; |
---|
| 163 | int line3; |
---|
| 164 | int digital1; |
---|
| 165 | int digital2; |
---|
| 166 | int digital3; |
---|
| 167 | } sid; |
---|
| 168 | } _mix_settings = { |
---|
| 169 | .sid = { |
---|
| 170 | .volume = -1, |
---|
| 171 | .pcm = -1, |
---|
| 172 | .line = 0, |
---|
| 173 | .line1 = 1, |
---|
| 174 | .line2 = 2, |
---|
| 175 | .line3 = 3, |
---|
| 176 | .digital1 = 1, |
---|
| 177 | .digital2 = 2, |
---|
| 178 | .digital3 = 3 |
---|
| 179 | } |
---|
| 180 | }; |
---|
| 181 | |
---|
[3140] | 182 | static struct pointer { |
---|
[3138] | 183 | int fh; |
---|
| 184 | struct handle * handle; |
---|
| 185 | } _ptr[_MAX_POINTER]; |
---|
| 186 | |
---|
[3662] | 187 | |
---|
[3666] | 188 | static char _sf__dev_sndstat[] = |
---|
| 189 | "Sound Driver:RoarAudio\n" |
---|
| 190 | "Config options: 0\n" |
---|
| 191 | "\n" |
---|
| 192 | "Installed drivers:\n" |
---|
| 193 | "Type 10: RoarAudio emulation\n" |
---|
| 194 | "\n" |
---|
| 195 | "Card config:\n" |
---|
| 196 | "\n" |
---|
| 197 | "Audio devices:\n" |
---|
| 198 | "0: RoarAudio OSS emulation (DUPLEX)\n" |
---|
| 199 | "\n" |
---|
| 200 | "Midi devices:\n" |
---|
| 201 | "0: RoarAudio OSS emulation MIDI\n" |
---|
| 202 | "\n" |
---|
| 203 | "Timers:\n" |
---|
| 204 | "\n" |
---|
| 205 | "Mixers:\n" |
---|
| 206 | "0: RoarAudio OSS emulation Mixer\n" |
---|
| 207 | ; |
---|
| 208 | |
---|
[3662] | 209 | static struct devices { |
---|
| 210 | char * prefix; |
---|
| 211 | int type; |
---|
[3666] | 212 | size_t len; |
---|
| 213 | void * userdata; |
---|
[3662] | 214 | } _device_list[] = { |
---|
[3666] | 215 | {"/dev/dsp", HT_WAVEFORM, 0, NULL}, |
---|
| 216 | {"/dev/audio", HT_WAVEFORM, 0, NULL}, |
---|
| 217 | {"/dev/sound/dsp", HT_WAVEFORM, 0, NULL}, |
---|
| 218 | {"/dev/sound/audio", HT_WAVEFORM, 0, NULL}, |
---|
| 219 | {"/dev/mixer", HT_MIXER, 0, NULL}, |
---|
| 220 | {"/dev/sound/mixer", HT_MIXER, 0, NULL}, |
---|
| 221 | {"/dev/midi", HT_MIDI, 0, NULL}, |
---|
| 222 | {"/dev/rmidi", HT_MIDI, 0, NULL}, |
---|
| 223 | {"/dev/sound/midi", HT_MIDI, 0, NULL}, |
---|
| 224 | {"/dev/sound/rmidi", HT_MIDI, 0, NULL}, |
---|
| 225 | {"/dev/dmx", HT_DMX, 0, NULL}, |
---|
| 226 | {"/dev/misc/dmx", HT_DMX, 0, NULL}, |
---|
| 227 | {"/dev/dmxin", HT_DMX, 0, NULL}, |
---|
| 228 | {"/dev/misc/dmxin", HT_DMX, 0, NULL}, |
---|
| 229 | {"/dev/sndstat", HT_STATIC, sizeof(_sf__dev_sndstat)-1, _sf__dev_sndstat}, |
---|
[3662] | 230 | #ifdef ROAR_DEFAULT_OSS_DEV |
---|
[3666] | 231 | {ROAR_DEFAULT_OSS_DEV, HT_WAVEFORM, 0, NULL}, |
---|
[3662] | 232 | #endif |
---|
[3666] | 233 | {NULL, HT_NONE, 0, NULL}, |
---|
[3662] | 234 | }; |
---|
| 235 | |
---|
[3849] | 236 | |
---|
| 237 | static int _update_nonblock (struct handle * handle); |
---|
| 238 | |
---|
[3138] | 239 | static void _init_os (void) { |
---|
| 240 | memset(&_os, 0, sizeof(_os)); |
---|
[3139] | 241 | |
---|
[3775] | 242 | // if call roar_dl_getsym() here all applications will segfaul. |
---|
| 243 | // why? |
---|
| 244 | |
---|
[3772] | 245 | _os.open = dlsym(REAL_LIBC, "open"); |
---|
| 246 | _os.close = dlsym(REAL_LIBC, "close"); |
---|
| 247 | _os.write = dlsym(REAL_LIBC, "write"); |
---|
| 248 | _os.read = dlsym(REAL_LIBC, "read"); |
---|
[3166] | 249 | #ifndef IOCTL_IS_ALIAS |
---|
[3772] | 250 | _os.ioctl = dlsym(REAL_LIBC, "ioctl"); |
---|
[3166] | 251 | #endif |
---|
[3772] | 252 | _os.lseek = dlsym(REAL_LIBC, "lseek"); |
---|
| 253 | _os.fopen = dlsym(REAL_LIBC, "fopen"); |
---|
| 254 | _os.dup = dlsym(REAL_LIBC, "dup"); |
---|
| 255 | _os.dup2 = dlsym(REAL_LIBC, "dup2"); |
---|
| 256 | _os.select = dlsym(REAL_LIBC, "select"); |
---|
[3778] | 257 | _os.fcntl = dlsym(REAL_LIBC, "fcntl"); |
---|
[3782] | 258 | _os.access = dlsym(REAL_LIBC, "access"); |
---|
[3866] | 259 | _os.open64 = dlsym(REAL_LIBC, "open64"); |
---|
[3138] | 260 | } |
---|
| 261 | |
---|
| 262 | static void _init_ptr (void) { |
---|
| 263 | int i; |
---|
| 264 | |
---|
| 265 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
| 266 | _ptr[i].fh = -1; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | static void _init (void) { |
---|
| 271 | static int inited = 0; |
---|
| 272 | |
---|
| 273 | if ( !inited ) { |
---|
| 274 | _init_os(); |
---|
| 275 | _init_ptr(); |
---|
[3771] | 276 | roar_vio_select(NULL, 0, NULL, NULL); |
---|
[3138] | 277 | inited++; |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
[3157] | 281 | static void _find_volume_sid (struct session * session) { |
---|
| 282 | int i; |
---|
| 283 | int num; |
---|
| 284 | int id[ROAR_STREAMS_MAX]; |
---|
| 285 | struct roar_stream s; |
---|
| 286 | char name[1024]; |
---|
| 287 | |
---|
| 288 | ROAR_DBG("_find_volume_sid(session=%p) = ?", session); |
---|
| 289 | |
---|
| 290 | if ( (num = roar_list_streams(&(session->con), id, ROAR_STREAMS_MAX)) == -1 ) { |
---|
| 291 | return; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | for (i = 0; i < num; i++) { |
---|
| 295 | if ( roar_get_stream(&(session->con), &s, id[i]) == -1 ) |
---|
| 296 | continue; |
---|
| 297 | |
---|
| 298 | if ( s.dir != ROAR_DIR_MIXING ) |
---|
| 299 | continue; |
---|
| 300 | |
---|
| 301 | if ( roar_stream_get_name(&(session->con), &s, name, 1024) == -1 ) |
---|
| 302 | continue; |
---|
| 303 | |
---|
| 304 | if ( !strcasecmp(name, "Waveform Mixer") ) { |
---|
| 305 | _mix_settings.sid.volume = id[i]; |
---|
| 306 | ROAR_DBG("_find_volume_sid(session=%p): found waveform mixer at sid %i", session, id[i]); |
---|
| 307 | ROAR_DBG("_find_volume_sid(session=%p) = (void)", session); |
---|
| 308 | return; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
[3138] | 313 | static int _open_dummy (void) { |
---|
| 314 | int p[2]; |
---|
| 315 | |
---|
| 316 | if ( pipe(p) == -1 ) |
---|
| 317 | return -1; |
---|
| 318 | |
---|
| 319 | close(p[1]); |
---|
| 320 | |
---|
| 321 | return p[0]; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | static struct session * _open_session (char * server, char * name) { |
---|
[3652] | 325 | struct session * ses = &_session; |
---|
| 326 | int new_session = getenv("ROAR_OSS_NEW_SESSION") == NULL ? 0 : 1; |
---|
| 327 | |
---|
[3183] | 328 | ROAR_DBG("_open_session(server='%s', name='%s') = ?", server, name); |
---|
| 329 | ROAR_DBG("_open_session(server='%s', name='%s'): _session.refc=%i", server, name, _session.refc); |
---|
| 330 | |
---|
[3652] | 331 | if ( new_session ) { |
---|
[3850] | 332 | ses = roar_mm_malloc(sizeof(struct session)); |
---|
[3652] | 333 | if ( ses == NULL ) |
---|
| 334 | return NULL; |
---|
| 335 | |
---|
| 336 | memset(ses, 0, sizeof(struct session)); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | if ( ses->refc == 0 ) { |
---|
[3138] | 340 | |
---|
| 341 | if ( name == NULL ) |
---|
[3500] | 342 | name = getenv("ROAR_OSS_CLIENT_NAME"); |
---|
| 343 | |
---|
| 344 | if ( name == NULL ) |
---|
[3138] | 345 | name = "libroaross client"; |
---|
| 346 | |
---|
[3652] | 347 | if ( roar_simple_connect(&(ses->con), server, name) == -1 ) { |
---|
| 348 | if ( new_session ) |
---|
[3850] | 349 | roar_mm_free(ses); |
---|
[3652] | 350 | |
---|
[3138] | 351 | return NULL; |
---|
[3652] | 352 | } |
---|
[3157] | 353 | |
---|
[3652] | 354 | _find_volume_sid(ses); |
---|
[3184] | 355 | |
---|
[3652] | 356 | if ( !new_session ) { |
---|
| 357 | if ( getenv("ROAR_OSS_KEEP_SESSION") != NULL ) |
---|
| 358 | ses->refc++; |
---|
| 359 | } |
---|
[3143] | 360 | } |
---|
[3138] | 361 | |
---|
[3652] | 362 | ses->refc++; |
---|
[3183] | 363 | |
---|
[3652] | 364 | ROAR_DBG("_open_session(server='%s', name='%s') = %p", server, name, ses); |
---|
| 365 | return ses; |
---|
[3138] | 366 | } |
---|
| 367 | |
---|
| 368 | static void _close_session(struct session * session) { |
---|
| 369 | if ( session == NULL ) |
---|
| 370 | return; |
---|
| 371 | |
---|
| 372 | session->refc--; |
---|
| 373 | |
---|
[3144] | 374 | ROAR_DBG("_close_session(session=%p): session->refc=%i", session, session->refc); |
---|
| 375 | |
---|
[3138] | 376 | if ( session->refc == 0 ) { |
---|
| 377 | roar_disconnect(&(session->con)); |
---|
| 378 | } |
---|
[3652] | 379 | |
---|
| 380 | if ( session != &_session ) |
---|
[3850] | 381 | roar_mm_free(session); |
---|
[3138] | 382 | } |
---|
| 383 | |
---|
[3140] | 384 | static struct handle * _open_handle(struct session * session) { |
---|
| 385 | struct handle * handle; |
---|
| 386 | |
---|
[3183] | 387 | ROAR_DBG("_open_handle(session=%p) = ?", session); |
---|
| 388 | |
---|
[3140] | 389 | if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL ) |
---|
| 390 | return NULL; |
---|
| 391 | |
---|
| 392 | memset(handle, 0, sizeof(struct handle)); |
---|
| 393 | |
---|
| 394 | handle->refc = 1; |
---|
| 395 | handle->session = session; |
---|
[3666] | 396 | |
---|
| 397 | if ( session != NULL ) |
---|
| 398 | session->refc++; // TODO: better warp this |
---|
| 399 | |
---|
[3140] | 400 | handle->type = HT_NONE; |
---|
[3144] | 401 | handle->stream_dir = ROAR_DIR_PLAY; |
---|
| 402 | roar_stream_new(&(handle->stream), ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, ROAR_CODEC_DEFAULT); |
---|
[3140] | 403 | |
---|
[3183] | 404 | ROAR_DBG("_open_handle(session=%p) = %p", session, handle); |
---|
[3140] | 405 | return handle; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | static void _close_handle(struct handle * handle) { |
---|
[3664] | 409 | int need_close = 0; |
---|
| 410 | |
---|
[3140] | 411 | if (handle == NULL) |
---|
| 412 | return; |
---|
| 413 | |
---|
| 414 | handle->refc--; |
---|
| 415 | |
---|
[3144] | 416 | ROAR_DBG("_close_handle(handle=%p): handle->refc=%i", handle, handle->refc); |
---|
| 417 | |
---|
[3140] | 418 | if ( handle->refc == 0 ) { |
---|
[3664] | 419 | switch (handle->type) { |
---|
| 420 | case HT_VIO: |
---|
| 421 | need_close = 1; |
---|
| 422 | break; |
---|
| 423 | case HT_STREAM: |
---|
| 424 | if ( handle->stream_opened ) |
---|
| 425 | need_close = 1; |
---|
| 426 | break; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | if ( need_close ) |
---|
[3140] | 430 | roar_vio_close(&(handle->stream_vio)); |
---|
| 431 | |
---|
[3664] | 432 | if ( handle->session != NULL ) { |
---|
| 433 | handle->session->refc--; |
---|
[3144] | 434 | |
---|
[3664] | 435 | _close_session(handle->session); |
---|
| 436 | } |
---|
[3144] | 437 | |
---|
[3140] | 438 | roar_mm_free(handle); |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | static struct pointer * _get_pointer_by_fh (int fh) { |
---|
| 443 | int i; |
---|
| 444 | |
---|
| 445 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
| 446 | if ( _ptr[i].fh == fh ) |
---|
| 447 | return &(_ptr[i]); |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | return NULL; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | static struct pointer * _open_pointer(struct handle * handle) { |
---|
| 454 | struct pointer * ret = _get_pointer_by_fh(-1); |
---|
| 455 | |
---|
| 456 | if ( ret == NULL ) |
---|
| 457 | return NULL; |
---|
| 458 | |
---|
| 459 | if ( (ret->fh = _open_dummy()) == -1 ) |
---|
| 460 | return NULL; |
---|
| 461 | |
---|
| 462 | ret->handle = handle; |
---|
| 463 | |
---|
| 464 | return ret; |
---|
| 465 | } |
---|
| 466 | |
---|
[3657] | 467 | static struct pointer * _attach_pointer(struct handle * handle, int fh) { |
---|
| 468 | struct pointer * ret = _get_pointer_by_fh(-1); |
---|
| 469 | |
---|
| 470 | if ( ret == NULL ) |
---|
| 471 | return NULL; |
---|
| 472 | |
---|
| 473 | if ( (ret->fh = fh) == -1 ) |
---|
| 474 | return NULL; |
---|
| 475 | |
---|
| 476 | ret->handle = handle; |
---|
| 477 | |
---|
| 478 | handle->refc++; |
---|
| 479 | |
---|
| 480 | return ret; |
---|
| 481 | } |
---|
| 482 | |
---|
[3140] | 483 | static void _close_pointer(struct pointer * pointer) { |
---|
| 484 | if ( pointer == NULL ) |
---|
| 485 | return; |
---|
| 486 | |
---|
| 487 | _os.close(pointer->fh); |
---|
| 488 | |
---|
| 489 | pointer->fh = -1; |
---|
| 490 | |
---|
| 491 | _close_handle(pointer->handle); |
---|
| 492 | } |
---|
[3138] | 493 | |
---|
| 494 | // ------------------------------------- |
---|
[3142] | 495 | // central open function: |
---|
| 496 | // ------------------------------------- |
---|
| 497 | |
---|
| 498 | static int _open_file (const char *pathname, int flags) { |
---|
[3143] | 499 | struct session * session; |
---|
| 500 | struct handle * handle; |
---|
| 501 | struct pointer * pointer; |
---|
[3662] | 502 | struct devices * ptr = NULL; |
---|
[3143] | 503 | int i; |
---|
| 504 | |
---|
[3665] | 505 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = ?", pathname, flags); |
---|
| 506 | |
---|
[3777] | 507 | /* |
---|
| 508 | * Flags we ignore: |
---|
| 509 | * O_DIRECT, O_APPEND, O_LARGEFILE, O_NOATIME, O_NOCTTY, O_TRUNC |
---|
| 510 | */ |
---|
| 511 | |
---|
| 512 | if ( flags & O_ASYNC ) { |
---|
| 513 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1 // not supported O_ASYNC", pathname, flags); |
---|
| 514 | errno = ENOSYS; |
---|
| 515 | return -1; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | if ( (flags & O_DIRECTORY) || (flags & O_EXCL) ) { |
---|
| 519 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1 // invalid flags (O_DIRECTORY or O_EXCL)", pathname, flags); |
---|
| 520 | errno = EINVAL; |
---|
| 521 | return -1; |
---|
| 522 | } |
---|
| 523 | |
---|
[3662] | 524 | for (i = 0; _device_list[i].prefix != NULL; i++) { |
---|
| 525 | if ( !strcmp(pathname, _device_list[i].prefix) ) { |
---|
| 526 | ptr = &(_device_list[i]); |
---|
[3143] | 527 | } |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | if ( ptr == NULL ) |
---|
| 531 | return -2; |
---|
| 532 | |
---|
[3666] | 533 | if ( ptr->type == HT_STATIC || ptr->type == HT_VIO ) { // non-session handles |
---|
| 534 | session = NULL; |
---|
| 535 | } else { |
---|
| 536 | if ( (session = _open_session(NULL, NULL)) == NULL ) { |
---|
| 537 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1", pathname, flags); |
---|
| 538 | return -1; |
---|
| 539 | } |
---|
[3143] | 540 | } |
---|
| 541 | |
---|
| 542 | if ( (handle = _open_handle(session)) == NULL ) { |
---|
| 543 | _close_session(session); |
---|
[3665] | 544 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1", pathname, flags); |
---|
[3143] | 545 | return -1; |
---|
| 546 | } |
---|
| 547 | |
---|
[3780] | 548 | handle->type = ptr->type; |
---|
| 549 | handle->sysio_flags = flags; |
---|
| 550 | handle->stream_dir = -1; |
---|
[3143] | 551 | |
---|
[3145] | 552 | switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) { |
---|
| 553 | case O_RDONLY: |
---|
[3244] | 554 | switch (ptr->type) { |
---|
| 555 | case HT_WAVEFORM: |
---|
| 556 | handle->stream_dir = ROAR_DIR_MONITOR; |
---|
| 557 | break; |
---|
| 558 | case HT_MIDI: |
---|
| 559 | handle->stream_dir = ROAR_DIR_MIDI_OUT; |
---|
| 560 | break; |
---|
[3246] | 561 | case HT_DMX: |
---|
| 562 | handle->stream_dir = ROAR_DIR_LIGHT_OUT; |
---|
| 563 | break; |
---|
[3665] | 564 | case HT_MIXER: |
---|
[3666] | 565 | case HT_STATIC: |
---|
[3665] | 566 | break; |
---|
[3248] | 567 | default: |
---|
[3665] | 568 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1", pathname, flags); |
---|
[3248] | 569 | return -1; |
---|
[3244] | 570 | } |
---|
[3145] | 571 | break; |
---|
| 572 | case O_WRONLY: |
---|
[3244] | 573 | switch (ptr->type) { |
---|
| 574 | case HT_WAVEFORM: |
---|
| 575 | handle->stream_dir = ROAR_DIR_PLAY; |
---|
| 576 | break; |
---|
| 577 | case HT_MIDI: |
---|
| 578 | handle->stream_dir = ROAR_DIR_MIDI_IN; |
---|
| 579 | break; |
---|
[3246] | 580 | case HT_DMX: |
---|
| 581 | handle->stream_dir = ROAR_DIR_LIGHT_IN; |
---|
| 582 | break; |
---|
[3666] | 583 | case HT_MIXER: |
---|
| 584 | case HT_STATIC: |
---|
| 585 | break; |
---|
[3248] | 586 | default: |
---|
[3665] | 587 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1", pathname, flags); |
---|
[3248] | 588 | return -1; |
---|
[3244] | 589 | } |
---|
[3145] | 590 | break; |
---|
| 591 | case O_RDWR: |
---|
[3244] | 592 | switch (ptr->type) { |
---|
| 593 | case HT_WAVEFORM: |
---|
| 594 | handle->stream_dir = ROAR_DIR_BIDIR; |
---|
| 595 | break; |
---|
[3666] | 596 | case HT_MIXER: |
---|
| 597 | case HT_STATIC: |
---|
| 598 | break; |
---|
[3248] | 599 | default: |
---|
[3665] | 600 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1", pathname, flags); |
---|
[3248] | 601 | return -1; |
---|
[3244] | 602 | } |
---|
| 603 | break; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | switch (handle->type) { |
---|
| 607 | case HT_WAVEFORM: |
---|
| 608 | handle->type = HT_STREAM; |
---|
| 609 | break; |
---|
| 610 | case HT_MIDI: |
---|
| 611 | handle->type = HT_STREAM; |
---|
| 612 | handle->stream.info.rate = 0; |
---|
| 613 | handle->stream.info.bits = ROAR_MIDI_BITS; |
---|
| 614 | handle->stream.info.channels = ROAR_MIDI_CHANNELS_DEFAULT; |
---|
| 615 | handle->stream.info.codec = ROAR_CODEC_MIDI; |
---|
[3145] | 616 | break; |
---|
[3261] | 617 | case HT_DMX: |
---|
| 618 | handle->stream.info.rate = 0; |
---|
| 619 | handle->stream.info.bits = ROAR_LIGHT_BITS; |
---|
| 620 | handle->stream.info.channels = 512; |
---|
| 621 | handle->stream.info.codec = ROAR_CODEC_ROARDMX; |
---|
| 622 | break; |
---|
[3666] | 623 | case HT_STATIC: |
---|
| 624 | handle->userdata.sf.len = ptr->len; |
---|
| 625 | handle->userdata.sf.data = ptr->userdata; |
---|
| 626 | break; |
---|
[3145] | 627 | } |
---|
| 628 | |
---|
[3143] | 629 | if ( (pointer = _open_pointer(handle)) == NULL ) { |
---|
| 630 | _close_handle(handle); |
---|
[3665] | 631 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = -1", pathname, flags); |
---|
[3143] | 632 | return -1; |
---|
| 633 | } |
---|
| 634 | |
---|
[3665] | 635 | ROAR_DBG("_open_file(pathname='%s', flags=0x%x) = %i", pathname, flags, pointer->fh); |
---|
| 636 | |
---|
[3143] | 637 | return pointer->fh; |
---|
[3142] | 638 | } |
---|
| 639 | |
---|
| 640 | // ------------------------------------- |
---|
[3144] | 641 | // open function for streams: |
---|
| 642 | // ------------------------------------- |
---|
| 643 | |
---|
| 644 | static int _open_stream (struct handle * handle) { |
---|
| 645 | // FIXME: this should be re-written much more cleanly: |
---|
| 646 | |
---|
| 647 | if ( handle == NULL ) |
---|
| 648 | return -1; |
---|
| 649 | |
---|
| 650 | if ( roar_vio_simple_new_stream_obj(&(handle->stream_vio), |
---|
| 651 | &(handle->session->con), &(handle->stream), |
---|
| 652 | handle->stream.info.rate, |
---|
| 653 | handle->stream.info.channels, |
---|
| 654 | handle->stream.info.bits, |
---|
| 655 | handle->stream.info.codec, |
---|
| 656 | handle->stream_dir |
---|
| 657 | ) == -1 ) |
---|
| 658 | return -1; |
---|
| 659 | |
---|
| 660 | handle->stream_opened++; |
---|
| 661 | |
---|
[3150] | 662 | _mix_settings.sid.pcm = roar_stream_get_id(&(handle->stream)); |
---|
| 663 | |
---|
[3849] | 664 | _update_nonblock(handle); |
---|
| 665 | |
---|
| 666 | return 0; |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | // ------------------------------------- |
---|
| 670 | // function to update O_NONBLOCK: |
---|
| 671 | // ------------------------------------- |
---|
| 672 | |
---|
| 673 | static int _update_nonblock (struct handle * handle) { |
---|
| 674 | int opened = 0; |
---|
| 675 | int state = handle->sysio_flags & O_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; |
---|
| 676 | |
---|
| 677 | switch (handle->type) { |
---|
| 678 | case HT_NONE: |
---|
| 679 | case HT_STATIC: |
---|
| 680 | case HT_MIXER: |
---|
| 681 | // we can ignore setting of nonblock flag here. |
---|
| 682 | return 0; |
---|
| 683 | break; |
---|
| 684 | case HT_VIO: |
---|
| 685 | opened = 1; |
---|
| 686 | break; |
---|
| 687 | case HT_STREAM: |
---|
| 688 | case HT_WAVEFORM: |
---|
| 689 | case HT_MIDI: |
---|
| 690 | case HT_DMX: |
---|
| 691 | opened = handle->stream_opened; |
---|
| 692 | break; |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | if ( opened ) { |
---|
| 696 | return roar_vio_nonblock(&(handle->stream_vio), state); |
---|
| 697 | } |
---|
| 698 | |
---|
[3144] | 699 | return 0; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | // ------------------------------------- |
---|
[3147] | 703 | // function to parse format: |
---|
| 704 | // ------------------------------------- |
---|
| 705 | |
---|
| 706 | static int _ioctl_stream_format (struct handle * handle, int format) { |
---|
| 707 | struct roar_audio_info * info = &(handle->stream.info); |
---|
| 708 | |
---|
| 709 | switch (format) { |
---|
| 710 | case AFMT_S8: |
---|
| 711 | info->bits = 8; |
---|
| 712 | info->codec = ROAR_CODEC_PCM_S_LE; |
---|
| 713 | break; |
---|
| 714 | case AFMT_U8: |
---|
| 715 | info->bits = 8; |
---|
| 716 | info->codec = ROAR_CODEC_PCM_U_LE; |
---|
| 717 | break; |
---|
| 718 | case AFMT_S16_BE: |
---|
| 719 | info->bits = 16; |
---|
| 720 | info->codec = ROAR_CODEC_PCM_S_BE; |
---|
| 721 | break; |
---|
| 722 | case AFMT_S16_LE: |
---|
| 723 | info->bits = 16; |
---|
| 724 | info->codec = ROAR_CODEC_PCM_S_LE; |
---|
| 725 | break; |
---|
| 726 | case AFMT_U16_BE: |
---|
| 727 | info->bits = 16; |
---|
| 728 | info->codec = ROAR_CODEC_PCM_U_BE; |
---|
| 729 | break; |
---|
| 730 | case AFMT_U16_LE: |
---|
| 731 | info->bits = 16; |
---|
| 732 | info->codec = ROAR_CODEC_PCM_U_LE; |
---|
| 733 | break; |
---|
| 734 | #ifdef AFMT_S32_BE |
---|
| 735 | case AFMT_S32_BE: |
---|
| 736 | info->bits = 32; |
---|
| 737 | info->codec = ROAR_CODEC_PCM_S_BE; |
---|
| 738 | break; |
---|
| 739 | #endif |
---|
| 740 | #ifdef AFMT_S32_LE |
---|
| 741 | case AFMT_S32_LE: |
---|
| 742 | info->bits = 32; |
---|
| 743 | info->codec = ROAR_CODEC_PCM_S_LE; |
---|
| 744 | break; |
---|
| 745 | #endif |
---|
| 746 | case AFMT_A_LAW: |
---|
| 747 | info->bits = 8; |
---|
| 748 | info->codec = ROAR_CODEC_ALAW; |
---|
| 749 | break; |
---|
| 750 | case AFMT_MU_LAW: |
---|
| 751 | info->bits = 8; |
---|
| 752 | info->codec = ROAR_CODEC_MULAW; |
---|
| 753 | break; |
---|
| 754 | #ifdef AFMT_VORBIS |
---|
| 755 | case AFMT_VORBIS: |
---|
| 756 | info->codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 757 | break; |
---|
| 758 | #endif |
---|
[3148] | 759 | default: |
---|
[3229] | 760 | ROAR_DBG("_ioctl_stream_format(*): unsupported format"); |
---|
[3148] | 761 | errno = ENOSYS; |
---|
| 762 | return -1; |
---|
| 763 | break; |
---|
[3147] | 764 | } |
---|
| 765 | |
---|
[3148] | 766 | return 0; |
---|
[3147] | 767 | } |
---|
| 768 | |
---|
[3149] | 769 | static inline int _ioctl_stream_format_list (void) { |
---|
| 770 | int format = 0; |
---|
| 771 | |
---|
| 772 | format |= AFMT_S8; |
---|
| 773 | format |= AFMT_U8; |
---|
| 774 | |
---|
| 775 | format |= AFMT_S16_BE; |
---|
| 776 | format |= AFMT_S16_LE; |
---|
| 777 | |
---|
| 778 | format |= AFMT_U16_BE; |
---|
| 779 | format |= AFMT_U16_LE; |
---|
| 780 | |
---|
| 781 | #ifdef AFMT_S32_BE |
---|
| 782 | format |= AFMT_S32_BE; |
---|
| 783 | #endif |
---|
| 784 | #ifdef AFMT_S32_LE |
---|
| 785 | format |= AFMT_S32_LE; |
---|
| 786 | #endif |
---|
| 787 | |
---|
| 788 | format |= AFMT_A_LAW; |
---|
| 789 | format |= AFMT_MU_LAW; |
---|
| 790 | |
---|
| 791 | #ifdef AFMT_VORBIS |
---|
| 792 | format |= AFMT_VORBIS; |
---|
| 793 | #endif |
---|
| 794 | |
---|
| 795 | return format; |
---|
| 796 | } |
---|
| 797 | |
---|
[3147] | 798 | // ------------------------------------- |
---|
[3150] | 799 | // mixer ioctls: |
---|
| 800 | // ------------------------------------- |
---|
| 801 | |
---|
[3158] | 802 | static int _ioctl_mixer (struct handle * handle, long unsigned int req, void * vp) { |
---|
[3159] | 803 | mixer_info * info; |
---|
[3150] | 804 | int channels; |
---|
| 805 | struct roar_mixer_settings mixer; |
---|
[3155] | 806 | int o_w = 0; |
---|
[3150] | 807 | int o_sid = -1; |
---|
[3158] | 808 | int * ip = vp; |
---|
[3165] | 809 | #if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES) |
---|
| 810 | char * name = NULL; |
---|
| 811 | #endif |
---|
[3150] | 812 | |
---|
[3165] | 813 | #if defined(DEBUG) && defined(DEBUG_IOCTL_NAMES) |
---|
[3150] | 814 | switch (req) { |
---|
| 815 | #if 0 |
---|
| 816 | case SNDCTL_MIX_DESCRIPTION: name = "SNDCTL_MIX_DESCRIPTION"; break; |
---|
| 817 | case SNDCTL_MIX_ENUMINFO: name = "SNDCTL_MIX_ENUMINFO"; break; |
---|
| 818 | case SNDCTL_MIX_EXTINFO: name = "SNDCTL_MIX_EXTINFO"; break; |
---|
| 819 | case SNDCTL_MIX_NREXT: name = "SNDCTL_MIX_NREXT"; break; |
---|
| 820 | case SNDCTL_MIX_NRMIX: name = "SNDCTL_MIX_NRMIX"; break; |
---|
| 821 | case SNDCTL_MIX_READ: name = "SNDCTL_MIX_READ"; break; |
---|
| 822 | case SNDCTL_MIX_WRITE: name = "SNDCTL_MIX_WRITE"; break; |
---|
| 823 | #endif |
---|
[3159] | 824 | // case SOUND_MIXER_INFO: name = "SOUND_MIXER_INFO"; break; |
---|
[3152] | 825 | case SOUND_OLD_MIXER_INFO: name = "SOUND_OLD_MIXER_INFO"; break; |
---|
| 826 | case SOUND_MIXER_ACCESS: name = "SOUND_MIXER_ACCESS"; break; |
---|
| 827 | case SOUND_MIXER_AGC: name = "SOUND_MIXER_AGC"; break; |
---|
| 828 | case SOUND_MIXER_3DSE: name = "SOUND_MIXER_3DSE"; break; |
---|
[3156] | 829 | case SOUND_MIXER_GETLEVELS: name = "SOUND_MIXER_GETLEVELS"; break; |
---|
| 830 | case SOUND_MIXER_SETLEVELS: name = "SOUND_MIXER_SETLEVELS"; break; |
---|
[3152] | 831 | case SOUND_MIXER_PRIVATE1: name = "SOUND_MIXER_PRIVATE1"; break; |
---|
| 832 | case SOUND_MIXER_PRIVATE2: name = "SOUND_MIXER_PRIVATE2"; break; |
---|
| 833 | case SOUND_MIXER_PRIVATE3: name = "SOUND_MIXER_PRIVATE3"; break; |
---|
| 834 | case SOUND_MIXER_PRIVATE4: name = "SOUND_MIXER_PRIVATE4"; break; |
---|
| 835 | case SOUND_MIXER_PRIVATE5: name = "SOUND_MIXER_PRIVATE5"; break; |
---|
| 836 | case OSS_GETVERSION: name = "OSS_GETVERSION"; break; |
---|
[3156] | 837 | // case SOUND_MIXER_READ_CAPS: name = "SOUND_MIXER_READ_CAPS"; break; |
---|
| 838 | case SOUND_MIXER_READ_MUTE: name = "SOUND_MIXER_READ_MUTE"; break; |
---|
[3152] | 839 | /* |
---|
| 840 | case : name = ""; break; |
---|
[3156] | 841 | case : name = ""; break; |
---|
[3152] | 842 | */ |
---|
[3150] | 843 | } |
---|
| 844 | if ( name != NULL ) { |
---|
[3656] | 845 | ROAR_DBG("_ioctl_mixer(handle=%p, req=0x%lX, ip=%p): unspported mixer command %s", handle, req, ip, name); |
---|
| 846 | ROAR_DBG("_ioctl_mixer(handle=%p, req=0x%lX, ip=%p) = -1 // errno = ENOSYS", handle, req, ip); |
---|
[3150] | 847 | errno = ENOSYS; |
---|
| 848 | return -1; |
---|
| 849 | } |
---|
[3165] | 850 | #endif |
---|
[3150] | 851 | |
---|
| 852 | switch (req) { |
---|
| 853 | case SOUND_MIXER_READ_VOLUME: o_w = 0; o_sid = _mix_settings.sid.volume; break; |
---|
| 854 | case SOUND_MIXER_READ_LINE: o_w = 0; o_sid = _mix_settings.sid.line; break; |
---|
| 855 | case SOUND_MIXER_READ_LINE1: o_w = 0; o_sid = _mix_settings.sid.line1; break; |
---|
| 856 | case SOUND_MIXER_READ_LINE2: o_w = 0; o_sid = _mix_settings.sid.line2; break; |
---|
| 857 | case SOUND_MIXER_READ_LINE3: o_w = 0; o_sid = _mix_settings.sid.line3; break; |
---|
| 858 | #if 0 |
---|
| 859 | case SOUND_MIXER_READ_DIGITAL1: o_w = 0; o_sid = _mix_settings.sid.digital1; break; |
---|
| 860 | case SOUND_MIXER_READ_DIGITAL2: o_w = 0; o_sid = _mix_settings.sid.digital2; break; |
---|
| 861 | case SOUND_MIXER_READ_DIGITAL3: o_w = 0; o_sid = _mix_settings.sid.digital3; break; |
---|
| 862 | #endif |
---|
| 863 | case SOUND_MIXER_WRITE_VOLUME: o_w = 1; o_sid = _mix_settings.sid.volume; break; |
---|
| 864 | case SOUND_MIXER_WRITE_LINE: o_w = 1; o_sid = _mix_settings.sid.line; break; |
---|
| 865 | case SOUND_MIXER_WRITE_LINE1: o_w = 1; o_sid = _mix_settings.sid.line1; break; |
---|
| 866 | case SOUND_MIXER_WRITE_LINE2: o_w = 1; o_sid = _mix_settings.sid.line2; break; |
---|
| 867 | case SOUND_MIXER_WRITE_LINE3: o_w = 1; o_sid = _mix_settings.sid.line3; break; |
---|
| 868 | #if 0 |
---|
| 869 | case SOUND_MIXER_WRITE_DIGITAL1: o_w = 1; o_sid = _mix_settings.sid.digital1; break; |
---|
| 870 | case SOUND_MIXER_WRITE_DIGITAL2: o_w = 1; o_sid = _mix_settings.sid.digital2; break; |
---|
| 871 | case SOUND_MIXER_WRITE_DIGITAL3: o_w = 1; o_sid = _mix_settings.sid.digital3; break; |
---|
| 872 | #endif |
---|
[3177] | 873 | // we handle PCM seperatly as we want to be abled to abled to handle it on a stream (not mixer), too: |
---|
| 874 | case SOUND_MIXER_READ_PCM: |
---|
| 875 | o_w = 0; |
---|
| 876 | if ( handle->type == HT_STREAM ) { |
---|
| 877 | o_sid = roar_stream_get_id(&(handle->stream)); |
---|
| 878 | } else { |
---|
| 879 | o_sid = _mix_settings.sid.pcm; |
---|
| 880 | } |
---|
| 881 | break; |
---|
| 882 | case SOUND_MIXER_WRITE_PCM: |
---|
| 883 | o_w = 1; |
---|
| 884 | if ( handle->type == HT_STREAM ) { |
---|
| 885 | o_sid = roar_stream_get_id(&(handle->stream)); |
---|
| 886 | } else { |
---|
| 887 | o_sid = _mix_settings.sid.pcm; |
---|
| 888 | } |
---|
| 889 | break; |
---|
[3150] | 890 | } |
---|
| 891 | if ( o_sid != -1 ) { |
---|
| 892 | // set/get volume |
---|
| 893 | if ( o_w ) { |
---|
[3536] | 894 | mixer.scale = OSS_VOLUME_SCALE; |
---|
| 895 | mixer.mixer[0] = ( *ip & 0xFF); |
---|
| 896 | mixer.mixer[1] = ((*ip >> 8) & 0xFF); |
---|
[3150] | 897 | if ( roar_set_vol(&(handle->session->con), o_sid, &mixer, 2) == -1 ) { |
---|
[3156] | 898 | errno = EIO; |
---|
[3150] | 899 | return -1; |
---|
| 900 | } |
---|
| 901 | return 0; |
---|
| 902 | } else { |
---|
| 903 | if ( roar_get_vol(&(handle->session->con), o_sid, &mixer, &channels) == -1 ) { |
---|
[3156] | 904 | errno = EIO; |
---|
[3150] | 905 | return -1; |
---|
| 906 | } |
---|
[3153] | 907 | *ip = ((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale) | (((OSS_VOLUME_SCALE*mixer.mixer[0])/mixer.scale)<<8); |
---|
[3150] | 908 | return 0; |
---|
| 909 | } |
---|
| 910 | } |
---|
| 911 | |
---|
| 912 | switch (req) { |
---|
[3155] | 913 | case SOUND_MIXER_READ_STEREODEVS: /* FIXME: check the streams for channel config */ |
---|
[3150] | 914 | case SOUND_MIXER_READ_DEVMASK: |
---|
| 915 | *ip = 0; |
---|
| 916 | |
---|
| 917 | if ( _mix_settings.sid.volume != -1 ) |
---|
| 918 | *ip |= SOUND_MASK_VOLUME; |
---|
| 919 | if ( _mix_settings.sid.pcm != -1 ) |
---|
| 920 | *ip |= SOUND_MASK_PCM; |
---|
| 921 | if ( _mix_settings.sid.line != -1 ) |
---|
| 922 | *ip |= SOUND_MASK_LINE; |
---|
| 923 | if ( _mix_settings.sid.line1 != -1 ) |
---|
| 924 | *ip |= SOUND_MASK_LINE1; |
---|
| 925 | if ( _mix_settings.sid.line2 != -1 ) |
---|
| 926 | *ip |= SOUND_MASK_LINE2; |
---|
| 927 | if ( _mix_settings.sid.line3 != -1 ) |
---|
| 928 | *ip |= SOUND_MASK_LINE3; |
---|
| 929 | if ( _mix_settings.sid.digital1 != -1 ) |
---|
[3156] | 930 | #if 0 |
---|
[3150] | 931 | *ip |= SOUND_MASK_DIGITAL1; |
---|
| 932 | if ( _mix_settings.sid.digital2 != -1 ) |
---|
| 933 | *ip |= SOUND_MASK_DIGITAL2; |
---|
| 934 | if ( _mix_settings.sid.digital3 != -1 ) |
---|
| 935 | *ip |= SOUND_MASK_DIGITAL3; |
---|
[3156] | 936 | #endif |
---|
[3150] | 937 | |
---|
| 938 | return 0; |
---|
| 939 | break; |
---|
[3153] | 940 | case SOUND_MIXER_READ_RECMASK: |
---|
[3154] | 941 | case SOUND_MIXER_READ_RECSRC: |
---|
[3153] | 942 | *ip = SOUND_MASK_VOLUME; // we can currently only read from mixer |
---|
| 943 | return 0; |
---|
| 944 | break; |
---|
[3154] | 945 | case SOUND_MIXER_WRITE_RECSRC: |
---|
| 946 | if ( *ip == SOUND_MASK_VOLUME ) { |
---|
| 947 | return 0; |
---|
| 948 | } else { |
---|
| 949 | errno = ENOTSUP; |
---|
| 950 | return -1; |
---|
| 951 | } |
---|
| 952 | break; |
---|
[3156] | 953 | case SOUND_MIXER_READ_CAPS: |
---|
| 954 | *ip = 0; |
---|
| 955 | return 0; |
---|
| 956 | break; |
---|
[3159] | 957 | case SOUND_MIXER_INFO: |
---|
| 958 | info = vp; |
---|
| 959 | memset(info, 0, sizeof(*info)); |
---|
| 960 | strcpy(info->id, "RoarAudio"); |
---|
| 961 | strcpy(info->name, "RoarAudio"); |
---|
| 962 | return 0; |
---|
| 963 | break; |
---|
[3150] | 964 | } |
---|
| 965 | |
---|
[3656] | 966 | ROAR_DBG("_ioctl_mixer(handle=%p, req=0x%lX, ip=%p): unknown mixer CTL", handle, req, ip); |
---|
[3156] | 967 | // _os.ioctl(-1, req, ip); |
---|
[3656] | 968 | ROAR_DBG("_ioctl_mixer(handle=%p, req=0x%lX, ip=%p) = -1 // errno = ENOSYS", handle, req, ip); |
---|
[3150] | 969 | errno = ENOSYS; |
---|
| 970 | return -1; |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | // ------------------------------------- |
---|
[3178] | 974 | // buffer size calculation: |
---|
| 975 | // ------------------------------------- |
---|
| 976 | |
---|
| 977 | static size_t _get_stream_buffersize (struct handle * handle) { |
---|
| 978 | if ( handle->stream_buffersize ) |
---|
| 979 | return handle->stream_buffersize; |
---|
| 980 | |
---|
| 981 | return handle->stream_buffersize = handle->stream.info.rate * |
---|
| 982 | handle->stream.info.channels * |
---|
| 983 | handle->stream.info.bits / 800; |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | // ------------------------------------- |
---|
[3138] | 987 | // emulated functions follow: |
---|
| 988 | // ------------------------------------- |
---|
| 989 | |
---|
| 990 | int open(const char *pathname, int flags, ...) { |
---|
[3142] | 991 | int ret; |
---|
| 992 | mode_t mode = 0; |
---|
[3138] | 993 | va_list args; |
---|
| 994 | |
---|
| 995 | _init(); |
---|
| 996 | |
---|
[3261] | 997 | if ( pathname == NULL ) { |
---|
| 998 | errno = EFAULT; |
---|
| 999 | return -1; |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | ROAR_DBG("open(pathname='%s', flags=%x, ...) = ?\n", pathname, flags); |
---|
[3142] | 1003 | ret = _open_file(pathname, flags); |
---|
| 1004 | |
---|
| 1005 | switch (ret) { |
---|
| 1006 | case -2: // continue as normal, use _op.open() |
---|
| 1007 | break; |
---|
| 1008 | case -1: // pass error to caller |
---|
| 1009 | return -1; |
---|
| 1010 | break; |
---|
| 1011 | default: // return successfully opened pointer to caller |
---|
| 1012 | return ret; |
---|
| 1013 | break; |
---|
| 1014 | } |
---|
| 1015 | |
---|
[3138] | 1016 | if (flags & O_CREAT) { |
---|
| 1017 | va_start(args, flags); |
---|
| 1018 | mode = va_arg(args, mode_t); |
---|
| 1019 | va_end(args); |
---|
| 1020 | } |
---|
| 1021 | |
---|
| 1022 | return _os.open(pathname, flags, mode); |
---|
| 1023 | } |
---|
| 1024 | |
---|
[3866] | 1025 | int open64(const char *__file, int __oflag, ...) { |
---|
| 1026 | int ret; |
---|
| 1027 | mode_t mode = 0; |
---|
| 1028 | va_list args; |
---|
| 1029 | |
---|
| 1030 | _init(); |
---|
| 1031 | |
---|
| 1032 | if ( __file == NULL ) { |
---|
| 1033 | errno = EFAULT; |
---|
| 1034 | return -1; |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | ROAR_DBG("open64(__file='%s', __oflags=%x, ...) = ?\n", __file, __oflag); |
---|
| 1038 | ret = _open_file(__file, __oflag); |
---|
| 1039 | |
---|
| 1040 | switch (ret) { |
---|
| 1041 | case -2: // continue as normal, use _op.open() |
---|
| 1042 | break; |
---|
| 1043 | case -1: // pass error to caller |
---|
| 1044 | return -1; |
---|
| 1045 | break; |
---|
| 1046 | default: // return successfully opened pointer to caller |
---|
| 1047 | return ret; |
---|
| 1048 | break; |
---|
| 1049 | } |
---|
| 1050 | |
---|
| 1051 | if (__oflag & O_CREAT) { |
---|
| 1052 | va_start(args, __oflag); |
---|
| 1053 | mode = va_arg(args, mode_t); |
---|
| 1054 | va_end(args); |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | if ( _os.open64 != NULL ) { |
---|
| 1058 | return _os.open64(__file, __oflag, mode); |
---|
| 1059 | } else { |
---|
| 1060 | #ifdef O_LARGEFILE |
---|
| 1061 | return _os.open(__file, __oflag | O_LARGEFILE, mode); |
---|
| 1062 | #else |
---|
| 1063 | return _os.open(__file, __oflag, mode); |
---|
| 1064 | #endif |
---|
| 1065 | } |
---|
| 1066 | } |
---|
| 1067 | |
---|
[3138] | 1068 | int close(int fd) { |
---|
[3140] | 1069 | struct pointer * pointer; |
---|
[3138] | 1070 | _init(); |
---|
| 1071 | |
---|
[3140] | 1072 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
| 1073 | _close_pointer(pointer); |
---|
| 1074 | return 0; |
---|
| 1075 | } |
---|
| 1076 | |
---|
[3138] | 1077 | return _os.close(fd); |
---|
| 1078 | } |
---|
| 1079 | |
---|
| 1080 | ssize_t write(int fd, const void *buf, size_t count) { |
---|
[3261] | 1081 | struct roar_roardmx_message roardmxmsg; |
---|
[3140] | 1082 | struct pointer * pointer; |
---|
[3178] | 1083 | ssize_t ret; |
---|
[3639] | 1084 | size_t i; |
---|
[3140] | 1085 | |
---|
[3138] | 1086 | _init(); |
---|
| 1087 | |
---|
[3140] | 1088 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
[3655] | 1089 | ROAR_DBG("write(fd=%i, buf=%p, count=%lu) = ? // pointer write", fd, buf, (long unsigned int) count); |
---|
[3261] | 1090 | switch (pointer->handle->type) { |
---|
[3664] | 1091 | case HT_STREAM: // handle stream specific stuff |
---|
[3261] | 1092 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 1093 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 1094 | errno = EIO; |
---|
| 1095 | return -1; |
---|
| 1096 | } |
---|
| 1097 | } |
---|
[3664] | 1098 | case HT_VIO: // from here we only look at the VIO object of streams, or handle simple VIOs |
---|
[3261] | 1099 | ret = roar_vio_write(&(pointer->handle->stream_vio), (char*)buf, count); |
---|
| 1100 | if ( ret > 0 ) |
---|
| 1101 | pointer->handle->writec += ret; |
---|
| 1102 | return ret; |
---|
| 1103 | break; |
---|
[3664] | 1104 | case HT_DMX: // DMX need specal handling as we need to convert the protocol |
---|
[3261] | 1105 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 1106 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 1107 | errno = EIO; |
---|
| 1108 | return -1; |
---|
| 1109 | } |
---|
| 1110 | } |
---|
| 1111 | if ( count > 0 ) { |
---|
| 1112 | if ( roar_roardmx_message_new_sset(&roardmxmsg) == -1 ) { |
---|
| 1113 | errno = EIO; |
---|
| 1114 | return -1; |
---|
| 1115 | } |
---|
| 1116 | for (i = 0; i < count; i++) { |
---|
| 1117 | if ( roar_roardmx_message_add_chanval(&roardmxmsg, pointer->handle->pos + i, ((unsigned char*)buf)[i]) == -1 ) { |
---|
| 1118 | #ifdef EMSGSIZE |
---|
| 1119 | errno = EMSGSIZE; |
---|
| 1120 | #else |
---|
| 1121 | errno = EIO; |
---|
| 1122 | #endif |
---|
| 1123 | return -1; |
---|
| 1124 | } |
---|
| 1125 | } |
---|
| 1126 | if ( roar_roardmx_message_send(&roardmxmsg, &(pointer->handle->stream_vio)) == -1 ) { |
---|
| 1127 | errno = EIO; |
---|
| 1128 | return -1; |
---|
| 1129 | } |
---|
| 1130 | } |
---|
| 1131 | pointer->handle->pos += count; |
---|
| 1132 | return count; |
---|
| 1133 | break; |
---|
[3664] | 1134 | default: // we don't know what to do with other types |
---|
[3261] | 1135 | errno = EINVAL; |
---|
[3140] | 1136 | return -1; |
---|
[3261] | 1137 | break; |
---|
[3140] | 1138 | } |
---|
| 1139 | } |
---|
| 1140 | |
---|
[3138] | 1141 | return _os.write(fd, buf, count); |
---|
| 1142 | } |
---|
| 1143 | |
---|
| 1144 | ssize_t read(int fd, void *buf, size_t count) { |
---|
[3140] | 1145 | struct pointer * pointer; |
---|
[3178] | 1146 | ssize_t ret; |
---|
[3140] | 1147 | |
---|
[3138] | 1148 | _init(); |
---|
| 1149 | |
---|
[3140] | 1150 | if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { |
---|
[3666] | 1151 | ROAR_DBG("read(fd=%i, buf=%p, count=%lu) = ? // pointer read", fd, buf, (long unsigned int)count); |
---|
| 1152 | |
---|
[3664] | 1153 | switch (pointer->handle->type) { |
---|
| 1154 | case HT_STREAM: |
---|
| 1155 | if ( pointer->handle->stream_opened == 0 ) { |
---|
| 1156 | if ( _open_stream(pointer->handle) == -1 ) { |
---|
| 1157 | errno = EIO; |
---|
| 1158 | return -1; |
---|
| 1159 | } |
---|
| 1160 | } |
---|
| 1161 | case HT_VIO: |
---|
| 1162 | ret = roar_vio_read(&(pointer->handle->stream_vio), buf, count); |
---|
| 1163 | if ( ret > 0 ) |
---|
| 1164 | pointer->handle->readc += ret; |
---|
| 1165 | return ret; |
---|
| 1166 | break; |
---|
[3666] | 1167 | case HT_STATIC: |
---|
| 1168 | ROAR_DBG("read(fd=%i, buf=%p, count=%lu) = ? // type=HT_STATIC", fd, buf, (long unsigned int)count); |
---|
| 1169 | ret = pointer->handle->pos + count; // calc the end of the read |
---|
| 1170 | |
---|
| 1171 | if ( ret > (ssize_t)pointer->handle->userdata.sf.len ) { |
---|
| 1172 | count = pointer->handle->userdata.sf.len - pointer->handle->pos; |
---|
| 1173 | } |
---|
| 1174 | |
---|
| 1175 | memcpy(buf, pointer->handle->userdata.sf.data + pointer->handle->pos, count); |
---|
| 1176 | pointer->handle->pos += count; |
---|
| 1177 | return count; |
---|
| 1178 | break; |
---|
[3664] | 1179 | default: |
---|
| 1180 | errno = EINVAL; |
---|
[3140] | 1181 | return -1; |
---|
[3664] | 1182 | break; |
---|
[3140] | 1183 | } |
---|
| 1184 | } |
---|
| 1185 | |
---|
[3138] | 1186 | return _os.read(fd, buf, count); |
---|
| 1187 | } |
---|
| 1188 | |
---|
[3246] | 1189 | off_t lseek(int fildes, off_t offset, int whence) { |
---|
| 1190 | struct pointer * pointer; |
---|
[3666] | 1191 | ssize_t tmp; |
---|
[3246] | 1192 | |
---|
| 1193 | _init(); |
---|
| 1194 | |
---|
| 1195 | if ( (pointer = _get_pointer_by_fh(fildes)) != NULL ) { |
---|
[3664] | 1196 | switch (pointer->handle->type) { |
---|
| 1197 | case HT_DMX: |
---|
| 1198 | switch (whence) { |
---|
| 1199 | case SEEK_SET: |
---|
| 1200 | pointer->handle->pos = offset; |
---|
| 1201 | break; |
---|
| 1202 | case SEEK_CUR: |
---|
| 1203 | pointer->handle->pos += offset; |
---|
| 1204 | break; |
---|
| 1205 | case SEEK_END: |
---|
| 1206 | default: |
---|
| 1207 | errno = EINVAL; |
---|
| 1208 | return -1; |
---|
| 1209 | break; |
---|
| 1210 | } |
---|
| 1211 | return pointer->handle->pos; |
---|
| 1212 | break; |
---|
| 1213 | case HT_VIO: |
---|
| 1214 | return roar_vio_lseek(&(pointer->handle->stream_vio), offset, whence); |
---|
| 1215 | break; |
---|
[3666] | 1216 | case HT_STATIC: |
---|
| 1217 | switch (whence) { |
---|
| 1218 | case SEEK_SET: |
---|
| 1219 | if ( offset < 0 || offset > (ssize_t)pointer->handle->userdata.sf.len ) { |
---|
| 1220 | errno = EINVAL; |
---|
| 1221 | return -1; |
---|
| 1222 | } |
---|
| 1223 | pointer->handle->pos = offset; |
---|
| 1224 | break; |
---|
| 1225 | case SEEK_CUR: |
---|
| 1226 | tmp = pointer->handle->pos + offset; |
---|
| 1227 | if ( tmp < 0 || tmp > (ssize_t)pointer->handle->userdata.sf.len ) { |
---|
| 1228 | errno = EINVAL; |
---|
| 1229 | return -1; |
---|
| 1230 | } |
---|
| 1231 | pointer->handle->pos = tmp; |
---|
| 1232 | break; |
---|
| 1233 | case SEEK_END: |
---|
| 1234 | tmp = pointer->handle->userdata.sf.len + offset; |
---|
| 1235 | if ( tmp < 0 || tmp > (ssize_t)pointer->handle->userdata.sf.len ) { |
---|
| 1236 | errno = EINVAL; |
---|
| 1237 | return -1; |
---|
| 1238 | } |
---|
| 1239 | pointer->handle->pos = tmp; |
---|
| 1240 | break; |
---|
| 1241 | default: |
---|
| 1242 | errno = EINVAL; |
---|
| 1243 | return -1; |
---|
| 1244 | break; |
---|
| 1245 | } |
---|
| 1246 | break; |
---|
[3664] | 1247 | default: |
---|
| 1248 | errno = EINVAL; |
---|
| 1249 | return -1; |
---|
| 1250 | break; |
---|
[3246] | 1251 | } |
---|
| 1252 | } |
---|
| 1253 | |
---|
| 1254 | return _os.lseek(fildes, offset, whence); |
---|
| 1255 | } |
---|
| 1256 | |
---|
[3169] | 1257 | IOCTL() { |
---|
| 1258 | map_args; |
---|
[3147] | 1259 | struct pointer * pointer; |
---|
| 1260 | struct handle * handle; |
---|
[3169] | 1261 | int * ip = NULL; |
---|
[3655] | 1262 | size_t tmp; |
---|
[3176] | 1263 | audio_buf_info * bi; |
---|
[3178] | 1264 | count_info * ci; |
---|
[3229] | 1265 | #ifdef __FIXME__ |
---|
| 1266 | char * nosys_reqname = NULL; |
---|
| 1267 | #endif |
---|
[3169] | 1268 | #ifdef va_argp |
---|
[3146] | 1269 | va_list args; |
---|
[3169] | 1270 | #endif |
---|
[3146] | 1271 | |
---|
| 1272 | _init(); |
---|
| 1273 | |
---|
[3656] | 1274 | // ROAR_DBG("ioctl(__fd=%i, __request=0x%lX) = ?", __fd, (long unsigned int) __request); |
---|
[3148] | 1275 | |
---|
[3169] | 1276 | #ifdef va_argp |
---|
[3180] | 1277 | va_start (args, ioctl_lastarg); |
---|
[3146] | 1278 | argp = va_arg (args, void *); |
---|
| 1279 | va_end (args); |
---|
[3169] | 1280 | #endif |
---|
[3146] | 1281 | |
---|
[3656] | 1282 | // ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): argp=%p", __fd, (long unsigned int) __request, argp); |
---|
[3148] | 1283 | |
---|
[3147] | 1284 | if ( (pointer = _get_pointer_by_fh(__fd)) != NULL ) { |
---|
[3148] | 1285 | ip = argp; |
---|
[3656] | 1286 | // ROAR_DBG("ioctl(__fd=%i, __request=0x%lx): ip=%p", __fd, (long unsigned int) __request, ip); |
---|
[3229] | 1287 | #ifdef __FIXME__ |
---|
| 1288 | switch ((handle = pointer->handle)->type) { |
---|
[3792] | 1289 | case SOUND_PCM_READ_RATE: nosys_reqname = "SOUND_PCM_READ_RATE"; break; |
---|
[3229] | 1290 | case SOUND_PCM_READ_CHANNELS: nosys_reqname = "SOUND_PCM_READ_CHANNELS"; break; |
---|
[3792] | 1291 | case SOUND_PCM_READ_BITS: nosys_reqname = "SOUND_PCM_READ_BITS"; break; |
---|
| 1292 | case SOUND_PCM_READ_FILTER: nosys_reqname = "SOUND_PCM_READ_FILTER"; break; |
---|
| 1293 | case SNDCTL_COPR_RESET: nosys_reqname = "SNDCTL_COPR_RESET"; break; |
---|
| 1294 | case SNDCTL_COPR_LOAD: nosys_reqname = "SNDCTL_COPR_LOAD"; break; |
---|
| 1295 | case SNDCTL_COPR_HALT: nosys_reqname = "SNDCTL_COPR_HALT"; break; |
---|
| 1296 | case SNDCTL_COPR_RDATA: nosys_reqname = "SNDCTL_COPR_RDATA"; break; |
---|
| 1297 | case SNDCTL_COPR_RCODE: nosys_reqname = "SNDCTL_COPR_RCODE"; break; |
---|
| 1298 | case SNDCTL_COPR_WDATA: nosys_reqname = "SNDCTL_COPR_WDATA"; break; |
---|
| 1299 | case SNDCTL_COPR_WCODE: nosys_reqname = "SNDCTL_COPR_WCODE"; break; |
---|
| 1300 | case SNDCTL_COPR_RUN: nosys_reqname = "SNDCTL_COPR_RUN"; break; |
---|
| 1301 | case SNDCTL_COPR_SENDMSG: nosys_reqname = "SNDCTL_COPR_SENDMSG"; break; |
---|
| 1302 | case SNDCTL_COPR_RCVMSG: nosys_reqname = "SNDCTL_COPR_RCVMSG"; break; |
---|
| 1303 | case SNDCTL_DSP_GETCAPS: nosys_reqname = "SNDCTL_DSP_GETCAPS"; break; |
---|
[3654] | 1304 | default: nosys_reqname = "<<<UNKNOWN>>>"; break; |
---|
[3229] | 1305 | /* |
---|
| 1306 | case : nosys_reqname = ""; break; |
---|
| 1307 | case : nosys_reqname = ""; break; |
---|
| 1308 | case : nosys_reqname = ""; break; |
---|
| 1309 | */ |
---|
| 1310 | } |
---|
| 1311 | #endif |
---|
[3147] | 1312 | switch ((handle = pointer->handle)->type) { |
---|
| 1313 | case HT_STREAM: |
---|
| 1314 | switch (__request) { |
---|
| 1315 | case SNDCTL_DSP_RESET: |
---|
| 1316 | case SNDCTL_DSP_POST: |
---|
[3654] | 1317 | case SNDCTL_DSP_SYNC: // ignore for the moment. |
---|
[3162] | 1318 | case SNDCTL_DSP_SETFRAGMENT: // any fragments should be ok for us... |
---|
[3654] | 1319 | case SNDCTL_DSP_SETTRIGGER: // we should implement this using PAUSE flag. |
---|
[3175] | 1320 | return 0; |
---|
[3147] | 1321 | break; |
---|
| 1322 | case SNDCTL_DSP_SPEED: |
---|
| 1323 | handle->stream.info.rate = *ip; |
---|
[3656] | 1324 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): rate=%i", __fd, (long unsigned int) __request, *ip); |
---|
[3147] | 1325 | return 0; |
---|
| 1326 | break; |
---|
| 1327 | case SNDCTL_DSP_CHANNELS: |
---|
| 1328 | handle->stream.info.channels = *ip; |
---|
[3656] | 1329 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): channels=%i", __fd, (long unsigned int) __request, *ip); |
---|
[3147] | 1330 | return 0; |
---|
| 1331 | break; |
---|
[3160] | 1332 | case SNDCTL_DSP_STEREO: |
---|
| 1333 | handle->stream.info.channels = *ip ? 2 : 1; |
---|
| 1334 | return 0; |
---|
| 1335 | break; |
---|
[3161] | 1336 | case SNDCTL_DSP_GETBLKSIZE: |
---|
[3178] | 1337 | *ip = _get_stream_buffersize(handle); |
---|
[3161] | 1338 | return 0; |
---|
| 1339 | break; |
---|
[3147] | 1340 | case SNDCTL_DSP_SETFMT: |
---|
[3656] | 1341 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): fmt=0x%x", __fd, (long unsigned int) __request, *ip); |
---|
[3147] | 1342 | return _ioctl_stream_format(handle, *ip); |
---|
| 1343 | break; |
---|
[3148] | 1344 | case SNDCTL_DSP_GETFMTS: |
---|
[3656] | 1345 | // ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): ip=%p", __fd, (long unsigned int) __request, ip); |
---|
[3149] | 1346 | *ip = _ioctl_stream_format_list(); |
---|
[3148] | 1347 | return 0; |
---|
| 1348 | break; |
---|
[3176] | 1349 | case SNDCTL_DSP_GETOSPACE: |
---|
| 1350 | case SNDCTL_DSP_GETISPACE: |
---|
| 1351 | bi = argp; |
---|
| 1352 | memset(bi, 0, sizeof(*bi)); |
---|
[3178] | 1353 | bi->bytes = _get_stream_buffersize(handle); |
---|
[3176] | 1354 | bi->fragments = 1; |
---|
| 1355 | bi->fragsize = bi->bytes; |
---|
| 1356 | bi->fragstotal = 1; |
---|
| 1357 | return 0; |
---|
| 1358 | break; |
---|
[3178] | 1359 | case SNDCTL_DSP_GETOPTR: |
---|
[3655] | 1360 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): writec=%lu", __fd, (long unsigned int) __request, (long unsigned int) handle->writec); |
---|
[3178] | 1361 | ci = argp; |
---|
| 1362 | memset(ci, 0, sizeof(*ci)); |
---|
| 1363 | ci->bytes = handle->writec; |
---|
[3655] | 1364 | ci->blocks = ci->bytes / (tmp = _get_stream_buffersize(handle)); |
---|
| 1365 | ci->ptr = ci->bytes % tmp; |
---|
[3178] | 1366 | return 0; |
---|
| 1367 | break; |
---|
| 1368 | case SNDCTL_DSP_GETIPTR: |
---|
| 1369 | ci = argp; |
---|
| 1370 | memset(ci, 0, sizeof(*ci)); |
---|
| 1371 | ci->bytes = handle->readc; |
---|
[3655] | 1372 | ci->blocks = ci->bytes / (tmp = _get_stream_buffersize(handle)); |
---|
| 1373 | ci->ptr = ci->bytes % tmp; |
---|
[3178] | 1374 | return 0; |
---|
| 1375 | break; |
---|
[3177] | 1376 | #ifdef SNDCTL_DSP_GETPLAYVOL |
---|
| 1377 | case SNDCTL_DSP_GETPLAYVOL: |
---|
| 1378 | return _ioctl_mixer(handle, SOUND_MIXER_READ_PCM, argp); |
---|
| 1379 | break; |
---|
| 1380 | #endif |
---|
| 1381 | #ifdef SNDCTL_DSP_SETPLAYVOL |
---|
| 1382 | case SNDCTL_DSP_SETPLAYVOL: |
---|
| 1383 | return _ioctl_mixer(handle, SOUND_MIXER_WRITE_PCM, argp); |
---|
| 1384 | break; |
---|
| 1385 | #endif |
---|
[3864] | 1386 | #ifdef SNDCTL_DSP_NONBLOCK |
---|
| 1387 | case SNDCTL_DSP_NONBLOCK: |
---|
| 1388 | return fcntl(__fd, F_SETFL, handle->sysio_flags|O_NONBLOCK); |
---|
| 1389 | break; |
---|
| 1390 | #endif |
---|
[3147] | 1391 | default: |
---|
[3229] | 1392 | #ifdef __FIXME__ |
---|
[3656] | 1393 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX (%s)) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request, nosys_reqname); |
---|
[3229] | 1394 | #else |
---|
[3656] | 1395 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request); |
---|
[3229] | 1396 | #endif |
---|
[3147] | 1397 | errno = ENOSYS; |
---|
| 1398 | return -1; |
---|
| 1399 | } |
---|
| 1400 | break; |
---|
| 1401 | case HT_MIXER: |
---|
[3158] | 1402 | return _ioctl_mixer(handle, __request, argp); |
---|
[3147] | 1403 | break; |
---|
| 1404 | default: |
---|
[3656] | 1405 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX): unknown handle type: no ioctl()s supported", __fd, (long unsigned int) __request); |
---|
| 1406 | ROAR_DBG("ioctl(__fd=%i, __request=0x%lX) = -1 // errno = ENOSYS", __fd, (long unsigned int) __request); |
---|
[3147] | 1407 | errno = EINVAL; |
---|
| 1408 | return -1; |
---|
| 1409 | break; |
---|
| 1410 | } |
---|
| 1411 | } |
---|
| 1412 | |
---|
[3166] | 1413 | #ifdef IOCTL_IS_ALIAS |
---|
| 1414 | errno = ENOSYS; |
---|
| 1415 | return -1; |
---|
| 1416 | #else |
---|
[3146] | 1417 | return _os.ioctl(__fd, __request, argp); |
---|
[3166] | 1418 | #endif |
---|
[3146] | 1419 | } |
---|
| 1420 | |
---|
[3657] | 1421 | int dup(int oldfd) { |
---|
| 1422 | struct pointer * pointer; |
---|
| 1423 | int ret; |
---|
| 1424 | |
---|
| 1425 | _init(); |
---|
| 1426 | |
---|
| 1427 | ret = _os.dup(oldfd); |
---|
| 1428 | |
---|
| 1429 | if (ret == -1) |
---|
| 1430 | return -1; |
---|
| 1431 | |
---|
| 1432 | if ( (pointer = _get_pointer_by_fh(oldfd)) != NULL ) { |
---|
| 1433 | if ( _attach_pointer(pointer->handle, ret) == NULL ) { |
---|
| 1434 | _os.close(ret); |
---|
| 1435 | return -1; |
---|
| 1436 | } |
---|
| 1437 | } |
---|
| 1438 | |
---|
| 1439 | return ret; |
---|
| 1440 | } |
---|
| 1441 | |
---|
| 1442 | int dup2(int oldfd, int newfd) { |
---|
| 1443 | struct pointer * pointer; |
---|
| 1444 | int ret; |
---|
| 1445 | |
---|
| 1446 | _init(); |
---|
| 1447 | |
---|
| 1448 | ret = _os.dup2(oldfd, newfd); |
---|
| 1449 | |
---|
| 1450 | if (ret == -1) |
---|
| 1451 | return -1; |
---|
| 1452 | |
---|
| 1453 | if ( (pointer = _get_pointer_by_fh(oldfd)) != NULL ) { |
---|
| 1454 | if ( _attach_pointer(pointer->handle, ret) == NULL ) { |
---|
| 1455 | _os.close(ret); |
---|
| 1456 | return -1; |
---|
| 1457 | } |
---|
| 1458 | } |
---|
| 1459 | |
---|
| 1460 | return ret; |
---|
| 1461 | } |
---|
[3264] | 1462 | |
---|
[3771] | 1463 | int select(int nfds, fd_set *readfds, fd_set *writefds, |
---|
| 1464 | fd_set *exceptfds, struct timeval *timeout) { |
---|
| 1465 | struct roar_vio_selecttv rtv; |
---|
| 1466 | struct roar_vio_select * sv = NULL; |
---|
[3774] | 1467 | struct pointer * pointer; |
---|
| 1468 | struct handle * handle; |
---|
[3771] | 1469 | ssize_t ret; |
---|
| 1470 | size_t num = 0; |
---|
[3773] | 1471 | size_t idx; |
---|
[3771] | 1472 | int i; |
---|
| 1473 | int i_r, i_w, i_e; |
---|
| 1474 | int max_index = -1; |
---|
[3773] | 1475 | static volatile int is_critical = 0; |
---|
[3771] | 1476 | |
---|
| 1477 | _init(); |
---|
| 1478 | |
---|
| 1479 | if ( is_critical ) |
---|
| 1480 | return _os.select(nfds, readfds, writefds, exceptfds, timeout); |
---|
| 1481 | |
---|
| 1482 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = ?", nfds, readfds, writefds, exceptfds, timeout); |
---|
| 1483 | |
---|
| 1484 | if ( nfds == 0 ) { |
---|
| 1485 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = 0", nfds, readfds, writefds, exceptfds, timeout); |
---|
| 1486 | return 0; |
---|
| 1487 | } |
---|
| 1488 | |
---|
| 1489 | if ( readfds == NULL && writefds == NULL && exceptfds == NULL ) { |
---|
| 1490 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = 0", nfds, readfds, writefds, exceptfds, timeout); |
---|
| 1491 | return 0; |
---|
| 1492 | } |
---|
| 1493 | |
---|
| 1494 | if ( timeout != NULL ) { |
---|
| 1495 | rtv.sec = timeout->tv_sec; |
---|
| 1496 | rtv.nsec = timeout->tv_usec*1000; |
---|
| 1497 | } |
---|
| 1498 | |
---|
| 1499 | // count number of handles: |
---|
| 1500 | for (i = 0; i < nfds; i++) { |
---|
| 1501 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p): i=%i, EXISTS", nfds, readfds, writefds, exceptfds, timeout, i); |
---|
| 1502 | if ( (readfds != NULL && FD_ISSET(i, readfds )) || |
---|
| 1503 | (writefds != NULL && FD_ISSET(i, writefds )) || |
---|
| 1504 | (exceptfds != NULL && FD_ISSET(i, exceptfds)) |
---|
| 1505 | ) { |
---|
| 1506 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p): i=%i, EXISTS", nfds, readfds, writefds, exceptfds, timeout, i); |
---|
| 1507 | num++; |
---|
| 1508 | max_index = i; |
---|
| 1509 | } |
---|
| 1510 | } |
---|
| 1511 | |
---|
| 1512 | if ( num == 0 ) { |
---|
| 1513 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = 0", nfds, readfds, writefds, exceptfds, timeout); |
---|
| 1514 | return 0; |
---|
| 1515 | } |
---|
| 1516 | |
---|
| 1517 | nfds = max_index + 1; |
---|
| 1518 | |
---|
| 1519 | // create sv; |
---|
| 1520 | sv = roar_mm_malloc(sizeof(struct roar_vio_select)*num); |
---|
| 1521 | if ( sv == NULL ) { |
---|
| 1522 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = -1", nfds, readfds, writefds, exceptfds, timeout); |
---|
| 1523 | return -1; |
---|
| 1524 | } |
---|
| 1525 | |
---|
| 1526 | memset(sv, 0, sizeof(struct roar_vio_select)*num); |
---|
| 1527 | |
---|
| 1528 | for (i = 0, idx = 0; i < nfds; i++) { |
---|
| 1529 | if ( idx >= num ) { |
---|
| 1530 | roar_mm_free(sv); |
---|
| 1531 | errno = EFAULT; |
---|
| 1532 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = -1 // i=%i, idx=%i, num=%i", nfds, readfds, writefds, exceptfds, timeout, i, (int)idx, (int)num); |
---|
| 1533 | return -1; |
---|
| 1534 | } |
---|
| 1535 | i_r = readfds != NULL && FD_ISSET(i, readfds); |
---|
| 1536 | i_w = writefds != NULL && FD_ISSET(i, writefds); |
---|
| 1537 | i_e = exceptfds != NULL && FD_ISSET(i, exceptfds); |
---|
| 1538 | |
---|
| 1539 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p): i=%i, i_r=%i, i_w=%i, i_e=%i", nfds, readfds, writefds, exceptfds, timeout, i, i_r, i_w, i_e); |
---|
| 1540 | |
---|
| 1541 | if ( i_r || i_w || i_e ) { |
---|
| 1542 | // TODO: use VIO for pointers... |
---|
[3774] | 1543 | if ( (pointer = _get_pointer_by_fh(i)) != NULL ) { |
---|
| 1544 | handle = pointer->handle; |
---|
| 1545 | sv[idx].vio = NULL; |
---|
| 1546 | sv[idx].fh = -1; |
---|
| 1547 | switch (handle->type) { |
---|
| 1548 | case HT_DMX: |
---|
| 1549 | case HT_STREAM: |
---|
| 1550 | if ( ! handle->stream_opened ) { |
---|
| 1551 | // implement this as statichly return OK |
---|
| 1552 | errno = ENOSYS; |
---|
| 1553 | return -1; |
---|
| 1554 | } |
---|
| 1555 | case HT_VIO: |
---|
| 1556 | sv[idx].vio = &(handle->stream_vio); |
---|
| 1557 | break; |
---|
| 1558 | default: /* non supported type */ |
---|
| 1559 | errno = EINVAL; |
---|
| 1560 | return -1; |
---|
| 1561 | break; |
---|
| 1562 | } |
---|
| 1563 | } else { |
---|
| 1564 | sv[idx].vio = NULL; |
---|
| 1565 | sv[idx].fh = i; |
---|
| 1566 | } |
---|
[3771] | 1567 | |
---|
| 1568 | sv[idx].ud.si = i; |
---|
| 1569 | sv[idx].eventsq = (i_r ? ROAR_VIO_SELECT_READ : 0) | |
---|
| 1570 | (i_w ? ROAR_VIO_SELECT_WRITE : 0) | |
---|
| 1571 | (i_e ? ROAR_VIO_SELECT_EXCEPT : 0); |
---|
| 1572 | idx++; |
---|
| 1573 | } |
---|
| 1574 | } |
---|
| 1575 | |
---|
| 1576 | is_critical++; |
---|
| 1577 | ret = roar_vio_select(sv, num, timeout == NULL ? NULL : &rtv, NULL); |
---|
| 1578 | is_critical--; |
---|
| 1579 | |
---|
| 1580 | if ( ret < 1 ) { |
---|
| 1581 | roar_mm_free(sv); |
---|
| 1582 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = %i", nfds, readfds, writefds, exceptfds, timeout, (int)ret); |
---|
| 1583 | return ret; |
---|
| 1584 | } |
---|
| 1585 | |
---|
| 1586 | // update readfds, writefds, exceptfds: |
---|
| 1587 | if ( readfds != NULL ) |
---|
| 1588 | FD_ZERO(readfds); |
---|
| 1589 | |
---|
| 1590 | if ( writefds != NULL ) |
---|
| 1591 | FD_ZERO(writefds); |
---|
| 1592 | |
---|
| 1593 | if ( exceptfds != NULL ) |
---|
| 1594 | FD_ZERO(exceptfds); |
---|
| 1595 | |
---|
| 1596 | for (idx = 0; idx < num; idx++) { |
---|
| 1597 | if ( sv[idx].eventsa == 0 ) |
---|
| 1598 | continue; |
---|
| 1599 | |
---|
| 1600 | if ( sv[idx].eventsa & ROAR_VIO_SELECT_READ ) |
---|
| 1601 | if ( readfds != NULL ) |
---|
| 1602 | FD_SET(sv[idx].ud.si, readfds); |
---|
| 1603 | |
---|
| 1604 | if ( sv[idx].eventsa & ROAR_VIO_SELECT_WRITE ) |
---|
| 1605 | if ( writefds != NULL ) |
---|
| 1606 | FD_SET(sv[idx].ud.si, writefds); |
---|
| 1607 | |
---|
| 1608 | if ( sv[idx].eventsa & ROAR_VIO_SELECT_EXCEPT ) |
---|
| 1609 | if ( exceptfds != NULL ) |
---|
| 1610 | FD_SET(sv[idx].ud.si, exceptfds); |
---|
| 1611 | } |
---|
| 1612 | |
---|
| 1613 | roar_mm_free(sv); |
---|
| 1614 | |
---|
| 1615 | ROAR_DBG("select(nfds=%i, readfds=%p, writefds=%p, exceptfds=%p, timeout=%p) = %i", nfds, readfds, writefds, exceptfds, timeout, (int)ret); |
---|
| 1616 | return ret; |
---|
| 1617 | } |
---|
| 1618 | |
---|
[3778] | 1619 | int fcntl(int fd, int cmd, ...) { |
---|
| 1620 | enum { NONE, UNKNOWN, LONG, POINTER } type = NONE; |
---|
| 1621 | struct pointer * pointer; |
---|
| 1622 | va_list ap; |
---|
| 1623 | long argl = -1; |
---|
| 1624 | void * vp = NULL; |
---|
[3780] | 1625 | int ret = -1; |
---|
| 1626 | int diff; |
---|
[3778] | 1627 | |
---|
[3779] | 1628 | _init(); |
---|
| 1629 | |
---|
[3781] | 1630 | ROAR_DBG("fcntl(fd=%i, cmd=%i, ...) = ?", fd, cmd); |
---|
| 1631 | |
---|
[3778] | 1632 | switch (cmd) { |
---|
| 1633 | case F_DUPFD: |
---|
| 1634 | case F_SETFD: |
---|
| 1635 | case F_SETFL: |
---|
| 1636 | case F_SETOWN: |
---|
| 1637 | case F_SETSIG: |
---|
| 1638 | case F_SETLEASE: |
---|
| 1639 | case F_NOTIFY: |
---|
| 1640 | type = LONG; |
---|
| 1641 | break; |
---|
| 1642 | case F_GETFD: |
---|
| 1643 | case F_GETFL: |
---|
| 1644 | case F_GETOWN: |
---|
| 1645 | case F_GETSIG: |
---|
| 1646 | case F_GETLEASE: |
---|
| 1647 | type = NONE; |
---|
| 1648 | break; |
---|
| 1649 | case F_GETLK: |
---|
| 1650 | case F_SETLK: |
---|
| 1651 | case F_SETLKW: |
---|
| 1652 | type = POINTER; |
---|
| 1653 | break; |
---|
| 1654 | /* |
---|
| 1655 | case F_EXLCK: |
---|
| 1656 | case F_GETLK64: |
---|
| 1657 | case F_SETLK64: |
---|
| 1658 | case F_SETLKW64: |
---|
| 1659 | case F_SHLCK: |
---|
| 1660 | case F_LINUX_SPECIFIC_BASE: |
---|
| 1661 | case F_INPROGRESS: |
---|
| 1662 | */ |
---|
| 1663 | default: |
---|
| 1664 | type = UNKNOWN; |
---|
| 1665 | } |
---|
| 1666 | |
---|
| 1667 | if ( type == UNKNOWN ) { |
---|
| 1668 | errno = EINVAL; |
---|
| 1669 | return -1; |
---|
| 1670 | } |
---|
| 1671 | |
---|
| 1672 | if ( type != NONE ) { |
---|
| 1673 | va_start(ap, cmd); |
---|
| 1674 | switch (type) { |
---|
| 1675 | case LONG: |
---|
| 1676 | argl = va_arg(ap, long); |
---|
| 1677 | break; |
---|
| 1678 | case POINTER: |
---|
| 1679 | vp = va_arg(ap, void*); |
---|
| 1680 | break; |
---|
| 1681 | default: /* make compiler happy */ |
---|
| 1682 | break; |
---|
| 1683 | } |
---|
| 1684 | va_end(ap); |
---|
| 1685 | } |
---|
| 1686 | |
---|
| 1687 | if ( (pointer = _get_pointer_by_fh(fd)) == NULL ) { |
---|
| 1688 | switch (type) { |
---|
| 1689 | case NONE: |
---|
[3781] | 1690 | ROAR_DBG("fcntl(fd=%i, cmd=%i): fd is true sysio, pass call to kernel", fd, cmd); |
---|
[3778] | 1691 | return _os.fcntl(fd, cmd); |
---|
| 1692 | break; |
---|
| 1693 | case LONG: |
---|
[3781] | 1694 | ROAR_DBG("fcntl(fd=%i, cmd=%i, arg=%li): fd is true sysio, pass call to kernel", fd, cmd, argl); |
---|
[3778] | 1695 | return _os.fcntl(fd, cmd, argl); |
---|
| 1696 | break; |
---|
| 1697 | case POINTER: |
---|
[3781] | 1698 | ROAR_DBG("fcntl(fd=%i, cmd=%i, lock=%p): fd is true sysio, pass call to kernel", fd, cmd, vp); |
---|
[3778] | 1699 | return _os.fcntl(fd, cmd, vp); |
---|
| 1700 | break; |
---|
| 1701 | default: /* make compiler happy */ |
---|
| 1702 | break; |
---|
| 1703 | } |
---|
| 1704 | } |
---|
| 1705 | |
---|
[3781] | 1706 | ROAR_DBG("fcntl(fd=%i, cmd=%i, ...): fd is true pointer, handle internaly", fd, cmd); |
---|
| 1707 | |
---|
[3780] | 1708 | switch (cmd) { |
---|
| 1709 | case F_DUPFD: |
---|
| 1710 | ret = _os.fcntl(fd, F_DUPFD, argl); |
---|
| 1711 | |
---|
| 1712 | if ( ret != -1 ) { |
---|
| 1713 | if ( _attach_pointer(pointer->handle, ret) == NULL ) { |
---|
| 1714 | _os.close(ret); |
---|
| 1715 | ret = -1; |
---|
| 1716 | } |
---|
| 1717 | } |
---|
| 1718 | break; |
---|
| 1719 | case F_SETFD: |
---|
| 1720 | if ( argl == 0 ) { |
---|
| 1721 | ret = 0; |
---|
| 1722 | } else { |
---|
| 1723 | errno = ENOSYS; |
---|
| 1724 | ret = -1; |
---|
| 1725 | } |
---|
| 1726 | break; |
---|
| 1727 | case F_GETFD: |
---|
| 1728 | ret = 0; |
---|
| 1729 | break; |
---|
| 1730 | case F_GETFL: |
---|
| 1731 | ret = pointer->handle->sysio_flags; |
---|
| 1732 | break; |
---|
| 1733 | case F_SETFL: |
---|
| 1734 | diff = (int)argl ^ pointer->handle->sysio_flags; |
---|
| 1735 | diff &= (int)~(int)(O_DIRECT|O_APPEND|O_LARGEFILE|O_NOATIME|O_NOCTTY|O_TRUNC); |
---|
[3849] | 1736 | |
---|
| 1737 | if ( diff & O_NONBLOCK ) { |
---|
| 1738 | diff -= O_NONBLOCK; |
---|
| 1739 | pointer->handle->sysio_flags ^= O_NONBLOCK; |
---|
| 1740 | if ( _update_nonblock(pointer->handle) == -1 ) { |
---|
| 1741 | pointer->handle->sysio_flags ^= O_NONBLOCK; |
---|
| 1742 | return -1; |
---|
| 1743 | } |
---|
| 1744 | } |
---|
| 1745 | |
---|
[3780] | 1746 | if ( diff == 0 ) { // only flags changed we ignore anyway. |
---|
[3849] | 1747 | pointer->handle->sysio_flags = (int)argl; |
---|
[3780] | 1748 | ret = 0; |
---|
| 1749 | } else { |
---|
| 1750 | errno = EINVAL; |
---|
| 1751 | ret = -1; |
---|
| 1752 | } |
---|
| 1753 | break; |
---|
| 1754 | /* TODO: add support for those types: |
---|
| 1755 | case F_SETFD: |
---|
| 1756 | case F_SETOWN: |
---|
| 1757 | case F_SETSIG: |
---|
| 1758 | case F_SETLEASE: |
---|
| 1759 | case F_NOTIFY: |
---|
| 1760 | case F_GETOWN: |
---|
| 1761 | case F_GETSIG: |
---|
| 1762 | case F_GETLEASE: |
---|
| 1763 | case F_GETLK: |
---|
| 1764 | case F_SETLK: |
---|
| 1765 | case F_SETLKW: |
---|
| 1766 | */ |
---|
| 1767 | default: |
---|
| 1768 | errno = ENOSYS; |
---|
| 1769 | ret = -1; |
---|
| 1770 | break; |
---|
| 1771 | } |
---|
| 1772 | |
---|
| 1773 | return ret; |
---|
[3778] | 1774 | } |
---|
| 1775 | |
---|
[3782] | 1776 | int access(const char *pathname, int mode) { |
---|
[3783] | 1777 | struct devices * ptr = NULL; |
---|
| 1778 | int i; |
---|
[3782] | 1779 | |
---|
| 1780 | _init(); |
---|
| 1781 | |
---|
[3783] | 1782 | for (i = 0; _device_list[i].prefix != NULL; i++) { |
---|
| 1783 | if ( !strcmp(pathname, _device_list[i].prefix) ) { |
---|
| 1784 | ptr = &(_device_list[i]); |
---|
| 1785 | } |
---|
| 1786 | } |
---|
| 1787 | |
---|
| 1788 | if ( ptr != NULL ) { |
---|
| 1789 | // the only flag we do not support is +x, which means |
---|
| 1790 | // we need to reject all requets with X_OK. |
---|
| 1791 | if ( mode & X_OK ) { |
---|
| 1792 | errno = EACCES; |
---|
| 1793 | return -1; |
---|
| 1794 | } |
---|
| 1795 | |
---|
| 1796 | // in addition HT_STATIC files do not support write (+w) |
---|
| 1797 | // so we need to reject W_OK. |
---|
| 1798 | if ( ptr->type == HT_STATIC && (mode & W_OK) ) { |
---|
| 1799 | errno = EACCES; |
---|
| 1800 | return -1; |
---|
| 1801 | } |
---|
| 1802 | |
---|
| 1803 | // Else the access is granted: |
---|
| 1804 | return 0; |
---|
| 1805 | } |
---|
| 1806 | |
---|
[3782] | 1807 | return _os.access(pathname, mode); |
---|
| 1808 | } |
---|
| 1809 | |
---|
[3264] | 1810 | // ------------------------------------- |
---|
| 1811 | // emulated stdio functions follow: |
---|
| 1812 | // ------------------------------------- |
---|
| 1813 | |
---|
| 1814 | //roar_vio_to_stdio |
---|
| 1815 | |
---|
[3667] | 1816 | static int _vio_close (struct roar_vio_calls * vio) { |
---|
| 1817 | int ret = 0; |
---|
| 1818 | |
---|
| 1819 | if ( roar_vio_get_fh(vio) != -1 ) |
---|
| 1820 | ret = close(roar_vio_get_fh(vio)); |
---|
| 1821 | |
---|
| 1822 | roar_mm_free(vio); |
---|
| 1823 | |
---|
| 1824 | return ret; |
---|
| 1825 | } |
---|
| 1826 | |
---|
[3264] | 1827 | FILE *fopen(const char *path, const char *mode) { |
---|
[3667] | 1828 | struct roar_vio_calls * vio; |
---|
[3264] | 1829 | FILE * fr; |
---|
| 1830 | int ret; |
---|
| 1831 | int r = 0, w = 0; |
---|
| 1832 | int flags = 0; |
---|
| 1833 | int i; |
---|
| 1834 | register char c; |
---|
| 1835 | |
---|
| 1836 | _init(); |
---|
| 1837 | |
---|
| 1838 | if ( path == NULL || mode == NULL ) { |
---|
| 1839 | errno = EFAULT; |
---|
| 1840 | return NULL; |
---|
| 1841 | } |
---|
| 1842 | |
---|
[3265] | 1843 | ROAR_DBG("open(path='%s', mode='%s') = ?\n", path, mode); |
---|
[3264] | 1844 | |
---|
| 1845 | for (i = 0; (c = mode[i]) != 0; i++) { |
---|
| 1846 | switch (c) { |
---|
| 1847 | case 'r': r = 1; break; |
---|
| 1848 | case 'w': w = 1; break; |
---|
| 1849 | case 'a': w = 1; break; |
---|
| 1850 | case '+': |
---|
| 1851 | r = 1; |
---|
| 1852 | w = 1; |
---|
| 1853 | break; |
---|
| 1854 | } |
---|
| 1855 | } |
---|
| 1856 | |
---|
| 1857 | if ( r && w ) { |
---|
| 1858 | flags = O_RDWR; |
---|
| 1859 | } else if ( r ) { |
---|
| 1860 | flags = O_RDONLY; |
---|
| 1861 | } else if ( w ) { |
---|
| 1862 | flags = O_WRONLY; |
---|
| 1863 | } else { |
---|
| 1864 | errno = EINVAL; |
---|
| 1865 | return NULL; |
---|
| 1866 | } |
---|
| 1867 | |
---|
| 1868 | ret = _open_file(path, flags); |
---|
| 1869 | |
---|
| 1870 | switch (ret) { |
---|
| 1871 | case -2: // continue as normal, use _op.open() |
---|
| 1872 | break; |
---|
| 1873 | case -1: // pass error to caller |
---|
| 1874 | return NULL; |
---|
| 1875 | break; |
---|
| 1876 | default: // return successfully opened pointer to caller |
---|
[3667] | 1877 | if ( (vio = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) { |
---|
| 1878 | return NULL; // errno should be set correctly by roar_mm_malloc(). |
---|
| 1879 | } |
---|
| 1880 | |
---|
| 1881 | roar_vio_init_calls(vio); // TODO: add error handling. |
---|
| 1882 | roar_vio_set_fh(vio, ret); // TODO: add error handling. |
---|
| 1883 | vio->close = _vio_close; |
---|
| 1884 | if ( (fr = roar_vio_to_stdio(vio, flags)) == NULL ) { |
---|
| 1885 | _vio_close(vio); |
---|
| 1886 | errno = EIO; |
---|
| 1887 | return NULL; |
---|
| 1888 | } else { |
---|
| 1889 | return fr; |
---|
| 1890 | } |
---|
[3264] | 1891 | break; |
---|
| 1892 | } |
---|
| 1893 | |
---|
| 1894 | return _os.fopen(path, mode); |
---|
| 1895 | } |
---|
| 1896 | |
---|
[3138] | 1897 | #endif |
---|
| 1898 | |
---|
| 1899 | //ll |
---|