source: roaraudio/libroaross/libroaross.c @ 4008:e83733d722d9

Last change on this file since 4008:e83733d722d9 was 4008:e83733d722d9, checked in by phi, 14 years ago

corrected matching of devices, only allow $DEV, $DEV[0-9], so no strange prgram starts to detect endlessly soundcards

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