source: roaraudio/libroaross/libroaross.c @ 4853:307276c62f7c

Last change on this file since 4853:307276c62f7c was 4853:307276c62f7c, checked in by phi, 13 years ago

added warning about obsolete ioctl SNDCTL_DSP_SETDUPLEX.

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