source: roaraudio/libroaross/libroaross.c @ 4633:d3571af94e9d

Last change on this file since 4633:d3571af94e9d was 4633:d3571af94e9d, checked in by phi, 13 years ago

simple workaround for clang inlining of *stat()

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