source: roaraudio/libroaross/libroaross.c @ 3892:38312d099271

Last change on this file since 3892:38312d099271 was 3892:38312d099271, checked in by phi, 14 years ago

old NetBSD had no O_DIRECT

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