source: roaraudio/libroaross/libroaross.c @ 4078:19d24e1dc238

Last change on this file since 4078:19d24e1dc238 was 4078:19d24e1dc238, checked in by phi, 14 years ago

another fix to get it working again: handle internal fh=-1 correctly

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